diff dwt/browser/AppFileLocProvider.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 535243e6d16a
line wrap: on
line diff
--- a/dwt/browser/AppFileLocProvider.d	Fri Jan 16 12:19:08 2009 +0100
+++ b/dwt/browser/AppFileLocProvider.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,139 +7,103 @@
  *
  * Contributors:
  *     IBM Corporation - initial API and implementation
+ * Port to the D programming language:
+ *      John Reimer <terminal.node@gmail.com>
  *******************************************************************************/
 module dwt.browser.AppFileLocProvider;
 
-import dwt.dwthelper.utils;
+import tango.sys.Environment;
+import tango.text.Util;
 
-import java.util.Vector;
+import dwt.dwthelper.utils;
+import dwt.browser.Mozilla;
+import dwt.browser.SimpleEnumerator;
 
-import dwt.internal.C;
 import dwt.internal.Compatibility;
-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.nsISimpleEnumerator;
 import dwt.internal.mozilla.nsEmbedString;
 import dwt.internal.mozilla.nsID;
-import dwt.internal.mozilla.nsIDirectoryServiceProvider;
-import dwt.internal.mozilla.nsIDirectoryServiceProvider2;
+import dwt.internal.mozilla.nsIDirectoryService;
 import dwt.internal.mozilla.nsIFile;
 import dwt.internal.mozilla.nsILocalFile;
 import dwt.internal.mozilla.nsISupports;
+import dwt.internal.mozilla.nsStringAPI;
 
-class AppFileLocProvider {
-    XPCOMObject supports;
-    XPCOMObject directoryServiceProvider;
-    XPCOMObject directoryServiceProvider2;  
+class AppFileLocProvider : nsIDirectoryServiceProvider2 { 
     int refCount = 0;
     String mozillaPath, profilePath;
     String[] pluginDirs;
     bool isXULRunner;
     
-    static final String SEPARATOR_OS = System.getProperty ("file.separator"); //$NON-NLS-1$
+    static       String SEPARATOR_OS;
     static final String CHROME_DIR = "chrome"; //$NON-NLS-1$
     static final String COMPONENTS_DIR = "components"; //$NON-NLS-1$
     static final String HISTORY_FILE = "history.dat"; //$NON-NLS-1$
     static final String LOCALSTORE_FILE = "localstore.rdf"; //$NON-NLS-1$
     static final String MIMETYPES_FILE = "mimeTypes.rdf"; //$NON-NLS-1$
     static final String PLUGINS_DIR = "plugins"; //$NON-NLS-1$
-    static final String USER_PLUGINS_DIR = ".mozilla" + SEPARATOR_OS + "plugins"; //$NON-NLS-1$ //$NON-NLS-2$
+    static       String USER_PLUGINS_DIR;
     static final String PREFERENCES_FILE = "prefs.js"; //$NON-NLS-1$
-    
-this (String path) {
-    mozillaPath = path + SEPARATOR_OS;
-    createCOMInterfaces ();
+
+static this () {
+    SEPARATOR_OS = System.getProperty ("file.separator");
+    USER_PLUGINS_DIR = ".mozilla" ~ SEPARATOR_OS ~ "plugins";
 }
 
-int AddRef () {
+this (String path) {
+    mozillaPath = path ~ SEPARATOR_OS;
+}
+
+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 ();}
-    };
-    
-    directoryServiceProvider = new XPCOMObject (new int[] {2, 0, 0, 3}) {
-        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 getFile (args[0], args[1], args[2]);}
-    };
-        
-    directoryServiceProvider2 = new XPCOMObject (new int[] {2, 0, 0, 3, 2}) {
-        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 getFile (args[0], args[1], args[2]);}
-        public int /*long*/ method4 (int /*long*/[] args) {return getFiles (args[0], args[1]);}
-    };
-}
+extern(System)
+nsresult QueryInterface (nsID* riid, void** ppvObject) {
+    if (riid is null || ppvObject is null) return XPCOM.NS_ERROR_NO_INTERFACE;
 
-void disposeCOMInterfaces () {
-    if (supports !is null) {
-        supports.dispose ();
-        supports = null;
-    }   
-    if (directoryServiceProvider !is null) {
-        directoryServiceProvider.dispose ();
-        directoryServiceProvider = null;    
-    }
-    if (directoryServiceProvider2 !is null) {
-        directoryServiceProvider2.dispose ();
-        directoryServiceProvider2 = null;   
-    }   
-}
-
-int /*long*/ getAddress () {
-    return directoryServiceProvider.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);
-    
-    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 (nsIDirectoryServiceProvider.NS_IDIRECTORYSERVICEPROVIDER_IID)) {
-        XPCOM.memmove (ppvObject, new int /*long*/[] {directoryServiceProvider.getAddress ()}, C.PTR_SIZEOF);
+    if (*riid == nsIDirectoryServiceProvider.IID) {
+        *ppvObject = cast(void*)cast(nsIDirectoryServiceProvider)this;
         AddRef ();
         return XPCOM.NS_OK;
     }
-    if (guid.Equals (nsIDirectoryServiceProvider2.NS_IDIRECTORYSERVICEPROVIDER2_IID)) {
-        XPCOM.memmove (ppvObject, new int /*long*/[] {directoryServiceProvider2.getAddress ()}, C.PTR_SIZEOF);
+    if (*riid == nsIDirectoryServiceProvider2.IID) {
+        *ppvObject = cast(void*)cast(nsIDirectoryServiceProvider2)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;
 }
 
 void setProfilePath (String path) {
     profilePath = path;
     if (!Compatibility.fileExists (path, "")) { //$NON-NLS-1$
-        int /*long*/[] result = new int /*long*/[1];
-        nsEmbedString pathString = new nsEmbedString (path);
-        int rc = XPCOM.NS_NewLocalFile (pathString.getAddress (), 1, result);
+        nsILocalFile file;
+        scope auto nsEmbedString pathString = new nsEmbedString (toString16(path));
+        int rc = XPCOM.NS_NewLocalFile (cast(nsAString*)pathString, 1, &file);
         if (rc !is XPCOM.NS_OK) Mozilla.error (rc);
-        if (result[0] is 0) Mozilla.error (XPCOM.NS_ERROR_NULL_POINTER);
-        pathString.dispose ();
+        if (file is null) Mozilla.error (XPCOM.NS_ERROR_NULL_POINTER);
 
-        nsILocalFile file = new nsILocalFile (result [0]);
         rc = file.Create (nsILocalFile.DIRECTORY_TYPE, 0700);
         if (rc !is XPCOM.NS_OK) Mozilla.error (rc);
         file.Release ();
@@ -147,44 +111,21 @@
 }
 
 /* nsIDirectoryServiceProvider2 */
-
-int getFiles (int /*long*/ prop, int /*long*/ _retval) {
-    int size = XPCOM.strlen (prop);
-    byte[] bytes = new byte[size];
-    XPCOM.memmove (bytes, prop, size);
-    String propertyName = new String (MozillaDelegate.mbcsToWcs (null, bytes));
+extern(System)
+nsresult GetFiles (char* prop, nsISimpleEnumerator* _retval) {
+    String propertyName = fromStringz(prop);
     String[] propertyValues = null;
 
-    if (propertyName.equals (XPCOM.NS_APP_PLUGINS_DIR_LIST)) {
+    if (propertyName == XPCOM.NS_APP_PLUGINS_DIR_LIST) {
         if (pluginDirs is null) {
             int index = 0;
             /* set the first value(s) to the MOZ_PLUGIN_PATH environment variable value if it's defined */
-            int /*long*/ ptr = C.getenv (MozillaDelegate.wcsToMbcs (null, XPCOM.MOZILLA_PLUGIN_PATH, true));
-            if (ptr !is 0) {
-                int length = C.strlen (ptr);
-                byte[] buffer = new byte[length];
-                C.memmove (buffer, ptr, length);
-                String value = new String (MozillaDelegate.mbcsToWcs (null, buffer));
-                if (value.length () > 0) {
-                    String separator = System.getProperty ("path.separator"); // $NON-NLS-1$
-                    Vector segments = new Vector ();
-                    int start, end = -1;
-                    do {
-                        start = end + 1;
-                        end = value.indexOf (separator, start);
-                        String segment;
-                        if (end is -1) {
-                            segment = value.substring (start);
-                        } else {
-                            segment = value.substring (start, end);
-                        }
-                        if (segment.length () > 0) segments.addElement (segment);
-                    } while (end !is -1);
-                    int segmentsSize = segments.size ();
-                    pluginDirs = new String [segmentsSize + 2];
-                    for (index = 0; index < segmentsSize; index++) {
-                        pluginDirs[index] = cast(String)segments.elementAt (index);
-                    }
+            String value = Environment.get (XPCOM.MOZILLA_PLUGIN_PATH);
+            if (value !is null) {
+                if (value.length > 0) {
+                    String separator = System.getProperty ("file.separator"); // $NON-NLS-1$
+                    foreach (segment; delimiters(value, separator))
+                        pluginDirs ~= segment;
                 }
             }
             if (pluginDirs is null) {
@@ -192,51 +133,46 @@
             }
 
             /* set the next value to the GRE path + "plugins" */
-            pluginDirs[index++] = mozillaPath + PLUGINS_DIR;
+            pluginDirs ~= mozillaPath ~ PLUGINS_DIR;
 
             /* set the next value to the home directory + "/.mozilla/plugins" */
-            pluginDirs[index++] = System.getProperty("user.home") + SEPARATOR_OS + USER_PLUGINS_DIR;
+            pluginDirs ~= System.getProperty("user.home") ~ SEPARATOR_OS ~ USER_PLUGINS_DIR;
         }
         propertyValues = pluginDirs;
     }
 
-    XPCOM.memmove(_retval, new int /*long*/[] {0}, C.PTR_SIZEOF);
+    *_retval = null;
+    //XPCOM.memmove(_retval, new int /*long*/[] {0}, C.PTR_SIZEOF);
     if (propertyValues !is null) {
-        int /*long*/[] result = new int /*long*/[1];
+        nsILocalFile localFile;
+        nsIFile file;
         nsISupports[] files = new nsISupports [propertyValues.length];
         int index = 0;
         for (int i = 0; i < propertyValues.length; i++) {
-            nsEmbedString pathString = new nsEmbedString (propertyValues[i]);
-            int rc = XPCOM.NS_NewLocalFile (pathString.getAddress (), 1, result);
+            scope auto pathString = new nsEmbedString (toString16(propertyValues[i]));
+            int rc = XPCOM.NS_NewLocalFile (cast(nsAString*)pathString, 1, &localFile);
             if (rc !is XPCOM.NS_ERROR_FILE_UNRECOGNIZED_PATH) {
                 /* value appears to be a valid pathname */
                 if (rc !is XPCOM.NS_OK) Mozilla.error (rc);
-                if (result[0] is 0) Mozilla.error (XPCOM.NS_ERROR_NULL_POINTER);
+                if (localFile is null) Mozilla.error (XPCOM.NS_ERROR_NULL_POINTER);
 
-                nsILocalFile localFile = new nsILocalFile (result[0]);
-                result[0] = 0;
-                rc = localFile.QueryInterface (nsIFile.NS_IFILE_IID, result); 
+                rc = localFile.QueryInterface (&nsIFile.IID, cast(void**)&file); 
                 if (rc !is XPCOM.NS_OK) Mozilla.error (rc);
-                if (result[0] is 0) Mozilla.error (XPCOM.NS_ERROR_NO_INTERFACE);
+                if (file is null) Mozilla.error (XPCOM.NS_ERROR_NO_INTERFACE);
                 localFile.Release ();
 
-                nsIFile file = new nsIFile (result[0]);
-                files[index++] = file;
+                files[index++] = cast(nsISupports)file;
             }
-            pathString.dispose ();
-            result[0] = 0;
         }
 
         if (index < propertyValues.length) {
             /* there were some invalid values so remove the trailing empty array slots */
-            nsISupports[] temp = new nsISupports [index];
-            System.arraycopy (files, 0, temp, 0, index);
-            files = temp;
+            files = files[0..index];
         }
 
-        SimpleEnumerator enumerator = new SimpleEnumerator (files);
+        auto enumerator = new SimpleEnumerator (files);
         enumerator.AddRef ();
-        XPCOM.memmove (_retval, new int /*long*/[] {enumerator.getAddress ()}, C.PTR_SIZEOF);
+        *_retval = cast(nsISimpleEnumerator)enumerator;
         return XPCOM.NS_OK;
     }
 
@@ -244,47 +180,44 @@
 }   
     
 /* nsIDirectoryServiceProvider implementation */
-
-int getFile(int /*long*/ prop, int /*long*/ persistent, int /*long*/ _retval) {
-    int size = XPCOM.strlen (prop);
-    byte[] bytes = new byte[size];
-    XPCOM.memmove (bytes, prop, size);
-    String propertyName = new String (MozillaDelegate.mbcsToWcs (null, bytes));
+extern(System)
+nsresult GetFile(char* prop, PRBool* persistent, nsIFile* _retval) {
+    String propertyName = tango.stdc.stringz.fromStringz( prop );
     String propertyValue = null;
 
-    if (propertyName.equals (XPCOM.NS_APP_HISTORY_50_FILE)) {
-        propertyValue = profilePath + HISTORY_FILE;
-    } else if (propertyName.equals (XPCOM.NS_APP_USER_MIMETYPES_50_FILE)) {
-        propertyValue = profilePath + MIMETYPES_FILE;
-    } else if (propertyName.equals (XPCOM.NS_APP_PREFS_50_FILE)) {
-        propertyValue = profilePath + PREFERENCES_FILE;
-    } else if (propertyName.equals (XPCOM.NS_APP_PREFS_50_DIR)) {
+    if (propertyName == (XPCOM.NS_APP_HISTORY_50_FILE)) {
+        propertyValue = profilePath ~ HISTORY_FILE;
+    } else if (propertyName == (XPCOM.NS_APP_USER_MIMETYPES_50_FILE)) {
+        propertyValue = profilePath ~ MIMETYPES_FILE;
+    } else if (propertyName == (XPCOM.NS_APP_PREFS_50_FILE)) {
+        propertyValue = profilePath ~ PREFERENCES_FILE;
+    } else if (propertyName == (XPCOM.NS_APP_PREFS_50_DIR)) {
         propertyValue = profilePath;
-    } else if (propertyName.equals (XPCOM.NS_APP_USER_CHROME_DIR)) {
-        propertyValue = profilePath + CHROME_DIR;
-    } else if (propertyName.equals (XPCOM.NS_APP_USER_PROFILE_50_DIR)) {
+    } else if (propertyName == (XPCOM.NS_APP_USER_CHROME_DIR)) {
+        propertyValue = profilePath ~ CHROME_DIR;
+    } else if (propertyName == (XPCOM.NS_APP_USER_PROFILE_50_DIR)) {
         propertyValue = profilePath;
-    } else if (propertyName.equals (XPCOM.NS_APP_LOCALSTORE_50_FILE)) {
-        propertyValue = profilePath + LOCALSTORE_FILE;
-    } else if (propertyName.equals (XPCOM.NS_APP_CACHE_PARENT_DIR)) {
+    } else if (propertyName == (XPCOM.NS_APP_LOCALSTORE_50_FILE)) {
+        propertyValue = profilePath ~ LOCALSTORE_FILE;
+    } else if (propertyName == (XPCOM.NS_APP_CACHE_PARENT_DIR)) {
         propertyValue = profilePath;
-    } else if (propertyName.equals (XPCOM.NS_OS_HOME_DIR)) {
+    } else if (propertyName == (XPCOM.NS_OS_HOME_DIR)) {
         propertyValue = System.getProperty("user.home");    //$NON-NLS-1$
-    } else if (propertyName.equals (XPCOM.NS_OS_TEMP_DIR)) {
+    } else if (propertyName == (XPCOM.NS_OS_TEMP_DIR)) {
         propertyValue = System.getProperty("java.io.tmpdir");   //$NON-NLS-1$
-    } else if (propertyName.equals (XPCOM.NS_GRE_DIR)) {
+    } else if (propertyName == (XPCOM.NS_GRE_DIR)) {
         propertyValue = mozillaPath;
-    } else if (propertyName.equals (XPCOM.NS_GRE_COMPONENT_DIR)) {
-        propertyValue = mozillaPath + COMPONENTS_DIR;
-    } else if (propertyName.equals (XPCOM.NS_XPCOM_INIT_CURRENT_PROCESS_DIR)) {
+    } else if (propertyName == (XPCOM.NS_GRE_COMPONENT_DIR)) {
+        propertyValue = mozillaPath ~ COMPONENTS_DIR;
+    } else if (propertyName == (XPCOM.NS_XPCOM_INIT_CURRENT_PROCESS_DIR)) {
         propertyValue = mozillaPath;
-    } else if (propertyName.equals (XPCOM.NS_OS_CURRENT_PROCESS_DIR)) {
+    } else if (propertyName == (XPCOM.NS_OS_CURRENT_PROCESS_DIR)) {
         propertyValue = mozillaPath;
-    } else if (propertyName.equals (XPCOM.NS_XPCOM_COMPONENT_DIR)) {
-        propertyValue = mozillaPath + COMPONENTS_DIR;
-    } else if (propertyName.equals (XPCOM.NS_XPCOM_CURRENT_PROCESS_DIR)) {
+    } else if (propertyName == (XPCOM.NS_XPCOM_COMPONENT_DIR)) {
+        propertyValue = mozillaPath ~ COMPONENTS_DIR;
+    } else if (propertyName == (XPCOM.NS_XPCOM_CURRENT_PROCESS_DIR)) {
         propertyValue = mozillaPath;
-    } else if (propertyName.equals (XPCOM.NS_APP_PREF_DEFAULTS_50_DIR)) {
+    } else if (propertyName == (XPCOM.NS_APP_PREF_DEFAULTS_50_DIR)) {
         /*
         * Answering a value for this property causes problems in Mozilla versions
         * < 1.7.  Unfortunately this property is queried early enough in the
@@ -298,23 +231,21 @@
         if (isXULRunner) propertyValue = profilePath;
     }
 
-    XPCOM.memmove (persistent, new int[] {1}, 4); /* PRBool */
-    XPCOM.memmove (_retval, new int /*long*/[] {0}, C.PTR_SIZEOF);
-    if (propertyValue !is null && propertyValue.length () > 0) {
-        int /*long*/[] result = new int /*long*/[1];
-        nsEmbedString pathString = new nsEmbedString (propertyValue);
-        int rc = XPCOM.NS_NewLocalFile (pathString.getAddress (), 1, result);
+    *persistent = true; /* PRBool */
+    *_retval = null;
+    if (propertyValue !is null && propertyValue.length > 0) {
+        nsILocalFile localFile;
+        scope auto pathString = new nsEmbedString (propertyValue.toString16());
+        int rc = XPCOM.NS_NewLocalFile (cast(nsAString*)pathString, 1, &localFile);
         if (rc !is XPCOM.NS_OK) Mozilla.error (rc);
-        if (result[0] is 0) Mozilla.error (XPCOM.NS_ERROR_NULL_POINTER);
-        pathString.dispose ();
+        if (localFile is null) Mozilla.error (XPCOM.NS_ERROR_NULL_POINTER);
         
-        nsILocalFile localFile = new nsILocalFile (result [0]);
-        result[0] = 0;
-        rc = localFile.QueryInterface (nsIFile.NS_IFILE_IID, result); 
+        nsIFile file;
+        rc = localFile.QueryInterface (&nsIFile.IID, cast(void**)&file); 
         if (rc !is XPCOM.NS_OK) Mozilla.error (rc);
-        if (result[0] is 0) Mozilla.error (XPCOM.NS_ERROR_NO_INTERFACE);
+        if (file is null) Mozilla.error (XPCOM.NS_ERROR_NO_INTERFACE);
 
-        XPCOM.memmove (_retval, new int /*long*/[] {result[0]}, C.PTR_SIZEOF);
+        *_retval = file;
         localFile.Release ();
         return XPCOM.NS_OK;
     }