123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163 |
- /*
- * Copyright (c) 2006-2008 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.installer;
-
- import java.io.FileInputStream;
- import java.io.FileOutputStream;
- import java.io.IOException;
- import java.io.File;
- import java.io.FilenameFilter;
- import java.nio.channels.FileChannel;
-
- /**
- * Installs DMDirc
- *
- * @author Shane Mc Cormack
- */
- public abstract class Installer extends Thread {
- /** Types of shortcut */
- public static enum ShortcutType { DESKTOP, MENU, QUICKLAUNCH, UNINSTALLER, PROTOCOL; }
-
- /** Step where things happen. */
- protected StepInstall step = null;
-
- /**
- * Get the default install location
- */
- abstract String defaultInstallLocation();
-
- /**
- * This is what helps actually perform the installation in run().
- * This is a hack to keep the installing and the GUI separate.
- *
- * @param step The step that called this
- */
- public final void setInstallStep(final StepInstall step) {
- this.step = step;
- }
-
- /**
- * Create a new Installer
- */
- public Installer() {
- super("Installer-Thread");
- }
-
- /**
- * This step performs the installation, via the StepInstall step.
- */
- @Override
- public final void run() {
- step.performInstall(this);
- }
-
- /**
- * Is the given file name vaild to copy to the installation directory?
- *
- * @param filename File to check
- * @return true If the file should be copied, else false.
- */
- public abstract boolean validFile(final String filename);
-
- /**
- * Main Setup stuff
- *
- * @param location Location where app will be installed to.
- * @return True if installation passed, else false;
- */
- public boolean doSetup(final String location) {
- // Create the directory
- final File directory = new File(location);
- if (!directory.exists()) { directory.mkdir(); }
-
- try {
- final File dir = new File(".");
- final FilenameFilter filter = new FilenameFilter() {
- public boolean accept(final File dir, final String name) {
- return name.charAt(0) != '.' &&
- !name.equalsIgnoreCase("installer.jar") &&
- validFile(name);
- }
- };
- final String[] children = dir.list(filter);
- if (children != null) {
- for (String filename : children) {
- step.addText("Copying "+filename);
- copyFile(filename, location+File.separator+filename);
- }
- }
- } catch (IOException e) {
- step.addText("Error copying files: "+e.getMessage());
- return false;
- }
- step.addText("File Copying Complete.");
- return true;
- }
-
- /**
- * Check if this OS supports a given shortcut Type
- *
- * @param shortcutType Type of shortcut to check
- * @return True if this OS supports a given shortcut Type
- */
- public boolean supportsShortcut(final ShortcutType shortcutType) {
- return false;
- }
-
- /**
- * Setup shortcut
- *
- * @param location Location where app will be installed to.
- * @param shortcutType Type of shortcut to add.
- */
- abstract void setupShortcut(final String location, final ShortcutType shortcutType);
-
- /**
- * Copy a file from one location to another.
- * Based on http://www.exampledepot.com/egs/java.io/CopyFile.html
- *
- * @param srcFile Original file
- * @param dstFile New file
- */
- protected final void copyFile(final String srcFile, final String dstFile) throws IOException {
- if (new File(srcFile).exists()) {
- final FileChannel srcChannel = new FileInputStream(srcFile).getChannel();
- final FileChannel dstChannel = new FileOutputStream(dstFile).getChannel();
-
- dstChannel.transferFrom(srcChannel, 0, srcChannel.size());
-
- srcChannel.close();
- dstChannel.close();
- } else {
- throw new IOException(srcFile+" does not exist.");
- }
- }
-
-
- /**
- * Any post-install tasks should be done here.
- *
- * @param location Location where app was installed to.
- */
- public void postInstall(final String location) { }
- }
|