1
2
3
4 package net.sf.ovanttasks.ovnative.win32;
5
6 import java.io.File;
7 import java.io.FileOutputStream;
8 import java.io.FileReader;
9 import java.io.IOException;
10 import java.io.LineNumberReader;
11 import java.io.PrintStream;
12 import java.net.URL;
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50 public class UrlFile {
51
52 public static final String SIGNATURE = "[InternetShortcut]";
53 public static final String LINE_BREAK = "\r\n";
54 public static final int SHOWCOMMAND_NORMAL = -1;
55 public static final int SHOWCOMMAND_MAXIMIZED = 3;
56 public static final int SHOWCOMMAND_MINIMIZED = 7;
57 String workingDirectory;
58 String iconFile;
59 int iconIndex = -1;
60 URL url;
61 int showCommand = -1;
62 short hotKey = -1;
63 String modified;
64 File urlFile;
65
66
67
68
69
70
71
72
73
74
75
76
77 public UrlFile(File urlFile, boolean load) throws IOException {
78 if (!urlFile.getName().endsWith(".url")) {
79 throw new IllegalArgumentException("Url file name must end with \".url\"");
80 }
81 this.urlFile = urlFile;
82 if (load) {
83 load();
84 }
85 }
86
87 public void load() throws IOException {
88 if (!urlFile.exists() || (urlFile.length() == 0)) {
89 return;
90 }
91 LineNumberReader reader = new LineNumberReader(new FileReader(urlFile));
92
93 String line = reader.readLine();
94 if (!line.equals(SIGNATURE)) {
95 throw new Win32Exception("Shortcut Signature \"" + SIGNATURE + "\"" + " not found in file " + urlFile.getAbsolutePath());
96 }
97 while ((line = reader.readLine()) != null) {
98 int i = line.indexOf('=');
99 if (i == -1) {
100 throw new Win32Exception("Invalid Shortcut file format (line " + reader.getLineNumber() + "): \"=\" expected in \"" + line + "\"");
101 }
102 String key = line.substring(0, i);
103 String value = line.substring(i + 1);
104
105 if (key.equals("URL")) {
106 url = new URL(value);
107 } else if (key.equals("WorkingDirectory")) {
108 workingDirectory = value;
109 } else if (key.equals("IconFile")) {
110 iconFile = value;
111 } else if (key.equals("IconIndex")) {
112 iconIndex = Integer.parseInt(value);
113 } else if (key.equals("ShowCommand")) {
114 showCommand = Integer.parseInt(value);
115 } else if (key.equals("Modified")) {
116 modified = value;
117 } else if (key.equals("HotKey")) {
118 hotKey = Short.parseShort(value);
119 } else {
120 throw new Win32Exception("Invalid Shortcut file format (line " + reader.getLineNumber() + "): dont know key " + key);
121 }
122 }
123
124 reader.close();
125 }
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141 public UrlFile(String directory, String name, boolean load) throws IOException {
142 setUrlFile(directory, name);
143 if (load) {
144 load();
145 }
146 }
147
148 public File getUrlFile() {
149 return urlFile;
150 }
151
152 private void setUrlFile(final String directory, String name) {
153 if (!name.endsWith(".url")) {
154 name += ".url";
155 }
156 urlFile = new File(directory, name);
157
158 if (!urlFile.getParentFile().exists()) {
159 urlFile.getParentFile().mkdirs();
160 }
161 }
162
163 @Override
164 public String toString() {
165 StringBuffer sb = new StringBuffer();
166 sb.append(SIGNATURE).append(LINE_BREAK);
167
168 if (url != null) {
169 sb.append("URL=").append(url).append(LINE_BREAK);
170 }
171 if (showCommand != SHOWCOMMAND_NORMAL) {
172 sb.append("ShowCommand=").append(showCommand).append(LINE_BREAK);
173 }
174 if (workingDirectory != null) {
175 sb.append("WorkingDirectory=").append(workingDirectory).append(
176 LINE_BREAK);
177 }
178 if (iconFile != null) {
179 sb.append("IconFile=").append(iconFile).append(LINE_BREAK);
180 }
181 if (iconIndex != -1) {
182 sb.append("IconIndex=").append(iconIndex).append(LINE_BREAK);
183 }
184 if (modified != null) {
185 sb.append("Modified=").append(modified).append(LINE_BREAK);
186 }
187 if (hotKey != -1) {
188 sb.append("HotKey=").append(hotKey).append(LINE_BREAK);
189 }
190 return sb.toString();
191 }
192
193
194
195
196 public int getIconIndex() {
197 return iconIndex;
198 }
199
200
201
202
203 public String getModified() {
204 return modified;
205 }
206
207
208
209
210 public int getShowCommand() {
211 return showCommand;
212 }
213
214
215
216
217 public URL getUrl() {
218 return url;
219 }
220
221
222
223
224 public String getWorkingDirectory() {
225 return workingDirectory;
226 }
227
228
229
230
231
232 public void setModified(String string) {
233 modified = string;
234 }
235
236
237
238
239
240 public void setShowCommand(int i) {
241 showCommand = i;
242 }
243
244
245
246
247
248 public void setUrl(URL url) {
249 this.url = url;
250 }
251
252
253
254
255
256 public void setWorkingDirectory(String string) {
257 workingDirectory = string;
258 }
259
260 public void save(final String directory, final String name) throws IOException {
261 setUrlFile(directory, name);
262 save();
263 }
264
265 public void save() throws IOException {
266 PrintStream out = new PrintStream(new FileOutputStream(urlFile));
267 out.print(toString());
268 out.close();
269 }
270
271 public void save(File file) throws IOException {
272 this.urlFile = file;
273 save();
274 }
275
276 public String getHotKey() {
277 return HotKeyTools.toString(hotKey);
278 }
279
280
281
282
283 public String getIconFile() {
284 return iconFile;
285 }
286
287 public void setHotKey(String hotKey) {
288 this.hotKey = HotKeyTools.parseString(hotKey);
289 }
290
291
292
293
294
295
296 public void setIcon(String iconFile, int iconIndex) {
297 this.iconFile = iconFile;
298 this.iconIndex = iconIndex;
299 }
300 }