/*
 * Copyright (c) 2006-2011 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.plugins;

import com.dmdirc.config.prefs.PreferencesDialogModel;
import com.dmdirc.util.validators.ValidationResponse;

import java.io.File;

/**
 * Base implementation of the Plugin interface.
 */
public abstract class BasePlugin implements Plugin {

    /** Domain name for the settings in this plugin. */
    private String myDomain = "plugin-unknown";
    /** Has the domain been set? */
    private boolean domainSet;
    /** Associated Plugin info. */
    private PluginInfo pluginInfo;
    /** Files directory for this plugin. */
    private File filesDir;

    /** {@inheritDoc} */
    @Override
    public void setDomain(final String newDomain) {
        if (!domainSet) {
            domainSet = true;
            myDomain = newDomain;
            domainUpdated();
        }
    }

    /** {@inheritDoc} */
    @Override
    public void setPluginInfo(final PluginInfo pluginInfo) {
        this.pluginInfo = pluginInfo;
    }

    /** {@inheritDoc} */
    @Override
    public PluginInfo getPluginInfo() {
        return pluginInfo;
    }

    /** {@inheritDoc} */
    @Override
    public String getDomain() {
        return myDomain;
    }

    /**
     * Called when the domain for plugin settings has been set.
     * This will only be called once (either when the plugin is loading, or when
     * its config is being shown).
     */
    protected void domainUpdated() {
        //Define this here so only implementations that care have to override
    }

    /**
     * Get the files directory for this plugin.
     * This will attempt to create the directory if it doesn't exist the first
     * time the directory name is requested.
     *
     * @return Files directory for this plugin.
     */
    protected File getFilesDir() {
        if (filesDir == null) {
            final String fs = System.getProperty("file.separator");
            final String dir = PluginManager.getPluginManager()
                    .getFilesDirectory();
            filesDir = new File(dir + pluginInfo.getName() + fs);
            if (!filesDir.exists()) {
                filesDir.mkdirs();
            }
        }

        return filesDir;
    }

    /**
     * Convenience Method.
     *
     * @return Filesdir as a string with trailing path separator
     */
    protected String getFilesDirString() {
        return getFilesDir().getAbsolutePath()
                + System.getProperty("file.separator");
    }

    /** {@inheritDoc} */
    @Override
    public ValidationResponse checkPrerequisites() {
        return new ValidationResponse();
    }

    /** {@inheritDoc} */
    @Override
    public void showConfig(final PreferencesDialogModel manager) {
        //Define this here so only implementations that care have to override
    }
}

@@ -1178,16 +1178,159 @@
 ugins;%0A%0A
+import com.dmdirc.commandparser.CommandInfo;%0Aimport com.dmdirc.commandparser.CommandManager;%0Aimport com.dmdirc.commandparser.commands.Command;%0A
 import c
@@ -1451,16 +1451,92 @@
 io.File;
+%0Aimport java.util.HashMap;%0Aimport java.util.Map;%0Aimport java.util.Map.Entry;
 %0A%0A/**%0A *
@@ -1950,16 +1950,168 @@
 ilesDir;
+%0A    /** List of commands to load and unload. */%0A    private final Map%3CCommandInfo, Command%3E commands =%0A            new HashMap%3CCommandInfo, Command%3E();
 %0A%0A    /*
@@ -2323,32 +2323,1323 @@
        %7D%0A    %7D%0A%0A
+    /** %7B@inheritDoc%7D */%0A    @Override%0A    public void onLoad() %7B%0A        loadCommands();%0A    %7D%0A%0A    /** %7B@inheritDoc%7D */%0A    @Override%0A    public void onUnload() %7B%0A        unloadCommands();%0A    %7D%0A%0A    /**%0A     * Registers a command from this plugin.%0A     *%0A     * @param command Command to register%0A     * @param commandInfo Command info to register%0A     */%0A    protected void registerCommand(final Command command,%0A            final CommandInfo commandInfo) %7B%0A        commands.put(commandInfo, command);%0A    %7D%0A%0A    /**%0A     * Unregisters a command from this plugin.%0A     *%0A     * @param commandInfo Command info to register%0A     */%0A    protected void unregisterCommand(final CommandInfo commandInfo) %7B%0A        commands.remove(commandInfo);%0A    %7D%0A%0A    /** Loads the commands provided by this plugin. */%0A    private void loadCommands() %7B%0A        for (Entry%3CCommandInfo, Command%3E command : commands.entrySet()) %7B%0A            CommandManager.getCommandManager().registerCommand(%0A                    command.getValue(), command.getKey());%0A        %7D%0A    %7D%0A%0A    /** Unloads the commands loaded by this plugin. */%0A    private void unloadCommands() %7B%0A        for (CommandInfo command : commands.keySet()) %7B%0A            CommandManager.getCommandManager().unregisterCommand(command);%0A        %7D%0A    %7D%0A%0A
     /** %7B@inheri

Add command handling methods to BasePlugin