From d248017e2495170115537b72eb12ff663e2bb7ac Mon Sep 17 00:00:00 2001 From: Felix Ableitner Date: Fri, 19 Sep 2014 17:03:00 +0300 Subject: [PATCH] Improve handling of nodes in a repository (fixes #89). --- .../fragments/RepoSettingsFragment.java | 17 ++++++++------ .../syncthingandroid/syncthing/RestApi.java | 23 +++++++------------ .../syncthingandroid/util/ConfigXml.java | 2 +- 3 files changed, 19 insertions(+), 23 deletions(-) diff --git a/src/main/java/com/nutomic/syncthingandroid/fragments/RepoSettingsFragment.java b/src/main/java/com/nutomic/syncthingandroid/fragments/RepoSettingsFragment.java index d35c5f71..1c0e226a 100644 --- a/src/main/java/com/nutomic/syncthingandroid/fragments/RepoSettingsFragment.java +++ b/src/main/java/com/nutomic/syncthingandroid/fragments/RepoSettingsFragment.java @@ -25,6 +25,7 @@ import com.nutomic.syncthingandroid.syncthing.SyncthingService; import com.nutomic.syncthingandroid.util.ExtendedCheckBoxPreference; import java.util.ArrayList; +import java.util.Iterator; import java.util.List; /** @@ -107,7 +108,7 @@ public class RepoSettingsFragment extends PreferenceFragment mRepo = new RestApi.Repo(); mRepo.ID = ""; mRepo.Directory = ""; - mRepo.Nodes = new ArrayList<>(); + mRepo.NodeIds = new ArrayList<>(); mRepo.Versioning = new RestApi.Versioning(); } else { getActivity().setTitle(R.string.edit_repo); @@ -133,8 +134,8 @@ public class RepoSettingsFragment extends PreferenceFragment cbp.setOnPreferenceChangeListener(RepoSettingsFragment.this); // FIXME: something wrong here cbp.setChecked(false); - for (RestApi.Node n2 : mRepo.Nodes) { - if (n2.NodeID.equals(n.NodeID)) { + for (String n2 : mRepo.NodeIds) { + if (n2.equals(n.NodeID)) { cbp.setChecked(true); } } @@ -228,11 +229,13 @@ public class RepoSettingsFragment extends PreferenceFragment ExtendedCheckBoxPreference pref = (ExtendedCheckBoxPreference) preference; RestApi.Node node = (RestApi.Node) pref.getObject(); if ((Boolean) o) { - mRepo.Nodes.add(node); + mRepo.NodeIds.add(node.NodeID); } else { - for (RestApi.Node n : mRepo.Nodes) { - if (n.NodeID.equals(node.NodeID)) { - mRepo.Nodes.remove(n); + Iterator it = mRepo.NodeIds.iterator(); + while (it.hasNext()) { + String n = it.next(); + if (n.equals(node.NodeID)) { + it.remove(); } } } diff --git a/src/main/java/com/nutomic/syncthingandroid/syncthing/RestApi.java b/src/main/java/com/nutomic/syncthingandroid/syncthing/RestApi.java index 19d1b40e..592020ed 100644 --- a/src/main/java/com/nutomic/syncthingandroid/syncthing/RestApi.java +++ b/src/main/java/com/nutomic/syncthingandroid/syncthing/RestApi.java @@ -77,7 +77,7 @@ public class RestApi implements SyncthingService.OnWebGuiAvailableListener { public String Directory; public String ID; public String Invalid; - public List Nodes; + public List NodeIds; public boolean ReadOnly; public Versioning Versioning; } @@ -459,7 +459,6 @@ public class RestApi implements SyncthingService.OnWebGuiAvailableListener { List ret; try { JSONArray repos = mConfig.getJSONArray("Repositories"); - String x = repos.toString(); ret = new ArrayList<>(repos.length()); for (int i = 0; i < repos.length(); i++) { JSONObject json = repos.getJSONObject(i); @@ -467,17 +466,13 @@ public class RestApi implements SyncthingService.OnWebGuiAvailableListener { r.Directory = json.getString("Directory"); r.ID = json.getString("ID"); r.Invalid = json.getString("Invalid"); - r.Nodes = new ArrayList<>(); + r.NodeIds = new ArrayList<>(); JSONArray nodes = json.getJSONArray("Nodes"); for (int j = 0; j < nodes.length(); j++) { JSONObject n = nodes.getJSONObject(j); - String id = n.getString("NodeID"); - for (Node n2 : getNodes()) { - if (n2.NodeID.equals(id)) { - r.Nodes.add(n2); - } - } + r.NodeIds.add(n.getString("NodeID")); } + r.NodeIds.add(mLocalNodeId); r.ReadOnly = json.getBoolean("ReadOnly"); JSONObject versioning = json.getJSONObject("Versioning"); @@ -602,8 +597,8 @@ public class RestApi implements SyncthingService.OnWebGuiAvailableListener { boolean isShared = false; outerloop: for (Repo r : getRepos()) { - for (Node n : r.Nodes) { - if (n.NodeID.equals(nodeId)) { + for (String n : r.NodeIds) { + if (n.equals(nodeId)) { isShared = true; break outerloop; } @@ -765,11 +760,9 @@ public class RestApi implements SyncthingService.OnWebGuiAvailableListener { r.put("IgnorePerms", true); r.put("ReadOnly", repo.ReadOnly); JSONArray nodes = new JSONArray(); - for (Node n : repo.Nodes) { + for (String n : repo.NodeIds) { JSONObject element = new JSONObject(); - element.put("Addresses", listToJson(n.Addresses.split(" "))); - element.put("Name", n.Name); - element.put("NodeID", n.NodeID); + element.put("NodeID", n); nodes.put(element); } r.put("Nodes", nodes); diff --git a/src/main/java/com/nutomic/syncthingandroid/util/ConfigXml.java b/src/main/java/com/nutomic/syncthingandroid/util/ConfigXml.java index fe1af90a..1f57f825 100644 --- a/src/main/java/com/nutomic/syncthingandroid/util/ConfigXml.java +++ b/src/main/java/com/nutomic/syncthingandroid/util/ConfigXml.java @@ -27,7 +27,7 @@ import javax.xml.transform.stream.StreamResult; /** * Provides direct access to the config.xml file in the file system. - *

+ * * This class should only be used if the syncthing API is not available (usually during startup). */ public class ConfigXml {