1 11 package org.eclipse.swt.program; 12 13 import org.eclipse.swt.internal.win32.*; 14 import org.eclipse.swt.*; 15 import org.eclipse.swt.graphics.*; 16 17 22 public final class Program { 23 String name; 24 String command; 25 String iconName; 26 String extension; 27 static final String [] ARGUMENTS = new String [] {"%1", "%l", "%L"}; 29 32 Program () { 33 } 34 35 static String assocQueryString (int assocStr, TCHAR key, boolean expand) { 36 TCHAR pszOut = new TCHAR(0, 1024); 37 int[] pcchOut = new int[1]; 38 pcchOut[0] = pszOut.length(); 39 int result = OS.AssocQueryString(OS.ASSOCF_NOTRUNCATE, assocStr, key, null, pszOut, pcchOut); 40 if (result == OS.E_POINTER) { 41 pszOut = new TCHAR(0, pcchOut [0]); 42 result = OS.AssocQueryString(OS.ASSOCF_NOTRUNCATE, assocStr, key, null, pszOut, pcchOut); 43 } 44 if (result == 0) { 45 if (!OS.IsWinCE && expand) { 46 int length = OS.ExpandEnvironmentStrings (pszOut, null, 0); 47 if (length != 0) { 48 TCHAR lpDst = new TCHAR (0, length); 49 OS.ExpandEnvironmentStrings (pszOut, lpDst, length); 50 return lpDst.toString (0, Math.max (0, length - 1)); 51 } else { 52 return ""; 53 } 54 } else { 55 return pszOut.toString (0, Math.max (0, pcchOut [0] - 1)); 56 } 57 } 58 return null; 59 } 60 61 74 public static Program findProgram (String extension) { 75 if (extension == null) SWT.error (SWT.ERROR_NULL_ARGUMENT); 76 if (extension.length () == 0) return null; 77 if (extension.charAt (0) != '.') extension = "." + extension; 79 TCHAR key = new TCHAR (0, extension, true); 80 Program program = null; 81 if (OS.IsWinCE) { 82 int [] phkResult = new int [1]; 83 if (OS.RegOpenKeyEx (OS.HKEY_CLASSES_ROOT, key, 0, OS.KEY_READ, phkResult) != 0) { 84 return null; 85 } 86 int [] lpcbData = new int [1]; 87 int result = OS.RegQueryValueEx (phkResult [0], null, 0, null, (TCHAR) null, lpcbData); 88 if (result == 0) { 89 TCHAR lpData = new TCHAR (0, lpcbData [0] / TCHAR.sizeof); 90 result = OS.RegQueryValueEx (phkResult [0], null, 0, null, lpData, lpcbData); 91 if (result == 0) program = getProgram (lpData.toString (0, lpData.strlen ()), extension); 92 } 93 OS.RegCloseKey (phkResult [0]); 94 } else { 95 String command = assocQueryString (OS.ASSOCSTR_COMMAND, key, true); 96 if (command != null) { 97 String name = null; 98 if (name == null) name = assocQueryString (OS.ASSOCSTR_FRIENDLYDOCNAME, key, false); 99 if (name == null) name = assocQueryString (OS.ASSOCSTR_FRIENDLYAPPNAME, key, false); 100 if (name == null) name = ""; 101 String iconName = assocQueryString (OS.ASSOCSTR_DEFAULTICON, key, true); 102 if (iconName == null) iconName = ""; 103 program = new Program (); 104 program.name = name; 105 program.command = command; 106 program.iconName = iconName; 107 program.extension = extension; 108 } 109 } 110 return program; 111 } 112 113 120 public static String [] getExtensions () { 121 String [] extensions = new String [1024]; 122 123 TCHAR lpName = new TCHAR (0, 1024); 124 int [] lpcName = new int [] {lpName.length ()}; 125 FILETIME ft = new FILETIME (); 126 int dwIndex = 0, count = 0; 127 while (OS.RegEnumKeyEx (OS.HKEY_CLASSES_ROOT, dwIndex, lpName, lpcName, null, null, null, ft) != OS.ERROR_NO_MORE_ITEMS) { 128 String extension = lpName.toString (0, lpcName [0]); 129 lpcName [0] = lpName.length (); 130 if (extension.length () > 0 && extension.charAt (0) == '.') { 131 if (count == extensions.length) { 132 String [] newExtensions = new String [extensions.length + 1024]; 133 System.arraycopy (extensions, 0, newExtensions, 0, extensions.length); 134 extensions = newExtensions; 135 } 136 extensions [count++] = extension; 137 } 138 dwIndex++; 139 } 140 if (count != extensions.length) { 141 String [] newExtension = new String [count]; 142 System.arraycopy (extensions, 0, newExtension, 0, count); 143 extensions = newExtension; 144 } 145 return extensions; 146 } 147 148 static String getKeyValue (String string, boolean expand) { 149 150 TCHAR key = new TCHAR (0, string, true); 151 int [] phkResult = new int [1]; 152 if (OS.RegOpenKeyEx (OS.HKEY_CLASSES_ROOT, key, 0, OS.KEY_READ, phkResult) != 0) { 153 return null; 154 } 155 String result = null; 156 int [] lpcbData = new int [1]; 157 if (OS.RegQueryValueEx (phkResult [0], (TCHAR) null, 0, null, (TCHAR) null, lpcbData) == 0) { 158 result = ""; 159 int length = lpcbData [0] / TCHAR.sizeof; 160 if (length != 0) { 161 162 TCHAR lpData = new TCHAR (0, length); 163 if (OS.RegQueryValueEx (phkResult [0], null, 0, null, lpData, lpcbData) == 0) { 164 if (!OS.IsWinCE && expand) { 165 length = OS.ExpandEnvironmentStrings (lpData, null, 0); 166 if (length != 0) { 167 TCHAR lpDst = new TCHAR (0, length); 168 OS.ExpandEnvironmentStrings (lpData, lpDst, length); 169 result = lpDst.toString (0, Math.max (0, length - 1)); 170 } 171 } else { 172 length = Math.max (0, lpData.length () - 1); 173 result = lpData.toString (0, length); 174 } 175 } 176 } 177 } 178 if (phkResult [0] != 0) OS.RegCloseKey (phkResult [0]); 179 return result; 180 } 181 182 static Program getProgram (String key, String extension) { 183 184 185 String name = getKeyValue (key, false); 186 if (name == null || name.length () == 0) { 187 name = key; 188 } 189 190 191 String DEFAULT_COMMAND = "\\shell"; String defaultCommand = getKeyValue (key + DEFAULT_COMMAND, true); 193 if (defaultCommand == null || defaultCommand.length() == 0) defaultCommand = "open"; String COMMAND = "\\shell\\" + defaultCommand + "\\command"; String command = getKeyValue (key + COMMAND, true); 196 if (command == null || command.length () == 0) return null; 197 198 199 String DEFAULT_ICON = "\\DefaultIcon"; String iconName = getKeyValue (key + DEFAULT_ICON, true); 201 if (iconName == null) iconName = ""; 203 204 Program program = new Program (); 205 program.name = name; 206 program.command = command; 207 program.iconName = iconName; 208 program.extension = extension; 209 return program; 210 } 211 212 219 public static Program [] getPrograms () { 220 Program [] programs = new Program [1024]; 221 222 TCHAR lpName = new TCHAR (0, 1024); 223 int [] lpcName = new int [] {lpName.length ()}; 224 FILETIME ft = new FILETIME (); 225 int dwIndex = 0, count = 0; 226 while (OS.RegEnumKeyEx (OS.HKEY_CLASSES_ROOT, dwIndex, lpName, lpcName, null, null, null, ft) != OS.ERROR_NO_MORE_ITEMS) { 227 String path = lpName.toString (0, lpcName [0]); 228 lpcName [0] = lpName.length (); 229 Program program = getProgram (path, null); 230 if (program != null) { 231 if (count == programs.length) { 232 Program [] newPrograms = new Program [programs.length + 1024]; 233 System.arraycopy (programs, 0, newPrograms, 0, programs.length); 234 programs = newPrograms; 235 } 236 programs [count++] = program; 237 } 238 dwIndex++; 239 } 240 if (count != programs.length) { 241 Program [] newPrograms = new Program [count]; 242 System.arraycopy (programs, 0, newPrograms, 0, count); 243 programs = newPrograms; 244 } 245 return programs; 246 } 247 248 262 public static boolean launch (String fileName) { 263 if (fileName == null) SWT.error (SWT.ERROR_NULL_ARGUMENT); 264 265 266 int hHeap = OS.GetProcessHeap (); 267 TCHAR buffer = new TCHAR (0, fileName, true); 268 int byteCount = buffer.length () * TCHAR.sizeof; 269 int lpFile = OS.HeapAlloc (hHeap, OS.HEAP_ZERO_MEMORY, byteCount); 270 OS.MoveMemory (lpFile, buffer, byteCount); 271 SHELLEXECUTEINFO info = new SHELLEXECUTEINFO (); 272 info.cbSize = SHELLEXECUTEINFO.sizeof; 273 info.lpFile = lpFile; 274 info.nShow = OS.SW_SHOW; 275 boolean result = OS.ShellExecuteEx (info); 276 if (lpFile != 0) OS.HeapFree (hHeap, 0, lpFile); 277 return result; 278 } 279 280 293 public boolean execute (String fileName) { 294 if (fileName == null) SWT.error (SWT.ERROR_NULL_ARGUMENT); 295 int index = 0; 296 boolean append = true; 297 String prefix = command, suffix = ""; while (index < ARGUMENTS.length) { 299 int i = command.indexOf (ARGUMENTS [index]); 300 if (i != -1) { 301 append = false; 302 prefix = command.substring (0, i); 303 suffix = command.substring (i + ARGUMENTS [index].length (), command.length ()); 304 break; 305 } 306 index++; 307 } 308 if (append) fileName = " \"" + fileName + "\""; 309 String commandLine = prefix + fileName + suffix; 310 int hHeap = OS.GetProcessHeap (); 311 312 TCHAR buffer = new TCHAR (0, commandLine, true); 313 int byteCount = buffer.length () * TCHAR.sizeof; 314 int lpCommandLine = OS.HeapAlloc (hHeap, OS.HEAP_ZERO_MEMORY, byteCount); 315 OS.MoveMemory (lpCommandLine, buffer, byteCount); 316 STARTUPINFO lpStartupInfo = new STARTUPINFO (); 317 lpStartupInfo.cb = STARTUPINFO.sizeof; 318 PROCESS_INFORMATION lpProcessInformation = new PROCESS_INFORMATION (); 319 boolean success = OS.CreateProcess (0, lpCommandLine, 0, 0, false, 0, 0, 0, lpStartupInfo, lpProcessInformation); 320 if (lpCommandLine != 0) OS.HeapFree (hHeap, 0, lpCommandLine); 321 if (lpProcessInformation.hProcess != 0) OS.CloseHandle (lpProcessInformation.hProcess); 322 if (lpProcessInformation.hThread != 0) OS.CloseHandle (lpProcessInformation.hThread); 323 return success; 324 } 325 326 333 public ImageData getImageData () { 334 if (extension != null) { 335 SHFILEINFO shfi = OS.IsUnicode ? (SHFILEINFO) new SHFILEINFOW () : new SHFILEINFOA (); 336 int flags = OS.SHGFI_ICON | OS.SHGFI_SMALLICON | OS.SHGFI_USEFILEATTRIBUTES; 337 TCHAR pszPath = new TCHAR (0, extension, true); 338 OS.SHGetFileInfo (pszPath, OS.FILE_ATTRIBUTE_NORMAL, shfi, SHFILEINFO.sizeof, flags); 339 if (shfi.hIcon != 0) { 340 Image image = Image.win32_new (null, SWT.ICON, shfi.hIcon); 341 ImageData imageData = image.getImageData (); 342 image.dispose (); 343 return imageData; 344 } 345 } 346 int nIconIndex = 0; 347 String fileName = iconName; 348 int index = iconName.indexOf (','); 349 if (index != -1) { 350 fileName = iconName.substring (0, index); 351 String iconIndex = iconName.substring (index + 1, iconName.length ()).trim (); 352 try { 353 nIconIndex = Integer.parseInt (iconIndex); 354 } catch (NumberFormatException e) {} 355 } 356 357 TCHAR lpszFile = new TCHAR (0, fileName, true); 358 int [] phiconSmall = new int[1], phiconLarge = null; 359 OS.ExtractIconEx (lpszFile, nIconIndex, phiconLarge, phiconSmall, 1); 360 if (phiconSmall [0] == 0) return null; 361 Image image = Image.win32_new (null, SWT.ICON, phiconSmall [0]); 362 ImageData imageData = image.getImageData (); 363 image.dispose (); 364 return imageData; 365 } 366 367 375 public String getName () { 376 return name; 377 } 378 379 389 public boolean equals(Object other) { 390 if (this == other) return true; 391 if (other instanceof Program) { 392 final Program program = (Program) other; 393 return name.equals(program.name) && command.equals(program.command) 394 && iconName.equals(program.iconName); 395 } 396 return false; 397 } 398 399 409 public int hashCode() { 410 return name.hashCode() ^ command.hashCode() ^ iconName.hashCode(); 411 } 412 413 419 public String toString () { 420 return "Program {" + name + "}"; } 422 423 } 424 | Popular Tags |