comparison dwt/program/Program.d @ 212:ab60f3309436

reverted the char[] to String and use the an alias.
author Frank Benoit <benoit@tionex.de>
date Mon, 05 May 2008 00:12:38 +0200
parents 184ab53b7785
children 36f5cb12e1a2
comparison
equal deleted inserted replaced
211:ff59aeb96cac 212:ab60f3309436
24 * Instances of this class represent programs and 24 * Instances of this class represent programs and
25 * their associated file extensions in the operating 25 * their associated file extensions in the operating
26 * system. 26 * system.
27 */ 27 */
28 public final class Program { 28 public final class Program {
29 char[] name; 29 String name;
30 char[] command; 30 String command;
31 char[] iconName; 31 String iconName;
32 char[] extension; 32 String extension;
33 static const char[][] ARGUMENTS = ["%1"[], "%l", "%L"]; //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ 33 static const String[] ARGUMENTS = ["%1"[], "%l", "%L"]; //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
34 34
35 /** 35 /**
36 * Prevents uninitialized instances from being created outside the package. 36 * Prevents uninitialized instances from being created outside the package.
37 */ 37 */
38 this () { 38 this () {
39 } 39 }
40 40
41 static char[] assocQueryString (int assocStr, TCHAR[] key, bool expand) { 41 static String assocQueryString (int assocStr, TCHAR[] key, bool expand) {
42 TCHAR[] pszOut = NewTCHARs(0, 1024); 42 TCHAR[] pszOut = NewTCHARs(0, 1024);
43 uint[1] pcchOut; 43 uint[1] pcchOut;
44 pcchOut[0] = pszOut.length; 44 pcchOut[0] = pszOut.length;
45 int result = OS.AssocQueryString(OS.ASSOCF_NOTRUNCATE, assocStr, key.ptr, null, pszOut.ptr, pcchOut.ptr); 45 int result = OS.AssocQueryString(OS.ASSOCF_NOTRUNCATE, assocStr, key.ptr, null, pszOut.ptr, pcchOut.ptr);
46 if (result is OS.E_POINTER) { 46 if (result is OS.E_POINTER) {
75 * 75 *
76 * @exception IllegalArgumentException <ul> 76 * @exception IllegalArgumentException <ul>
77 * <li>ERROR_NULL_ARGUMENT when extension is null</li> 77 * <li>ERROR_NULL_ARGUMENT when extension is null</li>
78 * </ul> 78 * </ul>
79 */ 79 */
80 public static Program findProgram (char[] extension) { 80 public static Program findProgram (String extension) {
81 if (extension is null) DWT.error (DWT.ERROR_NULL_ARGUMENT); 81 if (extension is null) DWT.error (DWT.ERROR_NULL_ARGUMENT);
82 if (extension.length is 0) return null; 82 if (extension.length is 0) return null;
83 if (extension.charAt (0) !is '.') extension = "." ~ extension; //$NON-NLS-1$ 83 if (extension.charAt (0) !is '.') extension = "." ~ extension; //$NON-NLS-1$
84 /* Use the character encoding for the default locale */ 84 /* Use the character encoding for the default locale */
85 TCHAR[] key = StrToTCHARs (0, extension, true); 85 TCHAR[] key = StrToTCHARs (0, extension, true);
96 result = OS.RegQueryValueEx (phkResult [0], null, null, null, cast(ubyte*)lpData.ptr, lpcbData.ptr); 96 result = OS.RegQueryValueEx (phkResult [0], null, null, null, cast(ubyte*)lpData.ptr, lpcbData.ptr);
97 if (result is 0) program = getProgram ( TCHARzToStr( lpData.ptr ), extension); 97 if (result is 0) program = getProgram ( TCHARzToStr( lpData.ptr ), extension);
98 } 98 }
99 OS.RegCloseKey (phkResult [0]); 99 OS.RegCloseKey (phkResult [0]);
100 } else { 100 } else {
101 char[] command = assocQueryString (OS.ASSOCSTR_COMMAND, key, true); 101 String command = assocQueryString (OS.ASSOCSTR_COMMAND, key, true);
102 if (command !is null) { 102 if (command !is null) {
103 char[] name = null; 103 String name = null;
104 if (name is null) name = assocQueryString (OS.ASSOCSTR_FRIENDLYDOCNAME, key, false); 104 if (name is null) name = assocQueryString (OS.ASSOCSTR_FRIENDLYDOCNAME, key, false);
105 if (name is null) name = assocQueryString (OS.ASSOCSTR_FRIENDLYAPPNAME, key, false); 105 if (name is null) name = assocQueryString (OS.ASSOCSTR_FRIENDLYAPPNAME, key, false);
106 if (name is null) name = ""; 106 if (name is null) name = "";
107 char[] iconName = assocQueryString (OS.ASSOCSTR_DEFAULTICON, key, true); 107 String iconName = assocQueryString (OS.ASSOCSTR_DEFAULTICON, key, true);
108 if (iconName is null) iconName = ""; 108 if (iconName is null) iconName = "";
109 program = new Program (); 109 program = new Program ();
110 program.name = name; 110 program.name = name;
111 program.command = command; 111 program.command = command;
112 program.iconName = iconName; 112 program.iconName = iconName;
121 * that a <code>Display</code> must already exist to guarantee 121 * that a <code>Display</code> must already exist to guarantee
122 * that this method returns an appropriate result. 122 * that this method returns an appropriate result.
123 * 123 *
124 * @return an array of extensions 124 * @return an array of extensions
125 */ 125 */
126 public static char[] [] getExtensions () { 126 public static String [] getExtensions () {
127 char[] [] extensions = new char[] [1024]; 127 String [] extensions = new String [1024];
128 /* Use the character encoding for the default locale */ 128 /* Use the character encoding for the default locale */
129 TCHAR[] lpName = NewTCHARs (0, 1024); 129 TCHAR[] lpName = NewTCHARs (0, 1024);
130 uint [1] lpcName; lpcName[0] = lpName.length; 130 uint [1] lpcName; lpcName[0] = lpName.length;
131 FILETIME ft; 131 FILETIME ft;
132 int dwIndex = 0, count = 0; 132 int dwIndex = 0, count = 0;
133 while (OS.RegEnumKeyEx ( cast(void*)OS.HKEY_CLASSES_ROOT, dwIndex, lpName.ptr, lpcName.ptr, null, null, null, &ft) !is OS.ERROR_NO_MORE_ITEMS) { 133 while (OS.RegEnumKeyEx ( cast(void*)OS.HKEY_CLASSES_ROOT, dwIndex, lpName.ptr, lpcName.ptr, null, null, null, &ft) !is OS.ERROR_NO_MORE_ITEMS) {
134 char[] extension = TCHARsToStr( lpName[0 .. lpcName[0] ]); 134 String extension = TCHARsToStr( lpName[0 .. lpcName[0] ]);
135 lpcName [0] = lpName.length; 135 lpcName [0] = lpName.length;
136 if (extension.length > 0 && extension.charAt (0) is '.') { 136 if (extension.length > 0 && extension.charAt (0) is '.') {
137 if (count is extensions.length) { 137 if (count is extensions.length) {
138 char[][] newExtensions = new char[][]( extensions.length + 1024 ); 138 String[] newExtensions = new String[]( extensions.length + 1024 );
139 System.arraycopy (extensions, 0, newExtensions, 0, extensions.length); 139 System.arraycopy (extensions, 0, newExtensions, 0, extensions.length);
140 extensions = newExtensions; 140 extensions = newExtensions;
141 } 141 }
142 extensions [count++] = extension; 142 extensions [count++] = extension;
143 } 143 }
144 dwIndex++; 144 dwIndex++;
145 } 145 }
146 if (count !is extensions.length) { 146 if (count !is extensions.length) {
147 char[][] newExtension = new char[][]( count ); 147 String[] newExtension = new String[]( count );
148 System.arraycopy (extensions, 0, newExtension, 0, count); 148 System.arraycopy (extensions, 0, newExtension, 0, count);
149 extensions = newExtension; 149 extensions = newExtension;
150 } 150 }
151 return extensions; 151 return extensions;
152 } 152 }
153 153
154 static char[] getKeyValue (char[] string, bool expand) { 154 static String getKeyValue (String string, bool expand) {
155 /* Use the character encoding for the default locale */ 155 /* Use the character encoding for the default locale */
156 TCHAR[] key = StrToTCHARs (0, string, true); 156 TCHAR[] key = StrToTCHARs (0, string, true);
157 void* [1] phkResult; 157 void* [1] phkResult;
158 if (OS.RegOpenKeyEx (cast(void*)OS.HKEY_CLASSES_ROOT, key.ptr, 0, OS.KEY_READ, phkResult.ptr) !is 0) { 158 if (OS.RegOpenKeyEx (cast(void*)OS.HKEY_CLASSES_ROOT, key.ptr, 0, OS.KEY_READ, phkResult.ptr) !is 0) {
159 return null; 159 return null;
160 } 160 }
161 char[] result = null; 161 String result = null;
162 uint [1] lpcbData; 162 uint [1] lpcbData;
163 if (OS.RegQueryValueEx (phkResult [0], null, null, null, null, lpcbData.ptr) is 0) { 163 if (OS.RegQueryValueEx (phkResult [0], null, null, null, null, lpcbData.ptr) is 0) {
164 result = ""; 164 result = "";
165 int length_ = lpcbData [0] / TCHAR.sizeof; 165 int length_ = lpcbData [0] / TCHAR.sizeof;
166 if (length_ !is 0) { 166 if (length_ !is 0) {
183 } 183 }
184 if (phkResult [0] !is null) OS.RegCloseKey (phkResult [0]); 184 if (phkResult [0] !is null) OS.RegCloseKey (phkResult [0]);
185 return result; 185 return result;
186 } 186 }
187 187
188 static Program getProgram (char[] key, char[] extension) { 188 static Program getProgram (String key, String extension) {
189 189
190 /* Name */ 190 /* Name */
191 char[] name = getKeyValue (key, false); 191 String name = getKeyValue (key, false);
192 if (name is null || name.length is 0) { 192 if (name is null || name.length is 0) {
193 name = key; 193 name = key;
194 } 194 }
195 195
196 /* Command */ 196 /* Command */
197 char[] DEFAULT_COMMAND = "\\shell"; //$NON-NLS-1$ 197 String DEFAULT_COMMAND = "\\shell"; //$NON-NLS-1$
198 char[] defaultCommand = getKeyValue (key ~ DEFAULT_COMMAND, true); 198 String defaultCommand = getKeyValue (key ~ DEFAULT_COMMAND, true);
199 if (defaultCommand is null || defaultCommand.length is 0) defaultCommand = "open"; //$NON-NLS-1$ 199 if (defaultCommand is null || defaultCommand.length is 0) defaultCommand = "open"; //$NON-NLS-1$
200 char[] COMMAND = "\\shell\\" ~ defaultCommand ~ "\\command"; //$NON-NLS-1$ 200 String COMMAND = "\\shell\\" ~ defaultCommand ~ "\\command"; //$NON-NLS-1$
201 char[] command = getKeyValue (key ~ COMMAND, true); 201 String command = getKeyValue (key ~ COMMAND, true);
202 if (command is null || command.length is 0) return null; 202 if (command is null || command.length is 0) return null;
203 203
204 /* Icon */ 204 /* Icon */
205 char[] DEFAULT_ICON = "\\DefaultIcon"; //$NON-NLS-1$ 205 String DEFAULT_ICON = "\\DefaultIcon"; //$NON-NLS-1$
206 char[] iconName = getKeyValue (key ~ DEFAULT_ICON, true); 206 String iconName = getKeyValue (key ~ DEFAULT_ICON, true);
207 if (iconName is null) iconName = ""; //$NON-NLS-1$ 207 if (iconName is null) iconName = ""; //$NON-NLS-1$
208 208
209 /* Program */ 209 /* Program */
210 Program program = new Program (); 210 Program program = new Program ();
211 program.name = name; 211 program.name = name;
228 TCHAR[] lpName = NewTCHARs (0, 1024); 228 TCHAR[] lpName = NewTCHARs (0, 1024);
229 uint [1] lpcName; lpcName[0] = lpName.length; 229 uint [1] lpcName; lpcName[0] = lpName.length;
230 FILETIME ft; 230 FILETIME ft;
231 int dwIndex = 0, count = 0; 231 int dwIndex = 0, count = 0;
232 while (OS.RegEnumKeyEx (cast(void*)OS.HKEY_CLASSES_ROOT, dwIndex, lpName.ptr, lpcName.ptr, null, null, null, &ft) !is OS.ERROR_NO_MORE_ITEMS) { 232 while (OS.RegEnumKeyEx (cast(void*)OS.HKEY_CLASSES_ROOT, dwIndex, lpName.ptr, lpcName.ptr, null, null, null, &ft) !is OS.ERROR_NO_MORE_ITEMS) {
233 char[] path = tango.text.convert.Utf.toString ( lpName[0 .. lpcName [0]]); 233 String path = tango.text.convert.Utf.toString ( lpName[0 .. lpcName [0]]);
234 lpcName [0] = lpName.length ; 234 lpcName [0] = lpName.length ;
235 Program program = getProgram (path, null); 235 Program program = getProgram (path, null);
236 if (program !is null) { 236 if (program !is null) {
237 if (count is programs.length) { 237 if (count is programs.length) {
238 Program [] newPrograms = new Program [programs.length + 1024]; 238 Program [] newPrograms = new Program [programs.length + 1024];
263 * 263 *
264 * @exception IllegalArgumentException <ul> 264 * @exception IllegalArgumentException <ul>
265 * <li>ERROR_NULL_ARGUMENT when fileName is null</li> 265 * <li>ERROR_NULL_ARGUMENT when fileName is null</li>
266 * </ul> 266 * </ul>
267 */ 267 */
268 public static bool launch (char[] fileName) { 268 public static bool launch (String fileName) {
269 if (fileName is null) DWT.error (DWT.ERROR_NULL_ARGUMENT); 269 if (fileName is null) DWT.error (DWT.ERROR_NULL_ARGUMENT);
270 270
271 /* Use the character encoding for the default locale */ 271 /* Use the character encoding for the default locale */
272 auto hHeap = OS.GetProcessHeap (); 272 auto hHeap = OS.GetProcessHeap ();
273 TCHAR[] buffer = StrToTCHARs (0, fileName, true); 273 TCHAR[] buffer = StrToTCHARs (0, fileName, true);
294 * 294 *
295 * @exception IllegalArgumentException <ul> 295 * @exception IllegalArgumentException <ul>
296 * <li>ERROR_NULL_ARGUMENT when fileName is null</li> 296 * <li>ERROR_NULL_ARGUMENT when fileName is null</li>
297 * </ul> 297 * </ul>
298 */ 298 */
299 public bool execute (char[] fileName) { 299 public bool execute (String fileName) {
300 if (fileName is null) DWT.error (DWT.ERROR_NULL_ARGUMENT); 300 if (fileName is null) DWT.error (DWT.ERROR_NULL_ARGUMENT);
301 int index = 0; 301 int index = 0;
302 bool append = true; 302 bool append = true;
303 char[] prefix = command, suffix = ""; //$NON-NLS-1$ 303 String prefix = command, suffix = ""; //$NON-NLS-1$
304 while (index < ARGUMENTS.length) { 304 while (index < ARGUMENTS.length) {
305 int i = command.indexOf (ARGUMENTS [index]); 305 int i = command.indexOf (ARGUMENTS [index]);
306 if (i !is -1) { 306 if (i !is -1) {
307 append = false; 307 append = false;
308 prefix = command.substring (0, i); 308 prefix = command.substring (0, i);
310 break; 310 break;
311 } 311 }
312 index++; 312 index++;
313 } 313 }
314 if (append) fileName = " \"" ~ fileName ~ "\""; 314 if (append) fileName = " \"" ~ fileName ~ "\"";
315 char[] commandLine = prefix ~ fileName ~ suffix; 315 String commandLine = prefix ~ fileName ~ suffix;
316 auto hHeap = OS.GetProcessHeap (); 316 auto hHeap = OS.GetProcessHeap ();
317 /* Use the character encoding for the default locale */ 317 /* Use the character encoding for the default locale */
318 TCHAR[] buffer = StrToTCHARs (0, commandLine, true); 318 TCHAR[] buffer = StrToTCHARs (0, commandLine, true);
319 int byteCount = buffer.length * TCHAR.sizeof; 319 int byteCount = buffer.length * TCHAR.sizeof;
320 auto lpCommandLine = cast(TCHAR*)OS.HeapAlloc (hHeap, OS.HEAP_ZERO_MEMORY, byteCount); 320 auto lpCommandLine = cast(TCHAR*)OS.HeapAlloc (hHeap, OS.HEAP_ZERO_MEMORY, byteCount);
348 image.dispose (); 348 image.dispose ();
349 return imageData; 349 return imageData;
350 } 350 }
351 } 351 }
352 int nIconIndex = 0; 352 int nIconIndex = 0;
353 char[] fileName = iconName; 353 String fileName = iconName;
354 int index = iconName.indexOf (','); 354 int index = iconName.indexOf (',');
355 if (index !is -1) { 355 if (index !is -1) {
356 fileName = iconName.substring (0, index); 356 fileName = iconName.substring (0, index);
357 char[] iconIndex = iconName.substring (index + 1, iconName.length ).trim (); 357 String iconIndex = iconName.substring (index + 1, iconName.length ).trim ();
358 try { 358 try {
359 nIconIndex = Integer.parseInt (iconIndex); 359 nIconIndex = Integer.parseInt (iconIndex);
360 } catch (NumberFormatException e) {} 360 } catch (NumberFormatException e) {}
361 } 361 }
362 /* Use the character encoding for the default locale */ 362 /* Use the character encoding for the default locale */
376 * the program has no descriptive name, this string may 376 * the program has no descriptive name, this string may
377 * be the executable name, path or empty. 377 * be the executable name, path or empty.
378 * 378 *
379 * @return the name of the program 379 * @return the name of the program
380 */ 380 */
381 public char[] getName () { 381 public String getName () {
382 return name; 382 return name;
383 } 383 }
384 384
385 /** 385 /**
386 * Compares the argument to the receiver, and returns true 386 * Compares the argument to the receiver, and returns true
419 * Returns a string containing a concise, human-readable 419 * Returns a string containing a concise, human-readable
420 * description of the receiver. 420 * description of the receiver.
421 * 421 *
422 * @return a string representation of the program 422 * @return a string representation of the program
423 */ 423 */
424 public char[] toString () { 424 public String toString () {
425 return "Program {" ~ name ~ "}"; //$NON-NLS-1$ //$NON-NLS-2$ 425 return "Program {" ~ name ~ "}"; //$NON-NLS-1$ //$NON-NLS-2$
426 } 426 }
427 427
428 } 428 }