comparison dwt/browser/FilePicker.d @ 286:44258e0b6687

More fixes for xpcom
author John Reimer<terminal.node@gmail.com>
date Tue, 05 Aug 2008 10:11:58 -0700
parents 93409d9838c5
children 942da4b6558a
comparison
equal deleted inserted replaced
280:e72345914350 286:44258e0b6687
6 * http://www.eclipse.org/legal/epl-v10.html 6 * http://www.eclipse.org/legal/epl-v10.html
7 * 7 *
8 * Contributors: 8 * Contributors:
9 * IBM Corporation - initial API and implementation 9 * IBM Corporation - initial API and implementation
10 * Port to the D programming language: 10 * Port to the D programming language:
11 * John Reimer <terminal.node@gmail.com> 11 * John Reimer <terminal.node@gmail.com>
12 *******************************************************************************/ 12 *******************************************************************************/
13
14 module dwt.browser.FilePicker; 13 module dwt.browser.FilePicker;
15 14
16 import tango.io.model.IFile;
17 import Utf = tango.text.convert.Utf;
18
19 import dwt.dwthelper.utils; 15 import dwt.dwthelper.utils;
20 16
21 import dwt.DWT; 17 import dwt.DWT;
22 18
23 import XPCOM = dwt.internal.mozilla.nsXPCOM;
24
25 import dwt.browser.Mozilla;
26 import dwt.browser.MozillaDelegate;
27
28 import dwt.internal.mozilla.nsEmbedString; 19 import dwt.internal.mozilla.nsEmbedString;
29 import dwt.internal.mozilla.nsIDOMWindow;
30 import dwt.internal.mozilla.nsID; 20 import dwt.internal.mozilla.nsID;
31 import dwt.internal.mozilla.nsIFilePicker; 21 import dwt.internal.mozilla.nsIFilePicker;
22 import dwt.internal.mozilla.nsIFilePicker_1_8;
32 import dwt.internal.mozilla.nsILocalFile; 23 import dwt.internal.mozilla.nsILocalFile;
33 import dwt.internal.mozilla.nsISupports; 24 import dwt.internal.mozilla.nsISupports;
34 import dwt.internal.mozilla.nsSimpleEnumerator;
35 import dwt.internal.mozilla.nsStringAPI;
36 import dwt.internal.mozilla.nsError;
37
38 import dwt.widgets.DirectoryDialog; 25 import dwt.widgets.DirectoryDialog;
39 import dwt.widgets.Display; 26 import dwt.widgets.Display;
40 import dwt.widgets.FileDialog; 27 import dwt.widgets.FileDialog;
41 import dwt.widgets.Shell; 28 import dwt.widgets.Shell;
42 29
43 30 class FilePicker : nsIFilePicker {
44 class FilePicker : nsISupports, nsIFilePicker 31
45 { 32 int refCount = 0;
46 /************************************************************************** 33 short mode;
47 34 nsIDOMWindow parentHandle;
48 **************************************************************************/ 35 String[] files, masks;
36 String defaultFilename, directory, title;
37
38 static final String SEPARATOR = System.getProperty ("file.separator"); //$NON-NLS-1$
39
40 this () {
41 }
42
43 nsrefcnt AddRef () {
44 refCount++;
45 return refCount;
46 }
47
48 nsresult QueryInterface (nsID* riid, void** ppvObject) {
49 if (riid is null || ppvObject is null) return NS_ERROR_NO_INTERFACE;
49 50
50 private 51 if (riid == nsISupports.IID) {
51 { 52 *ppvObject = cast(void*)cast(nsISupports) this;
52 int _refCount = 0; 53 AddRef ();
53 short _mode; 54 return NS_OK;
54 55 }
55 nsIDOMWindow _parent; 56 if (riid == nsIFilePicker.IID)) {
56 57 *ppvObject = cast(void*)cast(nsIFilePicker) this;
57 String[] _files; 58 AddRef ();
58 String _masks; 59 return NS_OK;
59 String _defaultFilename, 60 }
60 _directory, 61 if (riid == nsIFilePicker_1_8.IID) {
61 _title; 62 *ppvObject = cast(void*)cast(nsIFilePicker_1_8) this;
62 } 63 AddRef ();
63 /************************************************************************** 64 return NS_OK;
64 65 }
65 **************************************************************************/ 66 *ppvObject = null;
66 67 return NS_ERROR_NO_INTERFACE;
67 static final String SEPARATOR = FileConst.FileSeparatorChar; 68 }
68
69 /**************************************************************************
70
71 **************************************************************************/
72
73 this ()
74 {
75 }
76
77 /**************************************************************************
78
79 **************************************************************************/
80
81 nsrefcnt AddRef ()
82 {
83 _refCount++;
84 return _refCount;
85 }
86
87 /**************************************************************************
88
89 **************************************************************************/
90
91 nsresult QueryInterface ( inout nsID riid, void** ppvObject)
92 {
93 if (riid is null || ppvObject is null)
94 return NS_ERROR_NO_INTERFACE;
95 69
96 if (riid == nsISupports.IID) 70 nsrefcnt Release () {
97 { 71 refCount--;
98 *ppvObject = cast(void*)cast(nsISupports) this; 72 if (refCount is 0) return 0;
99 AddRef (); 73 return refCount;
100 return NS_OK; 74 }
75
76 /*
77 * As of Mozilla 1.8 some of nsIFilePicker's string arguments changed type. This method
78 * answers a java string based on the type of string that is appropriate for the Mozilla
79 * version being used.
80 */
81 override String parseAString (nsAString* string) {
82 return null;
83 }
84
85 /* nsIFilePicker */
86
87 nsresult Init (nsIDOMWindow parent, nsAString* title, PRInt16 mode) {
88 parentHandle = parent;
89 this.mode = mode;
90 this.title = parseAString (title);
91 return XPCOM.NS_OK;
92 }
93
94 nsresult Show (PRUint32* _retval) {
95 if (mode is nsIFilePicker.modeGetFolder) {
96 /* picking a directory */
97 int result = showDirectoryPicker ();
98 *_retval = cast(int)cast(short)result; /* PRInt16 */
99 return XPCOM.NS_OK;
100 }
101
102 /* picking a file */
103 int style = mode is nsIFilePicker.modeSave ? DWT.SAVE : DWT.OPEN;
104 if (mode is nsIFilePicker.modeOpenMultiple) style |= DWT.MULTI;
105 Display display = Display.getCurrent ();
106 Shell parent = null; // TODO compute parent
107 if (parent is null) {
108 parent = new Shell (display);
109 }
110 FileDialog dialog = new FileDialog (parent, style);
111 if (title !is null) dialog.setText (title);
112 if (directory !is null) dialog.setFilterPath (directory);
113 if (masks !is null) dialog.setFilterExtensions (masks);
114 if (defaultFilename !is null) dialog.setFileName (defaultFilename);
115 String filename = dialog.open ();
116 files = dialog.getFileNames ();
117 directory = dialog.getFilterPath ();
118 title = defaultFilename = null;
119 masks = null;
120 int result = filename is null ? nsIFilePicker.returnCancel : nsIFilePicker.returnOK;
121 *_retval = cast(int)cast(short)result;; /* PRInt16 */
122 return XPCOM.NS_OK;
123 }
124
125 int showDirectoryPicker () {
126 Display display = Display.getCurrent ();
127 Shell parent = null; // TODO compute parent
128 if (parent is null) {
129 parent = new Shell (display);
130 }
131 DirectoryDialog dialog = new DirectoryDialog (parent, DWT.NONE);
132 if (title !is null) dialog.setText (title);
133 if (directory !is null) dialog.setFilterPath (directory);
134 directory = dialog.open ();
135 title = defaultFilename = null;
136 files = masks = null;
137 return directory is null ? nsIFilePicker.returnCancel : nsIFilePicker.returnOK;
138 }
139
140 nsresult GetFiles (nsISimpleEnumerator* aFiles) {
141 return XPCOM.NS_ERROR_NOT_IMPLEMENTED;
142 }
143
144 nsresult GetFileURL ( nsIFileURL aFileURL ) {
145 return XPCOM.NS_ERROR_NOT_IMPLEMENTED;
146 }
147
148 nsresult GetFile (nsILocalFile* aFile) {
149 String filename = ""; //$NON-NLS-1$
150 if (directory !is null) filename ~= directory ~ SEPARATOR;
151 if (files !is null && files.length > 0) filename ~= files[0];
152 scope auto path = new nsEmbedString (filename);
153 int rc = XPCOM.NS_NewLocalFile (cast(nsAString*)path, 1, aFile);
154 if (rc !is XPCOM.NS_OK) Mozilla.error (rc);
155 if (aFile is null) Mozilla.error (XPCOM.NS_ERROR_NULL_POINTER);
156 return XPCOM.NS_OK;
157 }
158
159 nsresult SetDisplayDirectory (nsILocalFile aDisplayDirectory) {
160 if (aDisplayDirectory is null) return XPCOM.NS_OK;
161 scope auto pathname = new nsEmbedCString();
162 aDisplayDirectory.GetNativePath (cast(nsACString*)pathname);
163 // wchar[] chars = MozillaDelegate.mbcsToWcs (null, bytes);
164 directory = pathname.toString;
165 return XPCOM.NS_OK;
166 }
167
168 nsresult GetDisplayDirectory (nsILocalFile* aDisplayDirectory) {
169 String directoryName = directory !is null ? directory : ""; //$NON-NLS-1$
170 scope auto path = new nsEmbedString (Utf.toString16(directoryName));
171 int rc = XPCOM.NS_NewLocalFile (cast(nsAString*)path, 1, aDisplayDirectory);
172 if (rc !is XPCOM.NS_OK) Mozilla.error (rc);
173 if (aDisplayDirectory is null) Mozilla.error (XPCOM.NS_ERROR_NULL_POINTER);
174 return XPCOM.NS_OK;
175 }
176
177 nsresult SetFilterIndex (PRInt32 aFilterIndex) {
178 return XPCOM.NS_ERROR_NOT_IMPLEMENTED;
179 }
180
181 nsresult GetFilterIndex (PRInt32* aFilterIndex) {
182 return XPCOM.NS_ERROR_NOT_IMPLEMENTED;
183 }
184
185 nsresult SetDefaultExtension (nsAString* aDefaultExtension) {
186 /* note that the type of argument 1 changed as of Mozilla 1.8 */
187 return XPCOM.NS_ERROR_NOT_IMPLEMENTED;
188 }
189
190 nsresult GetDefaultExtension (nsAString* aDefaultExtension) {
191 /* note that the type of argument 1 changed as of Mozilla 1.8 */
192 return XPCOM.NS_ERROR_NOT_IMPLEMENTED;
193 }
194
195 nsresult SetDefaultString (nsAString* aDefaultString) {
196 defaultFilename = parseAString (aDefaultString);
197 return XPCOM.NS_OK;
198 }
199
200 nsresult GetDefaultString (nsAString* aDefaultString) {
201 /* note that the type of argument 1 changed as of Mozilla 1.8 */
202 return XPCOM.NS_ERROR_NOT_IMPLEMENTED;
203 }
204
205 nsresult AppendFilter (nsAString* title, nsAString* filter) {
206 /* note that the type of arguments 1 and 2 changed as of Mozilla 1.8 */
207 return XPCOM.NS_ERROR_NOT_IMPLEMENTED;
208 }
209
210 nsresult AppendFilters (PRInt32 filterMask) {
211 String[] addFilters = null;
212 switch (filterMask) {
213 case nsIFilePicker.filterAll:
214 case nsIFilePicker.filterApps:
215 masks = null; /* this is equivalent to no filter */
216 break;
217 case nsIFilePicker.filterHTML:
218 addFilters[0] = "*.htm;*.html"; //$NON-NLS-1$
219 break;
220 case nsIFilePicker.filterImages:
221 addFilters[0] = "*.gif;*.jpeg;*.jpg;*.png"; //$NON-NLS-1$
222 break;
223 case nsIFilePicker.filterText:
224 addFilters[0] = "*.txt"; //$NON-NLS-1$
225 break;
226 case nsIFilePicker.filterXML:
227 addFilters[0] = "*.xml"; //$NON-NLS-1$
228 break;
229 case nsIFilePicker.filterXUL:
230 addFilters[0] = "*.xul"; //$NON-NLS-1$
231 break;
232 }
233 if (masks is null) {
234 masks = addFilters;
235 } else {
236 if (addFilters !is null) {
237 masks ~= addFilters;
101 } 238 }
102 239 }
103 if (riid == nsIFilePicker.IID)) 240 return XPCOM.NS_OK;
104 { 241 }
105 *ppvObject = cast(void*)cast(nsIFilePicker) this; 242 }
106 AddRef ();
107 return NS_OK;
108 }
109
110 if (riid == nsIFilePicker_1_8.IID)
111 {
112 *ppvObject = cast(void*)cast(nsIFilePicker_1_8) this;
113 AddRef ();
114 return NS_OK;
115 }
116
117 *ppvObject = null;
118 return NS_ERROR_NO_INTERFACE;
119 }
120
121 /**************************************************************************
122
123 **************************************************************************/
124
125 nsrefcnt Release ()
126 {
127 _refCount--;
128
129 if (_refCount is 0)
130 // No big deal here; just allow the GC to reap this object
131 // once all references are expired. -JJR
132 // delete this;
133 return 0;
134
135 return _refCount;
136 }
137
138 /**************************************************************************
139
140 **************************************************************************/
141
142 /* SWT Comment:
143 *
144 * As of Mozilla 1.8 some of nsIFilePicker's string arguments changed type. This method
145 * answers a java string based on the type of string that is appropriate for the Mozilla
146 * version being used.
147 */
148
149 override String parseAString (nsEmbedString str)
150 {
151 return null;
152 }
153
154 /**************************************************************************
155
156 **************************************************************************/
157
158 /* nsIFilePicker */
159
160 nsresult Init (nsIDOMWindow parent, nsAString* title, PRInt16 mode)
161 {
162 _parent = parent;
163 _mode = mode;
164 _title = parseAString (title);
165 return XPCOM;
166 }
167
168 /**************************************************************************
169
170 **************************************************************************/
171
172 nsresult Show (PRUint32* retval)
173 {
174 if (_mode is nsIFilePicker.modeGetFolder)
175 {
176 /* picking a directory */
177 int result = this.showDirectoryPicker ();
178 *retval = result;
179 return NS_OK;
180 }
181
182 /* picking a file */
183 int style = _mode is nsIFilePicker.modeSave ? DWT.SAVE : DWT.OPEN;
184
185 if (_mode is nsIFilePicker.modeOpenMultiple)
186 style |= DWT.MULTI;
187
188 Display display = Display.getCurrent ();
189 Shell parent = null; // TODO compute parent
190
191 if (parent is null)
192 {
193 parent = new Shell (display);
194 }
195
196 FileDialog dialog = new FileDialog (parent, style);
197
198 if (_title !is null)
199 dialog.setText (_title);
200
201 if (_directory !is null)
202 dialog.setFilterPath (_directory);
203
204 if (_masks !is null) {
205 String[] str ~= _masks;
206 dialog.setFilterExtensions ( str );
207 }
208
209 if (_defaultFilename !is null)
210 dialog.setFileName (_defaultFilename);
211
212 String filename = dialog.open ();
213 files = dialog.getFileNames ();
214 _directory = dialog.getFilterPath ();
215 _title = _defaultFilename = null;
216 _masks = null;
217 int result = filename is null ? nsIFilePicker.returnCancel : nsIFilePicker.returnOK;
218 *retval = result;
219 return NS_OK;
220 }
221
222 /**************************************************************************
223
224 **************************************************************************/
225
226 int showDirectoryPicker ()
227 {
228 Display display = Display.getCurrent ();
229 Shell parent = null; // TODO compute parent
230
231 if (parent is null)
232 {
233 parent = new Shell (display);
234 }
235
236 DirectoryDialog dialog = new DirectoryDialog (parent, DWT.NONE);
237
238 if (_title !is null)
239 dialog.setText (_title);
240 if (_directory !is null)
241 dialog.setFilterPath (_directory);
242 _directory = dialog.open ();
243 _title = _defaultFilename = null;
244 _files = _masks = null;
245 return _directory is null ? nsIFilePicker.returnCancel : nsIFilePicker.returnOK;
246 }
247
248 /**************************************************************************
249
250 **************************************************************************/
251
252 nsresult GetFiles ( nsISimpleEnumerator* aFiles)
253 {
254 return NS_ERROR_NOT_IMPLEMENTED;
255 }
256
257 /**************************************************************************
258
259 **************************************************************************/
260
261 nsresult GetFileURL (nsIFileURL aFileURL)
262 {
263 return NS_ERROR_NOT_IMPLEMENTED;
264 }
265
266 /**************************************************************************
267
268 **************************************************************************/
269
270 nsresult GetFile (nsILocalFile* aFile)
271 {
272 String filename = ""; //$NON-NLS-1$
273
274 if (_directory !is null)
275 filename ~= _directory ~ SEPARATOR;
276 if (_files !is null && _files.length > 0)
277 filename ~= _files[0];
278
279 // Create a nsEmbedString which will be automatically disposed
280 // at the end of scope.
281 scope auto path = new nsEmbedString( filename );
282
283 nsresult rc = XPCOM.NS_NewLocalFile ( cast(nsAString*)path, 1, aFile);
284
285 if (rc !is NS_OK)
286 Mozilla.error (rc);
287 if (aFile is null)
288 Mozilla.error (NS_ERROR_NULL_POINTER);
289
290 return NS_OK;
291 }
292
293 /**************************************************************************
294
295 **************************************************************************/
296
297 nsresult SetDisplayDirectory (nsILocalFile aDisplayDirectory)
298 {
299 if (aDisplayDirectory is null)
300 return NS_OK;
301
302 scope auto pathname = new nsEmbedCString;
303 aDisplayDirectory.GetNativePath ( cast(nsACString*)pathname );
304 // TODO: CHECK IF THIS DOES CORRECT STRING CONVERSION -JJR
305 char[] buffer = pathname.toString();
306 char[] chars = MozillaDelegate.mbcsToWcs (null, buffer);
307 // "buffer" contains a dup'ed string so we can safely reuse
308 // it's memory for the _directory member. -JJR
309 _directory = buffer;
310 return NS_OK;
311 }
312
313 /**************************************************************************
314
315 **************************************************************************/
316
317 nsresult GetDisplayDirectory (nsILocalFile* aDisplayDirectory)
318 {
319 String directoryName = _directory !is null ? _directory : ""; //$NON-NLS-1$
320 scope auto path = new nsEmbedString (directoryName);
321 nsresult rc = XPCOM.NS_NewLocalFile ( cast(nsAString*)path, 1, aDisplayDirectory);
322
323 if (rc !is NS_OK)
324 Mozilla.error (rc);
325 if (aDisplayDirectory is null)
326 Mozilla.error (NS_ERROR_NULL_POINTER);
327 return NS_OK;
328 }
329
330 /**************************************************************************
331
332 **************************************************************************/
333
334 nsresult SetFilterIndex (PRInt32 aFilterIndex)
335 {
336 return NS_ERROR_NOT_IMPLEMENTED;
337 }
338
339 /**************************************************************************
340
341 **************************************************************************/
342
343 nresult GetFilterIndex (PRInt32* aFilterIndex)
344 {
345 return NS_ERROR_NOT_IMPLEMENTED;
346 }
347
348 /**************************************************************************
349
350 **************************************************************************/
351
352 int SetDefaultExtension (nsAString* aDefaultExtension)
353 {
354 /* note that the type of argument 1 changed as of Mozilla 1.8 */
355 return NS_ERROR_NOT_IMPLEMENTED;
356 }
357
358 /**************************************************************************
359
360 **************************************************************************/
361
362 int GetDefaultExtension (nsAString* aDefaultExtension)
363 {
364 /* note that the type of argument 1 changed as of Mozilla 1.8 */
365 return NS_ERROR_NOT_IMPLEMENTED;
366 }
367
368 /**************************************************************************
369
370 **************************************************************************/
371
372 nresult SetDefaultString (nsAString* aDefaultString)
373 {
374 defaultFilename = parseAString (aDefaultString);
375 return NS_OK;
376 }
377
378 /**************************************************************************
379
380 **************************************************************************/
381
382 nresult GetDefaultString (nsAString* aDefaultString)
383 {
384 /* note that the type of argument 1 changed as of Mozilla 1.8 */
385 return NS_ERROR_NOT_IMPLEMENTED;
386 }
387
388 /**************************************************************************
389
390 **************************************************************************/
391
392 nresult AppendFilter (nsAString* title, nsAString* filter)
393 {
394 /* note that the type of arguments 1 and 2 changed as of Mozilla 1.8 */
395 return NS_ERROR_NOT_IMPLEMENTED;
396 }
397
398 /**************************************************************************
399
400 **************************************************************************/
401
402 nresult AppendFilters (PRInt32 filterMask)
403 {
404 String addFilters;
405
406 switch (filterMask)
407 {
408 case nsIFilePicker.filterAll:
409 case nsIFilePicker.filterApps:
410 _masks = null; /* this is equivalent to no filter */
411 break;
412 case nsIFilePicker.filterHTML:
413 addFilters = "*.htm;*.html" ;
414 break;
415 case nsIFilePicker.filterImages:
416 addFilters = "*.gif;*.jpeg;*.jpg;*.png";
417 break;
418 case nsIFilePicker.filterText:
419 addFilters = "*.txt";
420 break;
421 case nsIFilePicker.filterXML:
422 addFilters = "*.xml";
423 break;
424 case nsIFilePicker.filterXUL:
425 addFilters = "*.xul";
426 break;
427 }
428
429 if (_masks is null)
430 {
431 _masks = addFilters;
432 } else {
433 if (addFilters !is null)
434 _masks ~= addFilters;
435 }
436 return NS_OK;
437 }
438
439 /******************************************************************************
440
441 FilePicker for Mozilla Version 1.8
442
443 ******************************************************************************/
444
445 class FilePicker_1_8 : FilePicker
446 {
447 String parseAString (nsEmbedString str)
448 {
449 if (str is null)
450 return null;
451 return Utf.toString( str.toString16() );
452 }
453 }