diff 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
line wrap: on
line diff
--- a/dwt/browser/FilePicker.d	Thu Jul 31 21:56:03 2008 -0700
+++ b/dwt/browser/FilePicker.d	Tue Aug 05 10:11:58 2008 -0700
@@ -8,446 +8,235 @@
  * Contributors:
  *     IBM Corporation - initial API and implementation
  * Port to the D programming language:
- *     John Reimer <terminal.node@gmail.com>
+ *      John Reimer <terminal.node@gmail.com>
  *******************************************************************************/
-
 module dwt.browser.FilePicker;
 
-import tango.io.model.IFile;
-import Utf = tango.text.convert.Utf;
-
 import dwt.dwthelper.utils;
 
 import dwt.DWT;
 
-import XPCOM = dwt.internal.mozilla.nsXPCOM;
-
-import dwt.browser.Mozilla;
-import dwt.browser.MozillaDelegate;
-
 import dwt.internal.mozilla.nsEmbedString;
-import dwt.internal.mozilla.nsIDOMWindow;
 import dwt.internal.mozilla.nsID;
 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.nsSimpleEnumerator;
-import dwt.internal.mozilla.nsStringAPI;
-import dwt.internal.mozilla.nsError;
-
 import dwt.widgets.DirectoryDialog;
 import dwt.widgets.Display;
 import dwt.widgets.FileDialog;
 import dwt.widgets.Shell;
 
-
-class FilePicker : nsISupports, nsIFilePicker 
-{
-    /**************************************************************************
-
-    **************************************************************************/
-    
-    private 
-    {
-        int      _refCount = 0;
-        short    _mode;
-
-        nsIDOMWindow _parent;
-
-        String[] _files;
-        String   _masks;
-        String   _defaultFilename, 
-                 _directory, 
-                 _title;
-    }
-    /**************************************************************************
-
-    **************************************************************************/
-
-    static final String SEPARATOR = FileConst.FileSeparatorChar;
-
-    /**************************************************************************
-
-    **************************************************************************/
-
-    this () 
-    {
-    }
-
-    /**************************************************************************
-
-    **************************************************************************/
+class FilePicker : nsIFilePicker {
 
-    nsrefcnt AddRef () 
-    {
-        _refCount++;
-        return _refCount;
-    }
-
-    /**************************************************************************
-
-    **************************************************************************/
+    int refCount = 0;
+    short mode;
+    nsIDOMWindow parentHandle;
+    String[] files, masks;
+    String defaultFilename, directory, title;
 
-    nsresult QueryInterface ( inout nsID riid, void** ppvObject) 
-    {   
-        if (riid is null || ppvObject is null) 
-            return NS_ERROR_NO_INTERFACE;
-    
-        if (riid == nsISupports.IID) 
-        {
-            *ppvObject = cast(void*)cast(nsISupports) this;
-            AddRef ();
-            return NS_OK;
-        }
-    
-        if (riid == nsIFilePicker.IID)) 
-        {
-            *ppvObject = cast(void*)cast(nsIFilePicker) this;
-            AddRef ();
-            return NS_OK;
-        }
-    
-        if (riid == nsIFilePicker_1_8.IID) 
-        {
-            *ppvObject = cast(void*)cast(nsIFilePicker_1_8) this;
-            AddRef ();
-            return NS_OK;
-        }
-
-        *ppvObject = null; 
-        return NS_ERROR_NO_INTERFACE;
-    }
-
-    /**************************************************************************
-
-    **************************************************************************/
+    static final String SEPARATOR = System.getProperty ("file.separator"); //$NON-NLS-1$
 
-    nsrefcnt Release () 
-    {
-        _refCount--;
-        
-        if (_refCount is 0) 
-            // No big deal here; just allow the GC to reap this object
-            // once all references are expired. -JJR
-            // delete this;
-            return 0;
-        
-        return _refCount;
-    }
-
-    /**************************************************************************
-
-    **************************************************************************/
-
-    /* SWT Comment:
-     *
-     * 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.
-     */
-
-    override String parseAString (nsEmbedString str) 
-    {
-        return null;
-    }
-
-    /**************************************************************************
-
-    **************************************************************************/
-
-    /* nsIFilePicker */
-
-    nsresult Init (nsIDOMWindow parent, nsAString* title, PRInt16 mode) 
-    {
-        _parent = parent;
-        _mode = mode;
-        _title = parseAString (title);
-        return XPCOM;
-    }
-
-    /**************************************************************************
-
-    **************************************************************************/
+this () {
+}
 
-    nsresult Show (PRUint32* retval) 
-    {
-        if (_mode is nsIFilePicker.modeGetFolder) 
-        {
-            /* picking a directory */
-            int result = this.showDirectoryPicker ();
-            *retval = result;
-            return NS_OK;
-        }
-
-        /* picking a file */
-        int style = _mode is nsIFilePicker.modeSave ? DWT.SAVE : DWT.OPEN;
-
-        if (_mode is nsIFilePicker.modeOpenMultiple) 
-            style |= DWT.MULTI;
-
-        Display display = Display.getCurrent ();
-        Shell parent = null; // TODO compute parent
-
-        if (parent is null) 
-        {
-            parent = new Shell (display);
-        }
+nsrefcnt AddRef () {
+    refCount++;
+    return refCount;
+}
 
-        FileDialog dialog = new FileDialog (parent, style);
-        
-        if (_title !is null) 
-            dialog.setText (_title);
-        
-        if (_directory !is null) 
-            dialog.setFilterPath (_directory);
-        
-        if (_masks !is null) {
-            String[] str ~= _masks;
-            dialog.setFilterExtensions ( str );
-        }
-        
-        if (_defaultFilename !is null) 
-            dialog.setFileName (_defaultFilename);
-        
-        String filename = dialog.open ();
-        files = dialog.getFileNames ();
-        _directory = dialog.getFilterPath ();
-        _title = _defaultFilename = null;
-        _masks = null;
-        int result = filename is null ? nsIFilePicker.returnCancel : nsIFilePicker.returnOK; 
-        *retval = result;
+nsresult QueryInterface (nsID* riid, void** ppvObject) {
+    if (riid is null || ppvObject is null) return NS_ERROR_NO_INTERFACE;
+    
+    if (riid == nsISupports.IID) {
+        *ppvObject = cast(void*)cast(nsISupports) this;
+        AddRef ();
         return NS_OK;
     }
-
-    /**************************************************************************
-
-    **************************************************************************/
+    if (riid == nsIFilePicker.IID)) {
+        *ppvObject = cast(void*)cast(nsIFilePicker) this;
+        AddRef ();
+        return NS_OK;
+    }   
+    if (riid == nsIFilePicker_1_8.IID) {
+        *ppvObject = cast(void*)cast(nsIFilePicker_1_8) this;
+        AddRef ();
+        return NS_OK;
+    }
+    *ppvObject = null; 
+    return NS_ERROR_NO_INTERFACE;
+}
+    
+nsrefcnt Release () {
+    refCount--;
+    if (refCount is 0) return 0;
+    return refCount;
+}
 
-    int showDirectoryPicker () 
-    {
-        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);
-        if (_directory !is null) 
-            dialog.setFilterPath (_directory);
-        _directory = dialog.open ();
-        _title = _defaultFilename = null;
-        _files = _masks = null;
-        return _directory is null ? nsIFilePicker.returnCancel : nsIFilePicker.returnOK;
+/*
+ * 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.
+ */
+override String parseAString (nsAString* string) {
+    return null;
+}
+
+/* nsIFilePicker */
+
+nsresult Init (nsIDOMWindow parent, nsAString* title, PRInt16 mode) {
+    parentHandle = parent;
+    this.mode = mode;
+    this.title = parseAString (title);
+    return XPCOM.NS_OK;
+}
+
+nsresult Show (PRUint32* _retval) {
+    if (mode is nsIFilePicker.modeGetFolder) {
+        /* picking a directory */
+        int result = showDirectoryPicker ();
+        *_retval = cast(int)cast(short)result; /* PRInt16 */
+        return XPCOM.NS_OK;
     }
 
-    /**************************************************************************
-
-    **************************************************************************/
-
-    nsresult GetFiles ( nsISimpleEnumerator* aFiles) 
-    {
-        return NS_ERROR_NOT_IMPLEMENTED;
-    }
-
-    /**************************************************************************
-
-    **************************************************************************/
-
-    nsresult GetFileURL (nsIFileURL aFileURL) 
-    {
-        return NS_ERROR_NOT_IMPLEMENTED;
+    /* picking a file */
+    int style = mode is nsIFilePicker.modeSave ? DWT.SAVE : DWT.OPEN;
+    if (mode is nsIFilePicker.modeOpenMultiple) style |= DWT.MULTI;
+    Display display = Display.getCurrent ();
+    Shell parent = null; // TODO compute parent
+    if (parent is null) {
+        parent = new Shell (display);
     }
-
-    /**************************************************************************
-
-    **************************************************************************/
-
-    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];
-        
-        // Create a nsEmbedString which will be automatically disposed 
-        // at the end of scope.
-        scope auto path = new nsEmbedString( filename );
-        
-        nsresult rc = XPCOM.NS_NewLocalFile ( cast(nsAString*)path, 1, aFile);
-
-        if (rc !is NS_OK) 
-            Mozilla.error (rc);
-        if (aFile is null) 
-            Mozilla.error (NS_ERROR_NULL_POINTER);
-
-        return NS_OK;
-    }
-
-    /**************************************************************************
-
-    **************************************************************************/
+    FileDialog dialog = new FileDialog (parent, style);
+    if (title !is null) dialog.setText (title);
+    if (directory !is null) dialog.setFilterPath (directory);
+    if (masks !is null) dialog.setFilterExtensions (masks);
+    if (defaultFilename !is null) dialog.setFileName (defaultFilename);
+    String filename = dialog.open ();
+    files = dialog.getFileNames ();
+    directory = dialog.getFilterPath ();
+    title = defaultFilename = null;
+    masks = null;
+    int result = filename is null ? nsIFilePicker.returnCancel : nsIFilePicker.returnOK; 
+    *_retval = cast(int)cast(short)result;; /* PRInt16 */
+    return XPCOM.NS_OK;
+}
 
-    nsresult SetDisplayDirectory (nsILocalFile aDisplayDirectory) 
-    {
-        if (aDisplayDirectory is null) 
-            return NS_OK;
-        
-        scope auto pathname = new nsEmbedCString;
-        aDisplayDirectory.GetNativePath ( cast(nsACString*)pathname );
-        // TODO: CHECK IF THIS DOES CORRECT STRING CONVERSION -JJR
-        char[] buffer = pathname.toString();
-        char[] chars = MozillaDelegate.mbcsToWcs (null, buffer);
-        // "buffer" contains a dup'ed string so we can safely reuse
-        // it's memory for the _directory member. -JJR
-        _directory = buffer;
-        return NS_OK;
+int showDirectoryPicker () {
+    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);
+    if (directory !is null) dialog.setFilterPath (directory);
+    directory = dialog.open ();
+    title = defaultFilename = null;
+    files = masks = null;
+    return directory is null ? nsIFilePicker.returnCancel : nsIFilePicker.returnOK;
+}
 
-    /**************************************************************************
-
-    **************************************************************************/
+nsresult GetFiles (nsISimpleEnumerator* aFiles) {
+    return XPCOM.NS_ERROR_NOT_IMPLEMENTED;
+}
 
-    nsresult GetDisplayDirectory (nsILocalFile* aDisplayDirectory) 
-    {
-        String directoryName = _directory !is null ? _directory : ""; //$NON-NLS-1$
-        scope auto path = new nsEmbedString (directoryName);
-        nsresult rc = XPCOM.NS_NewLocalFile ( cast(nsAString*)path, 1, aDisplayDirectory);
-        
-        if (rc !is NS_OK) 
-            Mozilla.error (rc);
-        if (aDisplayDirectory is null) 
-            Mozilla.error (NS_ERROR_NULL_POINTER);
-        return NS_OK;
-    }
-
-    /**************************************************************************
+nsresult GetFileURL ( nsIFileURL aFileURL ) {
+    return XPCOM.NS_ERROR_NOT_IMPLEMENTED;
+}
 
-    **************************************************************************/
-
-    nsresult SetFilterIndex (PRInt32 aFilterIndex) 
-    {
-        return NS_ERROR_NOT_IMPLEMENTED;
-    }
-
-    /**************************************************************************
+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];
+    scope auto path = new nsEmbedString (filename);
+    int rc = XPCOM.NS_NewLocalFile (cast(nsAString*)path, 1, aFile);
+    if (rc !is XPCOM.NS_OK) Mozilla.error (rc);
+    if (aFile is null) Mozilla.error (XPCOM.NS_ERROR_NULL_POINTER);
+    return XPCOM.NS_OK;
+}
 
-    **************************************************************************/
-
-    nresult GetFilterIndex (PRInt32* aFilterIndex) 
-    {
-        return NS_ERROR_NOT_IMPLEMENTED;
-    }
-
-    /**************************************************************************
+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 SetDefaultExtension (nsAString* aDefaultExtension) 
-    {
-        /* note that the type of argument 1 changed as of Mozilla 1.8 */
-        return NS_ERROR_NOT_IMPLEMENTED;
-    }
-
-    /**************************************************************************
+nsresult GetDisplayDirectory (nsILocalFile* aDisplayDirectory) {
+    String directoryName = directory !is null ? directory : ""; //$NON-NLS-1$
+    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 (aDisplayDirectory is null) Mozilla.error (XPCOM.NS_ERROR_NULL_POINTER);
+    return XPCOM.NS_OK;
+}
 
-    **************************************************************************/
+nsresult SetFilterIndex (PRInt32 aFilterIndex) {
+    return XPCOM.NS_ERROR_NOT_IMPLEMENTED;
+}
 
-    int GetDefaultExtension (nsAString* aDefaultExtension) 
-    {
-        /* note that the type of argument 1 changed as of Mozilla 1.8 */
-        return NS_ERROR_NOT_IMPLEMENTED;
-    }
-
-    /**************************************************************************
-
-    **************************************************************************/
+nsresult GetFilterIndex (PRInt32* aFilterIndex) {
+    return XPCOM.NS_ERROR_NOT_IMPLEMENTED;
+}
 
-    nresult SetDefaultString (nsAString* aDefaultString) 
-    {
-        defaultFilename = parseAString (aDefaultString);
-        return NS_OK;
-    }
+nsresult SetDefaultExtension (nsAString* aDefaultExtension) {
+    /* note that the type of argument 1 changed as of Mozilla 1.8 */
+    return XPCOM.NS_ERROR_NOT_IMPLEMENTED;
+}
 
-    /**************************************************************************
-
-    **************************************************************************/
+nsresult GetDefaultExtension (nsAString* aDefaultExtension) {
+    /* note that the type of argument 1 changed as of Mozilla 1.8 */
+    return XPCOM.NS_ERROR_NOT_IMPLEMENTED;
+}
 
-    nresult GetDefaultString (nsAString* aDefaultString) 
-    {
-        /* note that the type of argument 1 changed as of Mozilla 1.8 */
-        return NS_ERROR_NOT_IMPLEMENTED;
-    }
-
-    /**************************************************************************
-
-    **************************************************************************/
+nsresult SetDefaultString (nsAString* aDefaultString) {
+    defaultFilename = parseAString (aDefaultString);
+    return XPCOM.NS_OK;
+}
 
-    nresult AppendFilter (nsAString* title, nsAString* filter)
-    {
-        /* note that the type of arguments 1 and 2 changed as of Mozilla 1.8 */
-        return NS_ERROR_NOT_IMPLEMENTED;
-    }
-
-    /**************************************************************************
-
-    **************************************************************************/
+nsresult GetDefaultString (nsAString* aDefaultString) {
+    /* note that the type of argument 1 changed as of Mozilla 1.8 */
+    return XPCOM.NS_ERROR_NOT_IMPLEMENTED;
+}
 
-    nresult AppendFilters (PRInt32 filterMask) 
-    {
-        String addFilters;
-        
-        switch (filterMask) 
-        {
-            case nsIFilePicker.filterAll:
-            case nsIFilePicker.filterApps:
-                _masks = null;           /* this is equivalent to no filter */
-                break;
-            case nsIFilePicker.filterHTML:
-                addFilters = "*.htm;*.html" ;
-                break;
-            case nsIFilePicker.filterImages:
-                addFilters = "*.gif;*.jpeg;*.jpg;*.png";
-                break;
-            case nsIFilePicker.filterText:
-                addFilters = "*.txt";
-                break;
-            case nsIFilePicker.filterXML:
-                addFilters = "*.xml";
-                break;
-            case nsIFilePicker.filterXUL:
-                addFilters = "*.xul";
-                break;
+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;
+}
+
+nsresult AppendFilters (PRInt32 filterMask) {
+    String[] addFilters = null;
+    switch (filterMask) {
+        case nsIFilePicker.filterAll:
+        case nsIFilePicker.filterApps:
+            masks = null;           /* this is equivalent to no filter */
+            break;
+        case nsIFilePicker.filterHTML:
+            addFilters[0] = "*.htm;*.html"; //$NON-NLS-1$
+            break;
+        case nsIFilePicker.filterImages:
+            addFilters[0] = "*.gif;*.jpeg;*.jpg;*.png"; //$NON-NLS-1$
+            break;
+        case nsIFilePicker.filterText:
+            addFilters[0] = "*.txt";    //$NON-NLS-1$
+            break;
+        case nsIFilePicker.filterXML:
+            addFilters[0] = "*.xml";    //$NON-NLS-1$
+            break;
+        case nsIFilePicker.filterXUL:
+            addFilters[0] = "*.xul";    //$NON-NLS-1$
+            break;
+    }
+    if (masks is null) {
+        masks = addFilters;
+    } else {
+        if (addFilters !is null) {
+            masks ~= addFilters;
         }
-    
-        if (_masks is null) 
-        {
-            _masks = addFilters;
-        } else {
-            if (addFilters !is null)
-                _masks ~= addFilters;
-        }
-        return NS_OK;
     }
-
-/******************************************************************************
-
-    FilePicker for Mozilla Version 1.8
-
-******************************************************************************/
-
-class FilePicker_1_8 : FilePicker
-{
-    String parseAString (nsEmbedString str) 
-    {
-        if (str is null) 
-            return null;
-        return Utf.toString( str.toString16() );
-    }
-}
\ No newline at end of file
+    return XPCOM.NS_OK;
+}
+}