diff dwt/browser/FilePicker.d @ 125:5583f8eeee6c

Synced mozilla with dwt-linux
author Jacob Carlborg <doob@me.com>
date Fri, 16 Jan 2009 12:49:08 +0100
parents d8635bb48c7c
children 0ba75290f8ce
line wrap: on
line diff
--- a/dwt/browser/FilePicker.d	Fri Jan 16 12:19:08 2009 +0100
+++ b/dwt/browser/FilePicker.d	Fri Jan 16 12:49:08 2009 +0100
@@ -1,5 +1,5 @@
-/*******************************************************************************
- * Copyright (c) 2003, 2008 IBM Corporation and others.
+/*******************************************************************************
+ * Copyright (c) 2003, 2007 IBM Corporation and others.
  * All rights reserved. This program and the accompanying materials
  * are made available under the terms of the Eclipse Public License v1.0
  * which accompanies this distribution, and is available at
@@ -7,164 +7,121 @@
  *
  * Contributors:
  *     IBM Corporation - initial API and implementation
+ * Port to the D programming language:
+ *      John Reimer <terminal.node@gmail.com>
  *******************************************************************************/
 module dwt.browser.FilePicker;
 
 import dwt.dwthelper.utils;
 
 import dwt.DWT;
-import dwt.internal.C;
-import dwt.internal.mozilla.XPCOM;
-import dwt.internal.mozilla.XPCOMObject;
+
+import XPCOM = dwt.internal.mozilla.XPCOM;
+
+import dwt.internal.mozilla.Common;
 import dwt.internal.mozilla.nsEmbedString;
+import dwt.internal.mozilla.nsISupports;
 import dwt.internal.mozilla.nsID;
-import dwt.internal.mozilla.nsIDOMWindow;
 import dwt.internal.mozilla.nsIFilePicker;
 import dwt.internal.mozilla.nsIFilePicker_1_8;
 import dwt.internal.mozilla.nsILocalFile;
-import dwt.internal.mozilla.nsISupports;
+import dwt.internal.mozilla.nsIFileURL;
+import dwt.internal.mozilla.nsIDOMWindow;
+import dwt.internal.mozilla.nsISimpleEnumerator;
+import dwt.internal.mozilla.nsStringAPI;
+
+import dwt.browser.Mozilla;
+
 import dwt.widgets.DirectoryDialog;
+import dwt.widgets.Display;
 import dwt.widgets.FileDialog;
 import dwt.widgets.Shell;
 
-class FilePicker {
-    XPCOMObject supports;
-    XPCOMObject filePicker;
+class FilePicker : nsIFilePicker {
 
     int refCount = 0;
     short mode;
-    int /*long*/ parentHandle;
+    nsIDOMWindow parentHandle;
     String[] files, masks;
     String defaultFilename, directory, title;
 
     static final String SEPARATOR = System.getProperty ("file.separator"); //$NON-NLS-1$
 
 this () {
-    createCOMInterfaces ();
 }
 
-int AddRef () {
+extern(System)
+nsrefcnt AddRef () {
     refCount++;
     return refCount;
 }
 
-void createCOMInterfaces () {
-    /* Create each of the interfaces that this object implements */
-    supports = new XPCOMObject (new int[] {2, 0, 0}) {
-        public int /*long*/ method0 (int /*long*/[] args) {return QueryInterface (args[0], args[1]);}
-        public int /*long*/ method1 (int /*long*/[] args) {return AddRef ();}
-        public int /*long*/ method2 (int /*long*/[] args) {return Release ();}
-    };
-
-    filePicker = new XPCOMObject (new int[] {2, 0, 0, 3, 1, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1}) {
-        public int /*long*/ method0 (int /*long*/[] args) {return QueryInterface (args[0], args[1]);}
-        public int /*long*/ method1 (int /*long*/[] args) {return AddRef ();}
-        public int /*long*/ method2 (int /*long*/[] args) {return Release ();}
-        public int /*long*/ method3 (int /*long*/[] args) {return Init (args[0], args[1], cast(short)args[2]);}
-        public int /*long*/ method4 (int /*long*/[] args) {return AppendFilters (cast(int)/*64*/args[0]);}
-        public int /*long*/ method5 (int /*long*/[] args) {return AppendFilter (args[0], args[1]);}
-        public int /*long*/ method6 (int /*long*/[] args) {return GetDefaultString (args[0]);}
-        public int /*long*/ method7 (int /*long*/[] args) {return SetDefaultString (args[0]);}
-        public int /*long*/ method8 (int /*long*/[] args) {return GetDefaultExtension (args[0]);}
-        public int /*long*/ method9 (int /*long*/[] args) {return SetDefaultExtension (args[0]);}
-        public int /*long*/ method10 (int /*long*/[] args) {return GetFilterIndex (args[0]);}
-        public int /*long*/ method11 (int /*long*/[] args) {return SetFilterIndex (cast(int)/*64*/args[0]);}
-        public int /*long*/ method12 (int /*long*/[] args) {return GetDisplayDirectory (args[0]);}
-        public int /*long*/ method13 (int /*long*/[] args) {return SetDisplayDirectory (args[0]);}
-        public int /*long*/ method14 (int /*long*/[] args) {return GetFile (args[0]);}
-        public int /*long*/ method15 (int /*long*/[] args) {return GetFileURL (args[0]);}
-        public int /*long*/ method16 (int /*long*/[] args) {return GetFiles (args[0]);}
-        public int /*long*/ method17 (int /*long*/[] args) {return Show (args[0]);}
-    };
-}
-
-void disposeCOMInterfaces () {
-    if (supports !is null) {
-        supports.dispose ();
-        supports = null;
-    }   
-    if (filePicker !is null) {
-        filePicker.dispose ();
-        filePicker = null;  
-    }
-}
-
-int /*long*/ getAddress () {
-    return filePicker.getAddress ();
-}
-
-int QueryInterface (int /*long*/ riid, int /*long*/ ppvObject) {
-    if (riid is 0 || ppvObject is 0) return XPCOM.NS_ERROR_NO_INTERFACE;
-    nsID guid = new nsID ();
-    XPCOM.memmove (guid, riid, nsID.sizeof);
+extern(System)
+nsresult QueryInterface (nsID* riid, void** ppvObject) {
+    if (riid is null || ppvObject is null) return XPCOM.NS_ERROR_NO_INTERFACE;
     
-    if (guid.Equals (nsISupports.NS_ISUPPORTS_IID)) {
-        XPCOM.memmove (ppvObject, new int /*long*/[] {supports.getAddress ()}, C.PTR_SIZEOF);
+    if (*riid == nsISupports.IID) {
+        *ppvObject = cast(void*)cast(nsISupports) this;
         AddRef ();
         return XPCOM.NS_OK;
     }
-    if (guid.Equals (nsIFilePicker.NS_IFILEPICKER_IID)) {
-        XPCOM.memmove(ppvObject, new int /*long*/[] {filePicker.getAddress ()}, C.PTR_SIZEOF);
+    if (*riid == nsIFilePicker.IID) {
+        *ppvObject = cast(void*)cast(nsIFilePicker) this;
         AddRef ();
         return XPCOM.NS_OK;
-    }
-    if (guid.Equals (nsIFilePicker_1_8.NS_IFILEPICKER_IID)) {
-        XPCOM.memmove(ppvObject, new int /*long*/[] {filePicker.getAddress ()}, C.PTR_SIZEOF);
+    }   
+    if (*riid == nsIFilePicker_1_8.IID) {
+        *ppvObject = cast(void*)cast(nsIFilePicker_1_8) this;
         AddRef ();
         return XPCOM.NS_OK;
     }
-
-    XPCOM.memmove (ppvObject, new int /*long*/[] {0}, C.PTR_SIZEOF);
+    *ppvObject = null; 
     return XPCOM.NS_ERROR_NO_INTERFACE;
 }
-
-int Release () {
+    
+extern(System)
+nsrefcnt Release () {
     refCount--;
-    if (refCount is 0) disposeCOMInterfaces ();
+    if (refCount is 0) return 0;
     return refCount;
 }
 
-Browser getBrowser (int /*long*/ aDOMWindow) {
-    if (aDOMWindow is 0) return null;
-    nsIDOMWindow window = new nsIDOMWindow (aDOMWindow);
-    return Mozilla.findBrowser (window);
-}
-
 /*
  * As of Mozilla 1.8 some of nsIFilePicker's string arguments changed type.  This method
  * answers a java string based on the type of string that is appropriate for the Mozilla
  * version being used.
  */
-String parseAString (int /*long*/ string) {
+extern(D)
+String parseAString (nsAString* string) {
     return null;
 }
 
 /* nsIFilePicker */
 
-int Init (int /*long*/ parent, int /*long*/ title, short mode) {
+extern(System)
+nsresult Init (nsIDOMWindow parent, nsAString* title, PRInt16 mode) {
     parentHandle = parent;
     this.mode = mode;
     this.title = parseAString (title);
     return XPCOM.NS_OK;
 }
 
-int Show (int /*long*/ _retval) {
+extern(System)
+nsresult Show (PRInt16* _retval) {
     if (mode is nsIFilePicker.modeGetFolder) {
         /* picking a directory */
         int result = showDirectoryPicker ();
-        XPCOM.memmove (_retval, new short[] {cast(short)result}, 2); /* PRInt16 */
+        *_retval = cast(int)cast(PRInt16)result; /* PRInt16 */
         return XPCOM.NS_OK;
     }
 
     /* picking a file */
     int style = mode is nsIFilePicker.modeSave ? DWT.SAVE : DWT.OPEN;
     if (mode is nsIFilePicker.modeOpenMultiple) style |= DWT.MULTI;
-    Browser browser = getBrowser (parentHandle);
-    Shell parent = null;
-    if (browser !is null) {
-        parent = browser.getShell ();
-    } else {
-        parent = new Shell ();
+    Display display = Display.getCurrent ();
+    Shell parent = null; // TODO compute parent
+    if (parent is null) {
+        parent = new Shell (display);
     }
     FileDialog dialog = new FileDialog (parent, style);
     if (title !is null) dialog.setText (title);
@@ -177,17 +134,15 @@
     title = defaultFilename = null;
     masks = null;
     int result = filename is null ? nsIFilePicker.returnCancel : nsIFilePicker.returnOK; 
-    XPCOM.memmove (_retval, new short[] {cast(short)result}, 2); /* PRInt16 */
+    *_retval = cast(int)cast(short)result;; /* PRInt16 */
     return XPCOM.NS_OK;
 }
 
 int showDirectoryPicker () {
-    Browser browser = getBrowser (parentHandle);
-    Shell parent = null;
-    if (browser !is null) {
-        parent = browser.getShell ();
-    } else {
-        parent = new Shell ();
+    Display display = Display.getCurrent ();
+    Shell parent = null; // TODO compute parent
+    if (parent is null) {
+        parent = new Shell (display);
     }
     DirectoryDialog dialog = new DirectoryDialog (parent, DWT.NONE);
     if (title !is null) dialog.setText (title);
@@ -198,89 +153,90 @@
     return directory is null ? nsIFilePicker.returnCancel : nsIFilePicker.returnOK;
 }
 
-int GetFiles (int /*long*/ aFiles) {
+extern(System)
+nsresult GetFiles (nsISimpleEnumerator* aFiles) {
     return XPCOM.NS_ERROR_NOT_IMPLEMENTED;
 }
 
-int GetFileURL (int /*long*/ aFileURL) {
+extern(System)
+nsresult GetFileURL ( nsIFileURL* aFileURL ) {
     return XPCOM.NS_ERROR_NOT_IMPLEMENTED;
 }
 
-int GetFile (int /*long*/ aFile) {
+extern(System)
+nsresult GetFile (nsILocalFile* aFile) {
     String filename = "";   //$NON-NLS-1$
-    if (directory !is null) filename += directory + SEPARATOR;
-    if (files !is null && files.length > 0) filename += files[0];
-    nsEmbedString path = new nsEmbedString (filename);
-    int /*long*/[] file = new int /*long*/[1];
-    int rc = XPCOM.NS_NewLocalFile (path.getAddress (), 1, file);
-    path.dispose ();
+    if (directory !is null) filename ~= directory ~ SEPARATOR;
+    if (files !is null && files.length > 0) filename ~= files[0];
+    scope auto path = new nsEmbedString (toString16(filename));
+    int rc = XPCOM.NS_NewLocalFile (cast(nsAString*)path, 1, aFile);
     if (rc !is XPCOM.NS_OK) Mozilla.error (rc);
-    if (file[0] is 0) Mozilla.error (XPCOM.NS_ERROR_NULL_POINTER);
-    XPCOM.memmove (aFile, file, C.PTR_SIZEOF);
+    if (aFile is null) Mozilla.error (XPCOM.NS_ERROR_NULL_POINTER);
     return XPCOM.NS_OK;
 }
 
-int SetDisplayDirectory (int /*long*/ aDisplayDirectory) {
-    if (aDisplayDirectory is 0) return XPCOM.NS_OK;
-    nsILocalFile file = new nsILocalFile (aDisplayDirectory);
-    int /*long*/ pathname = XPCOM.nsEmbedCString_new ();
-    file.GetNativePath (pathname);
-    int length = XPCOM.nsEmbedCString_Length (pathname);
-    int /*long*/ buffer = XPCOM.nsEmbedCString_get (pathname);
-    byte[] bytes = new byte[length];
-    XPCOM.memmove (bytes, buffer, length);
-    XPCOM.nsEmbedCString_delete (pathname);
-    char[] chars = MozillaDelegate.mbcsToWcs (null, bytes);
-    directory = new String (chars);
+extern(System)
+nsresult SetDisplayDirectory (nsILocalFile aDisplayDirectory) {
+    if (aDisplayDirectory is null) return XPCOM.NS_OK;
+    scope auto pathname = new nsEmbedCString();
+    aDisplayDirectory.GetNativePath (cast(nsACString*)pathname);
+    // wchar[] chars = MozillaDelegate.mbcsToWcs (null, bytes);
+    directory = pathname.toString;
     return XPCOM.NS_OK;
 }
 
-int GetDisplayDirectory (int /*long*/ aDisplayDirectory) {
+extern(System)
+nsresult GetDisplayDirectory (nsILocalFile* aDisplayDirectory) {
     String directoryName = directory !is null ? directory : ""; //$NON-NLS-1$
-    nsEmbedString path = new nsEmbedString (directoryName);
-    int /*long*/[] file = new int /*long*/[1];
-    int rc = XPCOM.NS_NewLocalFile (path.getAddress (), 1, file);
-    path.dispose ();
+    scope auto path = new nsEmbedString (Utf.toString16(directoryName));
+    int rc = XPCOM.NS_NewLocalFile (cast(nsAString*)path, 1, aDisplayDirectory);
     if (rc !is XPCOM.NS_OK) Mozilla.error (rc);
-    if (file[0] is 0) Mozilla.error (XPCOM.NS_ERROR_NULL_POINTER);
-    XPCOM.memmove (aDisplayDirectory, file, C.PTR_SIZEOF);
+    if (aDisplayDirectory is null) Mozilla.error (XPCOM.NS_ERROR_NULL_POINTER);
     return XPCOM.NS_OK;
 }
 
-int SetFilterIndex (int aFilterIndex) {
+extern(System)
+nsresult SetFilterIndex (PRInt32 aFilterIndex) {
     return XPCOM.NS_ERROR_NOT_IMPLEMENTED;
 }
 
-int GetFilterIndex (int /*long*/ aFilterIndex) {
+extern(System)
+nsresult GetFilterIndex (PRInt32* aFilterIndex) {
     return XPCOM.NS_ERROR_NOT_IMPLEMENTED;
 }
 
-int SetDefaultExtension (int /*long*/ aDefaultExtension) {
+extern(System)
+nsresult SetDefaultExtension (nsAString* aDefaultExtension) {
     /* note that the type of argument 1 changed as of Mozilla 1.8 */
     return XPCOM.NS_ERROR_NOT_IMPLEMENTED;
 }
 
-int GetDefaultExtension (int /*long*/ aDefaultExtension) {
+extern(System)
+nsresult GetDefaultExtension (nsAString* aDefaultExtension) {
     /* note that the type of argument 1 changed as of Mozilla 1.8 */
     return XPCOM.NS_ERROR_NOT_IMPLEMENTED;
 }
 
-int SetDefaultString (int /*long*/ aDefaultString) {
+extern(System)
+nsresult SetDefaultString (nsAString* aDefaultString) {
     defaultFilename = parseAString (aDefaultString);
     return XPCOM.NS_OK;
 }
 
-int GetDefaultString (int /*long*/ aDefaultString) {
+extern(System)
+nsresult GetDefaultString (nsAString* aDefaultString) {
     /* note that the type of argument 1 changed as of Mozilla 1.8 */
     return XPCOM.NS_ERROR_NOT_IMPLEMENTED;
 }
 
-int AppendFilter (int /*long*/ title, int /*long*/ filter) {
+extern(System)
+nsresult AppendFilter (nsAString* title, nsAString* filter) {
     /* note that the type of arguments 1 and 2 changed as of Mozilla 1.8 */
     return XPCOM.NS_ERROR_NOT_IMPLEMENTED;
 }
 
-int AppendFilters (int filterMask) {
+extern(System)
+nsresult AppendFilters (PRInt32 filterMask) {
     String[] addFilters = null;
     switch (filterMask) {
         case nsIFilePicker.filterAll:
@@ -288,29 +244,26 @@
             masks = null;           /* this is equivalent to no filter */
             break;
         case nsIFilePicker.filterHTML:
-            addFilters = new String[] {"*.htm;*.html"}; //$NON-NLS-1$
+            addFilters[0] = "*.htm;*.html"; //$NON-NLS-1$
             break;
         case nsIFilePicker.filterImages:
-            addFilters = new String[] {"*.gif;*.jpeg;*.jpg;*.png"}; //$NON-NLS-1$
+            addFilters[0] = "*.gif;*.jpeg;*.jpg;*.png"; //$NON-NLS-1$
             break;
         case nsIFilePicker.filterText:
-            addFilters = new String[] {"*.txt"};    //$NON-NLS-1$
+            addFilters[0] = "*.txt";    //$NON-NLS-1$
             break;
         case nsIFilePicker.filterXML:
-            addFilters = new String[] {"*.xml"};    //$NON-NLS-1$
+            addFilters[0] = "*.xml";    //$NON-NLS-1$
             break;
         case nsIFilePicker.filterXUL:
-            addFilters = new String[] {"*.xul"};    //$NON-NLS-1$
+            addFilters[0] = "*.xul";    //$NON-NLS-1$
             break;
     }
     if (masks is null) {
         masks = addFilters;
     } else {
         if (addFilters !is null) {
-            String[] newFilters = new String[masks.length + addFilters.length];
-            System.arraycopy (masks, 0, newFilters, 0, masks.length);
-            System.arraycopy (addFilters, 0, newFilters, masks.length, addFilters.length);
-            masks = newFilters;
+            masks ~= addFilters;
         }
     }
     return XPCOM.NS_OK;