KickJava   Java API By Example, From Geeks To Geeks.

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


1 /**
2  * $RCSfile: VALinkKDE.java,v $
3  * @creation 28/03/00
4  * @modification $Date: 2005/03/30 19:42:47 $
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: VALinkKDE.java,v 1.5 2005/03/30 19:42:47 deniger Exp $
20  * @author Guillaume Desnoix
21  */

22
23 public class VALinkKDE
24 {
25
26     private static void writeEntry(String JavaDoc _name, File JavaDoc _file) throws FileNotFoundException JavaDoc {
27       writeEntry(_name, _file, _name, _name);
28   }
29
30   private static void writeEntry(String JavaDoc _name, File JavaDoc _file, String JavaDoc shortcutName, String JavaDoc shortcutIconName)
31      throws FileNotFoundException JavaDoc
32   {
33     PrintWriter JavaDoc out=new PrintWriter JavaDoc(new FileOutputStream JavaDoc(_file));
34
35     out.println("[KDE Desktop Entry]");
36
37     out.println("Name="+shortcutName);
38     out.println("Comment="+VAGlobals.APP_NAME+" "+VAGlobals.APP_VERSION);
39     out.println("Exec="+new File JavaDoc
40     (VAGlobals.DEST_PATH,_name+".sh")
41     .getAbsolutePath());
42
43     out.println("Icon="+getIcon(shortcutIconName));
44     
45     out.println("Terminal=0");
46     out.println("Type=Application");
47
48     out.println("");
49     out.println("# KDE Config File");
50     out.println("# for "+VAGlobals.APP_NAME+" "+VAGlobals.APP_VERSION);
51     out.println("# produced by VAInstall on "+new Date JavaDoc());
52
53     out.close();
54   }
55
56   public static boolean create(String JavaDoc _name, Set JavaDoc shortcuts) throws IOException JavaDoc {
57     boolean r=false;
58     File JavaDoc tempFile, f;
59     String JavaDoc prefixDir;
60
61     // MENU
62

63     prefixDir = System.getProperty("user.home")+
64             File.separator + ".kde"+
65             File.separator + "share";
66     f = new File JavaDoc (prefixDir + File.separator + "applnk-mdk");
67     if (!f.exists())
68         f = new File JavaDoc (prefixDir + File.separator + "applnk");
69     tempFile = new java.io.File JavaDoc(f, VAGlobals.LINK_SECTION_NAME);
70     if (!tempFile.exists()) {
71         if (tempFile.mkdirs()) {
72             shortcuts.add(tempFile.getAbsolutePath());
73             f = tempFile;
74         }
75     }
76     else if(tempFile.isDirectory()) {
77         f = tempFile;
78         shortcuts.add(tempFile.getAbsolutePath());
79     }
80
81     if(f.canWrite())
82     {
83       f=new File JavaDoc(f, _name+".desktop");
84       // System.err.println(""+f);
85
shortcuts.add(f.getAbsolutePath());
86       try { writeEntry(_name, f); r=true; }
87       catch(FileNotFoundException JavaDoc ex) { }
88     }
89
90     // DESKTOP
91

92     f=new File JavaDoc(System.getProperty("user.home"),"Desktop");
93     
94     if(f.canWrite())
95     {
96       f=new File JavaDoc(f, _name+".desktop");
97       // System.err.println(""+f);
98
shortcuts.add(f.getAbsolutePath());
99       try { writeEntry(_name, f); }
100       catch(FileNotFoundException JavaDoc ex) { }
101     }
102
103     return r;
104   }
105
106   public static boolean createUninstallShortcut(Set JavaDoc shortcuts) throws IOException JavaDoc {
107     boolean r=false;
108     File JavaDoc tempFile, f;
109     String JavaDoc uninstallName;
110     String JavaDoc prefixDir;
111
112     // MENU
113

114     prefixDir = System.getProperty("user.home")+
115             File.separator + ".kde"+
116             File.separator + "share";
117     f = new File JavaDoc (prefixDir + File.separator + "applnk-mdk");
118     if (!f.exists())
119         f = new File JavaDoc (prefixDir + File.separator + "applnk");
120     tempFile = new java.io.File JavaDoc(f, VAGlobals.LINK_SECTION_NAME);
121     if (!tempFile.exists()) {
122         if (tempFile.mkdirs()) {
123             shortcuts.add(tempFile.getAbsolutePath());
124             f = tempFile;
125         }
126     }
127     else if(tempFile.isDirectory()) {
128         f = tempFile;
129         shortcuts.add(tempFile.getAbsolutePath());
130     }
131
132     // MENU
133

134     if(f.canWrite())
135     {
136       uninstallName = "uninstall_"+VAGlobals.APP_NAME+"_"+VAGlobals.APP_VERSION;
137       f=new File JavaDoc(f, uninstallName + ".desktop");
138       // System.err.println(""+f);
139
shortcuts.add(f.getAbsolutePath());
140       try { writeEntry(uninstallName,f, "Uninstall "+VAGlobals.APP_NAME, "uninstall"); r=true; }
141       catch(FileNotFoundException JavaDoc ex) { }
142     }
143     return r;
144   }
145
146   public static boolean createAll(Object JavaDoc[] _launchparams, Set JavaDoc shortCuts) throws IOException JavaDoc
147   {
148     boolean r=true;
149     if(_launchparams!=null)
150       for(int i=0;i<_launchparams.length;i++)
151     r&=create((String JavaDoc)((Object JavaDoc[])_launchparams[i])[0], shortCuts);
152     if (VAGlobals.CREATE_UNINSTALL_SHORTCUT)
153         r &= createUninstallShortcut(shortCuts);
154     return r;
155   }
156
157   public static boolean delete() throws IOException JavaDoc
158   {
159     boolean r=false;
160
161     File JavaDoc f=new File JavaDoc(System.getProperty("user.home")+
162             System.getProperty("file.separator")+
163             ".kde"+
164             System.getProperty("file.separator")+
165             "share"+
166             System.getProperty("file.separator")+
167             "applnk");
168
169     if(f.canWrite())
170     {
171       f=new File JavaDoc(f,VAGlobals.LINK_ENTRY_NAME+".desktop");
172       if(f.exists()) { f.delete(); r=true; }
173     }
174
175 /* f=new File(System.getProperty("user.home"),"Desktop");
176
177     if(f.canWrite())
178     {
179       f=new File(f,VAGlobals.LINK_ENTRY_NAME+".desktop");
180       if(f.exists()) { f.delete(); r=true; }
181     }*/

182
183     return r;
184   }
185
186   private static String JavaDoc getIcon(String JavaDoc _name)
187   {
188     File JavaDoc common_icon=null;
189     if( (VAGlobals.LINK_ENTRY_ICON!=null)
190        &&(VAGlobals.LINK_ENTRY_ICON.length()>0))
191     {
192       common_icon=new File JavaDoc
193     (VAGlobals.DEST_PATH,
194      VAGlobals.LINK_ENTRY_ICON.replace('/',File.separatorChar)+".xpm");
195       VAGlobals.printDebug("common_icon="+common_icon);
196     }
197     File JavaDoc script_icon=null;
198     if(common_icon!=null)
199       script_icon=new File JavaDoc(common_icon.getParentFile(),_name + ".xpm");
200     else
201       script_icon=new File JavaDoc(VAGlobals.DEST_PATH,_name+".xpm");
202     VAGlobals.printDebug("script_icon="+script_icon);
203
204     if(script_icon != null && script_icon.exists()) return script_icon.getAbsolutePath();
205     if(common_icon != null && common_icon.exists()) return common_icon.getAbsolutePath();
206     return null;
207   }
208 }
209
Popular Tags