1 6 package com.memoire.vainstall; 7 8 import java.io.File ; 9 import java.io.FileInputStream ; 10 import java.io.FileOutputStream ; 11 import java.io.IOException ; 12 import java.util.HashSet ; 13 import java.util.Iterator ; 14 import java.util.Set ; 15 16 20 public class VALinkWindows { 21 22 public static boolean move(File _from, File _to) throws IOException { 23 if (_from == null || !_from.exists()) 24 return false; 25 boolean b = _from.renameTo(_to); 26 if (b) 27 return true; 28 copy(_from, _to); 29 return _from.delete(); 30 } 31 32 public static void copy(File _from, File _to) throws IOException { 33 if (_from == null || !_from.exists()) 34 return; 35 FileOutputStream out = null; 36 FileInputStream in = null; 37 try { 38 out = new FileOutputStream (_to); 39 in = new FileInputStream (_from); 40 byte[] buf = new byte[2048]; 41 int read = in.read(buf); 42 while (read > 0) { 43 out.write(buf, 0, read); 44 read = in.read(buf); 45 } 46 } catch (IOException _e) { 47 throw _e; 48 } finally { 49 if (in != null) 50 in.close(); 51 if (out != null) 52 out.close(); 53 } 54 } 55 56 70 public static boolean create(VAShortcutEntry[] _launchparms, 71 File sharedDir, String _installClassName, Set shortcuts) 72 throws IOException { 73 try { 74 String linkname, iconfile; 75 76 if (_launchparms == null) 77 return false; 78 String section = null; 79 if ((!"applications".equals(VAGlobals.LINK_SECTION_NAME 80 .toLowerCase())) 81 && (!"utilities".equals(VAGlobals.LINK_SECTION_NAME 82 .toLowerCase())) 83 && (!"programs".equals(VAGlobals.LINK_SECTION_NAME 84 .toLowerCase()))) 85 section = VAGlobals.LINK_SECTION_NAME; 86 if (section == null 87 && (_launchparms.length > 1 || _launchparms.length == 1 88 && VAGlobals.CREATE_UNINSTALL_SHORTCUT)) { 89 section = VAGlobals.APP_NAME; 90 } 91 String menulinkdir = null; 92 Set shortcutToCreate = new HashSet (); 93 if (section != null) { 94 menulinkdir = JNIWindowsShortcut.getShortcutDir( 95 JNIWindowsShortcut.ON_START_MENU, 96 JNIWindowsShortcut.EVERYBODY) 97 + "\\" + section; 98 File mld = new File (menulinkdir); 99 if (!mld.exists()) { 100 if (!mld.mkdirs()) { 101 throw new IOException ("unable to create " + menulinkdir); 102 } 103 } else { 104 shortcuts.add(mld.getAbsolutePath()); 107 } 108 if (VAGlobals.DEBUG) 109 VAGlobals.printDebug("menu link dir=" + menulinkdir); 110 111 } 112 for (int i = 0; i < _launchparms.length; i++) { 113 VAShortcutEntry parmset = _launchparms[i]; 114 if (parmset.getIconPath() == null) { 115 if (parmset.isUninstall()) { 116 iconfile = getWindowsIconFile("uninstall", false); 117 } else 118 iconfile = getWindowsIconFile(parmset.getName(), true); 119 parmset.setIconPath(iconfile); 120 } 121 parmset.setWorkingDirectory(VAGlobals.DEST_PATH); 122 if (parmset.isCreateOnDesktop()) { 124 String shortcut = create(parmset, 125 JNIWindowsShortcut.ON_DESKTOP); 126 if (new File (shortcut).exists()) 128 shortcuts.add(shortcut); 129 } 130 String shortcut = null; 131 if (menulinkdir != null) { 132 if (VAGlobals.DEBUG) 133 VAGlobals.printDebug("menu to write=" 134 + parmset.getName()); 135 136 try { 137 shortcut = createCustom(parmset, menulinkdir + "\\" 138 + parmset.getName()); 139 } catch (IOException _e1) { 140 shortcut = create(parmset, 143 JNIWindowsShortcut.ON_START_MENU); 144 File shortcutFile = new File (shortcut); 145 if (shortcutFile.exists()) { 146 File destFile = new File (menulinkdir, shortcutFile 147 .getName()); 148 if (VAGlobals.DEBUG && shortcutFile.exists()) 149 VAGlobals.printDebug("rename shortcut from=\n " 150 + shortcutFile.getAbsolutePath() 151 + "\nto\n" + destFile); 152 move(shortcutFile, destFile); 153 if (shortcutFile.exists()) 154 shortcutFile.delete(); 155 shortcut = destFile.getAbsolutePath(); 156 } 157 } 158 if (VAGlobals.DEBUG) 159 VAGlobals.printDebug("menu written=" 160 + parmset.getName()); 161 } else { 162 shortcut = create(parmset, JNIWindowsShortcut.ON_START_MENU); 163 } 164 if (shortcut != null && new File (shortcut).exists()) { 165 shortcuts.add(shortcut); 166 shortcutToCreate.add(new File (shortcut)); 167 } 168 } 169 if (VAGlobals.SHORTCUTS_IN_INSTALLDIR) { 194 File dest = new File (VAGlobals.DEST_PATH); 195 for (Iterator it = shortcutToCreate.iterator(); it.hasNext();) { 196 File f = (File ) it.next(); 197 File shortcutInInstall = new File (dest, f.getName()); 198 copy(f, shortcutInInstall); 199 shortcuts.add(shortcutInInstall.getAbsolutePath()); 200 } 201 202 } 203 return true; 204 } catch (IOException _e) { 205 206 _e.printStackTrace(); 207 throw _e; 208 } 209 } 210 211 218 private static final String create(VAShortcutEntry parmset, int dest) 219 throws IOException { 220 return JNIWindowsShortcut.createShortcut(parmset.getExePath(), parmset 221 .getWorkingDirectory(), "", JNIWindowsShortcut.SHOW_NORMAL, parmset.getName(), dest, 223 JNIWindowsShortcut.EVERYBODY, parmset.getIconPath(), 0, parmset.getComment()); 226 } 227 228 233 private static final String createCustom(VAShortcutEntry parmset, 234 String dest) throws IOException { 235 return JNIWindowsShortcut.createShortcut(parmset.getExePath(), parmset 236 .getWorkingDirectory(), "", JNIWindowsShortcut.SHOW_NORMAL, dest, 238 JNIWindowsShortcut.CUSTOM_LOCATION, 239 JNIWindowsShortcut.EVERYBODY, parmset.getIconPath(), 0, parmset.getComment()); 242 } 243 244 249 private static final String getWindowsIconFile(String name, 250 boolean _useDefault) { 251 File defaultIcon = null; 253 if ((VAGlobals.LINK_ENTRY_ICON != null) 254 && (VAGlobals.LINK_ENTRY_ICON.length() > 0)) { 255 defaultIcon = new File (VAGlobals.DEST_PATH, 256 VAGlobals.LINK_ENTRY_ICON.replace('/', File.separatorChar) 257 + ".ico"); 258 VAGlobals.printDebug("default icon= " + defaultIcon); 259 } 260 File tryIcon; 261 if (defaultIcon != null) 262 tryIcon = new File (defaultIcon.getParentFile(), name + ".ico"); 263 else 264 tryIcon = new File (VAGlobals.DEST_PATH, name + ".ico"); 265 VAGlobals.printDebug("try icon= " + tryIcon); 266 if (tryIcon.exists()) { 267 return tryIcon.getAbsolutePath(); 268 } 269 if (_useDefault && defaultIcon.exists()) 270 return defaultIcon.getAbsolutePath(); 271 return null; 272 } 273 274 }
| Popular Tags
|