123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398 |
- /*
- * Copyright (c) 2006-2007 Chris Smith, Shane Mc Cormack, Gregory Holmes
- *
- * 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.config;
-
- import com.dmdirc.Main;
- import com.dmdirc.Precondition;
- import com.dmdirc.logger.ErrorLevel;
- import com.dmdirc.logger.Logger;
-
- import java.io.File;
- import java.io.IOException;
- import java.io.InputStream;
- import java.util.ArrayList;
- import java.util.Collections;
- import java.util.List;
-
- /**
- * The identity manager manages all known identities, providing easy methods
- * to access them.
- * @author chris
- */
- public final class IdentityManager {
-
- /** The identities that have been loaded into this manager. */
- private final static List<Identity> identities = new ArrayList<Identity>();
-
- /** The config managers that have registered with this manager. */
- // XXX: Should this be a weak list?
- private final static List<ConfigManager> managers = new ArrayList<ConfigManager>();
-
- /** The identity file used for the global config. */
- private static Identity config;
-
- /** The config manager used for global settings. */
- private static ConfigManager globalconfig;
-
- /** Creates a new instance of IdentityManager. */
- private IdentityManager() {
- }
-
- /** Loads all identity files. */
- public static void load() {
- identities.clear();
- managers.clear();
-
- loadDefaults();
- loadUser();
- loadConfig();
-
- if (getProfiles().size() == 0) {
- Identity.buildProfile("Default Profile");
- }
- }
-
- /** Loads the default (built in) identities. */
- private static void loadDefaults() {
- final ClassLoader cldr = IdentityManager.class.getClassLoader();
-
- final String base = "com/dmdirc/config/defaults/";
-
- final String[] urls = {"default/defaults", "default/formatter"};
-
- for (String url : urls) {
- try {
- final InputStream res = cldr.getResourceAsStream(base + url);
- if (res == null) {
- Logger.userError(ErrorLevel.MEDIUM,
- "Unable to load default identity: " + url);
- } else {
- addIdentity(new Identity(res, false));
- }
- } catch (InvalidIdentityFileException ex) {
- Logger.userError(ErrorLevel.MEDIUM, "Invalid identity file");
- } catch (IOException ex) {
- Logger.userError(ErrorLevel.MEDIUM, "Unable to load identity file");
- }
- }
- }
-
- /**
- * Retrieves the directory used to store identities in.
- *
- * @return The identity directory path
- */
- public static String getDirectory() {
- return Main.getConfigDir() + "identities" + System.getProperty("file.separator");
- }
-
- /** Loads user-defined identity files. */
- public static void loadUser() {
- final File dir = new File(getDirectory());
-
- if (!dir.exists()) {
- try {
- dir.mkdirs();
- dir.createNewFile();
- } catch (IOException ex) {
- Logger.userError(ErrorLevel.MEDIUM, "Unable to create identity dir");
- }
- }
-
- loadUser(dir);
- }
-
- /**
- * Recursively loads files from the specified directory.
- *
- * @param dir The directory to be loaded
- */
- @Precondition({
- "The specified File is not null",
- "The specified File is a directory"
- })
- private static void loadUser(final File dir) {
- assert(dir != null);
- assert(dir.isDirectory());
-
- if (dir.listFiles() == null) {
- Logger.userError(ErrorLevel.MEDIUM,
- "Unable to load user identity files from "
- + dir.getAbsolutePath());
- } else {
- for (File file : dir.listFiles()) {
- if (file.isDirectory()) {
- loadUser(file);
- } else {
- try {
- addIdentity(new Identity(file, false));
- } catch (InvalidIdentityFileException ex) {
- Logger.userError(ErrorLevel.MEDIUM,
- "Invalid identity file: " + file.getAbsolutePath()
- + " (" + ex.getMessage() + ")");
- } catch (IOException ex) {
- Logger.userError(ErrorLevel.MEDIUM,
- "I/O error when reading identity file: "
- + file.getAbsolutePath());
- }
- }
- }
- }
- }
-
- /** Loads the config identity. */
- private static void loadConfig() {
- try {
- final File file = new File(Main.getConfigDir() + "dmdirc.config");
-
- if (!file.exists()) {
- file.createNewFile();
- }
-
- config = new Identity(file, true);
- config.setOption("identity", "name", "Global config");
- addIdentity(config);
- } catch (InvalidIdentityFileException ex) {
- // This shouldn't happen as we're forcing it to global
- Logger.appError(ErrorLevel.HIGH, "Unable to load global config", ex);
- } catch (IOException ex) {
- Logger.userError(ErrorLevel.MEDIUM, "I/O error when loading file: "
- + ex.getMessage());
- }
- }
-
- /**
- * Retrieves the identity used for the global config.
- *
- * @return The global config identity
- */
- public static Identity getConfigIdentity() {
- return config;
- }
-
- /**
- * Saves all modified identity files to disk.
- */
- public static void save() {
- for (Identity identity : identities) {
- identity.save();
- }
- }
-
- /**
- * Adds the specific identity to this manager.
- * @param identity The identity to be added
- */
- @Precondition("The specified Identity is not null")
- public static void addIdentity(final Identity identity) {
- assert(identity != null);
-
- if (identities.contains(identity)) {
- removeIdentity(identity);
- }
-
- identities.add(identity);
-
- for (ConfigManager manager : managers) {
- manager.checkIdentity(identity);
- }
- }
-
- /**
- * Removes an identity from this manager.
- * @param identity The identity to be removed
- */
- @Precondition({
- "The specified Identity is not null",
- "The specified Identity has previously been added and not removed"
- })
- public static void removeIdentity(final Identity identity) {
- assert(identity != null);
- assert(identities.contains(identity));
-
- identities.remove(identity);
- }
-
- /**
- * Adds a config manager to this manager.
- * @param manager The ConfigManager to add
- */
- @Precondition("The specified ConfigManager is not null")
- public static void addConfigManager(final ConfigManager manager) {
- assert(manager != null);
-
- managers.add(manager);
- }
-
- /**
- * Retrieves a list of identities that serve as profiles.
- * @return A list of profiles
- */
- public static List<Identity> getProfiles() {
- final List<Identity> profiles = new ArrayList<Identity>();
-
- for (Identity identity : identities) {
- if (identity.isProfile()) {
- profiles.add(identity);
- }
- }
-
- return profiles;
- }
-
- /**
- * Retrieves a list of all config sources that should be applied to the
- * specified target.
- * @param ircd The server's ircd
- * @param network The name of the network
- * @param server The server's name
- * @param channel The channel name (in the form channel@network)
- * @return A list of all matching config sources
- */
- public static List<Identity> getSources(final String ircd,
- final String network, final String server, final String channel) {
-
- final List<Identity> sources = new ArrayList<Identity>();
-
- String comp = "";
-
- for (Identity identity : identities) {
- switch (identity.getTarget().getType()) {
- case IRCD:
- comp = ircd;
- break;
- case NETWORK:
- comp = network;
- break;
- case SERVER:
- comp = server;
- break;
- case CHANNEL:
- comp = channel;
- break;
- case PROFILE:
- comp = null;
- break;
- default:
- comp = "";
- break;
- }
-
- if (comp != null && comp.equalsIgnoreCase(identity.getTarget().getData())) {
- sources.add(identity);
- }
- }
-
- Collections.sort(sources);
-
- return sources;
- }
-
- /**
- * Retrieves a list of all config sources that should be applied to the
- * specified target.
- * @param ircd The server's ircd
- * @param network The name of the network
- * @param server The server's name
- * @return A list of all matching config sources
- */
- public static List<Identity> getSources(final String ircd,
- final String network, final String server) {
- return getSources(ircd, network, server, "<Unknown>");
- }
-
- /**
- * Retrieves the global config manager.
- *
- * @return The global config manager
- */
- public static synchronized ConfigManager getGlobalConfig() {
- if (globalconfig == null) {
- globalconfig = new ConfigManager("", "", "");
- }
-
- return globalconfig;
- }
-
- /**
- * Retrieves the config for the specified channel@network. The config is
- * created if it doesn't exist.
- * @param network The name of the network
- * @param channel The name of the channel
- * @return A config source for the channel
- */
- @Precondition({
- "The specified network is non-null and not empty",
- "The specified channel is non-null and not empty"
- })
- public static Identity getChannelConfig(final String network,
- final String channel) {
- assert(network != null);
- assert(!network.isEmpty());
- assert(channel != null);
- assert(!channel.isEmpty());
-
- final String myTarget = (channel + "@" + network).toLowerCase();
-
- for (Identity identity : identities) {
- if (identity.getTarget().getType() == ConfigTarget.TYPE.CHANNEL
- && identity.getTarget().getData().equalsIgnoreCase(myTarget)) {
- return identity;
- }
- }
-
- // We need to create one
- final ConfigTarget target = new ConfigTarget();
- target.setChannel(myTarget);
-
- return Identity.buildIdentity(target);
- }
-
- /**
- * Retrieves the config for the specified network. The config is
- * created if it doesn't exist.
- * @param network The name of the network
- * @return A config source for the network
- */
- @Precondition("The specified network is non-null and not empty")
- public static Identity getNetworkConfig(final String network) {
- assert(network != null);
- assert(!network.isEmpty());
-
- final String myTarget = network.toLowerCase();
-
- for (Identity identity : identities) {
- if (identity.getTarget().getType() == ConfigTarget.TYPE.NETWORK
- && identity.getTarget().getData().equalsIgnoreCase(myTarget)) {
- return identity;
- }
- }
-
- // We need to create one
- final ConfigTarget target = new ConfigTarget();
- target.setNetwork(myTarget);
-
- return Identity.buildIdentity(target);
- }
-
- }
|