001: /**
002: * $RCSfile: VALinkWindows.java,v $
003: * @creation 12/03/00
004: * @modification $Date: 2005/03/07 20:52:02 $
005: */package com.memoire.vainstall;
006:
007: import java.io.File;
008: import java.io.FileInputStream;
009: import java.io.FileOutputStream;
010: import java.io.IOException;
011: import java.util.HashSet;
012: import java.util.Iterator;
013: import java.util.Set;
014:
015: /**
016: * @version $Id: VALinkWindows.java,v 1.8 2005/03/07 20:52:02 deniger Exp $
017: * @author Axel von Arnim
018: */
019: public class VALinkWindows {
020:
021: public static boolean move(File _from, File _to) throws IOException {
022: if (_from == null || !_from.exists())
023: return false;
024: boolean b = _from.renameTo(_to);
025: if (b)
026: return true;
027: copy(_from, _to);
028: return _from.delete();
029: }
030:
031: public static void copy(File _from, File _to) throws IOException {
032: if (_from == null || !_from.exists())
033: return;
034: FileOutputStream out = null;
035: FileInputStream in = null;
036: try {
037: out = new FileOutputStream(_to);
038: in = new FileInputStream(_from);
039: byte[] buf = new byte[2048];
040: int read = in.read(buf);
041: while (read > 0) {
042: out.write(buf, 0, read);
043: read = in.read(buf);
044: }
045: } catch (IOException _e) {
046: throw _e;
047: } finally {
048: if (in != null)
049: in.close();
050: if (out != null)
051: out.close();
052: }
053: }
054:
055: /**
056: * Create one Windows shortcut for each javalauncher script that was
057: * created.
058: * <p>
059: * Links will be named after the corresponding launch scripts.
060: * <p>
061: * Icons can be used. If in the install dir an icon file (suffix .ico)
062: * exists that matches the name of the launcher, it will be used as the icon
063: * for that launcher. Alternatively, if an icon file exists in the install
064: * dir that matches LINK_ENTRY_ICON, that icon file will be used. Otherwise,
065: * no icon is used.
066: *
067: * @see com.memoire.vainstall.VASetup
068: */
069: public static boolean create(VAShortcutEntry[] _launchparms,
070: File sharedDir, String _installClassName, Set shortcuts)
071: throws IOException {
072: try {
073: String linkname, iconfile;
074:
075: if (_launchparms == null)
076: return false;
077: String section = null;
078: if ((!"applications".equals(VAGlobals.LINK_SECTION_NAME
079: .toLowerCase()))
080: && (!"utilities".equals(VAGlobals.LINK_SECTION_NAME
081: .toLowerCase()))
082: && (!"programs".equals(VAGlobals.LINK_SECTION_NAME
083: .toLowerCase())))
084: section = VAGlobals.LINK_SECTION_NAME;
085: if (section == null
086: && (_launchparms.length > 1 || _launchparms.length == 1
087: && VAGlobals.CREATE_UNINSTALL_SHORTCUT)) {
088: section = VAGlobals.APP_NAME;
089: }
090: String menulinkdir = null;
091: Set shortcutToCreate = new HashSet();
092: if (section != null) {
093: menulinkdir = JNIWindowsShortcut.getShortcutDir(
094: JNIWindowsShortcut.ON_START_MENU,
095: JNIWindowsShortcut.EVERYBODY)
096: + "\\" + section;
097: File mld = new File(menulinkdir);
098: if (!mld.exists()) {
099: if (!mld.mkdirs()) {
100: throw new IOException("unable to create "
101: + menulinkdir);
102: }
103: } else {
104: //add the new folder to the shortcut lists ->delete when
105: // uninstall
106: shortcuts.add(mld.getAbsolutePath());
107: }
108: if (VAGlobals.DEBUG)
109: VAGlobals
110: .printDebug("menu link dir=" + menulinkdir);
111:
112: }
113: for (int i = 0; i < _launchparms.length; i++) {
114: VAShortcutEntry parmset = _launchparms[i];
115: if (parmset.getIconPath() == null) {
116: if (parmset.isUninstall()) {
117: iconfile = getWindowsIconFile("uninstall",
118: false);
119: } else
120: iconfile = getWindowsIconFile(
121: parmset.getName(), true);
122: parmset.setIconPath(iconfile);
123: }
124: parmset.setWorkingDirectory(VAGlobals.DEST_PATH);
125: //we don't create a shortcut if the user does not want to
126: if (parmset.isCreateOnDesktop()) {
127: String shortcut = create(parmset,
128: JNIWindowsShortcut.ON_DESKTOP);
129: //if created
130: if (new File(shortcut).exists())
131: shortcuts.add(shortcut);
132: }
133: String shortcut = null;
134: if (menulinkdir != null) {
135: if (VAGlobals.DEBUG)
136: VAGlobals.printDebug("menu to write="
137: + parmset.getName());
138:
139: try {
140: shortcut = createCustom(parmset, menulinkdir
141: + "\\" + parmset.getName());
142: } catch (IOException _e1) {
143: //Fred : there is bug with the accent é,é
144: //I don't know how to solve it ....
145: shortcut = create(parmset,
146: JNIWindowsShortcut.ON_START_MENU);
147: File shortcutFile = new File(shortcut);
148: if (shortcutFile.exists()) {
149: File destFile = new File(menulinkdir,
150: shortcutFile.getName());
151: if (VAGlobals.DEBUG
152: && shortcutFile.exists())
153: VAGlobals
154: .printDebug("rename shortcut from=\n "
155: + shortcutFile
156: .getAbsolutePath()
157: + "\nto\n" + destFile);
158: move(shortcutFile, destFile);
159: if (shortcutFile.exists())
160: shortcutFile.delete();
161: shortcut = destFile.getAbsolutePath();
162: }
163: }
164: if (VAGlobals.DEBUG)
165: VAGlobals.printDebug("menu written="
166: + parmset.getName());
167: } else {
168: shortcut = create(parmset,
169: JNIWindowsShortcut.ON_START_MENU);
170: }
171: if (shortcut != null && new File(shortcut).exists()) {
172: shortcuts.add(shortcut);
173: shortcutToCreate.add(new File(shortcut));
174: }
175: }
176: //the shortcut for the uninstaller is already created
177: // if (VAGlobals.CREATE_UNINSTALL_SHORTCUT) {
178: // int location = JNIWindowsShortcut.ON_START_MENU;
179: // if (menulinkdir != null) {
180: // location = JNIWindowsShortcut.CUSTOM_LOCATION;
181: // linkname = menulinkdir + "\\";
182: // } else {
183: // linkname = "";
184: // }
185: // linkname += "Uninstall " + VAGlobals.APP_NAME;
186: // iconfile = getWindowsIconFile("uninstall");
187: // String shortcut = JNIWindowsShortcut.createShortcut(
188: // VAGlobals.DEST_PATH
189: // + System.getProperty("file.separator")
190: // + "uninstall_" + VAGlobals.APP_NAME + "_"
191: // + VAGlobals.APP_VERSION + ".bat",
192: // VAGlobals.DEST_PATH, "", // no arguments
193: // JNIWindowsShortcut.SHOW_NORMAL, linkname, location,
194: // JNIWindowsShortcut.EVERYBODY, iconfile, 0, // icon
195: // // offset
196: // "" // description
197: // );
198: // shortcuts.add(shortcut);
199: // }
200: if (VAGlobals.SHORTCUTS_IN_INSTALLDIR) {
201: File dest = new File(VAGlobals.DEST_PATH);
202: for (Iterator it = shortcutToCreate.iterator(); it
203: .hasNext();) {
204: File f = (File) it.next();
205: File shortcutInInstall = new File(dest, f.getName());
206: copy(f, shortcutInInstall);
207: shortcuts.add(shortcutInInstall.getAbsolutePath());
208: }
209:
210: }
211: return true;
212: } catch (IOException _e) {
213:
214: _e.printStackTrace();
215: throw _e;
216: }
217: }
218:
219: /**
220: * Create a short cut from the entry.
221: *
222: * @parm dest should be one of JNIWindowsShortcut.ON_DESKTOP or
223: * JNIWindowsShortcut.ON_START_MENU.
224: *
225: */
226: private static final String create(VAShortcutEntry parmset, int dest)
227: throws IOException {
228: return JNIWindowsShortcut.createShortcut(parmset.getExePath(),
229: parmset.getWorkingDirectory(),
230: "", // no arguments
231: JNIWindowsShortcut.SHOW_NORMAL, parmset.getName(),
232: dest, JNIWindowsShortcut.EVERYBODY, parmset
233: .getIconPath(), 0, // icon
234: // offset
235: parmset.getComment());
236: }
237:
238: /**
239: * @parm dest should be one of JNIWindowsShortcut.ON_DESKTOP or
240: * JNIWindowsShortcut.ON_START_MENU.
241: *
242: */
243: private static final String createCustom(VAShortcutEntry parmset,
244: String dest) throws IOException {
245: return JNIWindowsShortcut.createShortcut(
246: parmset.getExePath(),
247: parmset.getWorkingDirectory(),
248: "", // no arguments
249: JNIWindowsShortcut.SHOW_NORMAL, dest,
250: JNIWindowsShortcut.CUSTOM_LOCATION,
251: JNIWindowsShortcut.EVERYBODY, parmset.getIconPath(), 0, // icon
252: // offset
253: parmset.getComment());
254: }
255:
256: /**
257: * Return the absolute path of the icon for the <code>name</code>
258: * executable. If this icon can't be found, the default icon
259: * <code>LINK_ENTRY_ICON</code> is tried.
260: */
261: private static final String getWindowsIconFile(String name,
262: boolean _useDefault) {
263: // get the default icon
264: File defaultIcon = null;
265: if ((VAGlobals.LINK_ENTRY_ICON != null)
266: && (VAGlobals.LINK_ENTRY_ICON.length() > 0)) {
267: defaultIcon = new File(VAGlobals.DEST_PATH,
268: VAGlobals.LINK_ENTRY_ICON.replace('/',
269: File.separatorChar)
270: + ".ico");
271: VAGlobals.printDebug("default icon= " + defaultIcon);
272: }
273: File tryIcon;
274: if (defaultIcon != null)
275: tryIcon = new File(defaultIcon.getParentFile(), name
276: + ".ico");
277: else
278: tryIcon = new File(VAGlobals.DEST_PATH, name + ".ico");
279: VAGlobals.printDebug("try icon= " + tryIcon);
280: if (tryIcon.exists()) {
281: return tryIcon.getAbsolutePath();
282: }
283: if (_useDefault && defaultIcon.exists())
284: return defaultIcon.getAbsolutePath();
285: return null;
286: }
287:
288: }
|