You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Installer.java 8.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. /*
  2. * Copyright (c) 2006-2011 Chris Smith, Shane Mc Cormack, Gregory Holmes
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a copy
  5. * of this software and associated documentation files (the "Software"), to deal
  6. * in the Software without restriction, including without limitation the rights
  7. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. * copies of the Software, and to permit persons to whom the Software is
  9. * furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice shall be included in
  12. * all copies or substantial portions of the Software.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  20. * SOFTWARE.
  21. */
  22. package com.dmdirc.installer;
  23. import com.dmdirc.installer.cliparser.CLIParser;
  24. import java.io.File;
  25. import java.io.FileInputStream;
  26. import java.io.FileOutputStream;
  27. import java.io.FilenameFilter;
  28. import java.io.IOException;
  29. import java.nio.channels.FileChannel;
  30. /**
  31. * Installs DMDirc.
  32. */
  33. public abstract class Installer extends Thread {
  34. /** Types of shortcut. */
  35. public static enum ShortcutType {
  36. /** Desktop shortcut. */
  37. DESKTOP,
  38. /** Menu (start/k/etc) shortcut. */
  39. MENU,
  40. /** Quick launch shortcut. */
  41. QUICKLAUNCH,
  42. /** The actual uninstaller (not a shortcut, as far as I can tell). */
  43. UNINSTALLER,
  44. /** Associate DMDirc with the irc:// protocol (not a shortcut). */
  45. PROTOCOL;
  46. }
  47. /** Step where the installation output should be. */
  48. protected TextStep step;
  49. /**
  50. * Create a new Installer.
  51. */
  52. public Installer() {
  53. super("Installer-Thread");
  54. }
  55. /**
  56. * Get the default install location.
  57. *
  58. * @return The default install location
  59. */
  60. public abstract String defaultInstallLocation();
  61. /**
  62. * This is what helps actually perform the installation in run().
  63. * This is a hack to keep the installing and the GUI separate.
  64. *
  65. * @param step The step that called this
  66. */
  67. public final void setInstallStep(final TextStep step) {
  68. this.step = step;
  69. }
  70. /** {@inheritDoc} */
  71. @Override
  72. public final void run() {
  73. step.setText("Beginning Install..\n");
  74. final boolean isUnattended = (CLIParser.getCLIParser().getParamNumber(
  75. "-unattended") != 0);
  76. final Settings settings = (isUnattended) ? new DefaultSettings() : ((Settings) Main.getWizardFrame().
  77. getStep(1));
  78. final String location = settings.getInstallLocation();
  79. step.addText("Installing files to: " + location);
  80. if (!doSetup(location)) {
  81. step.addText("");
  82. step.addText("Installation failed\n");
  83. Main.getWizardFrame().enableNextStep(true);
  84. return;
  85. }
  86. if (Main.getInstaller().supportsShortcut(ShortcutType.MENU)) {
  87. if (settings.getShortcutMenuState()) {
  88. step.addText("Setting up " + Main.getInstaller().getMenuName()
  89. + " shortcut");
  90. setupShortcut(location, ShortcutType.MENU);
  91. } else {
  92. step.addText("Not setting up "
  93. + Main.getInstaller().getMenuName() + " shortcut");
  94. }
  95. }
  96. if (Main.getInstaller().supportsShortcut(ShortcutType.DESKTOP)) {
  97. if (settings.getShortcutDesktopState()) {
  98. step.addText("Setting up Desktop shortcut");
  99. setupShortcut(location, ShortcutType.DESKTOP);
  100. } else {
  101. step.addText("Not setting up Desktop shortcut");
  102. }
  103. }
  104. if (Main.getInstaller().supportsShortcut(ShortcutType.QUICKLAUNCH)) {
  105. if (settings.getShortcutQuickState()) {
  106. step.addText("Setting up Quick Launch shortcut");
  107. setupShortcut(location, ShortcutType.QUICKLAUNCH);
  108. } else {
  109. step.addText("Not setting up Quick Launch shortcut");
  110. }
  111. }
  112. if (Main.getInstaller().supportsShortcut(ShortcutType.UNINSTALLER)) {
  113. step.addText("Creating uninstaller");
  114. setupShortcut(location, ShortcutType.UNINSTALLER);
  115. }
  116. if (Main.getInstaller().supportsShortcut(ShortcutType.PROTOCOL)) {
  117. if (settings.getShortcutProtocolState()) {
  118. step.addText("Setting up irc:// handler");
  119. setupShortcut(location, ShortcutType.PROTOCOL);
  120. } else {
  121. step.addText("Not setting up irc:// handler");
  122. }
  123. }
  124. postInstall(location);
  125. step.addText("");
  126. step.addText("Installation finished\n");
  127. Main.getWizardFrame().enableNextStep(true);
  128. }
  129. /**
  130. * Is the given file name vaild to copy to the installation directory?
  131. *
  132. * @param filename File to check
  133. * @return true If the file should be copied, else false.
  134. */
  135. public abstract boolean validFile(final String filename);
  136. /**
  137. * Main Setup stuff.
  138. *
  139. * @param location Location where app will be installed to.
  140. * @return True if installation passed, else false
  141. */
  142. public boolean doSetup(final String location) {
  143. // Create the directory
  144. final File directory = new File(location);
  145. if (!directory.exists()) {
  146. directory.mkdir();
  147. }
  148. try {
  149. final File dir = new File(".");
  150. final FilenameFilter filter = new FilenameFilter() {
  151. /** {@inheritDoc} */
  152. @Override
  153. public boolean accept(final File dir, final String name) {
  154. return name.charAt(0) != '.' && validFile(name);
  155. }
  156. };
  157. final String[] children = dir.list(filter);
  158. if (children != null) {
  159. for (String filename : children) {
  160. step.addText("Copying " + filename);
  161. copyFile(filename, location + File.separator + filename);
  162. }
  163. }
  164. } catch (IOException e) {
  165. step.addText("Error copying files: " + e.getMessage());
  166. return false;
  167. }
  168. step.addText("File Copying Complete.");
  169. return true;
  170. }
  171. /**
  172. * Check if this OS supports a given shortcut Type.
  173. *
  174. * @param shortcutType Type of shortcut to check
  175. * @return True if this OS supports a given shortcut Type
  176. */
  177. public boolean supportsShortcut(final ShortcutType shortcutType) {
  178. return false;
  179. }
  180. /**
  181. * Check what name to show for the menu shortcut
  182. *
  183. * @return Name for menu shortcutType
  184. */
  185. public String getMenuName() {
  186. return "menu";
  187. }
  188. /**
  189. * Setup shortcut.
  190. *
  191. * @param location Location where app will be installed to.
  192. * @param shortcutType Type of shortcut to add.
  193. */
  194. protected abstract void setupShortcut(final String location,
  195. final ShortcutType shortcutType);
  196. /**
  197. * Copy a file from one location to another.
  198. * Based on http://www.exampledepot.com/egs/java.io/CopyFile.html
  199. *
  200. * @param srcFile Original file
  201. * @param dstFile New file
  202. * @throws java.io.IOException If an exception occurs while copying
  203. */
  204. protected final void copyFile(final String srcFile, final String dstFile)
  205. throws IOException {
  206. if (new File(srcFile).exists()) {
  207. final FileChannel srcChannel = new FileInputStream(srcFile).
  208. getChannel();
  209. final FileChannel dstChannel = new FileOutputStream(dstFile).
  210. getChannel();
  211. dstChannel.transferFrom(srcChannel, 0, srcChannel.size());
  212. srcChannel.close();
  213. dstChannel.close();
  214. } else {
  215. throw new IOException(srcFile + " does not exist.");
  216. }
  217. }
  218. /**
  219. * Any post-install tasks should be done here.
  220. *
  221. * @param location Location where app was installed to.
  222. */
  223. public void postInstall(final String location) {
  224. // Nothing to do by default, installers may override
  225. }
  226. }