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

import com.dmdirc.plugins.PluginMetaData;

import java.io.File;

import lombok.Getter;

/**
 * Helper file to manage plugins that need extracted files on disk.
 */
public class PluginFilesHelper {

    /** This plugins meta data object. */
    private final PluginMetaData metaData;
    /** This plugins files directory. */
    @Getter
    private File filesDir;

    /**
     * Creates a new instance of this helper
     *
     * @param metaData This plugin's meta data
     */
    public PluginFilesHelper(final PluginMetaData metaData) {
        super();
        this.metaData = metaData;
        filesDir = initFilesDir();
    }

    /**
     * Initialises 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.
     */
    private File initFilesDir() {
        if (filesDir == null) {
            final String fs = System.getProperty("file.separator");
            final String dir = metaData.getManager().getFilesDirectory();
            filesDir = new File(dir + metaData.getName() + fs);
            if (!filesDir.exists()) {
                filesDir.mkdirs();
            }
        }
        return filesDir;
    }

    /**
     * Returns the path of the directory this plugin should use for files it
     * requires extracted.
     *
     * @return File directory as a string
     */
    public String getFilesDirString() {
        return getFilesDir().getAbsolutePath()
                + System.getProperty("file.separator");
    }
}

@@ -1166,16 +1166,54 @@
 tions;%0A%0A
+import com.dmdirc.plugins.PluginInfo;%0A
 import c
@@ -1267,16 +1267,44 @@
 io.File;
+%0Aimport java.io.IOException;
 %0A%0Aimport
@@ -1511,24 +1511,109 @@
 a metaData;%0A
+    /** This plugins information object. */%0A    private final PluginInfo pluginInfo;%0A
     /** This
@@ -1755,24 +1755,26 @@
  @param 
-metaData
+pluginInfo
  This pl
@@ -1780,25 +1780,34 @@
 lugin's 
-meta data
+information object
 %0A     */
@@ -1848,33 +1848,31 @@
 l Plugin
-MetaData metaData
+Info pluginInfo
 ) %7B%0A    
@@ -1901,27 +1901,81 @@
 his.
-metaData = m
+pluginInfo = pluginInfo;%0A        this.metaData = pluginInfo.getM
 etaData
+()
 ;%0A  
@@ -2983,11 +2983,1188 @@
 );%0A    %7D
+%0A%0A    /**%0A     * Extracts the specified resource to the specified directory.%0A     *%0A     * @param resourceName The name of the resource to extract%0A     *%0A     * @throws IOException if the write operation fails%0A     */%0A    public void extractResource(final String resourceName) throws IOException %7B%0A        pluginInfo.getResourceManager().extractResource(resourceName, filesDir.toString(), true);%0A    %7D%0A%0A    /**%0A     * Extracts files starting with the given prefix to the files directory.%0A     *%0A     * @param prefix Prefix to extract%0A     *%0A     * @throws IOException if the resources failed to extract%0A     */%0A    public void extractResoucesStartingWith(final String prefix) throws IOException %7B%0A        pluginInfo.getResourceManager().extractResources(prefix, filesDir.toString(), true);%0A    %7D%0A%0A    /**%0A     * Extracts files ending with the given suffix to the files directory.%0A     *%0A     * @param suffix Suffix to extract%0A     *%0A     * @throws IOException if the resources failed to extract%0A     */%0A    public void extractResoucesEndingWith(final String suffix) throws IOException %7B%0A        pluginInfo.getResourceManager().extractResoucesEndingWith(filesDir, suffix);%0A    %7D
 %0A%7D%0A

Simplify PluginsFilesHelper.