001: /**
002: * Created on Dec 28, 2004
003: *
004: * licence GPL 2
005: *
006: */package com.memoire.vainstall;
007:
008: import java.io.BufferedWriter;
009: import java.io.File;
010: import java.io.FileNotFoundException;
011: import java.io.FileOutputStream;
012: import java.io.IOException;
013: import java.io.OutputStreamWriter;
014: import java.io.UnsupportedEncodingException;
015: import java.util.List;
016: import java.util.Set;
017:
018: /**
019: * @author fred deniger
020: * @version $Id: VALinkLinux.java,v 1.5 2005/03/30 19:42:40 deniger Exp $
021: */
022: public class VALinkLinux {
023:
024: public static File getIconDir() {
025: return new File(System.getProperty("user.home"), "Desktop");
026: }
027:
028: public static void createAll(VAShortcutEntry[] _e, Set file)
029: throws IOException {
030: VALinkLinux link = new VALinkLinux();
031: File dest = VAGlobals.SHORTCUTS_IN_INSTALLDIR ? new File(
032: VAGlobals.DEST_PATH) : null;
033: File kdeMenuDir, gnomeMenuDir;
034:
035: kdeMenuDir = link.createKDEMenuDir(file);
036: gnomeMenuDir = link.createGnomeMenuDir(file);
037: for (int i = 0; i < _e.length; i++) {
038: if (_e[i].isCreateOnDesktop()) {
039: File f = link.writeIconEntryOnDesktop(_e[i]);
040: if (f != null)
041: file.add(f.getAbsolutePath());
042: }
043: if (VAGlobals.SHORTCUTS_IN_INSTALLDIR) {
044: File f = link.writeIconEntrynDir(_e[i], dest);
045: if (f != null)
046: file.add(f.getAbsolutePath());
047:
048: }
049: if (kdeMenuDir != null) {
050: File f = link.writeIconEntrynDir(_e[i], kdeMenuDir);
051: if (f != null)
052: file.add(f.getAbsolutePath());
053: }
054: if (gnomeMenuDir != null) {
055: File f = link.writeIconEntrynDir(_e[i], gnomeMenuDir);
056: if (f != null)
057: file.add(f.getAbsolutePath());
058: }
059: }
060:
061: }
062:
063: File desktopDir_;
064:
065: List iconEntry_;
066:
067: public VALinkLinux() {
068:
069: }
070:
071: public String getDesktopFileName(VAShortcutEntry _e) {
072: return _e.getName() + ".desktop";
073: }
074:
075: private File writeIcon(File _desktopDir, VAShortcutEntry _e)
076: throws IOException {
077: File dest = new File(_desktopDir, getDesktopFileName(_e));
078: //the encoding used by default
079: String encoding = "UTF-8";
080: BufferedWriter w = null;
081: //to be sure that the stream is close we use a try catch and close the
082: // stream in the finally
083: //bloc
084: try {
085: w = new BufferedWriter(new OutputStreamWriter(
086: new FileOutputStream(dest.getAbsolutePath()),
087: encoding));
088: w.write("[Desktop Entry]" + VAConstant.LINE_SEP);
089: w.write("Encoding=" + encoding + VAConstant.LINE_SEP);
090: if (_e.getType() != null)
091: w.write("Type=" + _e.getType() + VAConstant.LINE_SEP);
092: if (_e.getIconPath() != null) {
093: w.write("Icon=" + _e.getIconPath()
094: + VAConstant.LINE_SEP);
095: }
096: if (_e.getWorkingDirectory() != null) {
097: w.write("Path=" + _e.getWorkingDirectory()
098: + VAConstant.LINE_SEP);
099: }
100: w.write("Name=" + _e.getName() + VAConstant.LINE_SEP);
101: w.write("Exec=" + _e.getExePath() + VAConstant.LINE_SEP);
102: if (_e.getComment() != null) {
103: w.write("Comment=" + _e.getComment()
104: + VAConstant.LINE_SEP);
105: }
106: w.write("Terminal="
107: + (_e.isLaunchInTerminal() ? "true" : "false")
108: + VAConstant.LINE_SEP);
109: w.flush();
110: } catch (UnsupportedEncodingException _e1) {
111: throw _e1;
112: } catch (FileNotFoundException _e1) {
113: throw _e1;
114: } catch (IOException _e1) {
115: throw _e1;
116: } finally {
117: if (w != null)
118: w.close();
119: }
120: return dest;
121: }
122:
123: /**
124: * @param _e
125: * a new Icon to write
126: */
127: public File writeIconEntryOnDesktop(VAShortcutEntry _e)
128: throws IOException {
129: if (desktopDir_ == null)
130: desktopDir_ = getIconDir();
131: if (_e.getIconPath() == null) {
132: if (_e.isUninstall()) {
133: _e.setIconPath(getIcon("uninstall", false));
134: } else
135: _e.setIconPath(getIcon(_e.getName(), true));
136: }
137: return writeIcon(desktopDir_, _e);
138:
139: }
140:
141: /**
142: * @param _e
143: * a new Icon to write
144: */
145: public File writeIconEntrynDir(VAShortcutEntry _e, File _destDir)
146: throws IOException {
147: if (_e.getIconPath() == null) {
148: if (_e.isUninstall()) {
149: _e.setIconPath(getIcon("uninstall", false));
150: } else
151: _e.setIconPath(getIcon(_e.getName(), true));
152: }
153: return writeIcon(_destDir, _e);
154:
155: }
156:
157: public String[] getIconFmt() {
158: return new String[] { "xpm", "png", "jpg" };
159: }
160:
161: public File findIcon(File _dir, String _name, String[] ext) {
162: for (int i = ext.length - 1; i >= 0; i--) {
163: File r = new File(_dir, _name + "." + ext[i]);
164: if (r.exists())
165: return r;
166: }
167: return null;
168: }
169:
170: public String getIcon(String _name, boolean useDefaultIcon) {
171: File common_icon = null;
172: File icons_dir = new File(VAGlobals.DEST_PATH);
173: String[] ext = getIconFmt();
174: if ((VAGlobals.LINK_ENTRY_ICON != null)
175: && (VAGlobals.LINK_ENTRY_ICON.length() > 0)) {
176: File icon = new File(VAGlobals.DEST_PATH,
177: VAGlobals.LINK_ENTRY_ICON.replace('/',
178: File.separatorChar));
179: icons_dir = icon.getParentFile();
180: common_icon = findIcon(icons_dir, icon.getName(), ext);
181: }
182: File script_icon = findIcon(icons_dir, _name, ext);
183: if (script_icon != null && script_icon.exists())
184: return script_icon.getAbsolutePath();
185: if (useDefaultIcon && common_icon != null
186: && common_icon.exists())
187: return common_icon.getAbsolutePath();
188: return null;
189: }
190:
191: public File createKDEMenuDir(Set shortcuts) throws IOException {
192: boolean r = false;
193: File tempFile, f;
194: String prefixDir;
195:
196: // MENU
197:
198: prefixDir = System.getProperty("user.home") + File.separator
199: + ".kde" + File.separator + "share";
200: f = new File(prefixDir + File.separator + "applnk-mdk");
201: if (!f.exists())
202: f = new File(prefixDir + File.separator + "applnk");
203: if (f.exists()) {
204: tempFile = new java.io.File(f, VAGlobals.LINK_SECTION_NAME);
205: if (!tempFile.exists()) {
206: if (tempFile.mkdirs()) {
207: shortcuts.add(tempFile.getAbsolutePath());
208: f = tempFile;
209: }
210: } else if (tempFile.isDirectory()) {
211: f = tempFile;
212: shortcuts.add(tempFile.getAbsolutePath());
213: }
214: } else {
215: f = null;
216: }
217: return f;
218: }
219:
220: public File createGnomeMenuDir(Set shortcuts) throws IOException {
221: boolean r = false;
222: File tempFile, f;
223: String tempDir;
224:
225: // MENU
226:
227: tempDir = System.getProperty("user.home") + File.separator
228: + ".gnome" + File.separator + "apps";
229: f = new File(tempDir);
230: if (f.exists()) {
231: tempFile = new java.io.File(f, VAGlobals.LINK_SECTION_NAME);
232: if (!tempFile.exists()) {
233: if (tempFile.mkdirs()) {
234: shortcuts.add(tempFile.getAbsolutePath());
235: f = tempFile;
236: }
237: } else if (tempFile.isDirectory()) {
238: f = tempFile;
239: shortcuts.add(tempFile.getAbsolutePath());
240: }
241: } else {
242: f = null;
243: }
244: return f;
245: }
246:
247: }
|