1 /* 
2  * Copyright 2005 Paul Hinds
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16package org.tp23.antinstaller.taskdefs;
17
18import java.io.BufferedReader;
19import java.io.File;
20import java.io.IOException;
21import java.io.InputStreamReader;
22import java.io.PrintWriter;
23import java.io.StringWriter;
24
25import org.apache.tools.ant.BuildException;
26import org.apache.tools.ant.Task;
27import org.tp23.antinstaller.selfextract.ResourceExtractor;
28/**
29 * 
30 * @author teknopaul
31 *
32 */
33public class KdeIconTask extends Task{
34
35    private String desktop;
36    private String icon;
37    private String installDir;
38    
39    public void execute() throws BuildException{
40        if(desktop == null || icon == null || installDir == null){
41            throw new BuildException("Missing attribute in KdeIconTask");
42        }
43        
44        ResourceExtractor re = new ResourceExtractor();
45
46        File appsDir = null;
47        try {
48            appsDir = getAppsDir();
49        } catch (Exception e) {
50            throw new BuildException("Can not determine KDE directory");
51        }
52        if( appsDir != null && appsDir.exists() ) {
53            try {
54                String desktopFileName = desktop.substring( desktop.lastIndexOf('/') + 1 );
55                log("creating: " + new File( appsDir, desktopFileName).getCanonicalPath());
56                re.copyResource(desktop, new File( appsDir, desktopFileName), "@installDir@", installDir);
57            } catch (Exception e) {
58                StringWriter sw = new StringWriter();
59                e.printStackTrace(new PrintWriter(sw));
60                throw new BuildException(sw.getBuffer().toString());
61            }
62        }
63        else{
64            log("apps dir does not exist");
65        }
66        File iconDir = null;
67        try {
68            iconDir = getIconDir();
69        } catch (Exception e) {
70            throw new BuildException("Can not determine KDE directory");
71        }
72        if(iconDir != null && iconDir.exists()){
73            try {               
74                String iconFileName = icon.substring( icon.lastIndexOf('/') + 1 );
75                log("creating: " + new File( iconDir, iconFileName).getCanonicalPath());
76                re.copyResourceBinary(icon, new File( iconDir, iconFileName ));
77            } catch (IOException e) {
78                throw new BuildException(e.getMessage());
79            }
80        }
81        else{
82            log("icon dir does not exist");
83        }
84        try {   
85            Process proc = Runtime.getRuntime().exec("kbuildsycoca");
86        } catch (Exception e) {
87            log("error running kbuildsycoca: " + e.getMessage());
88        }
89        
90    }
91    
92    private File getKdeDir(String pathdef, String extension) throws IOException, InterruptedException{
93        String[] args = {"kde-config", "--path", pathdef};
94        Process proc = Runtime.getRuntime().exec(args);
95        BufferedReader br = new BufferedReader( new InputStreamReader(proc.getInputStream()));
96        String appsDirs = br.readLine();
97        int idx = -1;
98        if(appsDirs != null) {
99            idx = appsDirs.indexOf(':');
00        }
01        br.close();
02    
03        if(idx > -1 && idx < appsDirs.length() - 1){
04            String privatedir = appsDirs.substring(0, idx - 1);
05            if(!privatedir.endsWith("/")){
06                privatedir = privatedir + "/"; 
07            }
08            return new File(privatedir + extension);
09        }
10        else{
11            if(!appsDirs.endsWith("/")){
12                appsDirs = appsDirs + "/"; 
13            }
14            return  new File(appsDirs + extension);
15        }
16    }
17    private File getAppsDir() throws IOException, InterruptedException{
18        return getKdeDir("xdgdata-apps", "");
19    }
20    private File getIconDir() throws IOException, InterruptedException{
21        return getKdeDir("icon", "hicolor/48x48/apps");
22    }
23    
24    
25    public String getDesktop() {
26        return desktop;
27    }
28    public void setDesktop(String desktop) {
29        this.desktop = desktop;
30    }
31    public String getIcon() {
32        return icon;
33    }
34    public void setIcon(String icon) {
35        this.icon = icon;
36    }
37
38    public String getInstallDir() {
39        return installDir;
40    }
41
42    public void setInstallDir(String installDir) {
43        this.installDir = installDir;
44    }
45    
46}
47