From 6e83b7c66d6cbf7899e5380a138008be20d8a06d Mon Sep 17 00:00:00 2001 From: Catfriend1 Date: Sun, 14 Apr 2019 16:31:46 +0200 Subject: [PATCH] Add ConfigRouter#getGui, updateGui (fixes #403) (#402) * ConfigXml: Add updateGui * ConfigRouter: Add getGui, updateGui * ConfigXml: "apiKey" => "apikey" * RestApi: Add updateGui --- .../syncthingandroid/service/RestApi.java | 7 ++++++ .../syncthingandroid/util/ConfigRouter.java | 25 +++++++++++++++++++ .../syncthingandroid/util/ConfigXml.java | 23 ++++++++++++++++- 3 files changed, 54 insertions(+), 1 deletion(-) diff --git a/app/src/main/java/com/nutomic/syncthingandroid/service/RestApi.java b/app/src/main/java/com/nutomic/syncthingandroid/service/RestApi.java index 2b9b5dd1..8ccec5bf 100644 --- a/app/src/main/java/com/nutomic/syncthingandroid/service/RestApi.java +++ b/app/src/main/java/com/nutomic/syncthingandroid/service/RestApi.java @@ -596,6 +596,13 @@ public class RestApi { } } + public void updateGui(Gui newGui) { + synchronized (mConfigLock) { + mConfig.gui = newGui; + sendConfig(); + } + } + /** * Returns a deep copy of object. * diff --git a/app/src/main/java/com/nutomic/syncthingandroid/util/ConfigRouter.java b/app/src/main/java/com/nutomic/syncthingandroid/util/ConfigRouter.java index 7b71d997..80f3d622 100644 --- a/app/src/main/java/com/nutomic/syncthingandroid/util/ConfigRouter.java +++ b/app/src/main/java/com/nutomic/syncthingandroid/util/ConfigRouter.java @@ -6,6 +6,7 @@ import android.content.Context; import com.nutomic.syncthingandroid.model.Device; import com.nutomic.syncthingandroid.model.Folder; import com.nutomic.syncthingandroid.model.FolderIgnoreList; +import com.nutomic.syncthingandroid.model.Gui; import com.nutomic.syncthingandroid.model.Options; import com.nutomic.syncthingandroid.service.RestApi; import com.nutomic.syncthingandroid.util.ConfigXml; @@ -164,6 +165,30 @@ public class ConfigRouter { restApi.removeDevice(deviceID); // This will send the config afterwards. } + public Gui getGui(RestApi restApi) { + if (restApi == null || !restApi.isConfigLoaded()) { + // Syncthing is not running or REST API is not (yet) available. + configXml.loadConfig(); + return configXml.getGui(); + } + + // Syncthing is running and REST API is available. + return restApi.getGui(); + } + + public void updateGui(RestApi restApi, final Gui gui) { + if (restApi == null || !restApi.isConfigLoaded()) { + // Syncthing is not running or REST API is not (yet) available. + configXml.loadConfig(); + configXml.updateGui(gui); + configXml.saveChanges(); + return; + } + + // Syncthing is running and REST API is available. + restApi.updateGui(gui); // This will send the config afterwards. + } + public Options getOptions(RestApi restApi) { if (restApi == null || !restApi.isConfigLoaded()) { // Syncthing is not running or REST API is not (yet) available. diff --git a/app/src/main/java/com/nutomic/syncthingandroid/util/ConfigXml.java b/app/src/main/java/com/nutomic/syncthingandroid/util/ConfigXml.java index 33c7aad6..a6a12162 100644 --- a/app/src/main/java/com/nutomic/syncthingandroid/util/ConfigXml.java +++ b/app/src/main/java/com/nutomic/syncthingandroid/util/ConfigXml.java @@ -838,7 +838,7 @@ public class ConfigXml { gui.address = getContentOrDefault(elementGui.getElementsByTagName("address").item(0), gui.address); gui.user = getContentOrDefault(elementGui.getElementsByTagName("user").item(0), gui.user); gui.password = getContentOrDefault(elementGui.getElementsByTagName("password").item(0), ""); - gui.apiKey = getContentOrDefault(elementGui.getElementsByTagName("apiKey").item(0), ""); + gui.apiKey = getContentOrDefault(elementGui.getElementsByTagName("apikey").item(0), ""); gui.theme = getContentOrDefault(elementGui.getElementsByTagName("theme").item(0), gui.theme); gui.insecureAdminAccess = getContentOrDefault(elementGui.getElementsByTagName("insecureAdminAccess").item(0), gui.insecureAdminAccess); gui.insecureAllowFrameLoading = getContentOrDefault(elementGui.getElementsByTagName("insecureAllowFrameLoading").item(0), gui.insecureAllowFrameLoading); @@ -846,6 +846,27 @@ public class ConfigXml { return gui; } + public void updateGui(final Gui gui) { + Element elementGui = (Element) mConfig.getDocumentElement().getElementsByTagName("gui").item(0); + if (elementGui == null) { + Log.e(TAG, "updateGui: elementGui == null"); + return; + } + + elementGui.setAttribute("debugging", Boolean.toString(gui.debugging)); + elementGui.setAttribute("enabled", Boolean.toString(gui.enabled)); + elementGui.setAttribute("tls", Boolean.toString(gui.useTLS)); + + setConfigElement(elementGui, "address", gui.address); + setConfigElement(elementGui, "user", gui.user); + setConfigElement(elementGui, "password", gui.password); + setConfigElement(elementGui, "apikey", gui.apiKey); + setConfigElement(elementGui, "theme", gui.theme); + setConfigElement(elementGui, "insecureAdminAccess", Boolean.toString(gui.insecureAdminAccess)); + setConfigElement(elementGui, "insecureAllowFrameLoading", Boolean.toString(gui.insecureAllowFrameLoading)); + setConfigElement(elementGui, "insecureSkipHostCheck", Boolean.toString(gui.insecureSkipHostCheck)); + } + public Options getOptions() { Element elementOptions = (Element) mConfig.getDocumentElement().getElementsByTagName("options").item(0); Options options = new Options();