Mercurial > projects > dwt-mac
comparison dwt/program/Program.d @ 45:d8635bb48c7c
Merge with SWT 3.5
author | Jacob Carlborg <doob@me.com> |
---|---|
date | Mon, 01 Dec 2008 17:07:00 +0100 |
parents | a9ab4c738ed8 |
children | cfa563df4fdd |
comparison
equal
deleted
inserted
replaced
44:ca5e494f2bbf | 45:d8635bb48c7c |
---|---|
1 /******************************************************************************* | 1 /******************************************************************************* |
2 * Copyright (c) 2000, 2007 IBM Corporation and others. | 2 * Copyright (c) 2000, 2008 IBM Corporation and others. |
3 * All rights reserved. This program and the accompanying materials | 3 * All rights reserved. This program and the accompanying materials |
4 * are made available under the terms of the Eclipse Public License v1.0 | 4 * are made available under the terms of the Eclipse Public License v1.0 |
5 * which accompanies this distribution, and is available at | 5 * which accompanies this distribution, and is available at |
6 * http://www.eclipse.org/legal/epl-v10.html | 6 * http://www.eclipse.org/legal/epl-v10.html |
7 * | 7 * |
11 module dwt.program.Program; | 11 module dwt.program.Program; |
12 | 12 |
13 import dwt.dwthelper.utils; | 13 import dwt.dwthelper.utils; |
14 | 14 |
15 | 15 |
16 import java.util.Vector; | |
17 | |
16 import dwt.DWT; | 18 import dwt.DWT; |
17 import dwt.graphics.ImageData; | 19 import dwt.graphics.ImageData; |
20 import dwt.graphics.PaletteData; | |
21 import dwt.internal.C; | |
22 import dwt.internal.cocoa.NSArray; | |
23 import dwt.internal.cocoa.NSAutoreleasePool; | |
24 import dwt.internal.cocoa.NSBitmapImageRep; | |
25 import dwt.internal.cocoa.NSBundle; | |
26 import dwt.internal.cocoa.NSDictionary; | |
27 import dwt.internal.cocoa.NSDirectoryEnumerator; | |
28 import dwt.internal.cocoa.NSEnumerator; | |
29 import dwt.internal.cocoa.NSFileManager; | |
30 import dwt.internal.cocoa.NSImage; | |
31 import dwt.internal.cocoa.NSImageRep; | |
32 import dwt.internal.cocoa.NSMutableSet; | |
33 import dwt.internal.cocoa.NSSize; | |
18 import dwt.internal.cocoa.NSString; | 34 import dwt.internal.cocoa.NSString; |
19 import dwt.internal.cocoa.NSURL; | 35 import dwt.internal.cocoa.NSURL; |
20 import dwt.internal.cocoa.NSWorkspace; | 36 import dwt.internal.cocoa.NSWorkspace; |
37 import dwt.internal.cocoa.OS; | |
38 import dwt.internal.cocoa.id; | |
21 | 39 |
22 /** | 40 /** |
23 * Instances of this class represent programs and | 41 * Instances of this class represent programs and |
24 * their associated file extensions in the operating | 42 * their associated file extensions in the operating |
25 * system. | 43 * system. |
44 * | |
45 * @see <a href="http://www.eclipse.org/swt/snippets/#program">Program snippets</a> | |
46 * @see <a href="http://www.eclipse.org/swt/">Sample code and further information</a> | |
26 */ | 47 */ |
27 public final class Program { | 48 public final class Program { |
28 String name; | 49 String name, fullPath, identifier; |
29 byte[] fsRef; | 50 |
51 static final String PREFIX_FILE = "file://"; //$NON-NLS-1$ | |
52 static final String PREFIX_HTTP = "http://"; //$NON-NLS-1$ | |
53 static final String PREFIX_HTTPS = "https://"; //$NON-NLS-1$ | |
30 | 54 |
31 /** | 55 /** |
32 * Prevents uninitialized instances from being created outside the package. | 56 * Prevents uninitialized instances from being created outside the package. |
33 */ | 57 */ |
34 this () { | 58 Program () { |
35 } | 59 } |
36 | 60 |
37 /** | 61 /** |
38 * Finds the program that is associated with an extension. | 62 * Finds the program that is associated with an extension. |
39 * The extension may or may not begin with a '.'. Note that | 63 * The extension may or may not begin with a '.'. Note that |
48 * </ul> | 72 * </ul> |
49 */ | 73 */ |
50 public static Program findProgram (String extension) { | 74 public static Program findProgram (String extension) { |
51 if (extension is null) DWT.error (DWT.ERROR_NULL_ARGUMENT); | 75 if (extension is null) DWT.error (DWT.ERROR_NULL_ARGUMENT); |
52 if (extension.length () is 0) return null; | 76 if (extension.length () is 0) return null; |
53 // char[] chars; | 77 if (extension.charAt(0) !is '.') extension = "." + extension; |
54 // if (extension.charAt (0) !is '.') { | 78 NSAutoreleasePool pool = (NSAutoreleasePool) new NSAutoreleasePool().alloc().init(); |
55 // chars = new char[extension.length()]; | 79 try { |
56 // extension.getChars(0, chars.length, chars, 0); | 80 NSWorkspace workspace = NSWorkspace.sharedWorkspace(); |
57 // } else { | 81 int /*long*/ appName = OS.malloc(C.PTR_SIZEOF); |
58 // chars = new char[extension.length() - 1]; | 82 int /*long*/ type = OS.malloc(C.PTR_SIZEOF); |
59 // extension.getChars(1, extension.length(), chars, 0); | 83 NSString temp = new NSString(OS.NSTemporaryDirectory()); |
60 // } | 84 NSString fileName = NSString.stringWith("swt" + System.currentTimeMillis() + extension); |
61 // int ext = OS.CFStringCreateWithCharacters(OS.kCFAllocatorDefault, chars, chars.length); | 85 NSString fullPath = temp.stringByAppendingPathComponent(fileName); |
62 // Program program = null; | 86 NSFileManager fileManager = NSFileManager.defaultManager(); |
63 // if (ext !is 0) { | 87 fileManager.createFileAtPath(fullPath, null, null); |
64 // byte[] fsRef = new byte[80]; | 88 workspace.getInfoForFile(fullPath, appName, type); |
65 // if (OS.LSGetApplicationForInfo(OS.kLSUnknownType, OS.kLSUnknownCreator, ext, OS.kLSRolesAll, fsRef, null) is OS.noErr) { | 89 fileManager.removeItemAtPath(fullPath, 0); |
66 // program = getProgram(fsRef); | 90 int /*long*/ [] buffer = new int /*long*/[1]; |
67 // } | 91 OS.memmove(buffer, appName, C.PTR_SIZEOF); |
68 // OS.CFRelease(ext); | 92 OS.free(appName); |
69 // } | 93 OS.free(type); |
70 // return program; | 94 if (buffer [0] !is 0) { |
71 return null; | 95 NSString appPath = new NSString(buffer[0]); |
96 NSBundle bundle = NSBundle.bundleWithPath(appPath); | |
97 if (bundle !is null) return getProgram(bundle); | |
98 } | |
99 return null; | |
100 } finally { | |
101 pool.release(); | |
102 } | |
72 } | 103 } |
73 | 104 |
74 /** | 105 /** |
75 * Answer all program extensions in the operating system. Note | 106 * Answer all program extensions in the operating system. Note |
76 * that a <code>Display</code> must already exist to guarantee | 107 * that a <code>Display</code> must already exist to guarantee |
77 * that this method returns an appropriate result. | 108 * that this method returns an appropriate result. |
78 * | 109 * |
79 * @return an array of extensions | 110 * @return an array of extensions |
80 */ | 111 */ |
81 public static String [] getExtensions () { | 112 public static String [] getExtensions () { |
82 return new String [] { | 113 NSAutoreleasePool pool = (NSAutoreleasePool) new NSAutoreleasePool().alloc().init(); |
83 // From System-Declared Uniform Type Identifiers | 114 try { |
84 ".txt", | 115 NSMutableSet supportedDocumentTypes = (NSMutableSet)NSMutableSet.set(); |
85 ".rtf", | 116 NSWorkspace workspace = NSWorkspace.sharedWorkspace(); |
86 ".html", | 117 NSString CFBundleDocumentTypes = NSString.stringWith("CFBundleDocumentTypes"); |
87 ".htm", | 118 NSString CFBundleTypeExtensions = NSString.stringWith("CFBundleTypeExtensions"); |
88 ".xml", | 119 NSArray array = new NSArray(OS.NSSearchPathForDirectoriesInDomains(OS.NSAllApplicationsDirectory, OS.NSAllDomainsMask, true)); |
89 ".c", | 120 int count = (int)/*64*/array.count(); |
90 ".m", | 121 for (int i = 0; i < count; i++) { |
91 ".cp", ".cpp", ".c++", ".cc", ".cxx", | 122 NSString path = new NSString(array.objectAtIndex(i)); |
92 ".mm", | 123 NSFileManager fileManager = NSFileManager.defaultManager(); |
93 ".h", | 124 NSDirectoryEnumerator enumerator = fileManager.enumeratorAtPath(path); |
94 ".hpp", | 125 if (enumerator !is null) { |
95 ".h++", | 126 id id; |
96 ".hxx", | 127 while ((id = enumerator.nextObject()) !is null) { |
97 ".java", | 128 enumerator.skipDescendents(); |
98 ".jav", | 129 NSString filePath = new NSString(id.id); |
99 ".s", | 130 NSString fullPath = path.stringByAppendingPathComponent(filePath); |
100 ".r", | 131 if (workspace.isFilePackageAtPath(fullPath)) { |
101 ".defs", | 132 NSBundle bundle = NSBundle.bundleWithPath(fullPath); |
102 ".mig", | 133 id = bundle.infoDictionary().objectForKey(CFBundleDocumentTypes); |
103 ".exp", | 134 if (id !is null) { |
104 ".js", | 135 NSDictionary documentTypes = new NSDictionary(id.id); |
105 ".jscript", | 136 NSEnumerator documentTypesEnumerator = documentTypes.objectEnumerator(); |
106 ".javascript", | 137 while ((id = documentTypesEnumerator.nextObject()) !is null) { |
107 ".sh", | 138 NSDictionary documentType = new NSDictionary(id.id); |
108 ".command", | 139 id = documentType.objectForKey(CFBundleTypeExtensions); |
109 ".csh", | 140 if (id !is null) { |
110 ".pl", | 141 supportedDocumentTypes.addObjectsFromArray(new NSArray(id.id)); |
111 ".pm", | 142 } |
112 ".py", | 143 } |
113 ".rb", | 144 } |
114 ".rbw", | 145 } |
115 ".php", | 146 } |
116 ".php3", | 147 } |
117 ".php4", | 148 } |
118 ".ph3", | 149 int i = 0; |
119 ".ph4", | 150 String[] exts = new String[(int)/*64*/supportedDocumentTypes.count()]; |
120 ".phtml", | 151 NSEnumerator enumerator = supportedDocumentTypes.objectEnumerator(); |
121 ".jnlp", | 152 id id; |
122 ".applescript", | 153 while ((id = enumerator.nextObject()) !is null) { |
123 ".scpt", | 154 String ext = new NSString(id.id).getString(); |
124 ".o", | 155 if (!ext.equals("*")) exts[i++] = "." + ext; |
125 ".exe", | 156 } |
126 ".dll", | 157 if (i !is exts.length) { |
127 ".class", | 158 String[] temp = new String[i]; |
128 ".jar", | 159 System.arraycopy(exts, 0, temp, 0, i); |
129 ".qtz", | 160 exts = temp; |
130 ".gtar", | 161 } |
131 ".tar", | 162 return exts; |
132 ".gz", | 163 } finally { |
133 ".gzip", | 164 pool.release(); |
134 ".tgz", | 165 } |
135 ".hqx", | 166 } |
136 ".bin", | 167 |
137 ".vcf", | 168 static Program getProgram(NSBundle bundle) { |
138 ".vcard", | 169 NSString CFBundleName = NSString.stringWith("CFBundleName"); |
139 ".jpg", | 170 NSString CFBundleDisplayName = NSString.stringWith("CFBundleDisplayName"); |
140 ".jpeg", | 171 NSString fullPath = bundle.bundlePath(); |
141 ".jp2", | 172 NSString identifier = bundle.bundleIdentifier(); |
142 ".tif", | 173 id bundleName = bundle.objectForInfoDictionaryKey(CFBundleDisplayName); |
143 ".tiff", | 174 if (bundleName is null) { |
144 ".pic", | 175 bundleName = bundle.objectForInfoDictionaryKey(CFBundleName); |
145 ".pct", | 176 } |
146 ".pict", | 177 if (bundleName is null) { |
147 ".pntg", | 178 bundleName = fullPath.lastPathComponent().stringByDeletingPathExtension(); |
148 ".png", | 179 } |
149 ".xbm", | 180 NSString name = new NSString(bundleName.id); |
150 ".qif", | 181 Program program = new Program(); |
151 ".qtif", | 182 program.name = name.getString(); |
152 ".icns", | 183 program.fullPath = fullPath.getString(); |
153 ".mov", | 184 program.identifier = identifier !is null ? identifier.getString() : ""; |
154 ".qt", | 185 return program; |
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 } | 186 } |
276 | 187 |
277 /** | 188 /** |
278 * Answers all available programs in the operating system. Note | 189 * Answers all available programs in the operating system. Note |
279 * that a <code>Display</code> must already exist to guarantee | 190 * that a <code>Display</code> must already exist to guarantee |
280 * that this method returns an appropriate result. | 191 * that this method returns an appropriate result. |
281 * | 192 * |
282 * @return an array of programs | 193 * @return an array of programs |
283 */ | 194 */ |
284 public static Program [] getPrograms () { | 195 public static Program [] getPrograms () { |
285 return new Program[0]; | 196 NSAutoreleasePool pool = (NSAutoreleasePool) new NSAutoreleasePool().alloc().init(); |
286 // Hashtable bundles = new Hashtable(); | 197 try { |
287 // String[] extensions = getExtensions(); | 198 Vector vector = new Vector(); |
288 // byte[] fsRef = new byte[80]; | 199 NSWorkspace workspace = NSWorkspace.sharedWorkspace(); |
289 // for (int i = 0; i < extensions.length; i++) { | 200 NSArray array = new NSArray(OS.NSSearchPathForDirectoriesInDomains(OS.NSAllApplicationsDirectory, OS.NSAllDomainsMask, true)); |
290 // String extension = extensions[i]; | 201 int count = (int)/*64*/array.count(); |
291 // char[] chars = new char[extension.length() - 1]; | 202 for (int i = 0; i < count; i++) { |
292 // extension.getChars(1, extension.length(), chars, 0); | 203 NSString path = new NSString(array.objectAtIndex(i)); |
293 // int ext = OS.CFStringCreateWithCharacters(OS.kCFAllocatorDefault, chars, chars.length); | 204 NSFileManager fileManager = NSFileManager.defaultManager(); |
294 // if (ext !is 0) { | 205 NSDirectoryEnumerator enumerator = fileManager.enumeratorAtPath(path); |
295 // if (OS.LSGetApplicationForInfo(OS.kLSUnknownType, OS.kLSUnknownCreator, ext, OS.kLSRolesAll, fsRef, null) is OS.noErr) { | 206 if (enumerator !is null) { |
296 // Program program = getProgram(fsRef); | 207 id id; |
297 // if (program !is null && bundles.get(program.getName()) is null) { | 208 while ((id = enumerator.nextObject()) !is null) { |
298 // bundles.put(program.getName(), program); | 209 enumerator.skipDescendents(); |
299 // fsRef = new byte[80]; | 210 NSString fullPath = path.stringByAppendingPathComponent(new NSString(id.id)); |
300 // } | 211 if (workspace.isFilePackageAtPath(fullPath)) { |
301 // } | 212 NSBundle bundle = NSBundle.bundleWithPath(fullPath); |
302 // if (OS.VERSION >= 0x1040) { | 213 vector.addElement(getProgram(bundle)); |
303 // int utis = OS.UTTypeCreateAllIdentifiersForTag(OS.kUTTagClassFilenameExtension(), ext, 0); | 214 } |
304 // if (utis !is 0) { | 215 } |
305 // int utiCount = OS.CFArrayGetCount(utis); | 216 } |
306 // for (int j = 0; j < utiCount; j++) { | 217 } |
307 // int uti = OS.CFArrayGetValueAtIndex(utis, j); | 218 Program[] programs = new Program[vector.size()]; |
308 // if (uti !is 0) { | 219 vector.copyInto(programs); |
309 // int apps = OS.LSCopyAllRoleHandlersForContentType(uti, OS.kLSRolesAll); | 220 return programs; |
310 // if (apps !is 0) { | 221 } finally { |
311 // int appCount = OS.CFArrayGetCount(apps); | 222 pool.release(); |
312 // for (int k = 0; k < appCount; k++) { | 223 } |
313 // int app = OS.CFArrayGetValueAtIndex(apps, k); | 224 } |
314 // if (app !is 0) {; | 225 |
315 // if (OS.LSFindApplicationForInfo(OS.kLSUnknownCreator, app, 0, fsRef, null) is OS.noErr) { | 226 /** |
316 // Program program = getProgram(fsRef); | 227 * Launches the operating system executable associated with the file or |
317 // if (program !is null && bundles.get(program.getName()) is null) { | 228 * URL (http:// or https://). If the file is an executable then the |
318 // bundles.put(program.getName(), program); | 229 * executable is launched. Note that a <code>Display</code> must already |
319 // fsRef = new byte[80]; | 230 * exist to guarantee that this method returns an appropriate result. |
320 // } | 231 * |
321 // } | 232 * @param fileName the file or program name or URL (http:// or https://) |
322 // } | 233 * @return <code>true</code> if the file is launched, otherwise <code>false</code> |
323 // } | 234 * |
324 // OS.CFRelease(apps); | 235 * @exception IllegalArgumentException <ul> |
325 // } | 236 * <li>ERROR_NULL_ARGUMENT when fileName is null</li> |
326 // } | 237 * </ul> |
327 // } | 238 */ |
328 // OS.CFRelease(utis); | 239 public static bool launch (String fileName) { |
329 // } | 240 if (fileName is null) DWT.error (DWT.ERROR_NULL_ARGUMENT); |
330 // } | 241 NSAutoreleasePool pool = (NSAutoreleasePool) new NSAutoreleasePool().alloc().init(); |
331 // OS.CFRelease(ext); | 242 try { |
332 // } | 243 NSString unescapedStr = NSString.stringWith("%"); //$NON-NLS-1$ |
333 // } | 244 if (fileName.indexOf(':') is -1) { |
334 // int count = 0; | 245 fileName = PREFIX_FILE + fileName; |
335 // Program[] programs = new Program[bundles.size()]; | 246 } else { |
336 // Enumeration values = bundles.elements(); | 247 String lowercaseName = fileName.toLowerCase (); |
337 // while (values.hasMoreElements()) { | 248 if (lowercaseName.startsWith (PREFIX_HTTP) || lowercaseName.startsWith (PREFIX_HTTPS)) { |
338 // programs[count++] = cast(Program)values.nextElement(); | 249 unescapedStr = NSString.stringWith("%#"); //$NON-NLS-1$ |
339 // } | 250 } |
340 // return programs; | 251 } |
341 } | 252 NSString fullPath = NSString.stringWith(fileName); |
342 | 253 int /*long*/ ptr = OS.CFURLCreateStringByAddingPercentEscapes(0, fullPath.id, unescapedStr.id, 0, OS.kCFStringEncodingUTF8); |
343 /** | 254 NSString escapedString = new NSString(ptr); |
344 * Launches the executable associated with the file in | 255 NSWorkspace workspace = NSWorkspace.sharedWorkspace(); |
345 * the operating system. If the file is an executable, | 256 bool result = workspace.openURL(NSURL.URLWithString(escapedString)); |
346 * then the executable is launched. Note that a <code>Display</code> | 257 OS.CFRelease(ptr); |
347 * must already exist to guarantee that this method returns | 258 return result; |
348 * an appropriate result. | 259 } finally { |
260 pool.release(); | |
261 } | |
262 } | |
263 | |
264 /** | |
265 * Executes the program with the file as the single argument | |
266 * in the operating system. It is the responsibility of the | |
267 * programmer to ensure that the file contains valid data for | |
268 * this program. | |
349 * | 269 * |
350 * @param fileName the file or program name | 270 * @param fileName the file or program name |
351 * @return <code>true</code> if the file is launched, otherwise <code>false</code> | 271 * @return <code>true</code> if the file is launched, otherwise <code>false</code> |
352 * | 272 * |
353 * @exception IllegalArgumentException <ul> | 273 * @exception IllegalArgumentException <ul> |
354 * <li>ERROR_NULL_ARGUMENT when fileName is null</li> | 274 * <li>ERROR_NULL_ARGUMENT when fileName is null</li> |
355 * </ul> | 275 * </ul> |
356 */ | 276 */ |
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) { | 277 public bool execute (String fileName) { |
378 if (fileName is null) DWT.error(DWT.ERROR_NULL_ARGUMENT); | 278 if (fileName is null) DWT.error(DWT.ERROR_NULL_ARGUMENT); |
379 // if (OS.VERSION < 0x1040) return launch(fileName); | 279 NSAutoreleasePool pool = (NSAutoreleasePool) new NSAutoreleasePool().alloc().init(); |
380 // int rc = -1; | 280 try { |
381 // int fsRefPtr = OS.NewPtr(fsRef.length); | 281 NSWorkspace workspace = NSWorkspace.sharedWorkspace(); |
382 // if (fsRefPtr !is 0) { | 282 NSString fullPath = NSString.stringWith(fileName); |
383 // OS.memmove(fsRefPtr, fsRef, fsRef.length); | 283 if (fileName.indexOf(':') is -1) { |
384 // LSApplicationParameters params = new LSApplicationParameters(); | 284 return workspace.openFile(fullPath, NSString.stringWith(name)); |
385 // params.version = 0; | 285 } |
386 // params.flags = 0; | 286 NSString unescapedStr = NSString.stringWith("%"); //$NON-NLS-1$ |
387 // params.application = fsRefPtr; | 287 String lowercaseName = fileName.toLowerCase (); |
388 // if (fileName.length() is 0) { | 288 if (lowercaseName.startsWith (PREFIX_HTTP) || lowercaseName.startsWith (PREFIX_HTTPS)) { |
389 // rc = OS.LSOpenApplication(params, null); | 289 unescapedStr = NSString.stringWith("%#"); //$NON-NLS-1$ |
390 // } else { | 290 } |
391 // if (fileName.indexOf(':') is -1) fileName = "file://" + fileName; | 291 int /*long*/ ptr = OS.CFURLCreateStringByAddingPercentEscapes(0, fullPath.id, unescapedStr.id, 0, OS.kCFStringEncodingUTF8); |
392 // char[] chars = new char[fileName.length()]; | 292 NSString escapedString = new NSString(ptr); |
393 // fileName.getChars(0, chars.length, chars, 0); | 293 NSArray urls = NSArray.arrayWithObject(NSURL.URLWithString(escapedString)); |
394 // int str = OS.CFStringCreateWithCharacters(0, chars, chars.length); | 294 OS.CFRelease(ptr); |
395 // if (str !is 0) { | 295 return workspace.openURLs(urls, NSString.stringWith(identifier), 0, null, 0); |
396 // int unscapedStr = OS.CFStringCreateWithCharacters(0, new char[]{'%'}, 1); | 296 } finally { |
397 // int escapedStr = OS.CFURLCreateStringByAddingPercentEscapes(OS.kCFAllocatorDefault, str, unscapedStr, 0, OS.kCFStringEncodingUTF8); | 297 pool.release(); |
398 // if (escapedStr !is 0) { | 298 } |
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 } | 299 } |
419 | 300 |
420 /** | 301 /** |
421 * Returns the receiver's image data. This is the icon | 302 * Returns the receiver's image data. This is the icon |
422 * that is associated with the receiver in the operating | 303 * that is associated with the receiver in the operating |
423 * system. | 304 * system. |
424 * | 305 * |
425 * @return the image data for the program, may be null | 306 * @return the image data for the program, may be null |
426 */ | 307 */ |
427 public ImageData getImageData () { | 308 public ImageData getImageData () { |
428 // int[] iconRef = new int[1]; | 309 NSAutoreleasePool pool = (NSAutoreleasePool) new NSAutoreleasePool().alloc().init(); |
429 // OS.GetIconRefFromFileInfo(fsRef, 0, null, 0, 0, 0, iconRef, null); | 310 try { |
430 // int[] family = new int[1]; | 311 NSWorkspace workspace = NSWorkspace.sharedWorkspace(); |
431 // int rc = OS.IconRefToIconFamily(iconRef[0], OS.kSelectorAlLAvailableData, family); | 312 NSString fullPath; |
432 // OS.ReleaseIconRef(iconRef[0]); | 313 if (this.fullPath !is null) { |
433 // if (rc !is OS.noErr) return null; | 314 fullPath = NSString.stringWith(this.fullPath); |
434 //// ImageData result = createImageFromFamily(family[0], OS.kLarge32BitData, OS.kLarge8BitMask, 32, 32); | 315 } else { |
435 // ImageData result = createImageFromFamily(family[0], OS.kSmall32BitData, OS.kSmall8BitMask, 16, 16); | 316 fullPath = workspace.fullPathForApplication(NSString.stringWith(name)); |
436 // OS.DisposeHandle(family[0]); | 317 } |
437 // if (result is null) { | 318 if (fullPath !is null) { |
438 // RGB[] rgbs = new RGB[] { | 319 NSImage nsImage = workspace.iconForFile(fullPath); |
439 // new RGB(0xff, 0xff, 0xff), | 320 if (nsImage !is null) { |
440 // new RGB(0x5f, 0x5f, 0x5f), | 321 NSSize size = new NSSize(); |
441 // new RGB(0x80, 0x80, 0x80), | 322 size.width = size.height = 16; |
442 // new RGB(0xC0, 0xC0, 0xC0), | 323 nsImage.setSize(size); |
443 // new RGB(0xDF, 0xDF, 0xBF), | 324 NSBitmapImageRep imageRep = null; |
444 // new RGB(0xFF, 0xDF, 0x9F), | 325 NSImageRep rep = nsImage.bestRepresentationForDevice(null); |
445 // new RGB(0x00, 0x00, 0x00), | 326 if (rep.isKindOfClass(OS.class_NSBitmapImageRep)) { |
446 // }; | 327 imageRep = new NSBitmapImageRep(rep.id); |
447 // result = new ImageData(16, 16, 4, new PaletteData(rgbs) ); | 328 } |
448 // result.transparentPixel = 6; // use black for transparency | 329 if (imageRep !is null) { |
449 // String[] p= { | 330 int width = (int)/*64*/imageRep.pixelsWide(); |
450 // "CCCCCCCCGGG", | 331 int height = (int)/*64*/imageRep.pixelsHigh(); |
451 // "CFAAAAACBGG", | 332 int bpr = (int)/*64*/imageRep.bytesPerRow(); |
452 // "CAAAAAACFBG", | 333 int bpp = (int)/*64*/imageRep.bitsPerPixel(); |
453 // "CAAAAAACBBB", | 334 int dataSize = height * bpr; |
454 // "CAAAAAAAAEB", | 335 byte[] srcData = new byte[dataSize]; |
455 // "CAAAAAAAAEB", | 336 OS.memmove(srcData, imageRep.bitmapData(), dataSize); |
456 // "CAAAAAAAAEB", | 337 //TODO check color info |
457 // "CAAAAAAAAEB", | 338 PaletteData palette = new PaletteData(0xFF000000, 0xFF0000, 0xFF00); |
458 // "CAAAAAAAAEB", | 339 ImageData data = new ImageData(width, height, bpp, palette, 4, srcData); |
459 // "CAAAAAAAAEB", | 340 data.bytesPerLine = bpr; |
460 // "CAAAAAAAAEB", | 341 data.alphaData = new byte[width * height]; |
461 // "CAAAAAAAAEB", | 342 for (int i = 3, o = 0; i < srcData.length; i+= 4, o++) { |
462 // "CDDDDDDDDDB", | 343 data.alphaData[o] = srcData[i]; |
463 // "CBBBBBBBBBB", | 344 } |
464 // }; | 345 return data; |
465 // for (int y= 0; y < p.length; y++) { | 346 } |
466 // for (int x= 0; x < 11; x++) { | 347 } |
467 // result.setPixel(x+3, y+1, p[y].charAt(x)-'A'); | 348 } |
468 // } | 349 return null; |
469 // } | 350 } finally { |
470 // } | 351 pool.release(); |
471 // return result; | 352 } |
472 return null; | |
473 } | 353 } |
474 | 354 |
475 /** | 355 /** |
476 * Returns the receiver's name. This is as short and | 356 * Returns the receiver's name. This is as short and |
477 * descriptive a name as possible for the program. If | 357 * descriptive a name as possible for the program. If |
482 */ | 362 */ |
483 public String getName () { | 363 public String getName () { |
484 return name; | 364 return name; |
485 } | 365 } |
486 | 366 |
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 /** | 367 /** |
509 * Compares the argument to the receiver, and returns true | 368 * Compares the argument to the receiver, and returns true |
510 * if they represent the <em>same</em> object using a class | 369 * if they represent the <em>same</em> object using a class |
511 * specific comparison. | 370 * specific comparison. |
512 * | 371 * |
515 * | 374 * |
516 * @see #hashCode() | 375 * @see #hashCode() |
517 */ | 376 */ |
518 public bool equals(Object other) { | 377 public bool equals(Object other) { |
519 if (this is other) return true; | 378 if (this is other) return true; |
520 if ( null !is cast(Program)other ) { | 379 if (other instanceof Program) { |
521 final Program program = cast(Program) other; | 380 final Program program = (Program) other; |
522 return name.equals(program.name); | 381 return name.equals(program.name); |
523 } | 382 } |
524 return false; | 383 return false; |
525 } | 384 } |
526 | 385 |