svn-gvsig-desktop / trunk / build / distribution / IzPack / src / lib / com / izforge / izpack / util / os / Unix_Shortcut.java @ 21757
History | View | Annotate | Download (14.8 KB)
1 |
/*
|
---|---|
2 |
* Created on 10.11.2003 by marc.eppelmann
|
3 |
*
|
4 |
*
|
5 |
* This represents a Implementation of the KDE/GNOME DesktopEntry.
|
6 |
* which is standard from
|
7 |
* "Desktop Entry Standard"
|
8 |
* "The format of .desktop files, supported by KDE and GNOME."
|
9 |
* http://www.freedesktop.org/standards/desktop-entry-spec/
|
10 |
*
|
11 |
* [Desktop Entry]
|
12 |
// Comment=$Comment
|
13 |
// Comment[de]=
|
14 |
// Encoding=$UTF-8
|
15 |
// Exec=$'/home/marc/CPS/tomcat/bin/catalina.sh' run
|
16 |
// GenericName=$
|
17 |
// GenericName[de]=$
|
18 |
// Icon=$inetd
|
19 |
// MimeType=$
|
20 |
// Name=$Start Tomcat
|
21 |
// Name[de]=$Start Tomcat
|
22 |
// Path=$/home/marc/CPS/tomcat/bin/
|
23 |
// ServiceTypes=$
|
24 |
// SwallowExec=$
|
25 |
// SwallowTitle=$
|
26 |
// Terminal=$true
|
27 |
// TerminalOptions=$
|
28 |
// Type=$Application
|
29 |
// X-KDE-SubstituteUID=$false
|
30 |
// X-KDE-Username=$
|
31 |
*
|
32 |
*
|
33 |
* * File : Unix_Shortcut.java
|
34 |
* Description : UNix-Implementaion of the shortcut API
|
35 |
* Author's email : marc.eppelmann@gmx.de
|
36 |
* Website : http://www.izforge.com
|
37 |
*
|
38 |
* This program is free software; you can redistribute it and/or
|
39 |
* modify it under the terms of the GNU General Public License
|
40 |
* as published by the Free Software Foundation; either version 2
|
41 |
* of the License, or any later version.
|
42 |
*
|
43 |
* This program is distributed in the hope that it will be useful,
|
44 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
45 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
46 |
* GNU General Public License for more details.
|
47 |
*
|
48 |
* You should have received a copy of the GNU General Public License
|
49 |
* along with this program; if not, write to the Free Software
|
50 |
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
|
51 |
*
|
52 |
*/
|
53 |
package com.izforge.izpack.util.os; |
54 |
|
55 |
import java.io.File; |
56 |
import java.io.FileWriter; |
57 |
import java.io.IOException; |
58 |
import java.util.Enumeration; |
59 |
import java.util.Properties; |
60 |
import java.util.Vector; |
61 |
|
62 |
import com.izforge.izpack.util.StringTool; |
63 |
|
64 |
/**
|
65 |
* @author marc.eppelmann
|
66 |
*
|
67 |
* This is the Implementaion of the RFC-Based Desktop-Link.
|
68 |
* Used in KDE and GNOME.
|
69 |
*/
|
70 |
public class Unix_Shortcut extends Shortcut implements Unix_ShortcutConstants |
71 |
{ |
72 |
|
73 |
private static String version = "$Id: Unix_Shortcut.java 5819 2006-06-14 07:29:09Z cesar $"; |
74 |
private static String rev = "$Revision: 5819 $"; |
75 |
private String createdDirectory; |
76 |
/** DESKTOP_EXT = ".desktop" */
|
77 |
private static String DESKTOP_EXT = ".desktop"; |
78 |
private int itsShow; |
79 |
private int itsUserType; |
80 |
private int itsType; |
81 |
private int itsIconIndex; |
82 |
private String itsWorkingDirectory; |
83 |
private String itsGroupName; |
84 |
private String itsTargetPath; |
85 |
private String itsIconPath; |
86 |
private String itsDescription; |
87 |
private String itsArguments; |
88 |
private String itsName; |
89 |
private String itsFileName; |
90 |
private String itsApplnkFolder="applnk"; |
91 |
|
92 |
private static String template = ""; |
93 |
|
94 |
private Properties props; |
95 |
/** N = "\n" */
|
96 |
private final static String N = "\n"; |
97 |
/** H = "#" */
|
98 |
private final static String H = "#"; |
99 |
/** S = " " */
|
100 |
private final static String S = " "; |
101 |
/** C = Comment = H+S = "# " */
|
102 |
private final static String C = H + S; |
103 |
|
104 |
|
105 |
|
106 |
public Unix_Shortcut()
|
107 |
{ |
108 |
StringBuffer hlp = new StringBuffer(); |
109 |
|
110 |
String userLanguage = System.getProperty("user.language", "en"); |
111 |
|
112 |
hlp.append( "[Desktop Entry]" + N );
|
113 |
|
114 |
hlp.append( "Comment=" + $Comment + N ); |
115 |
hlp.append( "Comment["+userLanguage+"]=" + $Comment + N ); |
116 |
hlp.append( "Encoding=" + $Encoding + N ); |
117 |
hlp.append( "Exec='" + $Exec + "'" + S + $Arguments + N ); |
118 |
hlp.append( "GenericName=" + $GenericName + N ); |
119 |
|
120 |
hlp.append( "GenericName["+userLanguage+"]=" + $GenericName + N ); |
121 |
hlp.append( "Icon=" + $Icon + N ); |
122 |
hlp.append( "MimeType=" + $MimeType + N ); |
123 |
hlp.append( "Name=" + $Name + N ); |
124 |
hlp.append( "Name["+userLanguage+"]=" + $Name + N ); |
125 |
|
126 |
hlp.append( "Path='" + $Path + "'" + N ); |
127 |
hlp.append( "ServiceTypes=" + $ServiceTypes + N ); |
128 |
hlp.append( "SwallowExec=" + $SwallowExec + N ); |
129 |
hlp.append( "SwallowTitle=" + $SwallowTitle + N ); |
130 |
hlp.append( "Terminal=" + $Terminal + N ); |
131 |
|
132 |
hlp.append( "TerminalOptions=" + $Options_For_Terminal + N ); |
133 |
hlp.append( "Type=" + $Type + N ); |
134 |
hlp.append( "URL=" + $URL + N ); |
135 |
hlp.append( "X-KDE-SubstituteUID=" + $X_KDE_SubstituteUID + N ); |
136 |
hlp.append( "X-KDE-Username=" + $X_KDE_Username + N ); |
137 |
|
138 |
template = hlp.toString(); |
139 |
|
140 |
props = new Properties(); |
141 |
|
142 |
initProps(); |
143 |
} |
144 |
|
145 |
/**
|
146 |
* This initialisizes all Properties Values with null.
|
147 |
*
|
148 |
**/
|
149 |
private void initProps() |
150 |
{ |
151 |
String [] propsArray = { $Comment, $$LANG_Comment, $Encoding, $Exec, $Arguments, |
152 |
$GenericName, $$LANG_GenericName, $MimeType, $Name, $$LANG_Name, |
153 |
$Path, $ServiceTypes, $SwallowExec, $SwallowTitle, $Terminal, |
154 |
$Options_For_Terminal, $Type, $X_KDE_SubstituteUID, $X_KDE_Username, |
155 |
$Icon, $URL |
156 |
}; |
157 |
|
158 |
for( int i = 0; i < propsArray.length; i++ ) |
159 |
{ |
160 |
props.put( propsArray[i], "" );
|
161 |
} |
162 |
} |
163 |
|
164 |
|
165 |
/** Overridden Method
|
166 |
* @see com.izforge.izpack.util.os.Shortcut#initialize(int, java.lang.String)
|
167 |
*/
|
168 |
public void initialize( int aType, String aName ) throws Exception |
169 |
{ |
170 |
this.itsType=aType;
|
171 |
this.itsName=aName;
|
172 |
props.put( $Name, aName ); |
173 |
} |
174 |
|
175 |
|
176 |
|
177 |
|
178 |
|
179 |
/**
|
180 |
* This indicates that Unix will be supported.
|
181 |
*
|
182 |
* @see com.izforge.izpack.util.os.Shortcut#supported()
|
183 |
*/
|
184 |
public boolean supported() |
185 |
{ |
186 |
return true; |
187 |
} |
188 |
|
189 |
|
190 |
|
191 |
/** Dummy
|
192 |
* @see com.izforge.izpack.util.os.Shortcut#getDirectoryCreated()
|
193 |
*/
|
194 |
public String getDirectoryCreated() |
195 |
{ |
196 |
return this.createdDirectory; //while not stored... |
197 |
} |
198 |
|
199 |
/** Dummy
|
200 |
* @see com.izforge.izpack.util.os.Shortcut#getFileName()
|
201 |
*/
|
202 |
public String getFileName() |
203 |
{ |
204 |
return( this.itsFileName ); |
205 |
} |
206 |
|
207 |
/** Overridden Compatibility Method.
|
208 |
*
|
209 |
* returns all directories in $USER/.kde/share/applink
|
210 |
*
|
211 |
* @see com.izforge.izpack.util.os.Shortcut#getProgramGroups(int)
|
212 |
*/
|
213 |
public Vector getProgramGroups( int userType ) |
214 |
{ |
215 |
//if( userType > CURRENT_USER )
|
216 |
//if( System.getProperty( "user.name" ).equals( "root" ) )
|
217 |
String FS = File.separator; |
218 |
//System.out.println( "userType" + userType );
|
219 |
Vector groups = new Vector(); |
220 |
|
221 |
File homedir = new File( System.getProperty( "user.home" ) ); |
222 |
|
223 |
File kdeHomeShareApplnk;
|
224 |
try
|
225 |
{ |
226 |
File kdeHome = new File( homedir.toString() + FS + ".kde" ); |
227 |
if( ! kdeHome.exists() )
|
228 |
return( groups );
|
229 |
File kdeHomeShare = new File( kdeHome.toString()+ FS + "share" ); |
230 |
if( ! kdeHomeShare.exists() )
|
231 |
return( groups );
|
232 |
kdeHomeShareApplnk = new File(kdeHomeShare.toString() + FS + "applnk"); |
233 |
String[] applnkFolders={"applnk-mdk","applnk-redhat","applnk"}; |
234 |
for (int i=0;i<applnkFolders.length;i++) |
235 |
{ |
236 |
kdeHomeShareApplnk = new File(kdeHomeShare.toString() + FS + applnkFolders[i]); |
237 |
if (kdeHomeShareApplnk.exists())
|
238 |
{ |
239 |
itsApplnkFolder=applnkFolders[i]; |
240 |
break;
|
241 |
} |
242 |
} |
243 |
if( ! kdeHomeShareApplnk.exists() )
|
244 |
return( groups );
|
245 |
} |
246 |
catch( Exception e1 ) |
247 |
{ |
248 |
//
|
249 |
e1.printStackTrace(); |
250 |
return( groups );
|
251 |
} |
252 |
|
253 |
try
|
254 |
{ |
255 |
File [] listing = kdeHomeShareApplnk.listFiles(); |
256 |
|
257 |
for( int i = 0; i < listing.length; i++ ) |
258 |
{ |
259 |
if( listing[ i ].isDirectory() )
|
260 |
{ |
261 |
groups.add( listing[ i ].getName() ); |
262 |
} |
263 |
} |
264 |
} |
265 |
catch( Exception e ) |
266 |
{ |
267 |
// ignore and return an empty vector.
|
268 |
} |
269 |
return( groups );
|
270 |
} |
271 |
|
272 |
|
273 |
|
274 |
/** overridden method
|
275 |
* @see com.izforge.izpack.util.os.Shortcut#multipleUsers()
|
276 |
* @return true
|
277 |
*/
|
278 |
public boolean multipleUsers() |
279 |
{ |
280 |
// EVER true for UNIXes ;-)
|
281 |
return (true); |
282 |
} |
283 |
|
284 |
/** Creates and stores the shortcut-files.
|
285 |
*
|
286 |
* @see com.izforge.izpack.util.os.Shortcut#save()
|
287 |
*/
|
288 |
public void save() throws Exception |
289 |
{ |
290 |
String FS = File.separator; |
291 |
String target = null; |
292 |
|
293 |
String shortCutDef = this.replace(); |
294 |
|
295 |
if( ( this.itsGroupName == null ) || ( "".equals( this.itsGroupName ) ) ) |
296 |
{ |
297 |
target = System.getProperty( "user.home" ) + FS + "Desktop" + FS + this.itsName + DESKTOP_EXT; |
298 |
} |
299 |
else
|
300 |
{ |
301 |
File homedir = new File( System.getProperty( "user.home" ) ); |
302 |
File kdeHome = new File( homedir.toString() + FS + ".kde" ); |
303 |
File kdeHomeShare = new File( kdeHome.toString()+ FS + "share" ); |
304 |
File kdeHomeShareApplnk = new File( kdeHomeShare.toString()+ FS + itsApplnkFolder); |
305 |
target = kdeHomeShareApplnk.toString() + FS + this.itsGroupName + FS + this.itsName + DESKTOP_EXT; |
306 |
} |
307 |
File targetPath = new File( target.toString().substring( 0, target.toString().lastIndexOf( File.separatorChar ) ) ); |
308 |
if( ! targetPath.exists() )
|
309 |
{ |
310 |
targetPath.mkdirs(); |
311 |
this.createdDirectory = targetPath.toString();
|
312 |
} |
313 |
|
314 |
|
315 |
this.itsFileName = new String( target ); |
316 |
File targetFileName = new File( target ); |
317 |
FileWriter fileWriter = null; |
318 |
try
|
319 |
{ |
320 |
fileWriter = new FileWriter( targetFileName ); |
321 |
} |
322 |
catch (IOException e1) |
323 |
{ |
324 |
e1.printStackTrace(); |
325 |
} |
326 |
|
327 |
try
|
328 |
{ |
329 |
fileWriter.write( shortCutDef ); |
330 |
} |
331 |
catch (IOException e) |
332 |
{ |
333 |
e.printStackTrace(); |
334 |
} |
335 |
try
|
336 |
{ |
337 |
fileWriter.close(); |
338 |
} |
339 |
catch (IOException e2) |
340 |
{ |
341 |
e2.printStackTrace(); |
342 |
} |
343 |
} |
344 |
|
345 |
/** Set the Commandline Arguments
|
346 |
* @see com.izforge.izpack.util.os.Shortcut#setArguments(java.lang.String)
|
347 |
*/
|
348 |
public void setArguments( String args ) |
349 |
{ |
350 |
this.itsArguments=args;
|
351 |
props.put( $Arguments, args );
|
352 |
} |
353 |
|
354 |
/** Sets the Description
|
355 |
* @see com.izforge.izpack.util.os.Shortcut#setDescription(java.lang.String)
|
356 |
*/
|
357 |
public void setDescription( String description ) |
358 |
{ |
359 |
this.itsDescription=description;
|
360 |
props.put( $Comment, description );
|
361 |
} |
362 |
|
363 |
/** Sets The Icon Path
|
364 |
* @see com.izforge.izpack.util.os.Shortcut#setIconLocation(java.lang.String, int)
|
365 |
*/
|
366 |
public void setIconLocation( String path, int index ) |
367 |
{ |
368 |
this.itsIconPath = path;
|
369 |
this.itsIconIndex = index;
|
370 |
props.put( $Icon, path ); |
371 |
//
|
372 |
} |
373 |
|
374 |
/** Sets the Name of this Shortcut
|
375 |
* @see com.izforge.izpack.util.os.Shortcut#setLinkName(java.lang.String)
|
376 |
*/
|
377 |
public void setLinkName( String aName ) |
378 |
{ |
379 |
this.itsName = aName;
|
380 |
props.put( $Name, aName ); |
381 |
} |
382 |
|
383 |
/** Sets the type of this Shortcut
|
384 |
* @see com.izforge.izpack.util.os.Shortcut#setLinkType(int)
|
385 |
*/
|
386 |
public void setLinkType(int aType) throws IllegalArgumentException |
387 |
{ |
388 |
this.itsType=aType;
|
389 |
} |
390 |
|
391 |
/** Sets the ProgramGroup
|
392 |
* @see com.izforge.izpack.util.os.Shortcut#setProgramGroup(java.lang.String)
|
393 |
*/
|
394 |
public void setProgramGroup(String aGroupName) |
395 |
{ |
396 |
this.itsGroupName=aGroupName;
|
397 |
} |
398 |
|
399 |
/** Sets the ShowMode
|
400 |
* @see com.izforge.izpack.util.os.Shortcut#setShowCommand(int)
|
401 |
*/
|
402 |
public void setShowCommand(int show) |
403 |
{ |
404 |
this.itsShow = show;
|
405 |
} |
406 |
|
407 |
/** Sets The TargetPath
|
408 |
* @see com.izforge.izpack.util.os.Shortcut#setTargetPath(java.lang.String)
|
409 |
*/
|
410 |
public void setTargetPath(String aPath) |
411 |
{ |
412 |
this.itsTargetPath = aPath;
|
413 |
|
414 |
props.put($Exec, aPath);
|
415 |
} |
416 |
|
417 |
/** Sets the usertype.
|
418 |
* @see com.izforge.izpack.util.os.Shortcut#setUserType(int)
|
419 |
*/
|
420 |
public void setUserType(int aUserType) |
421 |
{ |
422 |
this.itsUserType = aUserType;
|
423 |
} |
424 |
|
425 |
/** Sets the working-directory
|
426 |
* @see com.izforge.izpack.util.os.Shortcut#setWorkingDirectory(java.lang.String)
|
427 |
*/
|
428 |
public void setWorkingDirectory(String aDirectory) |
429 |
{ |
430 |
this.itsWorkingDirectory = aDirectory;
|
431 |
|
432 |
props.put( $Path, aDirectory );
|
433 |
} |
434 |
|
435 |
/** Dumps the Name to console.
|
436 |
* @see java.lang.Object#toString()
|
437 |
*/
|
438 |
public String toString() |
439 |
{ |
440 |
return this.itsName + N + template; |
441 |
} |
442 |
|
443 |
|
444 |
/**
|
445 |
* Creates the Shortcut String which will be stored as File.
|
446 |
* @return
|
447 |
*/
|
448 |
public String replace() |
449 |
{ |
450 |
String result = new String( template ); |
451 |
Enumeration enum = props.keys(); |
452 |
|
453 |
while( enum.hasMoreElements() ) |
454 |
{ |
455 |
String key = (String) enum.nextElement(); |
456 |
//**DEBUG: System.out.println(key+"="+props.getProperty( key ));
|
457 |
result = StringTool.replace( result, key, props.getProperty( key ) ); |
458 |
} |
459 |
//**DEBUG: System.out.println(result);
|
460 |
return result;
|
461 |
} |
462 |
|
463 |
|
464 |
/**
|
465 |
* Test Method
|
466 |
* @param Commnandline-Args
|
467 |
*/
|
468 |
public static void main(String[] args) |
469 |
{ |
470 |
Unix_Shortcut aSample = new Unix_Shortcut();
|
471 |
try
|
472 |
{ |
473 |
aSample.initialize(APPLICATIONS, "Start Tomcat");
|
474 |
} |
475 |
catch( Exception exc ) |
476 |
{ |
477 |
System.err.println("Could not init Unix_Shourtcut"); |
478 |
} |
479 |
aSample.replace(); |
480 |
System.out.println( aSample );
|
481 |
|
482 |
File targetFileName = new File( System.getProperty("user.home") + File.separator + "Start Tomcat" + DESKTOP_EXT ); |
483 |
FileWriter fileWriter = null; |
484 |
try
|
485 |
{ |
486 |
fileWriter = new FileWriter( targetFileName ); |
487 |
} |
488 |
catch (IOException e1) |
489 |
{ |
490 |
e1.printStackTrace(); |
491 |
} |
492 |
|
493 |
try
|
494 |
{ |
495 |
fileWriter.write( template ); |
496 |
} |
497 |
catch (IOException e) |
498 |
{ |
499 |
e.printStackTrace(); |
500 |
} |
501 |
try
|
502 |
{ |
503 |
fileWriter.close(); |
504 |
} |
505 |
catch (IOException e2) |
506 |
{ |
507 |
e2.printStackTrace(); |
508 |
} |
509 |
} |
510 |
/** Sets The Encoding
|
511 |
* @see com.izforge.izpack.util.os.Shortcut#setEncoding(java.lang.String)
|
512 |
*/
|
513 |
public void setEncoding(String aEncoding) |
514 |
{ |
515 |
props.put( $Encoding, aEncoding );
|
516 |
} |
517 |
|
518 |
/** Sets The KDE Specific subst UID property
|
519 |
* @see com.izforge.izpack.util.os.Shortcut#setKdeSubstUID(java.lang.String)
|
520 |
*/
|
521 |
public void setKdeSubstUID(String aKDESubstUID) |
522 |
{ |
523 |
props.put( $X_KDE_SubstituteUID, aKDESubstUID );
|
524 |
} |
525 |
|
526 |
/** Sets the MimeType
|
527 |
* @see com.izforge.izpack.util.os.Shortcut#setMimetype(java.lang.String)
|
528 |
*/
|
529 |
public void setMimetype( String aMimetype ) |
530 |
{ |
531 |
props.put( $MimeType, aMimetype );
|
532 |
} |
533 |
|
534 |
/** Sets the terminal
|
535 |
* @see com.izforge.izpack.util.os.Shortcut#setTerminal(java.lang.String)
|
536 |
*/
|
537 |
public void setTerminal(String trueFalseOrNothing ) |
538 |
{ |
539 |
props.put( $Terminal, trueFalseOrNothing );
|
540 |
} |
541 |
|
542 |
/** Sets the terminal options
|
543 |
* @see com.izforge.izpack.util.os.Shortcut#setTerminalOptions(java.lang.String)
|
544 |
*/
|
545 |
public void setTerminalOptions( String someTerminalOptions ) |
546 |
{ |
547 |
props.put( $Options_For_Terminal, someTerminalOptions );
|
548 |
} |
549 |
|
550 |
/** Sets the Shortcut type (one of Application, Link or Device)
|
551 |
* @see com.izforge.izpack.util.os.Shortcut#setType(java.lang.String)
|
552 |
*/
|
553 |
public void setType( String aType ) |
554 |
{ |
555 |
props.put( $Type, aType ); |
556 |
} |
557 |
|
558 |
/** Sets the Url for type Link.
|
559 |
* Can be also a apsolute file/path
|
560 |
*
|
561 |
* @see com.izforge.izpack.util.os.Shortcut#setURL(java.lang.String)
|
562 |
*/
|
563 |
public void setURL( String anUrl ) |
564 |
{ |
565 |
props.put( $URL, anUrl ); |
566 |
} |
567 |
|
568 |
} |