001: /**
002: * $RCSfile: VALinkGnome.java,v $
003: * @creation 28/03/00
004: * @modification $Date: 2005/03/30 19:42:44 $
005: * Bug-fixing and additional features by Antonio Petrelli
006: */package com.memoire.vainstall;
007:
008: import java.io.File;
009: import java.io.FileNotFoundException;
010: import java.io.FileOutputStream;
011: import java.io.IOException;
012: import java.io.PrintWriter;
013: import java.util.Date;
014: import java.util.Set;
015:
016: /**
017: * @version $Id: VALinkGnome.java,v 1.7 2005/03/30 19:42:44 deniger Exp $
018: * @author Guillaume Desnoix
019: */
020:
021: public class VALinkGnome {
022: private static String getIcon(String _name) {
023: File common_icon = null;
024: if ((VAGlobals.LINK_ENTRY_ICON != null)
025: && (VAGlobals.LINK_ENTRY_ICON.length() > 0)) {
026: common_icon = new File(VAGlobals.DEST_PATH,
027: VAGlobals.LINK_ENTRY_ICON.replace('/',
028: File.separatorChar)
029: + ".png");
030: VAGlobals.printDebug("common_icon=" + common_icon);
031: }
032: File script_icon = null;
033: if (common_icon != null)
034: script_icon = new File(common_icon.getParentFile(), _name
035: + ".png");
036: else
037: script_icon = new File(VAGlobals.DEST_PATH, _name + ".png");
038: VAGlobals.printDebug("script_icon=" + script_icon);
039:
040: if (script_icon.exists())
041: return script_icon.getAbsolutePath();
042: if (common_icon.exists())
043: return common_icon.getAbsolutePath();
044: return null;
045: }
046:
047: private static void writeEntry(String _name, File _file)
048: throws FileNotFoundException {
049: writeEntry(_name, _file, _name, _name);
050: }
051:
052: private static void writeEntry(String _name, File _file,
053: String shortcutName, String shortcutIconName)
054: throws FileNotFoundException {
055: PrintWriter out = new PrintWriter(new FileOutputStream(_file));
056:
057: out.println("[Desktop Entry]");
058:
059: out.println("Name=" + shortcutName);
060: out.println("Comment=" + VAGlobals.APP_NAME + " "
061: + VAGlobals.APP_VERSION);
062: out.println("Exec="
063: + new File(VAGlobals.DEST_PATH, _name + ".sh")
064: .getAbsolutePath());
065:
066: out.println("Icon=" + getIcon(shortcutIconName));
067: out.println("Terminal=0");
068: out.println("Type=Application");
069: out.println("Categories=Application;");
070:
071: out.println("");
072: out.println("# Gnome Config File");
073: out.println("# for " + _name + " (" + VAGlobals.APP_NAME + " "
074: + VAGlobals.APP_VERSION + ")");
075: out.println("# produced by VAInstall on " + new Date());
076:
077: out.flush();
078: out.close();
079:
080: // needed for Nautilus
081: _file.setLastModified(System.currentTimeMillis() + 5000l);
082: }
083:
084: public static boolean delete(String _name) throws IOException {
085: boolean r = false;
086:
087: File f = new File(System.getProperty("user.home")
088: + System.getProperty("file.separator") + ".gnome"
089: + System.getProperty("file.separator") + "apps");
090:
091: if (f.canWrite()) {
092: f = new File(f, _name + ".desktop");
093: if (f.exists()) {
094: f.delete();
095: r = true;
096: }
097: }
098:
099: f = new File(System.getProperty("user.home")
100: + System.getProperty("file.separator")
101: + ".gnome-desktop");
102:
103: if (f.canWrite()) {
104: f = new File(f, _name + ".desktop");
105: if (f.exists()) {
106: f.delete();
107: }
108: }
109:
110: return r;
111: }
112:
113: public static boolean create(String _name, Set shortcuts)
114: throws IOException {
115: return create(_name, shortcuts, System.getProperty("user.home")
116: + File.separator + ".gnome" + File.separator + "apps"
117: + File.separator, true);
118: }
119:
120: public static boolean create(String _name, Set shortcuts,
121: String shortcutDir, boolean appendAppDirName)
122: throws IOException {
123: boolean r = false;
124: File tempFile;
125:
126: // MENU
127:
128: File f = new File(shortcutDir);
129: if (appendAppDirName)
130: tempFile = new java.io.File(f, VAGlobals.LINK_SECTION_NAME);
131: else
132: tempFile = f;
133: if (!tempFile.exists()) {
134: if (tempFile.mkdirs()) {
135: shortcuts.add(tempFile.getAbsolutePath());
136: f = tempFile;
137: }
138: } else if (tempFile.isDirectory()) {
139: f = tempFile;
140: shortcuts.add(tempFile.getAbsolutePath());
141: }
142:
143: // MENU
144:
145: if (f.canWrite()) {
146: f = new File(f, _name + ".desktop");
147: // System.err.println(""+f);
148: shortcuts.add(f.getAbsolutePath());
149: try {
150: writeEntry(_name, f);
151: r = true;
152: } catch (FileNotFoundException ex) {
153: }
154: }
155:
156: // DESKTOP
157:
158: f = new File(System.getProperty("user.home") + File.separator
159: + ".gnome-desktop");
160:
161: if (f.canWrite()) {
162: f = new File(f, _name + ".desktop");
163: shortcuts.add(f.getAbsolutePath());
164: // System.err.println(""+f);
165: try {
166: writeEntry(_name, f);
167: } catch (FileNotFoundException ex) {
168: }
169: }
170:
171: return r;
172: }
173:
174: public static boolean createUninstallShortcut(Set shortcuts)
175: throws IOException {
176: return createUninstallShortcut(shortcuts, System
177: .getProperty("user.home")
178: + File.separator
179: + ".gnome"
180: + File.separator
181: + "apps"
182: + File.separator, true);
183: }
184:
185: public static boolean createUninstallShortcut(Set shortcuts,
186: String shortcutDir, boolean appendAppDirName)
187: throws IOException {
188: boolean r = false;
189: File tempFile;
190: String uninstallName;
191:
192: // MENU
193:
194: File f = new File(shortcutDir);
195: if (appendAppDirName)
196: tempFile = new java.io.File(f, VAGlobals.LINK_SECTION_NAME);
197: else
198: tempFile = f;
199: if (!tempFile.exists()) {
200: if (tempFile.mkdirs()) {
201: shortcuts.add(tempFile.getAbsolutePath());
202: f = tempFile;
203: }
204: } else if (tempFile.isDirectory()) {
205: f = tempFile;
206: shortcuts.add(tempFile.getAbsolutePath());
207: }
208:
209: // MENU
210:
211: if (f.canWrite()) {
212: uninstallName = "uninstall_" + VAGlobals.APP_NAME + "_"
213: + VAGlobals.APP_VERSION;
214: f = new File(f, uninstallName + ".desktop");
215: // System.err.println(""+f);
216: shortcuts.add(f.getAbsolutePath());
217: try {
218: writeEntry(uninstallName, f, "Uninstall "
219: + VAGlobals.APP_NAME, "uninstall");
220: r = true;
221: } catch (FileNotFoundException ex) {
222: }
223: }
224: return r;
225: }
226:
227: public static boolean createAll(Object[] _launchparams,
228: Set shortCuts) throws IOException {
229: boolean r = true;
230: String gnome2dir;
231:
232: gnome2dir = System.getProperty("user.home") + File.separator
233: + ".gnome2" + File.separator + "vfolders"
234: + File.separator + "applications" + File.separator;
235: if (_launchparams != null)
236: for (int i = 0; i < _launchparams.length; i++) {
237: r &= create((String) ((Object[]) _launchparams[i])[0],
238: shortCuts);
239: r &= create((String) ((Object[]) _launchparams[i])[0],
240: shortCuts, gnome2dir, false);
241: }
242: if (VAGlobals.CREATE_UNINSTALL_SHORTCUT) {
243: r &= createUninstallShortcut(shortCuts);
244: r &= createUninstallShortcut(shortCuts, gnome2dir, false);
245: }
246: return r;
247: }
248: }
|