File Source: WindowsInstaller.java

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








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