KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > memoire > vainstall > VALinkLinux


1 /**
2  * Created on Dec 28, 2004
3  *
4  * licence GPL 2
5  *
6  */

7 package com.memoire.vainstall;
8
9 import java.io.BufferedWriter JavaDoc;
10 import java.io.File JavaDoc;
11 import java.io.FileNotFoundException JavaDoc;
12 import java.io.FileOutputStream JavaDoc;
13 import java.io.IOException JavaDoc;
14 import java.io.OutputStreamWriter JavaDoc;
15 import java.io.UnsupportedEncodingException JavaDoc;
16 import java.util.List JavaDoc;
17 import java.util.Set JavaDoc;
18
19 /**
20  * @author fred deniger
21  * @version $Id: VALinkLinux.java,v 1.5 2005/03/30 19:42:40 deniger Exp $
22  */

23 public class VALinkLinux {
24
25     public static File JavaDoc getIconDir() {
26         return new File JavaDoc(System.getProperty("user.home"), "Desktop");
27     }
28
29     public static void createAll(VAShortcutEntry[] _e, Set JavaDoc file)
30             throws IOException JavaDoc {
31         VALinkLinux link = new VALinkLinux();
32         File JavaDoc dest = VAGlobals.SHORTCUTS_IN_INSTALLDIR ? new File JavaDoc(
33                 VAGlobals.DEST_PATH) : null;
34         File JavaDoc kdeMenuDir, gnomeMenuDir;
35         
36         kdeMenuDir = link.createKDEMenuDir(file);
37         gnomeMenuDir = link.createGnomeMenuDir(file);
38         for (int i = 0; i < _e.length; i++) {
39             if (_e[i].isCreateOnDesktop()) {
40                 File JavaDoc f = link.writeIconEntryOnDesktop(_e[i]);
41                 if (f != null)
42                     file.add(f.getAbsolutePath());
43             }
44             if (VAGlobals.SHORTCUTS_IN_INSTALLDIR) {
45                 File JavaDoc f = link.writeIconEntrynDir(_e[i], dest);
46                 if (f != null)
47                     file.add(f.getAbsolutePath());
48
49             }
50             if (kdeMenuDir != null) {
51                 File JavaDoc f = link.writeIconEntrynDir(_e[i], kdeMenuDir);
52                 if (f != null)
53                     file.add(f.getAbsolutePath());
54             }
55             if (gnomeMenuDir != null) {
56                 File JavaDoc f = link.writeIconEntrynDir(_e[i], gnomeMenuDir);
57                 if (f != null)
58                     file.add(f.getAbsolutePath());
59             }
60         }
61
62     }
63
64     File JavaDoc desktopDir_;
65
66     List JavaDoc iconEntry_;
67
68     public VALinkLinux() {
69
70     }
71
72     public String JavaDoc getDesktopFileName(VAShortcutEntry _e) {
73         return _e.getName() + ".desktop";
74     }
75
76     private File JavaDoc writeIcon(File JavaDoc _desktopDir, VAShortcutEntry _e)
77             throws IOException JavaDoc {
78         File JavaDoc dest = new File JavaDoc(_desktopDir, getDesktopFileName(_e));
79         //the encoding used by default
80
String JavaDoc encoding = "UTF-8";
81         BufferedWriter JavaDoc w = null;
82         //to be sure that the stream is close we use a try catch and close the
83
// stream in the finally
84
//bloc
85
try {
86             w = new BufferedWriter JavaDoc(new OutputStreamWriter JavaDoc(new FileOutputStream JavaDoc(
87                     dest.getAbsolutePath()), encoding));
88             w.write("[Desktop Entry]" + VAConstant.LINE_SEP);
89             w.write("Encoding=" + encoding + VAConstant.LINE_SEP);
90             if (_e.getType() != null)
91                 w.write("Type=" + _e.getType() + VAConstant.LINE_SEP);
92             if (_e.getIconPath() != null) {
93                 w.write("Icon=" + _e.getIconPath() + VAConstant.LINE_SEP);
94             }
95             if (_e.getWorkingDirectory() != null) {
96                 w.write("Path=" + _e.getWorkingDirectory()
97                         + VAConstant.LINE_SEP);
98             }
99             w.write("Name=" + _e.getName() + VAConstant.LINE_SEP);
100             w.write("Exec=" + _e.getExePath() + VAConstant.LINE_SEP);
101             if (_e.getComment() != null) {
102                 w.write("Comment=" + _e.getComment() + VAConstant.LINE_SEP);
103             }
104             w.write("Terminal=" + (_e.isLaunchInTerminal() ? "true" : "false")
105                     + VAConstant.LINE_SEP);
106             w.flush();
107         } catch (UnsupportedEncodingException JavaDoc _e1) {
108             throw _e1;
109         } catch (FileNotFoundException JavaDoc _e1) {
110             throw _e1;
111         } catch (IOException JavaDoc _e1) {
112             throw _e1;
113         } finally {
114             if (w != null)
115                 w.close();
116         }
117         return dest;
118     }
119
120     /**
121      * @param _e
122      * a new Icon to write
123      */

124     public File JavaDoc writeIconEntryOnDesktop(VAShortcutEntry _e) throws IOException JavaDoc {
125         if (desktopDir_ == null)
126             desktopDir_ = getIconDir();
127         if (_e.getIconPath() == null) {
128             if (_e.isUninstall()) {
129                 _e.setIconPath(getIcon("uninstall", false));
130             } else
131                 _e.setIconPath(getIcon(_e.getName(), true));
132         }
133         return writeIcon(desktopDir_, _e);
134
135     }
136
137     /**
138      * @param _e
139      * a new Icon to write
140      */

141     public File JavaDoc writeIconEntrynDir(VAShortcutEntry _e, File JavaDoc _destDir)
142             throws IOException JavaDoc {
143         if (_e.getIconPath() == null) {
144             if (_e.isUninstall()) {
145                 _e.setIconPath(getIcon("uninstall", false));
146             } else
147                 _e.setIconPath(getIcon(_e.getName(), true));
148         }
149         return writeIcon(_destDir, _e);
150
151     }
152
153     public String JavaDoc[] getIconFmt() {
154         return new String JavaDoc[] { "xpm", "png", "jpg" };
155     }
156
157     public File JavaDoc findIcon(File JavaDoc _dir, String JavaDoc _name, String JavaDoc[] ext) {
158         for (int i = ext.length - 1; i >= 0; i--) {
159             File JavaDoc r = new File JavaDoc(_dir, _name + "." + ext[i]);
160             if (r.exists())
161                 return r;
162         }
163         return null;
164     }
165
166     public String JavaDoc getIcon(String JavaDoc _name, boolean useDefaultIcon) {
167         File JavaDoc common_icon = null;
168         File JavaDoc icons_dir = new File JavaDoc(VAGlobals.DEST_PATH);
169         String JavaDoc[] ext = getIconFmt();
170         if ((VAGlobals.LINK_ENTRY_ICON != null)
171                 && (VAGlobals.LINK_ENTRY_ICON.length() > 0)) {
172             File JavaDoc icon = new File JavaDoc(VAGlobals.DEST_PATH, VAGlobals.LINK_ENTRY_ICON
173                     .replace('/', File.separatorChar));
174             icons_dir = icon.getParentFile();
175             common_icon = findIcon(icons_dir, icon.getName(), ext);
176         }
177         File JavaDoc script_icon = findIcon(icons_dir, _name, ext);
178         if (script_icon != null && script_icon.exists())
179             return script_icon.getAbsolutePath();
180         if (useDefaultIcon && common_icon != null && common_icon.exists())
181             return common_icon.getAbsolutePath();
182         return null;
183     }
184
185     public File JavaDoc createKDEMenuDir(Set JavaDoc shortcuts) throws IOException JavaDoc {
186         boolean r=false;
187         File JavaDoc tempFile, f;
188         String JavaDoc prefixDir;
189
190         // MENU
191

192         prefixDir = System.getProperty("user.home")+
193                 File.separator + ".kde"+
194                 File.separator + "share";
195         f = new File JavaDoc (prefixDir + File.separator + "applnk-mdk");
196         if (!f.exists())
197             f = new File JavaDoc (prefixDir + File.separator + "applnk");
198         if (f.exists()) {
199             tempFile = new java.io.File JavaDoc(f, VAGlobals.LINK_SECTION_NAME);
200             if (!tempFile.exists()) {
201                 if (tempFile.mkdirs()) {
202                     shortcuts.add(tempFile.getAbsolutePath());
203                     f = tempFile;
204                 }
205             }
206             else if(tempFile.isDirectory()) {
207                 f = tempFile;
208                 shortcuts.add(tempFile.getAbsolutePath());
209             }
210         }
211         else {
212             f = null;
213         }
214         return f;
215     }
216
217     public File JavaDoc createGnomeMenuDir(Set JavaDoc shortcuts) throws IOException JavaDoc {
218         boolean r=false;
219         File JavaDoc tempFile, f;
220         String JavaDoc tempDir;
221
222         // MENU
223

224         tempDir = System.getProperty("user.home")+
225                 File.separator + ".gnome"+
226                 File.separator + "apps";
227         f = new File JavaDoc(tempDir);
228         if (f.exists()) {
229             tempFile = new java.io.File JavaDoc(f, VAGlobals.LINK_SECTION_NAME);
230             if (!tempFile.exists()) {
231                 if (tempFile.mkdirs()) {
232                     shortcuts.add(tempFile.getAbsolutePath());
233                     f = tempFile;
234                 }
235             }
236             else if(tempFile.isDirectory()) {
237                 f = tempFile;
238                 shortcuts.add(tempFile.getAbsolutePath());
239             }
240         }
241         else {
242             f = null;
243         }
244         return f;
245     }
246
247 }
Popular Tags