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.

ServerList.java 6.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  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;
  23. import com.dmdirc.DMDircMBassador;
  24. import com.dmdirc.Precondition;
  25. import com.dmdirc.addons.serverlists.io.ServerGroupReader;
  26. import com.dmdirc.addons.serverlists.io.ServerGroupWriter;
  27. import com.dmdirc.addons.serverlists.service.ServerListServiceProvider;
  28. import com.dmdirc.interfaces.ConnectionManager;
  29. import com.dmdirc.interfaces.config.ConfigProvider;
  30. import com.dmdirc.interfaces.config.ConfigProviderListener;
  31. import com.dmdirc.interfaces.config.IdentityController;
  32. import com.dmdirc.interfaces.config.IdentityFactory;
  33. import com.dmdirc.plugins.PluginManager;
  34. import java.util.Collection;
  35. import java.util.Collections;
  36. import java.util.HashMap;
  37. import java.util.Map;
  38. import javax.inject.Inject;
  39. /**
  40. * Maintains a list of top level {@link ServerGroup}s and handles reading and writing of the lists
  41. * to disk.
  42. *
  43. * @since 0.6.4
  44. */
  45. public class ServerList implements ConfigProviderListener {
  46. /** A list of all known groups. */
  47. private final Map<ServerGroup, ServerGroupWriter> groups = new HashMap<>();
  48. /** ServerManager that ServerEntrys use to create servers */
  49. private final ConnectionManager connectionManager;
  50. /** The controller to read/write settings with. */
  51. private final IdentityController identityController;
  52. /** The factory to create new identities with. */
  53. private final IdentityFactory identityFactory;
  54. /**
  55. * Creates a new ServerList and loads groups and servers.
  56. *
  57. * @param pluginManager Plugin Manager to use.
  58. * @param connectionManager Server Manager to use.
  59. * @param identityController The controller to read/write settings with.
  60. * @param identityFactory The factory to create new identities with.
  61. * @param eventBus The event bus to post errors to
  62. */
  63. @Inject
  64. public ServerList(
  65. final PluginManager pluginManager,
  66. final ConnectionManager connectionManager,
  67. final IdentityController identityController,
  68. final IdentityFactory identityFactory,
  69. final DMDircMBassador eventBus) {
  70. this.connectionManager = connectionManager;
  71. this.identityController = identityController;
  72. this.identityFactory = identityFactory;
  73. identityController.registerIdentityListener("servergroup", this);
  74. for (ConfigProvider identity : identityController.getProvidersByType("servergroup")) {
  75. configProviderAdded(identity);
  76. }
  77. new ServerListServiceProvider(pluginManager, this, eventBus).register();
  78. }
  79. /**
  80. * Adds a server group to the master server list.
  81. *
  82. * @param group The group to be added
  83. * @param writer The writer to use to write the group to disk
  84. */
  85. public void addServerGroup(final ServerGroup group, final ServerGroupWriter writer) {
  86. groups.put(group, writer);
  87. }
  88. /**
  89. * Adds a server group to the master server list, and creates a new writer which will write the
  90. * group to an identity.
  91. *
  92. * @param group The group to be added
  93. */
  94. public void addServerGroup(final ServerGroup group) {
  95. final ServerGroupWriter writer = new ServerGroupWriter(
  96. identityFactory.createCustomConfig(group.getName(), "servergroup"));
  97. group.setModified(true);
  98. addServerGroup(group, writer);
  99. }
  100. /**
  101. * Saves all entries in this list.
  102. */
  103. public void save() {
  104. for (Map.Entry<ServerGroup, ServerGroupWriter> pair : groups.entrySet()) {
  105. if (pair.getKey().isModified()) {
  106. pair.getValue().write(pair.getKey());
  107. }
  108. }
  109. }
  110. /**
  111. * Saves the specified group.
  112. *
  113. * @param group The group to be saved
  114. */
  115. @Precondition("Specified group is a known top-level group in this list")
  116. public void save(final ServerGroup group) {
  117. groups.get(group).write(group);
  118. }
  119. /**
  120. * Retrieves a list of all known server groups.
  121. *
  122. * @return An immutable list of server groups.
  123. */
  124. public Collection<ServerGroup> getServerGroups() {
  125. return Collections.unmodifiableCollection(groups.keySet());
  126. }
  127. /**
  128. * Retrieves a ServerGroup with the specified name, if one exists. This method ignores the case
  129. * of group's name when comparing.
  130. *
  131. * @param name The name of the group to be retrieved
  132. *
  133. * @return A correspondingly named server group, or null if none exists
  134. */
  135. public ServerGroup getGroupByName(final String name) {
  136. for (ServerGroup group : getServerGroups()) {
  137. if (group.getName().equalsIgnoreCase(name)) {
  138. return group;
  139. }
  140. }
  141. return null;
  142. }
  143. @Override
  144. public void configProviderAdded(final ConfigProvider configProvider) {
  145. try {
  146. final ServerGroupReader reader
  147. = new ServerGroupReader(connectionManager, identityController, configProvider);
  148. addServerGroup(reader.read(), reader.getWriter());
  149. } catch (IllegalArgumentException ex) {
  150. // Silently ignore
  151. // TODO: Raise error if the identity isn't a server group being
  152. // currently added by addServerGroup()
  153. }
  154. }
  155. @Override
  156. public void configProviderRemoved(final ConfigProvider configProvider) {
  157. // TODO: Remove server group
  158. }
  159. }