123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179 |
- /*
- * Copyright (c) 2006-2014 DMDirc Developers
- *
- * Permission is hereby granted, free of charge, to any person obtaining a copy
- * of this software and associated documentation files (the "Software"), to deal
- * in the Software without restriction, including without limitation the rights
- * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
- * copies of the Software, and to permit persons to whom the Software is
- * furnished to do so, subject to the following conditions:
- *
- * The above copyright notice and this permission notice shall be included in
- * all copies or substantial portions of the Software.
- *
- * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
- * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
- * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
- * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
- * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
- * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
- * SOFTWARE.
- */
-
- package com.dmdirc.addons.serverlists;
-
- import com.dmdirc.DMDircMBassador;
- import com.dmdirc.Precondition;
- import com.dmdirc.addons.serverlists.io.ServerGroupReader;
- import com.dmdirc.addons.serverlists.io.ServerGroupWriter;
- import com.dmdirc.addons.serverlists.service.ServerListServiceProvider;
- import com.dmdirc.interfaces.ConnectionManager;
- import com.dmdirc.interfaces.config.ConfigProvider;
- import com.dmdirc.interfaces.config.ConfigProviderListener;
- import com.dmdirc.interfaces.config.IdentityController;
- import com.dmdirc.interfaces.config.IdentityFactory;
- import com.dmdirc.plugins.PluginManager;
-
- import java.util.Collection;
- import java.util.Collections;
- import java.util.HashMap;
- import java.util.Map;
-
- import javax.inject.Inject;
-
- /**
- * Maintains a list of top level {@link ServerGroup}s and handles reading and writing of the lists
- * to disk.
- *
- * @since 0.6.4
- */
- public class ServerList implements ConfigProviderListener {
-
- /** A list of all known groups. */
- private final Map<ServerGroup, ServerGroupWriter> groups = new HashMap<>();
- /** ServerManager that ServerEntrys use to create servers */
- private final ConnectionManager connectionManager;
- /** The controller to read/write settings with. */
- private final IdentityController identityController;
- /** The factory to create new identities with. */
- private final IdentityFactory identityFactory;
-
- /**
- * Creates a new ServerList and loads groups and servers.
- *
- * @param pluginManager Plugin Manager to use.
- * @param connectionManager Server Manager to use.
- * @param identityController The controller to read/write settings with.
- * @param identityFactory The factory to create new identities with.
- * @param eventBus The event bus to post errors to
- */
- @Inject
- public ServerList(
- final PluginManager pluginManager,
- final ConnectionManager connectionManager,
- final IdentityController identityController,
- final IdentityFactory identityFactory,
- final DMDircMBassador eventBus) {
- this.connectionManager = connectionManager;
- this.identityController = identityController;
- this.identityFactory = identityFactory;
-
- identityController.registerIdentityListener("servergroup", this);
-
- for (ConfigProvider identity : identityController.getProvidersByType("servergroup")) {
- configProviderAdded(identity);
- }
-
- new ServerListServiceProvider(pluginManager, this, eventBus).register();
- }
-
- /**
- * Adds a server group to the master server list.
- *
- * @param group The group to be added
- * @param writer The writer to use to write the group to disk
- */
- public void addServerGroup(final ServerGroup group, final ServerGroupWriter writer) {
- groups.put(group, writer);
- }
-
- /**
- * Adds a server group to the master server list, and creates a new writer which will write the
- * group to an identity.
- *
- * @param group The group to be added
- */
- public void addServerGroup(final ServerGroup group) {
- final ServerGroupWriter writer = new ServerGroupWriter(
- identityFactory.createCustomConfig(group.getName(), "servergroup"));
- group.setModified(true);
- addServerGroup(group, writer);
- }
-
- /**
- * Saves all entries in this list.
- */
- public void save() {
- for (Map.Entry<ServerGroup, ServerGroupWriter> pair : groups.entrySet()) {
- if (pair.getKey().isModified()) {
- pair.getValue().write(pair.getKey());
- }
- }
- }
-
- /**
- * Saves the specified group.
- *
- * @param group The group to be saved
- */
- @Precondition("Specified group is a known top-level group in this list")
- public void save(final ServerGroup group) {
- groups.get(group).write(group);
- }
-
- /**
- * Retrieves a list of all known server groups.
- *
- * @return An immutable list of server groups.
- */
- public Collection<ServerGroup> getServerGroups() {
- return Collections.unmodifiableCollection(groups.keySet());
- }
-
- /**
- * Retrieves a ServerGroup with the specified name, if one exists. This method ignores the case
- * of group's name when comparing.
- *
- * @param name The name of the group to be retrieved
- *
- * @return A correspondingly named server group, or null if none exists
- */
- public ServerGroup getGroupByName(final String name) {
- for (ServerGroup group : getServerGroups()) {
- if (group.getName().equalsIgnoreCase(name)) {
- return group;
- }
- }
-
- return null;
- }
-
- @Override
- public void configProviderAdded(final ConfigProvider configProvider) {
- try {
- final ServerGroupReader reader
- = new ServerGroupReader(connectionManager, identityController, configProvider);
- addServerGroup(reader.read(), reader.getWriter());
- } catch (IllegalArgumentException ex) {
- // Silently ignore
- // TODO: Raise error if the identity isn't a server group being
- // currently added by addServerGroup()
- }
- }
-
- @Override
- public void configProviderRemoved(final ConfigProvider configProvider) {
- // TODO: Remove server group
- }
-
- }
|