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.

WindowsInstaller.java 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  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.IOException;
  26. import java.util.ArrayList;
  27. /**
  28. * Installs DMDirc on windows
  29. *
  30. * @author Shane Mc Cormack
  31. */
  32. public class WindowsInstaller extends Installer {
  33. /** {@inheritDoc} */
  34. @Override
  35. public String defaultInstallLocation() {
  36. String result = "";
  37. if (CLIParser.getCLIParser().getParamNumber("-directory") > 0) {
  38. result = CLIParser.getCLIParser().getParam("-directory")
  39. .getStringValue();
  40. }
  41. if (result.isEmpty()) {
  42. String filename = System.getenv("PROGRAMFILES");
  43. if (filename == null) {
  44. if (isVista()) {
  45. filename = System.getProperty("user.home")
  46. + "\\Desktop\\DMDirc";
  47. } else {
  48. filename = "C:\\Program Files";
  49. }
  50. }
  51. result = filename + "\\DMDirc";
  52. }
  53. return result;
  54. }
  55. /** {@inheritDoc} */
  56. @Override
  57. public boolean validFile(final String filename) {
  58. return !filename.equalsIgnoreCase("setup.exe")
  59. && !filename.equalsIgnoreCase("jre.exe")
  60. && !filename.equalsIgnoreCase("wget.exe")
  61. && !filename.equalsIgnoreCase("wgetoutput")
  62. && !filename.equalsIgnoreCase("shortcut.exe");
  63. }
  64. /**
  65. * Are we running vista? -_-
  66. *
  67. * @return True if this is vista.
  68. */
  69. public boolean isVista() {
  70. return System.getProperty("os.name").indexOf("Vista") >= 0;
  71. }
  72. /**
  73. * Are we running NT?
  74. *
  75. * @return True if this is NT.
  76. */
  77. public boolean isNT() {
  78. final String osName = System.getProperty("os.name");
  79. return osName.indexOf("NT") >= 0 || osName.indexOf("2000") >= 0
  80. || osName.indexOf("2003") >= 0 || osName.indexOf("XP") >= 0;
  81. }
  82. /** {@inheritDoc} */
  83. @Override
  84. public boolean supportsShortcut(final ShortcutType shortcutType) {
  85. switch (shortcutType) {
  86. case QUICKLAUNCH:
  87. // Only windows 95 doesn't have quick launch
  88. return !(System.getProperty("os.name").indexOf("95") >= 0);
  89. case DESKTOP:
  90. case MENU:
  91. case UNINSTALLER:
  92. case PROTOCOL:
  93. // All versions of windows have desktop, menu, uninstaller and
  94. // protocol
  95. return true;
  96. default:
  97. // Anything else that gets added should be false until the
  98. // relevent
  99. // code is added
  100. return false;
  101. }
  102. }
  103. /** {@inheritDoc} */
  104. @Override
  105. public String getMenuName() {
  106. return "Start menu";
  107. }
  108. /**
  109. * Add a registry key.
  110. *
  111. * @param key
  112. * Key to add.
  113. */
  114. public void addRegistryKey(final String key) {
  115. step.addText(" - Adding Key: " + key);
  116. final String[] addKey = new String[] { "reg.exe", "add", key, "/f" };
  117. execAndWait(addKey);
  118. }
  119. /**
  120. * Modify a registry value.
  121. *
  122. * @param key
  123. * Key to use.
  124. * @param value
  125. * Value to modify.
  126. * @param data
  127. * Data for key.
  128. */
  129. public void editRegistryValue(final String key, final String value,
  130. final String data) {
  131. editRegistryValue(key, value, "REG_SZ", data);
  132. }
  133. /**
  134. * Modify a registry value.
  135. *
  136. * @param key
  137. * Key to use.
  138. * @param value
  139. * Value to modify.
  140. * @param type
  141. * Type of data.
  142. * @param data
  143. * Data for key.
  144. */
  145. public void editRegistryValue(final String key, final String value,
  146. final String type, final String data) {
  147. final ArrayList<String> params = new ArrayList<String>();
  148. step.addText(" - Editing value: " + key + "\\" + value);
  149. params.add("reg.exe");
  150. params.add("add");
  151. params.add(key);
  152. params.add("/f");
  153. if (value.isEmpty()) {
  154. params.add("/ve");
  155. } else {
  156. params.add("/v");
  157. params.add(value);
  158. }
  159. params.add("/t");
  160. params.add(type);
  161. if (!data.isEmpty()) {
  162. params.add("/d");
  163. params.add(data);
  164. }
  165. execAndWait(params.toArray(new String[params.size()]));
  166. }
  167. /**
  168. * Execute and wait for the requested command
  169. *
  170. * @param cmd
  171. * Command array to execute/
  172. * @return return value from command, or -1 if there was an error.
  173. */
  174. private int execAndWait(final String[] cmd) {
  175. try {
  176. final Process myProcess = Runtime.getRuntime().exec(cmd);
  177. new StreamReader(myProcess.getInputStream()).start();
  178. new StreamReader(myProcess.getErrorStream()).start();
  179. try {
  180. myProcess.waitFor();
  181. } catch (final InterruptedException e) {
  182. //Ignore, handled below
  183. }
  184. if (myProcess.exitValue() != 0) {
  185. step.addText("\t - Error: Unknown Reason");
  186. }
  187. return myProcess.exitValue();
  188. } catch (final SecurityException e) {
  189. step.addText("\t - Error: " + e.getMessage());
  190. } catch (final IOException e) {
  191. step.addText("\t - Error: " + e.getMessage());
  192. }
  193. return -1;
  194. }
  195. /** {@inheritDoc} */
  196. @Override
  197. public void setupShortcut(final String location,
  198. final ShortcutType shortcutType) {
  199. // Shortcut.exe is from http://www.optimumx.com/download/#Shortcut
  200. if (!supportsShortcut(shortcutType)) {
  201. step
  202. .addText(" - Error creating shortcut. Not applicable to this Operating System");
  203. return;
  204. }
  205. if (new File("Shortcut.exe").exists()) {
  206. String filename = "";
  207. File dir;
  208. switch (shortcutType) {
  209. case DESKTOP:
  210. if (isNT() || isVista()) {
  211. filename = System.getProperty("user.home")
  212. + "\\Desktop";
  213. } else {
  214. filename = System.getenv("WINDIR") + "\\Desktop";
  215. }
  216. break;
  217. case MENU:
  218. if (isVista()) {
  219. filename = System.getenv("APPDATA")
  220. + "\\Microsoft\\Windows";
  221. } else {
  222. filename = System.getProperty("user.home");
  223. }
  224. filename += "\\Start Menu\\Programs\\DMDirc";
  225. break;
  226. case QUICKLAUNCH:
  227. if (isVista()) {
  228. filename = System.getProperty("user.home")
  229. + "\\AppData\\Roaming\\Microsoft\\Internet Explorer\\Quick Launch";
  230. } else {
  231. filename = System.getProperty("user.home")
  232. + "\\Application Data\\Microsoft\\Internet Explorer\\Quick Launch";
  233. }
  234. break;
  235. case UNINSTALLER:
  236. // Registry hax!
  237. final String key = "HKLM\\SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Uninstall\\DMDirc";
  238. addRegistryKey(key);
  239. editRegistryValue(key, "Comments", "DMDirc IRC Client");
  240. editRegistryValue(key, "DisplayName", "DMDirc IRC Client");
  241. editRegistryValue(key, "DisplayIcon", location
  242. + "\\icon.ico");
  243. editRegistryValue(key, "UninstallString", location
  244. + "\\Uninstaller.exe");
  245. editRegistryValue(key, "Publisher", "DMDirc.com");
  246. editRegistryValue(key, "URLInfoAbout",
  247. "http://www.DMDirc.com/");
  248. editRegistryValue(key, "URLUpdateInfo",
  249. "http://www.DMDirc.com/");
  250. editRegistryValue(key, "InstallDir", location);
  251. editRegistryValue(key, "InstalledTime", String
  252. .valueOf(System.currentTimeMillis()));
  253. return;
  254. case PROTOCOL:
  255. // Add needed keys.
  256. addRegistryKey("HKCR\\irc");
  257. addRegistryKey("HKCR\\irc\\DefaultIcon");
  258. addRegistryKey("HKCR\\irc\\Shell");
  259. addRegistryKey("HKCR\\irc\\Shell\\open");
  260. addRegistryKey("HKCR\\irc\\Shell\\open\\command");
  261. // Now the values
  262. editRegistryValue("HKCR\\irc", "", "URL:IRC Protocol");
  263. editRegistryValue("HKCR\\irc", "URL Protocol", "");
  264. editRegistryValue("HKCR\\irc", "EditFlags", "REG_BINARY",
  265. "02000000");
  266. editRegistryValue("HKCR\\irc\\DefaultIcon", "", location
  267. + "\\icon.ico");
  268. editRegistryValue("HKCR\\irc\\Shell\\open\\command", "",
  269. "\\\"" + location + "\\DMDirc.exe\\\" -e -c %1");
  270. return;
  271. default:
  272. step
  273. .addText(" - Error creating shortcut. Not applicable to this Operating System");
  274. return;
  275. }
  276. if (filename.length() == 0) {
  277. step
  278. .addText(" - Error creating shortcut. Not applicable to this System");
  279. return;
  280. }
  281. // Check the dir exists
  282. dir = new File(filename);
  283. if (!dir.exists()) {
  284. dir.mkdir();
  285. }
  286. // Delete an older shortcut
  287. final File oldFile = new File(filename + "\\DMDirc.lnk");
  288. if (oldFile.exists()) {
  289. oldFile.delete();
  290. }
  291. // final String thisDirName = new File("").getAbsolutePath();
  292. final String[] command = new String[] {
  293. // thisDirName+"/Shortcut.exe",
  294. "Shortcut.exe",
  295. "/F:" + filename + "\\DMDirc.lnk",
  296. "/A:C",
  297. // "/T:"+location+"\\DMDirc.bat",
  298. // "/T:javaw.exe",
  299. // "/P:-jar DMDirc.jar",
  300. "/T:" + location + "\\DMDirc.exe", "/W:" + location,
  301. "/I:" + location + "\\icon.ico", "/D:DMDirc IRC Client" };
  302. execAndWait(command);
  303. } else {
  304. step
  305. .addText(" - Error creating shortcut: Unable to find Shortcut.exe");
  306. }
  307. }
  308. }