File Source: LinuxInstaller.java

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








SofCheck Inspector Build Version : 2.17854
LinuxInstaller.java 2009-Jun-25 01:54:24
LinuxInstaller.class 2009-Sep-02 17:04:16
LinuxInstaller$1.class 2009-Sep-02 17:04:16