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