You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ServerGroupWriter.java 5.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. /*
  2. * Copyright (c) 2006-2014 DMDirc Developers
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a copy
  5. * of this software and associated documentation files (the "Software"), to deal
  6. * in the Software without restriction, including without limitation the rights
  7. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. * copies of the Software, and to permit persons to whom the Software is
  9. * furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice shall be included in
  12. * all copies or substantial portions of the Software.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  20. * SOFTWARE.
  21. */
  22. package com.dmdirc.addons.serverlists.io;
  23. import com.dmdirc.addons.serverlists.ServerEntry;
  24. import com.dmdirc.addons.serverlists.ServerGroup;
  25. import com.dmdirc.addons.serverlists.ServerGroupItem;
  26. import com.dmdirc.interfaces.config.ConfigProvider;
  27. import java.net.URI;
  28. import java.util.ArrayList;
  29. import java.util.List;
  30. import java.util.Map;
  31. import java.util.Set;
  32. /**
  33. * Provides methods to instantiate a writer for a server group or entry.
  34. *
  35. * @since 0.6.4
  36. */
  37. public class ServerGroupWriter {
  38. /** The identity which will be written to. */
  39. private final ConfigProvider identity;
  40. /**
  41. * Creates a new writer which will write to the specified identity.
  42. *
  43. * @param identity The identity to write the server group to
  44. */
  45. public ServerGroupWriter(final ConfigProvider identity) {
  46. this.identity = identity;
  47. }
  48. /**
  49. * Writes the specified server group and all of its children to this writer's identity.
  50. *
  51. * @param group The group to be written
  52. */
  53. public void write(final ServerGroup group) {
  54. final Set<String> sections = identity.getDomains();
  55. sections.remove("identity");
  56. identity.setOption("identity", "name", group.getName() + " servergroup");
  57. writeGroup(group, sections);
  58. for (String missing : sections) {
  59. for (String setting : identity.getOptions(missing).keySet()) {
  60. identity.unsetOption(missing, setting);
  61. }
  62. }
  63. }
  64. /**
  65. * Appends the specified group to this writer's identity. Any keysections that are used will be
  66. * removed from the sections set, so callers can track which sections in the identity have not
  67. * been used.
  68. *
  69. * @param group The group to be written
  70. * @param sections A set of sections from which any new keysections should be removed
  71. */
  72. protected void writeGroup(final ServerGroup group, final Set<String> sections) {
  73. final String domain = group.getName() + " servergroup";
  74. final List<String> children = new ArrayList<>();
  75. for (ServerGroupItem item : group.getItems()) {
  76. if (item instanceof ServerGroup) {
  77. writeGroup((ServerGroup) item, sections);
  78. children.add(item.getName() + " servergroup");
  79. } else if (item instanceof ServerEntry) {
  80. writeEntry((ServerEntry) item, sections);
  81. children.add(item.getName() + " server");
  82. }
  83. }
  84. final boolean needsUpdating = group.isModified();
  85. sections.remove(domain);
  86. group.setModified(false);
  87. if (!needsUpdating) {
  88. return;
  89. }
  90. if (!group.getLinks().isEmpty()) {
  91. writeLinks(group.getName(), group.getLinks());
  92. sections.remove(group + " links");
  93. }
  94. if (group.getNetwork() == null) {
  95. identity.unsetOption(domain, "network");
  96. } else {
  97. identity.setOption(domain, "network", group.getNetwork());
  98. }
  99. identity.setOption(domain, "description", group.getDescription());
  100. identity.setOption(domain, "contents", children);
  101. identity.setOption(domain, "name", group.getName());
  102. }
  103. /**
  104. * Writes a map of links for the specified entry.
  105. *
  106. * @param name The name of the entry for which to write the links
  107. * @param links The links to be written
  108. */
  109. protected void writeLinks(final String name, final Map<String, URI> links) {
  110. final String domain = name + " links";
  111. final Set<String> existing = identity.getOptions(domain).keySet();
  112. existing.removeAll(links.keySet());
  113. for (String deleted : existing) {
  114. identity.unsetOption(domain, deleted);
  115. }
  116. for (Map.Entry<String, URI> link : links.entrySet()) {
  117. identity.setOption(domain, link.getKey(), link.getValue().toString());
  118. }
  119. }
  120. /**
  121. * Writes the specified entry to this writer's identity. Any keysections that are used will be
  122. * removed from the sections set, so callers can track which sections in the identity have not
  123. * been used.
  124. *
  125. * @param entry The entry to be written
  126. * @param sections A set of sections from which any new keysections should be removed
  127. */
  128. protected void writeEntry(final ServerEntry entry, final Set<String> sections) {
  129. final String domain = entry.getName() + " server";
  130. final boolean needsUpdating = entry.isModified();
  131. sections.remove(domain);
  132. entry.setModified(false);
  133. if (!needsUpdating) {
  134. return;
  135. }
  136. identity.setOption(domain, "name", entry.getName());
  137. identity.setOption(domain, "address", entry.getAddress().toString());
  138. }
  139. }