Mercurial > projects > dwt2
annotate org.eclipse.swt.gtk.linux.x86/src/org/eclipse/swt/program/Program.d @ 86:12b890a6392a
Work on databinding
author | Frank Benoit <benoit@tionex.de> |
---|---|
date | Sat, 18 Apr 2009 13:58:35 +0200 |
parents | ea8b884d85f6 |
children | b397a43d66d1 |
rev | line source |
---|---|
25 | 1 /******************************************************************************* |
2 * Copyright (c) 2000, 2008 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 * Port to the D programming language: | |
11 * Frank Benoit <benoit@tionex.de> | |
12 *******************************************************************************/ | |
13 module org.eclipse.swt.program.Program; | |
14 | |
15 import org.eclipse.swt.SWT; | |
16 import org.eclipse.swt.graphics.ImageData; | |
17 import org.eclipse.swt.internal.Compatibility; | |
18 import org.eclipse.swt.internal.Converter; | |
19 import org.eclipse.swt.internal.Library; | |
20 import org.eclipse.swt.internal.gtk.OS; | |
21 import org.eclipse.swt.widgets.Display; | |
22 import org.eclipse.swt.widgets.Event; | |
23 import org.eclipse.swt.widgets.Listener; | |
24 import java.lang.all; | |
50 | 25 import java.nonstandard.SharedLib; |
25 | 26 |
48 | 27 version(Tango){ |
50 | 28 static import tango.core.Array; |
29 static import tango.io.device.File; | |
30 static import tango.io.stream.Lines; | |
48 | 31 } else { // Phobos |
50 | 32 static import std.string; |
33 static import std.file; | |
34 static import std.algorithm; | |
35 static import std.iterator; | |
48 | 36 } |
25 | 37 |
38 version( build ){ | |
39 pragma(link, "gnomeui-2" ); | |
40 } | |
41 | |
42 private extern(C) { | |
43 alias int GnomeIconLookupResultFlags; | |
44 alias int GnomeIconLookupFlags; | |
45 GnomeIconTheme *gnome_icon_theme_new (); | |
46 char *gnome_icon_lookup ( | |
47 GtkIconTheme *icon_theme, | |
48 void *thumbnail_factory, | |
49 char *file_uri, | |
50 char *custom_icon, | |
51 void *file_info, | |
52 char *mime_type, | |
53 GnomeIconLookupFlags flags, | |
54 GnomeIconLookupResultFlags *result); | |
55 int gnome_vfs_init(); | |
56 char* gnome_icon_theme_lookup_icon(GnomeIconTheme *theme,char *icon_name,int size, GnomeIconData **icon_data, int *base_size) ; | |
57 | |
58 alias void GnomeIconTheme; | |
59 alias void GnomeIconData; | |
60 | |
61 struct GnomeVFSMimeApplication{ | |
62 /*< public > */ | |
63 char *id; | |
64 char *name; | |
65 | |
66 /*< private > */ | |
67 char *command; | |
68 int can_open_multiple_files; | |
69 int expects_uris; | |
70 GList *supported_uri_schemes; | |
71 int requires_terminal; | |
72 | |
73 /* Padded to avoid future breaks in ABI compatibility */ | |
74 void * reserved1; | |
75 | |
76 void * priv; | |
77 } | |
78 } | |
79 | |
80 struct GNOME { | |
81 private static extern(C){ | |
82 enum { | |
83 GNOME_ICON_LOOKUP_FLAGS_NONE = 0, | |
84 GNOME_ICON_LOOKUP_FLAGS_EMBEDDING_TEXT = 1<<0, | |
85 GNOME_ICON_LOOKUP_FLAGS_SHOW_SMALL_IMAGES_AS_THEMSELVES = 1<<1, | |
86 GNOME_ICON_LOOKUP_FLAGS_ALLOW_SVG_AS_THEMSELVES = 1<<2 | |
87 } | |
88 enum { | |
89 GNOME_VFS_MAKE_URI_DIR_NONE = 0, | |
90 GNOME_VFS_MAKE_URI_DIR_HOMEDIR = 1 << 0, | |
91 GNOME_VFS_MAKE_URI_DIR_CURRENT = 1 << 1 | |
92 } | |
93 alias int GnomeVFSMakeURIDirs; | |
94 enum { | |
95 GNOME_VFS_OK, | |
96 GNOME_VFS_ERROR_NOT_FOUND, | |
97 GNOME_VFS_ERROR_GENERIC, | |
98 GNOME_VFS_ERROR_INTERNAL, | |
99 GNOME_VFS_ERROR_BAD_PARAMETERS, | |
100 GNOME_VFS_ERROR_NOT_SUPPORTED, | |
101 GNOME_VFS_ERROR_IO, | |
102 GNOME_VFS_ERROR_CORRUPTED_DATA, | |
103 GNOME_VFS_ERROR_WRONG_FORMAT, | |
104 GNOME_VFS_ERROR_BAD_FILE, | |
105 GNOME_VFS_ERROR_TOO_BIG, | |
106 GNOME_VFS_ERROR_NO_SPACE, | |
107 GNOME_VFS_ERROR_READ_ONLY, | |
108 GNOME_VFS_ERROR_INVALID_URI, | |
109 GNOME_VFS_ERROR_NOT_OPEN, | |
110 GNOME_VFS_ERROR_INVALID_OPEN_MODE, | |
111 GNOME_VFS_ERROR_ACCESS_DENIED, | |
112 GNOME_VFS_ERROR_TOO_MANY_OPEN_FILES, | |
113 GNOME_VFS_ERROR_EOF, | |
114 GNOME_VFS_ERROR_NOT_A_DIRECTORY, | |
115 GNOME_VFS_ERROR_IN_PROGRESS, | |
116 GNOME_VFS_ERROR_INTERRUPTED, | |
117 GNOME_VFS_ERROR_FILE_EXISTS, | |
118 GNOME_VFS_ERROR_LOOP, | |
119 GNOME_VFS_ERROR_NOT_PERMITTED, | |
120 GNOME_VFS_ERROR_IS_DIRECTORY, | |
121 GNOME_VFS_ERROR_NO_MEMORY, | |
122 GNOME_VFS_ERROR_HOST_NOT_FOUND, | |
123 GNOME_VFS_ERROR_INVALID_HOST_NAME, | |
124 GNOME_VFS_ERROR_HOST_HAS_NO_ADDRESS, | |
125 GNOME_VFS_ERROR_LOGIN_FAILED, | |
126 GNOME_VFS_ERROR_CANCELLED, | |
127 GNOME_VFS_ERROR_DIRECTORY_BUSY, | |
128 GNOME_VFS_ERROR_DIRECTORY_NOT_EMPTY, | |
129 GNOME_VFS_ERROR_TOO_MANY_LINKS, | |
130 GNOME_VFS_ERROR_READ_ONLY_FILE_SYSTEM, | |
131 GNOME_VFS_ERROR_NOT_SAME_FILE_SYSTEM, | |
132 GNOME_VFS_ERROR_NAME_TOO_LONG, | |
133 GNOME_VFS_ERROR_SERVICE_NOT_AVAILABLE, | |
134 GNOME_VFS_ERROR_SERVICE_OBSOLETE, | |
135 GNOME_VFS_ERROR_PROTOCOL_ERROR, | |
136 GNOME_VFS_ERROR_NO_MASTER_BROWSER, | |
137 GNOME_VFS_ERROR_NO_DEFAULT, | |
138 GNOME_VFS_ERROR_NO_HANDLER, | |
139 GNOME_VFS_ERROR_PARSE, | |
140 GNOME_VFS_ERROR_LAUNCH, | |
141 GNOME_VFS_ERROR_TIMEOUT, | |
142 GNOME_VFS_ERROR_NAMESERVER, | |
143 GNOME_VFS_ERROR_LOCKED, | |
144 GNOME_VFS_ERROR_DEPRECATED_FUNCTION, | |
145 GNOME_VFS_ERROR_INVALID_FILENAME, | |
146 GNOME_VFS_ERROR_NOT_A_SYMBOLIC_LINK, | |
147 GNOME_VFS_NUM_ERRORS | |
148 } | |
149 alias int GnomeVFSResult; | |
150 | |
151 enum { | |
152 GNOME_VFS_MIME_APPLICATION_ARGUMENT_TYPE_URIS, | |
153 GNOME_VFS_MIME_APPLICATION_ARGUMENT_TYPE_PATHS, | |
154 GNOME_VFS_MIME_APPLICATION_ARGUMENT_TYPE_URIS_FOR_NON_FILES | |
155 } | |
156 | |
157 alias GtkIconTheme GnomeIconTheme; | |
158 alias .gnome_icon_theme_lookup_icon gnome_icon_theme_lookup_icon; | |
159 alias .gnome_vfs_init gnome_vfs_init; | |
160 alias .gnome_icon_lookup gnome_icon_lookup; | |
161 alias .gnome_icon_theme_new gnome_icon_theme_new; | |
162 | |
163 | |
164 GnomeVFSMimeApplication * function ( char *mime_type ) gnome_vfs_mime_get_default_application; | |
165 char* function(char*, int ) gnome_vfs_make_uri_from_input_with_dirs; | |
166 GnomeVFSResult function( GnomeVFSMimeApplication*, GList*) gnome_vfs_mime_application_launch; | |
167 void function(GnomeVFSMimeApplication*) gnome_vfs_mime_application_free; | |
168 GList* function(char*) gnome_vfs_mime_get_extensions_list; | |
169 void function(GList*) gnome_vfs_mime_extensions_list_free; | |
170 void function(GList*) gnome_vfs_mime_registered_mime_type_list_free; | |
171 GnomeVFSResult function(char*) gnome_vfs_url_show; | |
172 char* function(char*) gnome_vfs_make_uri_from_input; | |
173 GList* function() gnome_vfs_get_registered_mime_types; | |
174 char* function(char*) gnome_vfs_mime_type_from_name; | |
175 } | |
54 | 176 static Symbol symbols[] = [ |
50 | 177 { "gnome_vfs_mime_get_default_application", cast(void**)&gnome_vfs_mime_get_default_application }, |
178 { "gnome_vfs_make_uri_from_input_with_dirs", cast(void**)&gnome_vfs_make_uri_from_input_with_dirs }, | |
179 { "gnome_vfs_mime_application_launch", cast(void**)&gnome_vfs_mime_application_launch }, | |
180 { "gnome_vfs_mime_application_free", cast(void**)&gnome_vfs_mime_application_free }, | |
181 { "gnome_vfs_url_show", cast(void**)&gnome_vfs_url_show }, | |
182 { "gnome_vfs_make_uri_from_input", cast(void**)&gnome_vfs_make_uri_from_input }, | |
183 { "gnome_vfs_get_registered_mime_types", cast(void**)&gnome_vfs_get_registered_mime_types }, | |
184 { "gnome_vfs_mime_get_extensions_list", cast(void**)&gnome_vfs_mime_get_extensions_list }, | |
185 { "gnome_vfs_mime_extensions_list_free", cast(void**)&gnome_vfs_mime_extensions_list_free }, | |
186 { "gnome_vfs_mime_registered_mime_type_list_free", cast(void**)&gnome_vfs_mime_registered_mime_type_list_free }, | |
187 { "gnome_vfs_mime_type_from_name", cast(void**)&gnome_vfs_mime_type_from_name } | |
188 ]; | |
25 | 189 } |
190 | |
191 /** | |
192 * Instances of this class represent programs and | |
193 * their associated file extensions in the operating | |
194 * system. | |
195 * | |
196 * @see <a href="http://www.eclipse.org/swt/snippets/#program">Program snippets</a> | |
197 * @see <a href="http://www.eclipse.org/swt/">Sample code and further information</a> | |
198 */ | |
199 public final class Program { | |
200 String name; | |
201 String command; | |
202 String iconPath; | |
203 Display display; | |
204 | |
205 /* Gnome specific | |
206 * true if command expects a URI | |
207 * false if expects a path | |
208 */ | |
209 bool gnomeExpectUri; | |
210 | |
211 static int /*long*/ cdeShell; | |
212 | |
213 static const String[] CDE_ICON_EXT = [ ".m.pm"[], ".l.pm", ".s.pm", ".t.pm" ]; | |
214 static const String[] CDE_MASK_EXT = [ ".m_m.bm"[], ".l_m.bm", ".s_m.bm", ".t_m.bm" ]; | |
215 static const String DESKTOP_DATA = "Program_DESKTOP"; | |
216 static const String ICON_THEME_DATA = "Program_GNOME_ICON_THEME"; | |
217 static const String PREFIX_HTTP = "http://"; //$NON-NLS-1$ | |
218 static const String PREFIX_HTTPS = "https://"; //$NON-NLS-1$ | |
219 static const int DESKTOP_UNKNOWN = 0; | |
220 static const int DESKTOP_GNOME = 1; | |
221 static const int DESKTOP_GNOME_24 = 2; | |
222 static const int DESKTOP_CDE = 3; | |
223 static const int PREFERRED_ICON_SIZE = 16; | |
224 | |
225 /** | |
226 * Prevents uninitialized instances from being created outside the package. | |
227 */ | |
228 this() { | |
229 } | |
230 | |
231 /* Determine the desktop for the given display. */ | |
232 static int getDesktop(Display display) { | |
233 if (display is null) return DESKTOP_UNKNOWN; | |
86 | 234 Integer desktopValue = cast(Integer)display.getData(DESKTOP_DATA); |
25 | 235 if (desktopValue !is null) return desktopValue.value; |
236 int desktop = DESKTOP_UNKNOWN; | |
237 | |
238 /* Get the list of properties on the root window. */ | |
239 void* xDisplay = OS.GDK_DISPLAY(); | |
240 uint rootWindow = OS.XDefaultRootWindow(xDisplay); | |
241 int numProp; | |
242 uint* propList = OS.XListProperties(xDisplay, rootWindow, &numProp); | |
243 uint[] property = new uint[numProp]; | |
244 if (propList !is null) { | |
245 property[ 0 .. numProp ] = propList[ 0 .. numProp ]; | |
246 OS.XFree(propList); | |
247 } | |
248 | |
249 /* | |
250 * Feature in Linux Desktop. There is currently no official way to | |
251 * determine whether the Gnome window manager or gnome-vfs is | |
252 * available. Earlier versions including Red Hat 9 and Suse 9 provide | |
253 * a documented Gnome specific property on the root window | |
254 * WIN_SUPPORTING_WM_CHECK. This property is no longer supported in newer | |
255 * versions such as Fedora Core 2. | |
256 * The workaround is to simply check that the window manager is a | |
257 * compliant one (property _NET_SUPPORTING_WM_CHECK) and to attempt to load | |
258 * our native library that depends on gnome-vfs. | |
259 */ | |
260 if (desktop is DESKTOP_UNKNOWN) { | |
261 String gnomeName = "_NET_SUPPORTING_WM_CHECK"; | |
262 int /*long*/ gnome = OS.XInternAtom(xDisplay, gnomeName.ptr, true); | |
263 if (gnome !is OS.None && (OS.GTK_VERSION >= OS.buildVERSION (2, 2, 0)) && gnome_init()) { | |
264 desktop = DESKTOP_GNOME; | |
265 int icon_theme = cast(int)GNOME.gnome_icon_theme_new(); | |
86 | 266 display.setData(ICON_THEME_DATA, new Integer(icon_theme)); |
25 | 267 display.addListener(SWT.Dispose, new class(display) Listener { |
268 Display display; | |
269 this( Display display ){ this.display = display; } | |
270 public void handleEvent(Event event) { | |
86 | 271 Integer gnomeIconTheme = cast(Integer)display.getData(ICON_THEME_DATA); |
25 | 272 if (gnomeIconTheme is null) return; |
273 display.setData(ICON_THEME_DATA, null); | |
274 /* | |
275 * Note. gnome_icon_theme_new uses g_object_new to allocate the | |
276 * data it returns. Use g_object_unref to free the pointer it returns. | |
277 */ | |
278 if (gnomeIconTheme.value !is 0) OS.g_object_unref( cast(void*)gnomeIconTheme.value); | |
279 } | |
280 }); | |
281 /* Check for libgnomevfs-2 version 2.4 */ | |
282 String buffer = "libgnomevfs-2.so.0"; | |
50 | 283 SharedLib.tryUseSymbol( "gnome_vfs_url_show", buffer, (void*){ |
25 | 284 desktop = DESKTOP_GNOME_24; |
50 | 285 }); |
286 if( desktop is DESKTOP_GNOME_24 ){ | |
287 SharedLib.loadLibSymbols( GNOME.symbols, buffer ); | |
25 | 288 } |
289 } | |
290 } | |
291 | |
292 // PORTING CDE not supported | |
293 /+ | |
294 /* | |
295 * On CDE, the atom below may exist without DTWM running. If the atom | |
296 * below is defined, the CDE database exists and the available | |
297 * applications can be queried. | |
298 */ | |
299 if (desktop is DESKTOP_UNKNOWN) { | |
300 String cdeName = "_DT_SM_PREFERENCES"; | |
301 int /*long*/ cde = OS.XInternAtom(xDisplay, cdeName.ptr, true); | |
302 for (int index = 0; desktop is DESKTOP_UNKNOWN && index < property.length; index++) { | |
303 if (property[index] is OS.None) continue; /* do not match atoms that do not exist */ | |
304 if (property[index] is cde && cde_init(display)) desktop = DESKTOP_CDE; | |
305 } | |
306 } | |
307 +/ | |
308 | |
86 | 309 display.setData(DESKTOP_DATA, new Integer(desktop)); |
25 | 310 return desktop; |
311 } | |
312 | |
313 // PORTING CDE not supported | |
314 /+ | |
315 bool cde_execute(String fileName) { | |
316 /* Use the character encoding for the default locale */ | |
317 char* action = toStringz(command); | |
318 char* ptr = cast(char*)OS.g_malloc(fileName.length+1); | |
319 ptr[ 0 .. fileName.length ] = fileName; | |
320 ptr[ fileName.length ] = 0; | |
321 DtActionArg args = new DtActionArg(); | |
322 args.argClass = CDE.DtACTION_FILE; | |
323 args.name = ptr; | |
324 long actionID = CDE.DtActionInvoke(cdeShell, action, args, 1, null, null, null, 1, 0, 0); | |
325 OS.g_free(ptr); | |
326 return actionID !is 0; | |
327 } | |
328 | |
329 static String cde_getAction(String dataType) { | |
330 String action = null; | |
331 String actions = cde_getAttribute(dataType, CDE.DtDTS_DA_ACTION_LIST); | |
332 if (actions !is null) { | |
333 int index = actions.indexOf("Open"); | |
334 if (index !is -1) { | |
335 action = actions.substring(index, index + 4); | |
336 } else { | |
337 index = actions.indexOf(","); | |
338 action = index !is -1 ? actions.substring(0, index) : actions; | |
339 } | |
340 } | |
341 return action; | |
342 } | |
343 | |
344 static String cde_getAttribute(String dataType, String attrName) { | |
345 /* Use the character encoding for the default locale */ | |
346 byte[] dataTypeBuf = Converter.wcsToMbcs(null, dataType, true); | |
347 byte[] attrNameBuf = Converter.wcsToMbcs(null, attrName, true); | |
348 byte[] optNameBuf = null; | |
349 int /*long*/ attrValue = CDE.DtDtsDataTypeToAttributeValue(dataTypeBuf, attrNameBuf, optNameBuf); | |
350 if (attrValue is 0) return null; | |
351 int length = OS.strlen(attrValue); | |
352 byte[] attrValueBuf = new byte[length]; | |
353 OS.memmove(attrValueBuf, attrValue, length); | |
354 CDE.DtDtsFreeAttributeValue(attrValue); | |
355 /* Use the character encoding for the default locale */ | |
356 return new String(Converter.mbcsToWcs(null, attrValueBuf)); | |
357 } | |
358 | |
359 static String[][ String ] cde_getDataTypeInfo() { | |
360 String[][ String ] dataTypeInfo; | |
361 int index; | |
362 int /*long*/ dataTypeList = CDE.DtDtsDataTypeNames(); | |
363 if (dataTypeList !is 0) { | |
364 /* For each data type name in the list */ | |
365 index = 0; | |
366 int /*long*/ [] dataType = new int /*long*/ [1]; | |
367 OS.memmove(dataType, dataTypeList + (index++ * 4), 4); | |
368 while (dataType[0] !is 0) { | |
369 int length = OS.strlen(dataType[0]); | |
370 byte[] dataTypeBuf = new byte[length]; | |
371 OS.memmove(dataTypeBuf, dataType[0], length); | |
372 /* Use the character encoding for the default locale */ | |
373 String dataTypeName = new String(Converter.mbcsToWcs(null, dataTypeBuf)); | |
374 | |
375 /* The data type is valid if it is not an action, and it has an extension and an action. */ | |
376 String extension = cde_getExtension(dataTypeName); | |
377 if (!CDE.DtDtsDataTypeIsAction(dataTypeBuf) && | |
378 extension !is null && cde_getAction(dataTypeName) !is null) { | |
379 String[] exts; | |
380 exts ~= extension; | |
381 dataTypeInfo[ dataTypeName ] = exts; | |
382 } | |
383 OS.memmove(dataType, dataTypeList + (index++ * 4), 4); | |
384 } | |
385 CDE.DtDtsFreeDataTypeNames(dataTypeList); | |
386 } | |
387 | |
388 return dataTypeInfo; | |
389 } | |
390 | |
391 static String cde_getExtension(String dataType) { | |
392 String fileExt = cde_getAttribute(dataType, CDE.DtDTS_DA_NAME_TEMPLATE); | |
393 if (fileExt is null || fileExt.indexOf("%s.") is -1) return null; | |
394 int dot = fileExt.indexOf("."); | |
395 return fileExt.substring(dot); | |
396 } | |
397 | |
398 /** | |
399 * CDE - Get Image Data | |
400 * | |
401 * This method returns the image data of the icon associated with | |
402 * the data type. Since CDE supports multiple sizes of icons, several | |
403 * attempts are made to locate an icon of the desired size and format. | |
404 * CDE supports the sizes: tiny, small, medium and large. The best | |
405 * search order is medium, large, small and then tiny. Althoug CDE supports | |
406 * colour and monochrome bitmaps, only colour icons are tried. (The order is | |
407 * defined by the cdeIconExt and cdeMaskExt arrays above.) | |
408 */ | |
409 ImageData cde_getImageData() { | |
410 // TODO | |
411 return null; | |
412 } | |
413 | |
414 static String cde_getMimeType(String extension) { | |
415 String mimeType = null; | |
416 String[][ String ] mimeInfo = cde_getDataTypeInfo(); | |
417 if (mimeInfo is null) return null; | |
418 String[] keys = mimeInfo.keys(); | |
419 int keyIdx = 0; | |
420 while (mimeType is null && keyIdx < keys.length ) { | |
421 String type = keys[ keyIdx ]; | |
422 String[] mimeExts = mimeInfo[type]; | |
423 for (int index = 0; index < mimeExts.length; index++){ | |
424 if (extension.equals(mimeExts[index])) { | |
425 mimeType = type; | |
426 break; | |
427 } | |
428 } | |
429 keyIdx++; | |
430 } | |
431 return mimeType; | |
432 } | |
433 | |
434 static Program cde_getProgram(Display display, String mimeType) { | |
435 Program program = new Program(); | |
436 program.display = display; | |
437 program.name = mimeType; | |
438 program.command = cde_getAction(mimeType); | |
439 program.iconPath = cde_getAttribute(program.name, CDE.DtDTS_DA_ICON); | |
440 return program; | |
441 } | |
442 | |
443 static bool cde_init(Display display) { | |
444 try { | |
445 Library.loadLibrary("swt-cde"); | |
446 } catch (Throwable e) { | |
447 return false; | |
448 } | |
449 | |
450 /* Use the character encoding for the default locale */ | |
451 CDE.XtToolkitInitialize(); | |
452 int /*long*/ xtContext = CDE.XtCreateApplicationContext (); | |
453 int /*long*/ xDisplay = OS.GDK_DISPLAY(); | |
454 byte[] appName = Converter.wcsToMbcs(null, "CDE", true); | |
455 byte[] appClass = Converter.wcsToMbcs(null, "CDE", true); | |
456 int /*long*/ [] argc = [0]; | |
457 CDE.XtDisplayInitialize(xtContext, xDisplay, appName, appClass, 0, 0, argc, 0); | |
458 int /*long*/ widgetClass = CDE.topLevelShellWidgetClass (); | |
459 cdeShell = CDE.XtAppCreateShell (appName, appClass, widgetClass, xDisplay, null, 0); | |
460 CDE.XtSetMappedWhenManaged (cdeShell, false); | |
461 CDE.XtResizeWidget (cdeShell, 10, 10, 0); | |
462 CDE.XtRealizeWidget (cdeShell); | |
463 bool initOK = CDE.DtAppInitialize(xtContext, xDisplay, cdeShell, appName, appName); | |
464 if (initOK) CDE.DtDbLoad(); | |
465 return initOK; | |
466 } | |
467 +/ | |
468 | |
469 static String[] parseCommand(String cmd) { | |
470 String[] args; | |
471 int sIndex = 0; | |
472 int eIndex; | |
473 while (sIndex < cmd.length) { | |
474 /* Trim initial white space of argument. */ | |
475 while (sIndex < cmd.length && Compatibility.isWhitespace(cmd.charAt(sIndex))) { | |
476 sIndex++; | |
477 } | |
478 if (sIndex < cmd.length) { | |
479 /* If the command is a quoted string */ | |
480 if (cmd.charAt(sIndex) is '"' || cmd.charAt(sIndex) is '\'') { | |
481 /* Find the terminating quote (or end of line). | |
482 * This code currently does not handle escaped characters (e.g., " a\"b"). | |
483 */ | |
484 eIndex = sIndex + 1; | |
485 while (eIndex < cmd.length && cmd.charAt(eIndex) !is cmd.charAt(sIndex)) eIndex++; | |
486 if (eIndex >= cmd.length) { | |
487 /* The terminating quote was not found | |
488 * Add the argument as is with only one initial quote. | |
489 */ | |
490 args ~= cmd.substring(sIndex, eIndex); | |
491 } else { | |
492 /* Add the argument, trimming off the quotes. */ | |
493 args ~= cmd.substring(sIndex + 1, eIndex); | |
494 } | |
495 sIndex = eIndex + 1; | |
496 } | |
497 else { | |
498 /* Use white space for the delimiters. */ | |
499 eIndex = sIndex; | |
500 while (eIndex < cmd.length && !Compatibility.isWhitespace( firstCodePoint( cmd[ eIndex .. $ ]))) eIndex++; | |
501 args ~= cmd.substring(sIndex, eIndex); | |
502 sIndex = eIndex + 1; | |
503 } | |
504 } | |
505 } | |
506 | |
507 String[] strings = new String[args.length]; | |
508 for (int index =0; index < args.length; index++) { | |
509 strings[index] = args[index]; | |
510 } | |
511 return strings; | |
512 } | |
513 | |
514 /** | |
515 * GNOME 2.4 - Execute the program for the given file. | |
516 */ | |
517 bool gnome_24_execute(String fileName) { | |
518 char* mimeTypeBuffer = toStringz(name); | |
519 auto ptr = GNOME.gnome_vfs_mime_get_default_application(mimeTypeBuffer); | |
520 char* fileNameBuffer = toStringz(fileName); | |
521 char* uri = GNOME.gnome_vfs_make_uri_from_input_with_dirs(fileNameBuffer, GNOME.GNOME_VFS_MAKE_URI_DIR_CURRENT); | |
522 GList* list = OS.g_list_append( null, uri); | |
523 int result = GNOME.gnome_vfs_mime_application_launch(ptr, list); | |
524 GNOME.gnome_vfs_mime_application_free(ptr); | |
525 OS.g_free(uri); | |
526 OS.g_list_free(list); | |
527 return result is GNOME.GNOME_VFS_OK; | |
528 } | |
529 | |
530 /** | |
531 * GNOME 2.4 - Launch the default program for the given file. | |
532 */ | |
533 static bool gnome_24_launch(String fileName) { | |
534 char* fileNameBuffer = toStringz(fileName); | |
535 char* uri = GNOME.gnome_vfs_make_uri_from_input_with_dirs(fileNameBuffer, GNOME.GNOME_VFS_MAKE_URI_DIR_CURRENT); | |
536 int result = GNOME.gnome_vfs_url_show(uri); | |
537 OS.g_free(uri); | |
538 return (result is GNOME.GNOME_VFS_OK); | |
539 } | |
540 | |
541 /** | |
542 * GNOME 2.2 - Execute the program for the given file. | |
543 */ | |
544 bool gnome_execute(String fileName) { | |
545 if (gnomeExpectUri) { | |
546 /* Convert the given path into a URL */ | |
547 char* fileNameBuffer = toStringz(fileName); | |
548 char* uri = GNOME.gnome_vfs_make_uri_from_input(fileNameBuffer); | |
549 if (uri !is null) { | |
50 | 550 fileName = fromStringz( uri )._idup(); |
25 | 551 OS.g_free(uri); |
552 } | |
553 } | |
554 | |
555 /* Parse the command into its individual arguments. */ | |
556 String[] args = parseCommand(command); | |
557 int fileArg = -1; | |
558 int index; | |
559 for (index = 0; index < args.length; index++) { | |
560 int j = args[index].indexOf("%f"); | |
561 if (j !is -1) { | |
562 String value = args[index]; | |
563 fileArg = index; | |
564 args[index] = value.substring(0, j) ~ fileName ~ value.substring(j + 2); | |
565 } | |
566 } | |
567 | |
568 /* If a file name was given but the command did not have "%f" */ | |
569 if ((fileName.length > 0) && (fileArg < 0)) { | |
570 String[] newArgs = new String[args.length + 1]; | |
571 for (index = 0; index < args.length; index++) newArgs[index] = args[index]; | |
572 newArgs[args.length] = fileName; | |
573 args = newArgs; | |
574 } | |
575 | |
576 /* Execute the command. */ | |
577 try { | |
578 Compatibility.exec(args); | |
579 } catch (IOException e) { | |
580 return false; | |
581 } | |
582 return true; | |
583 } | |
584 | |
585 /** | |
586 * GNOME - Get Image Data | |
587 * | |
588 */ | |
589 ImageData gnome_getImageData() { | |
590 if (iconPath is null) return null; | |
591 try { | |
592 return new ImageData(iconPath); | |
593 } catch (Exception e) {} | |
594 return null; | |
595 } | |
596 | |
597 /++ | |
598 + SWT Extension | |
599 + This is a temporary workaround until SWT will get the real implementation. | |
600 +/ | |
601 static String[][ String ] gnome24_getMimeInfo() { | |
50 | 602 version(Tango){ |
55
ea8b884d85f6
[swt lin] fixed tango compile
Frank Benoit <benoit@tionex.de>
parents:
54
diff
changeset
|
603 scope file = new tango.io.device.File.File("/usr/share/mime/globs"); |
ea8b884d85f6
[swt lin] fixed tango compile
Frank Benoit <benoit@tionex.de>
parents:
54
diff
changeset
|
604 scope it = new tango.io.stream.Lines.Lines!(char)( file ); |
50 | 605 } else { // Phobos |
606 scope it = std.string.splitlines( cast(String)std.file.read("/usr/share/mime/globs")); | |
607 } | |
25 | 608 // process file one line at a time |
609 String[][ String ] mimeInfo; | |
610 foreach (line; it ){ | |
611 int colon = line.indexOf(':'); | |
612 if( colon is line.length ){ | |
613 continue; | |
614 } | |
615 if( line.length < colon+3 || line[colon+1 .. colon+3 ] != "*." ){ | |
616 continue; | |
617 } | |
50 | 618 String mimeType = line[0..colon]._idup(); |
619 String ext = line[colon+3 .. $]._idup(); | |
25 | 620 if( auto exts = mimeType in mimeInfo ){ |
621 mimeInfo[ mimeType ] = *exts ~ ext; | |
622 } | |
623 else{ | |
624 mimeInfo[ mimeType ] = [ ext ]; | |
625 } | |
626 } | |
627 return mimeInfo; | |
628 } | |
629 /** | |
630 * GNOME - Get mime types | |
631 * | |
632 * Obtain the registered mime type information and | |
633 * return it in a map. The key of each entry | |
634 * in the map is the mime type name. The value is | |
635 * a vector of the associated file extensions. | |
636 */ | |
637 static String[][ String ] gnome_getMimeInfo() { | |
638 String[][ String ] mimeInfo; | |
639 GList* mimeList = GNOME.gnome_vfs_get_registered_mime_types(); | |
640 GList* mimeElement = mimeList; | |
641 while (mimeElement !is null) { | |
50 | 642 auto mimePtr = cast(char*) OS.g_list_data(mimeElement); |
643 String mimeTypeBuffer = fromStringz(mimePtr)._idup(); | |
25 | 644 String mimeType = mimeTypeBuffer;//new String(Converter.mbcsToWcs(null, mimeTypeBuffer)); |
645 GList* extensionList = GNOME.gnome_vfs_mime_get_extensions_list(mimePtr); | |
646 if (extensionList !is null) { | |
647 String[] extensions; | |
648 GList* extensionElement = extensionList; | |
649 while (extensionElement !is null) { | |
650 char* extensionPtr = cast(char*) OS.g_list_data(extensionElement); | |
50 | 651 String extensionBuffer = fromStringz(extensionPtr)._idup(); |
25 | 652 String extension = extensionBuffer; |
653 extension = '.' ~ extension; | |
654 extensions ~= extension; | |
655 extensionElement = OS.g_list_next(extensionElement); | |
656 } | |
657 GNOME.gnome_vfs_mime_extensions_list_free(extensionList); | |
658 if (extensions.length > 0) mimeInfo[ mimeType ] = extensions; | |
659 } | |
660 mimeElement = OS.g_list_next(mimeElement); | |
661 } | |
662 if (mimeList !is null) GNOME.gnome_vfs_mime_registered_mime_type_list_free(mimeList); | |
663 return mimeInfo; | |
664 } | |
665 | |
666 static String gnome_getMimeType(String extension) { | |
667 String mimeType = null; | |
668 String fileName = "swt" ~ extension; | |
669 char* extensionBuffer = toStringz(fileName); | |
670 char* typeName = GNOME.gnome_vfs_mime_type_from_name(extensionBuffer); | |
671 if (typeName !is null) { | |
50 | 672 mimeType = fromStringz(typeName)._idup(); |
25 | 673 } |
674 return mimeType; | |
675 } | |
676 | |
677 static Program gnome_getProgram(Display display, String mimeType) { | |
678 Program program = null; | |
679 char* mimeTypeBuffer = toStringz(mimeType); | |
680 GnomeVFSMimeApplication* ptr = GNOME.gnome_vfs_mime_get_default_application(mimeTypeBuffer); | |
681 if (ptr !is null) { | |
682 program = new Program(); | |
683 program.display = display; | |
684 program.name = mimeType; | |
685 GnomeVFSMimeApplication* application = ptr; | |
50 | 686 String buffer = fromStringz(application.command)._idup(); |
25 | 687 program.command = buffer; |
688 program.gnomeExpectUri = application.expects_uris is GNOME.GNOME_VFS_MIME_APPLICATION_ARGUMENT_TYPE_URIS; | |
689 | |
50 | 690 buffer = (fromStringz( application.id) ~ '\0')._idup(); |
86 | 691 Integer gnomeIconTheme = cast(Integer)display.getData(ICON_THEME_DATA); |
50 | 692 char* icon_name = GNOME.gnome_icon_lookup( cast(GtkIconTheme*) gnomeIconTheme.value, null, null, cast(char*)buffer.ptr, null, mimeTypeBuffer, |
25 | 693 GNOME.GNOME_ICON_LOOKUP_FLAGS_NONE, null); |
694 char* path = null; | |
695 if (icon_name !is null) path = GNOME.gnome_icon_theme_lookup_icon(cast(GtkIconTheme*)gnomeIconTheme.value, icon_name, PREFERRED_ICON_SIZE, null, null); | |
696 if (path !is null) { | |
50 | 697 program.iconPath = fromStringz( path)._idup(); |
25 | 698 OS.g_free(path); |
699 } | |
700 if (icon_name !is null) OS.g_free(icon_name); | |
701 GNOME.gnome_vfs_mime_application_free(ptr); | |
702 } | |
703 return program; | |
704 } | |
705 | |
706 static bool gnome_init() { | |
707 return cast(bool) GNOME.gnome_vfs_init(); | |
708 } | |
709 | |
710 /** | |
711 * Finds the program that is associated with an extension. | |
712 * The extension may or may not begin with a '.'. Note that | |
713 * a <code>Display</code> must already exist to guarantee that | |
714 * this method returns an appropriate result. | |
715 * | |
716 * @param extension the program extension | |
717 * @return the program or <code>null</code> | |
718 * | |
719 */ | |
720 public static Program findProgram(String extension) { | |
721 return findProgram(Display.getCurrent(), extension); | |
722 } | |
723 | |
724 /* | |
725 * API: When support for multiple displays is added, this method will | |
726 * become public and the original method above can be deprecated. | |
727 */ | |
728 static Program findProgram(Display display, String extension) { | |
729 // SWT extension: allow null for zero length string | |
730 //if (extension is null) SWT.error(SWT.ERROR_NULL_ARGUMENT); | |
731 if (extension.length is 0) return null; | |
732 if (extension.charAt(0) !is '.') extension = "." ~ extension; | |
733 int desktop = getDesktop(display); | |
734 String mimeType = null; | |
735 switch (desktop) { | |
736 case DESKTOP_GNOME_24: | |
737 case DESKTOP_GNOME: mimeType = gnome_getMimeType(extension); break; | |
738 //case DESKTOP_CDE: mimeType = cde_getMimeType(extension); break; | |
739 default: | |
740 } | |
741 if (mimeType is null) return null; | |
742 Program program = null; | |
743 switch (desktop) { | |
744 case DESKTOP_GNOME_24: | |
745 case DESKTOP_GNOME: program = gnome_getProgram(display, mimeType); break; | |
746 //case DESKTOP_CDE: program = cde_getProgram(display, mimeType); break; | |
747 default: | |
748 } | |
749 return program; | |
750 } | |
751 | |
752 /** | |
753 * Answer all program extensions in the operating system. Note | |
754 * that a <code>Display</code> must already exist to guarantee | |
755 * that this method returns an appropriate result. | |
756 * | |
757 * @return an array of extensions | |
758 */ | |
759 public static String[] getExtensions() { | |
760 return getExtensions(Display.getCurrent()); | |
761 } | |
762 | |
763 /* | |
764 * API: When support for multiple displays is added, this method will | |
765 * become public and the original method above can be deprecated. | |
766 */ | |
767 static String[] getExtensions(Display display) { | |
768 int desktop = getDesktop(display); | |
769 String[][ String ] mimeInfo = null; | |
770 switch (desktop) { | |
771 case DESKTOP_GNOME_24: mimeInfo = gnome24_getMimeInfo(); break; | |
772 case DESKTOP_GNOME: mimeInfo = gnome_getMimeInfo(); break; | |
773 //case DESKTOP_CDE: mimeInfo = cde_getDataTypeInfo(); break; | |
774 default: | |
775 } | |
776 if (mimeInfo is null) return null; | |
777 | |
778 /* Create a unique set of the file extensions. */ | |
779 String[] extensions; | |
780 String[] keys = mimeInfo.keys; | |
781 int keyIdx = 0; | |
782 while ( keyIdx < keys.length ) { | |
783 String mimeType = keys[ keyIdx ]; | |
784 String[] mimeExts = mimeInfo[mimeType]; | |
785 for (int index = 0; index < mimeExts.length; index++){ | |
50 | 786 version(Tango){ |
55
ea8b884d85f6
[swt lin] fixed tango compile
Frank Benoit <benoit@tionex.de>
parents:
54
diff
changeset
|
787 bool contains = cast(bool)tango.core.Array.contains(extensions, mimeExts[index]); |
50 | 788 } else { // Phobos |
51 | 789 bool contains = std.algorithm.find(extensions, mimeExts[index]) != std.iterator.end(extensions); |
50 | 790 } |
791 if (!contains) { | |
25 | 792 extensions ~= mimeExts[index]; |
793 } | |
794 } | |
795 keyIdx++; | |
796 } | |
797 | |
798 /* Return the list of extensions. */ | |
799 String[] extStrings = new String[]( extensions.length ); | |
800 for (int index = 0; index < extensions.length; index++) { | |
801 extStrings[index] = extensions[index]; | |
802 } | |
803 return extStrings; | |
804 } | |
805 | |
806 /** | |
807 * Answers all available programs in the operating system. Note | |
808 * that a <code>Display</code> must already exist to guarantee | |
809 * that this method returns an appropriate result. | |
810 * | |
811 * @return an array of programs | |
812 */ | |
813 public static Program[] getPrograms() { | |
814 return getPrograms(Display.getCurrent()); | |
815 } | |
816 | |
817 /* | |
818 * API: When support for multiple displays is added, this method will | |
819 * become public and the original method above can be deprecated. | |
820 */ | |
821 static Program[] getPrograms(Display display) { | |
822 int desktop = getDesktop(display); | |
823 String[][ String ] mimeInfo = null; | |
824 switch (desktop) { | |
825 case DESKTOP_GNOME_24: break; | |
826 case DESKTOP_GNOME: mimeInfo = gnome_getMimeInfo(); break; | |
827 //case DESKTOP_CDE: mimeInfo = cde_getDataTypeInfo(); break; | |
828 default: | |
829 } | |
830 if (mimeInfo is null) return new Program[0]; | |
831 Program[] programs; | |
832 String[] keys = mimeInfo.keys; | |
833 int keyIdx = 0; | |
834 while ( keyIdx < keys.length ) { | |
835 String mimeType = keys[ keyIdx ]; | |
836 Program program = null; | |
837 switch (desktop) { | |
838 case DESKTOP_GNOME: program = gnome_getProgram(display, mimeType); break; | |
839 //case DESKTOP_CDE: program = cde_getProgram(display, mimeType); break; | |
840 default: | |
841 } | |
842 if (program !is null) programs ~= program; | |
843 keyIdx++; | |
844 } | |
845 Program[] programList = new Program[programs.length]; | |
846 for (int index = 0; index < programList.length; index++) { | |
847 programList[index] = programs[index]; | |
848 } | |
849 return programList; | |
850 } | |
851 | |
852 /** | |
853 * Launches the operating system executable associated with the file or | |
854 * URL (http:// or https://). If the file is an executable then the | |
855 * executable is launched. Note that a <code>Display</code> must already | |
856 * exist to guarantee that this method returns an appropriate result. | |
857 * | |
858 * @param fileName the file or program name or URL (http:// or https://) | |
859 * @return <code>true</code> if the file is launched, otherwise <code>false</code> | |
860 * | |
861 * @exception IllegalArgumentException <ul> | |
862 * <li>ERROR_NULL_ARGUMENT when fileName is null</li> | |
863 * </ul> | |
864 */ | |
865 public static bool launch(String fileName) { | |
866 return launch(Display.getCurrent(), fileName); | |
867 } | |
868 | |
869 /* | |
870 * API: When support for multiple displays is added, this method will | |
871 * become public and the original method above can be deprecated. | |
872 */ | |
873 static bool launch (Display display, String fileName) { | |
874 if (fileName is null) SWT.error (SWT.ERROR_NULL_ARGUMENT); | |
875 switch (getDesktop (display)) { | |
876 case DESKTOP_GNOME_24: | |
877 if (gnome_24_launch (fileName)) return true; | |
878 default: | |
879 int index = fileName.lastIndexOf ('.'); | |
880 if (index !is -1) { | |
881 String extension = fileName.substring (index); | |
882 Program program = Program.findProgram (display, extension); | |
883 if (program !is null && program.execute (fileName)) return true; | |
884 } | |
885 String lowercaseName = fileName.toLowerCase (); | |
886 if (lowercaseName.startsWith (PREFIX_HTTP) || lowercaseName.startsWith (PREFIX_HTTPS)) { | |
887 Program program = Program.findProgram (display, ".html"); //$NON-NLS-1$ | |
888 if (program is null) { | |
889 program = Program.findProgram (display, ".htm"); //$NON-NLS-1$ | |
890 } | |
891 if (program !is null && program.execute (fileName)) return true; | |
892 } | |
893 break; | |
894 } | |
895 try { | |
896 Compatibility.exec (fileName); | |
897 return true; | |
898 } catch (IOException e) { | |
899 return false; | |
900 } | |
901 } | |
902 | |
903 /** | |
904 * Compares the argument to the receiver, and returns true | |
905 * if they represent the <em>same</em> object using a class | |
906 * specific comparison. | |
907 * | |
908 * @param other the object to compare with this object | |
909 * @return <code>true</code> if the object is the same as this object and <code>false</code> otherwise | |
910 * | |
911 * @see #hashCode() | |
912 */ | |
26 | 913 public override equals_t opEquals(Object other) { |
25 | 914 if (this is other) return true; |
915 if (!(cast(Program)other)) return false; | |
916 Program program = cast(Program)other; | |
917 return display is program.display && name.equals(program.name) && command.equals(program.command); | |
918 } | |
919 | |
920 /** | |
921 * Executes the program with the file as the single argument | |
922 * in the operating system. It is the responsibility of the | |
923 * programmer to ensure that the file contains valid data for | |
924 * this program. | |
925 * | |
926 * @param fileName the file or program name | |
927 * @return <code>true</code> if the file is launched, otherwise <code>false</code> | |
928 * | |
929 * @exception IllegalArgumentException <ul> | |
930 * <li>ERROR_NULL_ARGUMENT when fileName is null</li> | |
931 * </ul> | |
932 */ | |
933 public bool execute(String fileName) { | |
934 if (fileName is null) SWT.error(SWT.ERROR_NULL_ARGUMENT); | |
935 int desktop = getDesktop(display); | |
936 switch (desktop) { | |
937 case DESKTOP_GNOME_24: return gnome_24_execute(fileName); | |
938 case DESKTOP_GNOME: return gnome_execute(fileName); | |
939 //case DESKTOP_CDE: return cde_execute(fileName); | |
940 default: | |
941 } | |
942 return false; | |
943 } | |
944 | |
945 /** | |
946 * Returns the receiver's image data. This is the icon | |
947 * that is associated with the receiver in the operating | |
948 * system. | |
949 * | |
950 * @return the image data for the program, may be null | |
951 */ | |
952 public ImageData getImageData() { | |
953 switch (getDesktop(display)) { | |
954 case DESKTOP_GNOME_24: | |
955 case DESKTOP_GNOME: return gnome_getImageData(); | |
956 //case DESKTOP_CDE: return cde_getImageData(); | |
957 default: | |
958 } | |
959 return null; | |
960 } | |
961 | |
962 /** | |
963 * Returns the receiver's name. This is as short and | |
964 * descriptive a name as possible for the program. If | |
965 * the program has no descriptive name, this string may | |
966 * be the executable name, path or empty. | |
967 * | |
968 * @return the name of the program | |
969 */ | |
970 public String getName() { | |
971 return name; | |
972 } | |
973 | |
974 /** | |
975 * Returns an integer hash code for the receiver. Any two | |
976 * objects that return <code>true</code> when passed to | |
977 * <code>equals</code> must return the same value for this | |
978 * method. | |
979 * | |
980 * @return the receiver's hash | |
981 * | |
982 * @see #equals(Object) | |
983 */ | |
984 public override hash_t toHash() { | |
985 return .toHash(name) ^ .toHash(command) ^ display.toHash(); | |
986 } | |
987 | |
988 /** | |
989 * Returns a string containing a concise, human-readable | |
990 * description of the receiver. | |
991 * | |
992 * @return a string representation of the program | |
993 */ | |
994 public String toString() { | |
995 return Format( "Program {{{}}", name ); | |
996 } | |
997 } |