View Javadoc

1    /*
2     * This is free software; you can redistribute it and/or modify it
3     * under the terms of the GNU Lesser General Public License as
4     * published by the Free Software Foundation; either version 2.1 of
5     * the License, or (at your option) any later version.
6     *
7     * This software is distributed in the hope that it will be useful,
8     * but WITHOUT ANY WARRANTY; without even the implied warranty of
9     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
10    * Lesser General Public License for more details.
11    *
12    * You should have received a copy of the GNU Lesser General Public
13    * License along with this software; if not, write to the Free
14    * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
15    * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
16    */
17  
18  package installtoolkit.deb;
19  
20  import java.io.File;
21  import java.io.FileOutputStream;
22  import java.io.IOException;
23  import java.io.PrintWriter;
24  import java.util.ArrayList;
25  import java.util.List;
26  
27  import org.apache.tools.ant.Task;
28  import org.apache.tools.ant.BuildException;
29  
30  /**
31   * Helper Task to create startmenue links.
32   * 
33   * @author Christian Elberfeld <elberfeld@web.de>
34   *
35   */
36  public class StartmenueLinksTask extends Task {
37  
38  	protected boolean debug = false;
39  	protected File destdir;
40  	protected String packagename;
41  	
42  	protected List<LinkType> links = new ArrayList<LinkType>();
43  
44  	/**
45  	 * @see Task#execute()
46  	 */
47  	@Override
48  	public void execute() throws BuildException {
49  		
50  		try {
51  		
52  			if (debug) handleOutput("starting execute()");
53  			
54  			if (this.destdir == null) throw new BuildException("Attribute \"destdir\" must be set");		
55  			if (this.packagename == null) throw new BuildException("Attribute \"packagename\" must be set");		
56  
57  			if (debug) handleOutput("Destdir: "+this.destdir);		
58  			destdir.mkdirs();
59  			
60  			for (LinkType link : this.links) createLink(link,links.indexOf(link));
61  			
62  			if (debug) handleOutput("execute() finished");
63  			
64  		} catch (IOException e) {
65  		
66  			throw new BuildException(e.getMessage(),e);
67  		}	
68  		
69  	} // execute()
70  
71  	/**
72  	 * Create a startmenue link
73  	 *  
74  	 * @param link Link informations as LinkType oblect
75  	 * @param index Index number of the link
76  	 * @throws IOException
77  	 */
78  	private void createLink(LinkType link, int index) throws IOException {
79  		
80  		if (debug) handleOutput(link.toString());
81  		
82  		File file = new File(this.destdir,this.packagename+"_"+index+".desktop");
83  		
84  		if (debug) handleOutput("writing File: "+file.getAbsolutePath());
85  		
86  		file.createNewFile();
87  		PrintWriter pw = new PrintWriter(new FileOutputStream(file));
88  		
89  		pw.println("[Desktop Entry]");
90  		pw.println("Name="+link.getName());
91  		pw.println("Comment="+link.getComment());
92  		if (link.getIcon() != null) pw.println("Icon="+link.getIcon());
93  		pw.println("Exec="+link.getCommand());
94  		pw.println("Terminal="+link.isTerminal());
95  		pw.println("Type=Application");
96  		pw.println("Categories="+link.getCategory());
97  		
98  		pw.close();
99  	}
100 	
101 	public void addLink(LinkType link) {
102 		
103 		this.links.add(link);
104 	}
105 	
106 	public void setDebug(boolean debug) {
107 		this.debug = debug;
108 	}
109 
110 	public void setDestdir(File destdir) {
111 		this.destdir = destdir;
112 	}
113 
114 	public void setPackagename(String packagename) {
115 		this.packagename = packagename;
116 	}
117 	
118 	
119 }