KickJava   Java API By Example, From Geeks To Geeks.

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


1 /**
2  * $RCSfile: VALinkGnome.java,v $
3  * @creation 28/03/00
4  * @modification $Date: 2005/03/30 19:42:44 $
5  * Bug-fixing and additional features by Antonio Petrelli
6  */

7
8 package com.memoire.vainstall;
9
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.PrintWriter JavaDoc;
15 import java.util.Date JavaDoc;
16 import java.util.Set JavaDoc;
17
18 /**
19  * @version $Id: VALinkGnome.java,v 1.7 2005/03/30 19:42:44 deniger Exp $
20  * @author Guillaume Desnoix
21  */

22
23 public class VALinkGnome
24 {
25   private static String JavaDoc getIcon(String JavaDoc _name)
26   {
27     File JavaDoc common_icon=null;
28     if( (VAGlobals.LINK_ENTRY_ICON!=null)
29        &&(VAGlobals.LINK_ENTRY_ICON.length()>0))
30     {
31       common_icon=new File JavaDoc
32     (VAGlobals.DEST_PATH,
33      VAGlobals.LINK_ENTRY_ICON.replace('/',File.separatorChar)+".png");
34       VAGlobals.printDebug("common_icon="+common_icon);
35     }
36     File JavaDoc script_icon=null;
37     if(common_icon!=null)
38       script_icon=new File JavaDoc(common_icon.getParentFile(),_name + ".png");
39     else
40       script_icon=new File JavaDoc(VAGlobals.DEST_PATH,_name+".png");
41     VAGlobals.printDebug("script_icon="+script_icon);
42
43     if(script_icon.exists()) return script_icon.getAbsolutePath();
44     if(common_icon.exists()) return common_icon.getAbsolutePath();
45     return null;
46   }
47
48   private static void writeEntry(String JavaDoc _name, File JavaDoc _file) throws FileNotFoundException JavaDoc {
49       writeEntry(_name, _file, _name, _name);
50   }
51
52   private static void writeEntry(String JavaDoc _name, File JavaDoc _file, String JavaDoc shortcutName, String JavaDoc shortcutIconName)
53     throws FileNotFoundException JavaDoc
54   {
55     PrintWriter JavaDoc out=new PrintWriter JavaDoc(new FileOutputStream JavaDoc(_file));
56
57     out.println("[Desktop Entry]");
58
59     out.println("Name="+ shortcutName);
60     out.println("Comment="+VAGlobals.APP_NAME+" "+VAGlobals.APP_VERSION);
61     out.println("Exec="+new File JavaDoc
62         (VAGlobals.DEST_PATH,_name+".sh")
63         .getAbsolutePath());
64     
65     out.println("Icon="+getIcon(shortcutIconName));
66     out.println("Terminal=0");
67     out.println("Type=Application");
68     out.println("Categories=Application;");
69
70     out.println("");
71     out.println("# Gnome Config File");
72     out.println("# for "+_name+" ("+
73         VAGlobals.APP_NAME+" "+VAGlobals.APP_VERSION+")");
74     out.println("# produced by VAInstall on "+new Date JavaDoc());
75
76     out.flush();
77     out.close();
78
79     // needed for Nautilus
80
_file.setLastModified(System.currentTimeMillis()+5000l);
81   }
82
83   public static boolean delete(String JavaDoc _name) throws IOException JavaDoc
84   {
85     boolean r=false;
86
87     File JavaDoc f=new File JavaDoc(System.getProperty("user.home")+
88             System.getProperty("file.separator")+
89             ".gnome"+
90             System.getProperty("file.separator")+
91             "apps");
92
93     if(f.canWrite())
94     {
95       f=new File JavaDoc(f,_name+".desktop");
96       if(f.exists()) { f.delete(); r=true; }
97     }
98
99     f=new File JavaDoc(System.getProperty("user.home")+
100            System.getProperty("file.separator")+
101            ".gnome-desktop");
102
103     if(f.canWrite())
104     {
105       f=new File JavaDoc(f,_name+".desktop");
106       if(f.exists()) { f.delete(); }
107     }
108
109     return r;
110   }
111   
112   public static boolean create(String JavaDoc _name, Set JavaDoc shortcuts) throws IOException JavaDoc {
113     return create(_name, shortcuts, System.getProperty("user.home")+
114         File.separator + ".gnome"+
115         File.separator + "apps"+
116         File.separator, true);
117   }
118
119   public static boolean create(String JavaDoc _name, Set JavaDoc shortcuts, String JavaDoc shortcutDir,
120     boolean appendAppDirName) throws IOException JavaDoc {
121     boolean r=false;
122     File JavaDoc tempFile;
123
124     // MENU
125

126     File JavaDoc f=new File JavaDoc(shortcutDir);
127     if (appendAppDirName)
128         tempFile = new java.io.File JavaDoc(f, VAGlobals.LINK_SECTION_NAME);
129     else
130         tempFile = f;
131     if (!tempFile.exists()) {
132         if (tempFile.mkdirs()) {
133             shortcuts.add(tempFile.getAbsolutePath());
134             f = tempFile;
135         }
136     }
137     else if(tempFile.isDirectory()) {
138         f = tempFile;
139         shortcuts.add(tempFile.getAbsolutePath());
140     }
141
142     // MENU
143

144     if(f.canWrite())
145     {
146       f=new File JavaDoc(f,_name+".desktop");
147       // System.err.println(""+f);
148
shortcuts.add(f.getAbsolutePath());
149       try { writeEntry(_name,f); r=true; }
150       catch(FileNotFoundException JavaDoc ex) { }
151     }
152
153     // DESKTOP
154

155     f=new File JavaDoc(System.getProperty("user.home")+
156            File.separator + ".gnome-desktop");
157
158     if(f.canWrite())
159     {
160       f=new File JavaDoc(f,_name+".desktop");
161       shortcuts.add(f.getAbsolutePath());
162       // System.err.println(""+f);
163
try { writeEntry(_name,f); }
164       catch(FileNotFoundException JavaDoc ex) { }
165     }
166
167     return r;
168   }
169   
170   public static boolean createUninstallShortcut(Set JavaDoc shortcuts) throws IOException JavaDoc {
171       return createUninstallShortcut(shortcuts,
172         System.getProperty("user.home")+
173         File.separator + ".gnome"+
174         File.separator + "apps"+
175         File.separator, true);
176   }
177
178   public static boolean createUninstallShortcut(Set JavaDoc shortcuts, String JavaDoc shortcutDir,
179         boolean appendAppDirName) throws IOException JavaDoc {
180     boolean r=false;
181     File JavaDoc tempFile;
182     String JavaDoc uninstallName;
183
184     // MENU
185

186     File JavaDoc f=new File JavaDoc(shortcutDir);
187     if (appendAppDirName)
188         tempFile = new java.io.File JavaDoc(f, VAGlobals.LINK_SECTION_NAME);
189     else
190         tempFile = f;
191     if (!tempFile.exists()) {
192         if (tempFile.mkdirs()) {
193             shortcuts.add(tempFile.getAbsolutePath());
194             f = tempFile;
195         }
196     }
197     else if(tempFile.isDirectory()) {
198         f = tempFile;
199         shortcuts.add(tempFile.getAbsolutePath());
200     }
201
202     // MENU
203

204     if(f.canWrite())
205     {
206       uninstallName = "uninstall_"+VAGlobals.APP_NAME+"_"+VAGlobals.APP_VERSION;
207       f=new File JavaDoc(f, uninstallName + ".desktop");
208       // System.err.println(""+f);
209
shortcuts.add(f.getAbsolutePath());
210       try { writeEntry(uninstallName,f, "Uninstall "+VAGlobals.APP_NAME, "uninstall"); r=true; }
211       catch(FileNotFoundException JavaDoc ex) { }
212     }
213     return r;
214   }
215
216   public static boolean createAll(Object JavaDoc[] _launchparams, Set JavaDoc shortCuts) throws IOException JavaDoc
217   {
218     boolean r=true;
219     String JavaDoc gnome2dir;
220     
221     gnome2dir = System.getProperty("user.home")+ File.separator + ".gnome2"+
222             File.separator + "vfolders"+File.separator + "applications" + File.separator;
223     if(_launchparams!=null)
224       for(int i=0;i<_launchparams.length;i++) {
225     r&=create((String JavaDoc)((Object JavaDoc[])_launchparams[i])[0], shortCuts);
226         r &= create((String JavaDoc)((Object JavaDoc[])_launchparams[i])[0], shortCuts,
227             gnome2dir, false);
228       }
229     if (VAGlobals.CREATE_UNINSTALL_SHORTCUT) {
230         r &= createUninstallShortcut(shortCuts);
231         r &= createUninstallShortcut(shortCuts, gnome2dir, false);
232     }
233     return r;
234   }
235 }
236
Popular Tags