001: /*
002: * Copyright 2005 Paul Hinds
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016: package org.tp23.antinstaller.taskdefs;
017:
018: import java.io.BufferedReader;
019: import java.io.File;
020: import java.io.IOException;
021: import java.io.InputStreamReader;
022: import java.io.PrintWriter;
023: import java.io.StringWriter;
024:
025: import org.apache.tools.ant.BuildException;
026: import org.apache.tools.ant.Task;
027: import org.tp23.antinstaller.selfextract.ResourceExtractor;
028:
029: /**
030: *
031: * @author teknopaul
032: *
033: */
034: public class KdeIconTask extends Task {
035:
036: private String desktop;
037: private String icon;
038: private String installDir;
039:
040: public void execute() throws BuildException {
041: if (desktop == null || icon == null || installDir == null) {
042: throw new BuildException("Missing attribute in KdeIconTask");
043: }
044:
045: ResourceExtractor re = new ResourceExtractor();
046:
047: File appsDir = null;
048: try {
049: appsDir = getAppsDir();
050: } catch (Exception e) {
051: throw new BuildException("Can not determine KDE directory");
052: }
053: if (appsDir != null && appsDir.exists()) {
054: try {
055: String desktopFileName = desktop.substring(desktop
056: .lastIndexOf('/') + 1);
057: log("creating: "
058: + new File(appsDir, desktopFileName)
059: .getCanonicalPath());
060: re.copyResource(desktop, new File(appsDir,
061: desktopFileName), "@installDir@", installDir);
062: } catch (Exception e) {
063: StringWriter sw = new StringWriter();
064: e.printStackTrace(new PrintWriter(sw));
065: throw new BuildException(sw.getBuffer().toString());
066: }
067: } else {
068: log("apps dir does not exist");
069: }
070: File iconDir = null;
071: try {
072: iconDir = getIconDir();
073: } catch (Exception e) {
074: throw new BuildException("Can not determine KDE directory");
075: }
076: if (iconDir != null && iconDir.exists()) {
077: try {
078: String iconFileName = icon.substring(icon
079: .lastIndexOf('/') + 1);
080: log("creating: "
081: + new File(iconDir, iconFileName)
082: .getCanonicalPath());
083: re.copyResourceBinary(icon, new File(iconDir,
084: iconFileName));
085: } catch (IOException e) {
086: throw new BuildException(e.getMessage());
087: }
088: } else {
089: log("icon dir does not exist");
090: }
091: try {
092: Process proc = Runtime.getRuntime().exec("kbuildsycoca");
093: } catch (Exception e) {
094: log("error running kbuildsycoca: " + e.getMessage());
095: }
096:
097: }
098:
099: private File getKdeDir(String pathdef, String extension)
100: throws IOException, InterruptedException {
101: String[] args = { "kde-config", "--path", pathdef };
102: Process proc = Runtime.getRuntime().exec(args);
103: BufferedReader br = new BufferedReader(new InputStreamReader(
104: proc.getInputStream()));
105: String appsDirs = br.readLine();
106: int idx = -1;
107: if (appsDirs != null) {
108: idx = appsDirs.indexOf(':');
109: }
110: br.close();
111:
112: if (idx > -1 && idx < appsDirs.length() - 1) {
113: String privatedir = appsDirs.substring(0, idx - 1);
114: if (!privatedir.endsWith("/")) {
115: privatedir = privatedir + "/";
116: }
117: return new File(privatedir + extension);
118: } else {
119: if (!appsDirs.endsWith("/")) {
120: appsDirs = appsDirs + "/";
121: }
122: return new File(appsDirs + extension);
123: }
124: }
125:
126: private File getAppsDir() throws IOException, InterruptedException {
127: return getKdeDir("xdgdata-apps", "");
128: }
129:
130: private File getIconDir() throws IOException, InterruptedException {
131: return getKdeDir("icon", "hicolor/48x48/apps");
132: }
133:
134: public String getDesktop() {
135: return desktop;
136: }
137:
138: public void setDesktop(String desktop) {
139: this .desktop = desktop;
140: }
141:
142: public String getIcon() {
143: return icon;
144: }
145:
146: public void setIcon(String icon) {
147: this .icon = icon;
148: }
149:
150: public String getInstallDir() {
151: return installDir;
152: }
153:
154: public void setInstallDir(String installDir) {
155: this.installDir = installDir;
156: }
157:
158: }
|