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.

LinuxInstaller.java 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  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.io.PrintWriter;
  27. /**
  28. * Installs DMDirc on linux
  29. *
  30. * @author Shane Mc Cormack
  31. */
  32. public class LinuxInstaller extends Installer {
  33. /**
  34. * Are we running as root?
  35. *
  36. * @return true iif running as root
  37. */
  38. private boolean isRoot() {
  39. return CLIParser.getCLIParser().getParamNumber("-isroot") > 0;
  40. }
  41. /** {@inheritDoc} */
  42. @Override
  43. public boolean validFile(final String filename) {
  44. return !filename.equalsIgnoreCase("setup.sh")
  45. && !filename.equalsIgnoreCase("getjre.sh")
  46. && !filename.equalsIgnoreCase("jre.bin")
  47. && !filename.equalsIgnoreCase("java-bin")
  48. && !filename.equalsIgnoreCase("progressbar.sh")
  49. && !filename.equalsIgnoreCase("installjre.sh")
  50. && !filename.matches(".*pipe.*");
  51. }
  52. /** {@inheritDoc} */
  53. @Override
  54. public String defaultInstallLocation() {
  55. String result = "";
  56. if (CLIParser.getCLIParser().getParamNumber("-directory") > 0) {
  57. return CLIParser.getCLIParser().getParam("-directory")
  58. .getStringValue();
  59. }
  60. if (result.isEmpty()) {
  61. if (isRoot()) {
  62. result = "/opt/dmdirc";
  63. } else {
  64. result = System.getProperty("user.home") + "/DMDirc";
  65. }
  66. }
  67. return result;
  68. }
  69. /** {@inheritDoc} */
  70. @Override
  71. public boolean supportsShortcut(final ShortcutType shortcutType) {
  72. switch (shortcutType) {
  73. case QUICKLAUNCH:
  74. return false;
  75. case DESKTOP:
  76. // No desktop for root
  77. return !isRoot();
  78. case PROTOCOL:
  79. case UNINSTALLER:
  80. case MENU:
  81. // Both root and non-root have a menu, uninstaller, and protocol
  82. return true;
  83. default:
  84. // Anything else that gets added should be false until the
  85. // relevent
  86. // code is added
  87. return false;
  88. }
  89. }
  90. /** {@inheritDoc} */
  91. @Override
  92. public void setupShortcut(final String location,
  93. final ShortcutType shortcutType) {
  94. if (!supportsShortcut(shortcutType)) {
  95. step
  96. .addText(" - Error creating shortcut. Not applicable to this Operating System");
  97. return;
  98. }
  99. PrintWriter writer = null;
  100. try {
  101. String filename = "";
  102. String command = "";
  103. switch (shortcutType) {
  104. case DESKTOP:
  105. filename = System.getProperty("user.home")
  106. + "/Desktop/DMDirc.desktop";
  107. break;
  108. case MENU:
  109. if (isRoot()) {
  110. filename = "/usr/share/applications/DMDirc.desktop";
  111. } else {
  112. filename = System.getProperty("user.home")
  113. + "/.local/share/applications/DMDirc.desktop";
  114. }
  115. break;
  116. case UNINSTALLER:
  117. // Write config for uninstaller
  118. writer = new PrintWriter(location + "/.uninstall.conf");
  119. writer.println("#!/bin/sh");
  120. writer.println("# DMDirc Uninstaller Settings");
  121. writer.println("INSTALLED_AS_ROOT="
  122. + (isRoot() ? "1" : "0"));
  123. writer.println("INSTALL_LOCATION=" + location);
  124. // Make sure uninstaller is executeable
  125. new File(location + "/uninstall.sh").setExecutable(true);
  126. return;
  127. case PROTOCOL:
  128. if (isRoot()) {
  129. command = "${TOOL} --config-source=`${TOOL} --get-default-source`";
  130. filename = "/usr/share/services/";
  131. } else {
  132. command = "${TOOL}";
  133. filename = "${HOME}/.kde/share/services/";
  134. }
  135. writer = new PrintWriter(location + "/protocolHandlers.sh");
  136. writer.println("#!/bin/sh");
  137. writer.println("TOOL=`which gconftool-2`");
  138. writer.println("if [ \"${TOOL}\" != \"\" ]; then");
  139. writer
  140. .println("\t"
  141. + command
  142. + " --set --type=bool /desktop/gnome/url-handlers/irc/enabled true");
  143. writer
  144. .println("\t"
  145. + command
  146. + " --set --type=string /desktop/gnome/url-handlers/irc/command \"\\\""
  147. + location + "/DMDirc.sh\\\" -e -c %s\"");
  148. writer
  149. .println("\t"
  150. + command
  151. + " --set --type=bool /desktop/gnome/url-handlers/irc/need-terminal false");
  152. writer.println("fi");
  153. writer.println("if [ ! -e \"" + filename + "\" ]; then");
  154. writer.println("\tmkdir -p \"" + filename + "\"");
  155. writer.println("fi");
  156. writer.println("echo \"[Protocol]\" > " + filename
  157. + "irc.protocol");
  158. writer.println("echo \"exec=\"" + location
  159. + "/DMDirc.sh\" -e -c %u\" >> " + filename
  160. + "irc.protocol");
  161. writer.println("echo \"protocol=irc\" >> " + filename
  162. + "irc.protocol");
  163. writer.println("echo \"input=none\" >> " + filename
  164. + "irc.protocol");
  165. writer.println("echo \"output=none\" >> " + filename
  166. + "irc.protocol");
  167. writer.println("echo \"helper=true\" >> " + filename
  168. + "irc.protocol");
  169. writer.println("echo \"listing=false\" >> " + filename
  170. + "irc.protocol");
  171. writer.println("echo \"reading=false\" >> " + filename
  172. + "irc.protocol");
  173. writer.println("echo \"writing=false\" >> " + filename
  174. + "irc.protocol");
  175. writer.println("echo \"makedir=false\" >> " + filename
  176. + "irc.protocol");
  177. writer.println("echo \"deleting=false\" >> " + filename
  178. + "irc.protocol");
  179. writer.println("chmod a+x " + filename + "irc.protocol");
  180. writer.println("exit 0;");
  181. writer.close();
  182. final File protocolFile = new File(location
  183. + "/protocolHandlers.sh");
  184. protocolFile.setExecutable(true);
  185. try {
  186. final Process gconfProcess = Runtime.getRuntime().exec(
  187. new String[] { "/bin/sh",
  188. location + "/protocolHandlers.sh" });
  189. new StreamReader(gconfProcess.getInputStream()).start();
  190. new StreamReader(gconfProcess.getErrorStream()).start();
  191. try {
  192. gconfProcess.waitFor();
  193. } catch (final InterruptedException e) {
  194. }
  195. protocolFile.delete();
  196. } catch (final SecurityException e) {
  197. step.addText(" - Error adding Protocol Handler: "
  198. + e.getMessage());
  199. } catch (final IOException e) {
  200. step.addText(" - Error adding Protocol Handler: "
  201. + e.getMessage());
  202. }
  203. return;
  204. default:
  205. step
  206. .addText(" - Error creating shortcut. Not applicable to this Operating System");
  207. return;
  208. }
  209. final File outFile = new File(filename);
  210. if (!outFile.getParentFile().exists()) {
  211. outFile.getParentFile().mkdir();
  212. }
  213. writer = new PrintWriter(outFile);
  214. writeFile(writer, location);
  215. outFile.setExecutable(true);
  216. } catch (final IOException e) {
  217. step.addText(" - Error creating shortcut: " + e.toString());
  218. } catch (final SecurityException e) {
  219. step.addText(" - Error creating shortcut: " + e.toString());
  220. } finally {
  221. if (writer != null) {
  222. writer.close();
  223. }
  224. }
  225. }
  226. /**
  227. * Write the .desktop file
  228. *
  229. * @param writer
  230. * PrintWriter to write to
  231. * @param location
  232. * Location of installed files
  233. * @throws IOException
  234. * if an error occurs when writing
  235. */
  236. private void writeFile(final PrintWriter writer, final String location)
  237. throws IOException {
  238. writer.println("[Desktop Entry]");
  239. writer.println("Categories=Network;IRCClient;");
  240. writer.println("Comment=DMDirc IRC Client");
  241. writer.println("Encoding=UTF-8");
  242. // writer.println("Exec=java -jar "+location+"/DMDirc.jar");
  243. writer.println("Exec=" + location + "/DMDirc.sh");
  244. writer.println("GenericName=IRC Client");
  245. writer.println("Icon=" + location + "/icon.svg");
  246. if (isRoot()) {
  247. writer.println("Name=DMDirc (Global)");
  248. } else {
  249. writer.println("Name=DMDirc");
  250. }
  251. writer.println("StartupNotify=true");
  252. writer.println("Terminal=false");
  253. writer.println("TerminalOptions=");
  254. writer.println("Type=Application");
  255. }
  256. /** {@inheritDoc} */
  257. @Override
  258. public void postInstall(final String location) {
  259. new File(location + "/DMDirc.sh").setExecutable(true, !isRoot());
  260. }
  261. }