Mercurial > projects > dwt-mac
comparison dwt/program/Program.d @ 0:380af2bdd8e5
Upload of whole dwt tree
author | Jacob Carlborg <doob@me.com> <jacob.carlborg@gmail.com> |
---|---|
date | Sat, 09 Aug 2008 17:00:02 +0200 |
parents | |
children | 1a8b3cb347e0 |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:380af2bdd8e5 |
---|---|
1 /******************************************************************************* | |
2 * Copyright (c) 2000, 2007 IBM Corporation and others. | |
3 * All rights reserved. This program and the accompanying materials | |
4 * are made available under the terms of the Eclipse Public License v1.0 | |
5 * which accompanies this distribution, and is available at | |
6 * http://www.eclipse.org/legal/epl-v10.html | |
7 * | |
8 * Contributors: | |
9 * IBM Corporation - initial API and implementation | |
10 *******************************************************************************/ | |
11 module dwt.program.Program; | |
12 | |
13 import dwt.dwthelper.utils; | |
14 | |
15 | |
16 import dwt.DWT; | |
17 import dwt.graphics.ImageData; | |
18 import dwt.internal.cocoa.NSString; | |
19 import dwt.internal.cocoa.NSURL; | |
20 import dwt.internal.cocoa.NSWorkspace; | |
21 | |
22 /** | |
23 * Instances of this class represent programs and | |
24 * their associated file extensions in the operating | |
25 * system. | |
26 */ | |
27 public final class Program { | |
28 String name; | |
29 byte[] fsRef; | |
30 | |
31 /** | |
32 * Prevents uninitialized instances from being created outside the package. | |
33 */ | |
34 Program () { | |
35 } | |
36 | |
37 /** | |
38 * Finds the program that is associated with an extension. | |
39 * The extension may or may not begin with a '.'. Note that | |
40 * a <code>Display</code> must already exist to guarantee that | |
41 * this method returns an appropriate result. | |
42 * | |
43 * @param extension the program extension | |
44 * @return the program or <code>null</code> | |
45 * | |
46 * @exception IllegalArgumentException <ul> | |
47 * <li>ERROR_NULL_ARGUMENT when extension is null</li> | |
48 * </ul> | |
49 */ | |
50 public static Program findProgram (String extension) { | |
51 if (extension is null) DWT.error (DWT.ERROR_NULL_ARGUMENT); | |
52 if (extension.length () is 0) return null; | |
53 // char[] chars; | |
54 // if (extension.charAt (0) !is '.') { | |
55 // chars = new char[extension.length()]; | |
56 // extension.getChars(0, chars.length, chars, 0); | |
57 // } else { | |
58 // chars = new char[extension.length() - 1]; | |
59 // extension.getChars(1, extension.length(), chars, 0); | |
60 // } | |
61 // int ext = OS.CFStringCreateWithCharacters(OS.kCFAllocatorDefault, chars, chars.length); | |
62 // Program program = null; | |
63 // if (ext !is 0) { | |
64 // byte[] fsRef = new byte[80]; | |
65 // if (OS.LSGetApplicationForInfo(OS.kLSUnknownType, OS.kLSUnknownCreator, ext, OS.kLSRolesAll, fsRef, null) is OS.noErr) { | |
66 // program = getProgram(fsRef); | |
67 // } | |
68 // OS.CFRelease(ext); | |
69 // } | |
70 // return program; | |
71 return null; | |
72 } | |
73 | |
74 /** | |
75 * Answer all program extensions in the operating system. Note | |
76 * that a <code>Display</code> must already exist to guarantee | |
77 * that this method returns an appropriate result. | |
78 * | |
79 * @return an array of extensions | |
80 */ | |
81 public static String [] getExtensions () { | |
82 return new String [] { | |
83 // From System-Declared Uniform Type Identifiers | |
84 ".txt", | |
85 ".rtf", | |
86 ".html", | |
87 ".htm", | |
88 ".xml", | |
89 ".c", | |
90 ".m", | |
91 ".cp", ".cpp", ".c++", ".cc", ".cxx", | |
92 ".mm", | |
93 ".h", | |
94 ".hpp", | |
95 ".h++", | |
96 ".hxx", | |
97 ".java", | |
98 ".jav", | |
99 ".s", | |
100 ".r", | |
101 ".defs", | |
102 ".mig", | |
103 ".exp", | |
104 ".js", | |
105 ".jscript", | |
106 ".javascript", | |
107 ".sh", | |
108 ".command", | |
109 ".csh", | |
110 ".pl", | |
111 ".pm", | |
112 ".py", | |
113 ".rb", | |
114 ".rbw", | |
115 ".php", | |
116 ".php3", | |
117 ".php4", | |
118 ".ph3", | |
119 ".ph4", | |
120 ".phtml", | |
121 ".jnlp", | |
122 ".applescript", | |
123 ".scpt", | |
124 ".o", | |
125 ".exe", | |
126 ".dll", | |
127 ".class", | |
128 ".jar", | |
129 ".qtz", | |
130 ".gtar", | |
131 ".tar", | |
132 ".gz", | |
133 ".gzip", | |
134 ".tgz", | |
135 ".hqx", | |
136 ".bin", | |
137 ".vcf", | |
138 ".vcard", | |
139 ".jpg", | |
140 ".jpeg", | |
141 ".jp2", | |
142 ".tif", | |
143 ".tiff", | |
144 ".pic", | |
145 ".pct", | |
146 ".pict", | |
147 ".pntg", | |
148 ".png", | |
149 ".xbm", | |
150 ".qif", | |
151 ".qtif", | |
152 ".icns", | |
153 ".mov", | |
154 ".qt", | |
155 ".avi", | |
156 ".vfw", | |
157 ".mpg", | |
158 ".mpeg", | |
159 ".m75", | |
160 ".m15", | |
161 ".mp4", | |
162 ".3gp", | |
163 ".3gpp", | |
164 ".3g2", | |
165 ".3gp2", | |
166 ".mp3", | |
167 ".m4a", | |
168 ".m4p", | |
169 ".m4b", | |
170 ".au", | |
171 ".ulw", | |
172 ".snd", | |
173 ".aifc", | |
174 ".aiff", | |
175 ".aif", | |
176 ".caf", | |
177 ".bundle", | |
178 ".app", | |
179 ".plugin", | |
180 ".mdimporter", | |
181 ".wdgt", | |
182 ".cpio", | |
183 ".zip", | |
184 ".framework", | |
185 ".rtfd", | |
186 ".dfont", | |
187 ".otf", | |
188 ".ttf", | |
189 ".ttc", | |
190 ".suit", | |
191 ".pfb", | |
192 ".pfa", | |
193 ".icc", | |
194 ".icm", | |
195 ".pf", | |
196 ".pdf", | |
197 ".ps", | |
198 ".eps", | |
199 ".psd", | |
200 ".ai", | |
201 ".gif", | |
202 ".bmp", | |
203 ".ico", | |
204 ".doc", | |
205 ".xls", | |
206 ".ppt", | |
207 ".wav", | |
208 ".wave", | |
209 ".asf", | |
210 ".wm", | |
211 ".wmv", | |
212 ".wmp", | |
213 ".wma", | |
214 ".asx", | |
215 ".wmx", | |
216 ".wvx", | |
217 ".wax", | |
218 ".key", | |
219 ".kth", | |
220 ".tga", | |
221 ".sgi", | |
222 ".exr", | |
223 ".fpx", | |
224 ".jfx", | |
225 ".efx", | |
226 ".sd2", | |
227 ".rm", | |
228 ".ram", | |
229 ".ra", | |
230 ".smil", | |
231 ".sit", | |
232 ".sitx", | |
233 // Others | |
234 ".plist", | |
235 ".nib", | |
236 ".lproj", | |
237 // iChat | |
238 ".iPhoto", | |
239 // iChat | |
240 ".iChat", | |
241 ".chat", | |
242 // acrobat reader | |
243 ".rmf", | |
244 ".xfdf", | |
245 ".fdf", | |
246 // Chess | |
247 ".game", | |
248 ".pgn", | |
249 // iCal | |
250 ".ics", | |
251 ".vcs", | |
252 ".aplmodel", | |
253 ".icbu", | |
254 ".icalevent", | |
255 ".icaltodo", | |
256 // Mail | |
257 ".mailhold", | |
258 ".mbox", | |
259 ".imapmbox", | |
260 ".emlx", | |
261 ".mailextract", | |
262 // Sherlock | |
263 ".sherlock", | |
264 // Stickies | |
265 ".tpl", | |
266 // System Preferences | |
267 ".prefPane", | |
268 ".sliderSaver", | |
269 ".saver", | |
270 // Console | |
271 ".log", | |
272 // Grapher | |
273 ".gcx", | |
274 }; | |
275 } | |
276 | |
277 /** | |
278 * Answers all available programs in the operating system. Note | |
279 * that a <code>Display</code> must already exist to guarantee | |
280 * that this method returns an appropriate result. | |
281 * | |
282 * @return an array of programs | |
283 */ | |
284 public static Program [] getPrograms () { | |
285 return new Program[0]; | |
286 // Hashtable bundles = new Hashtable(); | |
287 // String[] extensions = getExtensions(); | |
288 // byte[] fsRef = new byte[80]; | |
289 // for (int i = 0; i < extensions.length; i++) { | |
290 // String extension = extensions[i]; | |
291 // char[] chars = new char[extension.length() - 1]; | |
292 // extension.getChars(1, extension.length(), chars, 0); | |
293 // int ext = OS.CFStringCreateWithCharacters(OS.kCFAllocatorDefault, chars, chars.length); | |
294 // if (ext !is 0) { | |
295 // if (OS.LSGetApplicationForInfo(OS.kLSUnknownType, OS.kLSUnknownCreator, ext, OS.kLSRolesAll, fsRef, null) is OS.noErr) { | |
296 // Program program = getProgram(fsRef); | |
297 // if (program !is null && bundles.get(program.getName()) is null) { | |
298 // bundles.put(program.getName(), program); | |
299 // fsRef = new byte[80]; | |
300 // } | |
301 // } | |
302 // if (OS.VERSION >= 0x1040) { | |
303 // int utis = OS.UTTypeCreateAllIdentifiersForTag(OS.kUTTagClassFilenameExtension(), ext, 0); | |
304 // if (utis !is 0) { | |
305 // int utiCount = OS.CFArrayGetCount(utis); | |
306 // for (int j = 0; j < utiCount; j++) { | |
307 // int uti = OS.CFArrayGetValueAtIndex(utis, j); | |
308 // if (uti !is 0) { | |
309 // int apps = OS.LSCopyAllRoleHandlersForContentType(uti, OS.kLSRolesAll); | |
310 // if (apps !is 0) { | |
311 // int appCount = OS.CFArrayGetCount(apps); | |
312 // for (int k = 0; k < appCount; k++) { | |
313 // int app = OS.CFArrayGetValueAtIndex(apps, k); | |
314 // if (app !is 0) {; | |
315 // if (OS.LSFindApplicationForInfo(OS.kLSUnknownCreator, app, 0, fsRef, null) is OS.noErr) { | |
316 // Program program = getProgram(fsRef); | |
317 // if (program !is null && bundles.get(program.getName()) is null) { | |
318 // bundles.put(program.getName(), program); | |
319 // fsRef = new byte[80]; | |
320 // } | |
321 // } | |
322 // } | |
323 // } | |
324 // OS.CFRelease(apps); | |
325 // } | |
326 // } | |
327 // } | |
328 // OS.CFRelease(utis); | |
329 // } | |
330 // } | |
331 // OS.CFRelease(ext); | |
332 // } | |
333 // } | |
334 // int count = 0; | |
335 // Program[] programs = new Program[bundles.size()]; | |
336 // Enumeration values = bundles.elements(); | |
337 // while (values.hasMoreElements()) { | |
338 // programs[count++] = (Program)values.nextElement(); | |
339 // } | |
340 // return programs; | |
341 } | |
342 | |
343 /** | |
344 * Launches the executable associated with the file in | |
345 * the operating system. If the file is an executable, | |
346 * then the executable is launched. Note that a <code>Display</code> | |
347 * must already exist to guarantee that this method returns | |
348 * an appropriate result. | |
349 * | |
350 * @param fileName the file or program name | |
351 * @return <code>true</code> if the file is launched, otherwise <code>false</code> | |
352 * | |
353 * @exception IllegalArgumentException <ul> | |
354 * <li>ERROR_NULL_ARGUMENT when fileName is null</li> | |
355 * </ul> | |
356 */ | |
357 public static bool launch (String fileName) { | |
358 if (fileName is null) DWT.error (DWT.ERROR_NULL_ARGUMENT); | |
359 if (fileName.indexOf(':') is -1) fileName = "file://" + fileName; | |
360 NSWorkspace workspace = NSWorkspace.sharedWorkspace(); | |
361 return workspace.openURL(NSURL.static_URLWithString_(NSString.stringWith(fileName))); | |
362 } | |
363 | |
364 /** | |
365 * Executes the program with the file as the single argument | |
366 * in the operating system. It is the responsibility of the | |
367 * programmer to ensure that the file contains valid data for | |
368 * this program. | |
369 * | |
370 * @param fileName the file or program name | |
371 * @return <code>true</code> if the file is launched, otherwise <code>false</code> | |
372 * | |
373 * @exception IllegalArgumentException <ul> | |
374 * <li>ERROR_NULL_ARGUMENT when fileName is null</li> | |
375 * </ul> | |
376 */ | |
377 public bool execute (String fileName) { | |
378 if (fileName is null) DWT.error(DWT.ERROR_NULL_ARGUMENT); | |
379 // if (OS.VERSION < 0x1040) return launch(fileName); | |
380 // int rc = -1; | |
381 // int fsRefPtr = OS.NewPtr(fsRef.length); | |
382 // if (fsRefPtr !is 0) { | |
383 // OS.memmove(fsRefPtr, fsRef, fsRef.length); | |
384 // LSApplicationParameters params = new LSApplicationParameters(); | |
385 // params.version = 0; | |
386 // params.flags = 0; | |
387 // params.application = fsRefPtr; | |
388 // if (fileName.length() is 0) { | |
389 // rc = OS.LSOpenApplication(params, null); | |
390 // } else { | |
391 // if (fileName.indexOf(':') is -1) fileName = "file://" + fileName; | |
392 // char[] chars = new char[fileName.length()]; | |
393 // fileName.getChars(0, chars.length, chars, 0); | |
394 // int str = OS.CFStringCreateWithCharacters(0, chars, chars.length); | |
395 // if (str !is 0) { | |
396 // int unscapedStr = OS.CFStringCreateWithCharacters(0, new char[]{'%'}, 1); | |
397 // int escapedStr = OS.CFURLCreateStringByAddingPercentEscapes(OS.kCFAllocatorDefault, str, unscapedStr, 0, OS.kCFStringEncodingUTF8); | |
398 // if (escapedStr !is 0) { | |
399 // int urls = OS.CFArrayCreateMutable(OS.kCFAllocatorDefault, 1, 0); | |
400 // if (urls !is 0) { | |
401 // int url = OS.CFURLCreateWithString(OS.kCFAllocatorDefault, escapedStr, 0); | |
402 // if (url !is 0) { | |
403 // OS.CFArrayAppendValue(urls, url); | |
404 // rc = OS.LSOpenURLsWithRole(urls, OS.kLSRolesAll, 0, params, null, 0); | |
405 // } | |
406 // OS.CFRelease(urls); | |
407 // } | |
408 // OS.CFRelease(escapedStr); | |
409 // } | |
410 // if (unscapedStr !is 0) OS.CFRelease(unscapedStr); | |
411 // OS.CFRelease(str); | |
412 // } | |
413 // } | |
414 // OS.DisposePtr(fsRefPtr); | |
415 // } | |
416 // return rc is OS.noErr; | |
417 return false; | |
418 } | |
419 | |
420 /** | |
421 * Returns the receiver's image data. This is the icon | |
422 * that is associated with the receiver in the operating | |
423 * system. | |
424 * | |
425 * @return the image data for the program, may be null | |
426 */ | |
427 public ImageData getImageData () { | |
428 // int[] iconRef = new int[1]; | |
429 // OS.GetIconRefFromFileInfo(fsRef, 0, null, 0, 0, 0, iconRef, null); | |
430 // int[] family = new int[1]; | |
431 // int rc = OS.IconRefToIconFamily(iconRef[0], OS.kSelectorAlLAvailableData, family); | |
432 // OS.ReleaseIconRef(iconRef[0]); | |
433 // if (rc !is OS.noErr) return null; | |
434 //// ImageData result = createImageFromFamily(family[0], OS.kLarge32BitData, OS.kLarge8BitMask, 32, 32); | |
435 // ImageData result = createImageFromFamily(family[0], OS.kSmall32BitData, OS.kSmall8BitMask, 16, 16); | |
436 // OS.DisposeHandle(family[0]); | |
437 // if (result is null) { | |
438 // RGB[] rgbs = new RGB[] { | |
439 // new RGB(0xff, 0xff, 0xff), | |
440 // new RGB(0x5f, 0x5f, 0x5f), | |
441 // new RGB(0x80, 0x80, 0x80), | |
442 // new RGB(0xC0, 0xC0, 0xC0), | |
443 // new RGB(0xDF, 0xDF, 0xBF), | |
444 // new RGB(0xFF, 0xDF, 0x9F), | |
445 // new RGB(0x00, 0x00, 0x00), | |
446 // }; | |
447 // result = new ImageData(16, 16, 4, new PaletteData(rgbs) ); | |
448 // result.transparentPixel = 6; // use black for transparency | |
449 // String[] p= { | |
450 // "CCCCCCCCGGG", | |
451 // "CFAAAAACBGG", | |
452 // "CAAAAAACFBG", | |
453 // "CAAAAAACBBB", | |
454 // "CAAAAAAAAEB", | |
455 // "CAAAAAAAAEB", | |
456 // "CAAAAAAAAEB", | |
457 // "CAAAAAAAAEB", | |
458 // "CAAAAAAAAEB", | |
459 // "CAAAAAAAAEB", | |
460 // "CAAAAAAAAEB", | |
461 // "CAAAAAAAAEB", | |
462 // "CDDDDDDDDDB", | |
463 // "CBBBBBBBBBB", | |
464 // }; | |
465 // for (int y= 0; y < p.length; y++) { | |
466 // for (int x= 0; x < 11; x++) { | |
467 // result.setPixel(x+3, y+1, p[y].charAt(x)-'A'); | |
468 // } | |
469 // } | |
470 // } | |
471 // return result; | |
472 return null; | |
473 } | |
474 | |
475 /** | |
476 * Returns the receiver's name. This is as short and | |
477 * descriptive a name as possible for the program. If | |
478 * the program has no descriptive name, this string may | |
479 * be the executable name, path or empty. | |
480 * | |
481 * @return the name of the program | |
482 */ | |
483 public String getName () { | |
484 return name; | |
485 } | |
486 | |
487 //static Program getProgram(byte[] fsRef) { | |
488 // String name = ""; | |
489 // int[] namePtr = new int[1]; | |
490 // OS.LSCopyDisplayNameForRef(fsRef, namePtr); | |
491 // if (namePtr[0] !is 0) { | |
492 // int length = OS.CFStringGetLength(namePtr[0]); | |
493 // if (length !is 0) { | |
494 // char[] buffer= new char[length]; | |
495 // CFRange range = new CFRange(); | |
496 // range.length = length; | |
497 // OS.CFStringGetCharacters(namePtr[0], range, buffer); | |
498 // name = new String(buffer); | |
499 // } | |
500 // OS.CFRelease(namePtr[0]); | |
501 // } | |
502 // Program program = new Program(); | |
503 // program.fsRef = fsRef; | |
504 // program.name = name; | |
505 // return program; | |
506 //} | |
507 | |
508 /** | |
509 * Compares the argument to the receiver, and returns true | |
510 * if they represent the <em>same</em> object using a class | |
511 * specific comparison. | |
512 * | |
513 * @param other the object to compare with this object | |
514 * @return <code>true</code> if the object is the same as this object and <code>false</code> otherwise | |
515 * | |
516 * @see #hashCode() | |
517 */ | |
518 public bool equals(Object other) { | |
519 if (this is other) return true; | |
520 if (other instanceof Program) { | |
521 final Program program = (Program) other; | |
522 return name.equals(program.name); | |
523 } | |
524 return false; | |
525 } | |
526 | |
527 /** | |
528 * Returns an integer hash code for the receiver. Any two | |
529 * objects that return <code>true</code> when passed to | |
530 * <code>equals</code> must return the same value for this | |
531 * method. | |
532 * | |
533 * @return the receiver's hash | |
534 * | |
535 * @see #equals(Object) | |
536 */ | |
537 public int hashCode() { | |
538 return name.hashCode(); | |
539 } | |
540 | |
541 /** | |
542 * Returns a string containing a concise, human-readable | |
543 * description of the receiver. | |
544 * | |
545 * @return a string representation of the program | |
546 */ | |
547 public String toString () { | |
548 return "Program {" + name + "}"; | |
549 } | |
550 | |
551 } |