KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > swt > browser > AppFileLocProvider


1 /*******************************************************************************
2  * Copyright (c) 2003, 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  *******************************************************************************/

11 package org.eclipse.swt.browser;
12
13 import org.eclipse.swt.internal.C;
14 import org.eclipse.swt.internal.mozilla.*;
15
16 class AppFileLocProvider {
17     XPCOMObject supports;
18     XPCOMObject directoryServiceProvider;
19     XPCOMObject directoryServiceProvider2;
20     int refCount = 0;
21     String JavaDoc mozillaPath, profilePath;
22     String JavaDoc[] pluginDirs;
23     
24     static final String JavaDoc SEPARATOR_OS = System.getProperty ("file.separator"); //$NON-NLS-1$
25
static final String JavaDoc CHROME_DIR = "chrome"; //$NON-NLS-1$
26
static final String JavaDoc COMPONENTS_DIR = "components"; //$NON-NLS-1$
27
static final String JavaDoc HISTORY_FILE = "history.dat"; //$NON-NLS-1$
28
static final String JavaDoc LOCALSTORE_FILE = "localstore.rdf"; //$NON-NLS-1$
29
static final String JavaDoc MIMETYPES_FILE = "mimeTypes.rdf"; //$NON-NLS-1$
30
static final String JavaDoc PLUGINS_DIR = "plugins"; //$NON-NLS-1$
31
static final String JavaDoc USER_PLUGINS_DIR = ".mozilla" + SEPARATOR_OS + "plugins"; //$NON-NLS-1$ //$NON-NLS-2$
32
static final String JavaDoc PREFERENCES_FILE = "prefs.js"; //$NON-NLS-1$
33

34 public AppFileLocProvider (String JavaDoc path) {
35     mozillaPath = path + SEPARATOR_OS;
36     createCOMInterfaces ();
37 }
38
39 int AddRef () {
40     refCount++;
41     return refCount;
42 }
43
44 void createCOMInterfaces () {
45     /* Create each of the interfaces that this object implements */
46     supports = new XPCOMObject (new int[] {2, 0, 0}) {
47         public int /*long*/ method0 (int /*long*/[] args) {return queryInterface (args[0], args[1]);}
48         public int /*long*/ method1 (int /*long*/[] args) {return AddRef ();}
49         public int /*long*/ method2 (int /*long*/[] args) {return Release ();}
50     };
51     
52     directoryServiceProvider = new XPCOMObject (new int[] {2, 0, 0, 3}) {
53         public int /*long*/ method0 (int /*long*/[] args) {return queryInterface (args[0], args[1]);}
54         public int /*long*/ method1 (int /*long*/[] args) {return AddRef ();}
55         public int /*long*/ method2 (int /*long*/[] args) {return Release ();}
56         public int /*long*/ method3 (int /*long*/[] args) {return getFile (args[0], args[1], args[2]);}
57     };
58         
59     directoryServiceProvider2 = new XPCOMObject (new int[] {2, 0, 0, 3, 2}) {
60         public int /*long*/ method0 (int /*long*/[] args) {return queryInterface (args[0], args[1]);}
61         public int /*long*/ method1 (int /*long*/[] args) {return AddRef ();}
62         public int /*long*/ method2 (int /*long*/[] args) {return Release ();}
63         public int /*long*/ method3 (int /*long*/[] args) {return getFile (args[0], args[1], args[2]);}
64         public int /*long*/ method4 (int /*long*/[] args) {return getFiles (args[0], args[1]);}
65     };
66 }
67
68 void disposeCOMInterfaces () {
69     if (supports != null) {
70         supports.dispose ();
71         supports = null;
72     }
73     if (directoryServiceProvider != null) {
74         directoryServiceProvider.dispose ();
75         directoryServiceProvider = null;
76     }
77     if (directoryServiceProvider2 != null) {
78         directoryServiceProvider2.dispose ();
79         directoryServiceProvider2 = null;
80     }
81 }
82
83 int /*long*/ getAddress () {
84     return directoryServiceProvider.getAddress ();
85 }
86
87 int /*long*/ queryInterface (int /*long*/ riid, int /*long*/ ppvObject) {
88     if (riid == 0 || ppvObject == 0) return XPCOM.NS_ERROR_NO_INTERFACE;
89     nsID guid = new nsID ();
90     XPCOM.memmove (guid, riid, nsID.sizeof);
91     
92     if (guid.Equals (nsISupports.NS_ISUPPORTS_IID)) {
93         XPCOM.memmove (ppvObject, new int /*long*/[] {supports.getAddress ()}, C.PTR_SIZEOF);
94         AddRef ();
95         return XPCOM.NS_OK;
96     }
97     if (guid.Equals (nsIDirectoryServiceProvider.NS_IDIRECTORYSERVICEPROVIDER_IID)) {
98         XPCOM.memmove (ppvObject, new int /*long*/[] {directoryServiceProvider.getAddress ()}, C.PTR_SIZEOF);
99         AddRef ();
100         return XPCOM.NS_OK;
101     }
102     if (guid.Equals (nsIDirectoryServiceProvider2.NS_IDIRECTORYSERVICEPROVIDER2_IID)) {
103         XPCOM.memmove (ppvObject, new int /*long*/[] {directoryServiceProvider2.getAddress ()}, C.PTR_SIZEOF);
104         AddRef ();
105         return XPCOM.NS_OK;
106     }
107     
108     XPCOM.memmove (ppvObject, new int /*long*/[] {0}, C.PTR_SIZEOF);
109     return XPCOM.NS_ERROR_NO_INTERFACE;
110 }
111
112 int Release () {
113     refCount--;
114     if (refCount == 0) disposeCOMInterfaces ();
115     return refCount;
116 }
117
118 void setProfilePath (String JavaDoc path) {
119     profilePath = path;
120 }
121
122 /* nsIDirectoryServiceProvider2 */
123
124 int getFiles (int /*long*/ prop, int /*long*/ _retval) {
125     int size = XPCOM.strlen (prop);
126     byte[] bytes = new byte[size];
127     XPCOM.memmove (bytes, prop, size);
128     String JavaDoc propertyName = new String JavaDoc (MozillaDelegate.mbcsToWcs (null, bytes));
129     String JavaDoc[] propertyValues = null;
130
131     if (propertyName.equals (XPCOM.NS_APP_PLUGINS_DIR_LIST)) {
132         if (pluginDirs == null) {
133             int index = 0;
134             /* set the first value to the MOZ_PLUGIN_PATH environment variable value if it's defined */
135             int /*long*/ ptr = C.getenv (MozillaDelegate.wcsToMbcs (null, XPCOM.MOZILLA_PLUGIN_PATH, true));
136             if (ptr != 0) {
137                 int length = C.strlen (ptr);
138                 byte[] buffer = new byte[length];
139                 C.memmove (buffer, ptr, length);
140                 String JavaDoc value = new String JavaDoc (MozillaDelegate.mbcsToWcs (null, buffer));
141                 if (value.length () > 0) {
142                     pluginDirs = new String JavaDoc[3];
143                     pluginDirs[index++] = value;
144                 }
145             }
146             if (pluginDirs == null) {
147                 pluginDirs = new String JavaDoc[2];
148             }
149
150             /* set the next value to the GRE path + "plugins" */
151             pluginDirs[index++] = mozillaPath + PLUGINS_DIR;
152
153             /* set the next value to the home directory + "/.mozilla/plugins" */
154             pluginDirs[index++] = System.getProperty("user.home") + SEPARATOR_OS + USER_PLUGINS_DIR;
155         }
156         propertyValues = pluginDirs;
157     }
158
159     XPCOM.memmove(_retval, new int /*long*/[] {0}, C.PTR_SIZEOF);
160     if (propertyValues != null) {
161         int /*long*/[] result = new int /*long*/[1];
162         nsISupports[] files = new nsISupports [propertyValues.length];
163         for (int i = 0; i < propertyValues.length; i++) {
164             nsEmbedString pathString = new nsEmbedString (propertyValues[i]);
165             int rc = XPCOM.NS_NewLocalFile (pathString.getAddress (), true, result);
166             if (rc != XPCOM.NS_OK) Mozilla.error (rc);
167             if (result[0] == 0) Mozilla.error (XPCOM.NS_ERROR_NULL_POINTER);
168             pathString.dispose ();
169
170             nsILocalFile localFile = new nsILocalFile (result[0]);
171             result[0] = 0;
172             rc = localFile.QueryInterface (nsIFile.NS_IFILE_IID, result);
173             if (rc != XPCOM.NS_OK) Mozilla.error (rc);
174             if (result[0] == 0) Mozilla.error (XPCOM.NS_ERROR_NULL_POINTER);
175             localFile.Release ();
176
177             nsIFile file = new nsIFile (result[0]);
178             result[0] = 0;
179             files[i] = file;
180         }
181
182         SimpleEnumerator enumerator = new SimpleEnumerator (files);
183         enumerator.AddRef ();
184
185         XPCOM.memmove (_retval, new int /*long*/[] {enumerator.getAddress ()}, C.PTR_SIZEOF);
186         return XPCOM.NS_OK;
187     }
188
189     return XPCOM.NS_ERROR_FAILURE;
190 }
191     
192 /* nsIDirectoryServiceProvider implementation */
193
194 int getFile(int /*long*/ prop, int /*long*/ persistent, int /*long*/ _retval) {
195     int size = XPCOM.strlen (prop);
196     byte[] bytes = new byte[size];
197     XPCOM.memmove (bytes, prop, size);
198     String JavaDoc propertyName = new String JavaDoc (MozillaDelegate.mbcsToWcs (null, bytes));
199     String JavaDoc propertyValue = null;
200
201     if (propertyName.equals (XPCOM.NS_APP_HISTORY_50_FILE)) {
202         propertyValue = profilePath + HISTORY_FILE;
203     } else if (propertyName.equals (XPCOM.NS_APP_USER_MIMETYPES_50_FILE)) {
204         propertyValue = profilePath + MIMETYPES_FILE;
205     } else if (propertyName.equals (XPCOM.NS_APP_PREFS_50_FILE)) {
206         propertyValue = profilePath + PREFERENCES_FILE;
207     } else if (propertyName.equals (XPCOM.NS_APP_PREFS_50_DIR)) {
208         propertyValue = profilePath;
209     } else if (propertyName.equals (XPCOM.NS_APP_USER_CHROME_DIR)) {
210         propertyValue = profilePath + CHROME_DIR;
211     } else if (propertyName.equals (XPCOM.NS_APP_USER_PROFILE_50_DIR)) {
212         propertyValue = profilePath;
213     } else if (propertyName.equals (XPCOM.NS_APP_LOCALSTORE_50_FILE)) {
214         propertyValue = profilePath + LOCALSTORE_FILE;
215     } else if (propertyName.equals (XPCOM.NS_APP_CACHE_PARENT_DIR)) {
216         propertyValue = profilePath;
217     } else if (propertyName.equals (XPCOM.NS_OS_HOME_DIR)) {
218         propertyValue = System.getProperty("user.home"); //$NON-NLS-1$
219
} else if (propertyName.equals (XPCOM.NS_OS_TEMP_DIR)) {
220         propertyValue = System.getProperty("java.io.tmpdir"); //$NON-NLS-1$
221
} else if (propertyName.equals (XPCOM.NS_GRE_DIR)) {
222         propertyValue = mozillaPath;
223     } else if (propertyName.equals (XPCOM.NS_GRE_COMPONENT_DIR)) {
224         propertyValue = mozillaPath + COMPONENTS_DIR;
225     } else if (propertyName.equals (XPCOM.NS_XPCOM_INIT_CURRENT_PROCESS_DIR)) {
226         propertyValue = mozillaPath;
227     } else if (propertyName.equals (XPCOM.NS_OS_CURRENT_PROCESS_DIR)) {
228         propertyValue = mozillaPath;
229     } else if (propertyName.equals (XPCOM.NS_XPCOM_COMPONENT_DIR)) {
230         propertyValue = mozillaPath + COMPONENTS_DIR;
231     } else if (propertyName.equals (XPCOM.NS_XPCOM_CURRENT_PROCESS_DIR)) {
232         propertyValue = mozillaPath;
233     }
234
235     XPCOM.memmove (persistent, new int[] {1}, 4); /* PRBool */
236     XPCOM.memmove (_retval, new int /*long*/[] {0}, C.PTR_SIZEOF);
237     if (propertyValue != null && propertyValue.length () > 0) {
238         int /*long*/[] result = new int /*long*/[1];
239         nsEmbedString pathString = new nsEmbedString (propertyValue);
240         int rc = XPCOM.NS_NewLocalFile (pathString.getAddress (), true, result);
241         if (rc != XPCOM.NS_OK) Mozilla.error (rc);
242         if (result[0] == 0) Mozilla.error (XPCOM.NS_ERROR_NULL_POINTER);
243         pathString.dispose ();
244         
245         nsILocalFile localFile = new nsILocalFile (result [0]);
246         result[0] = 0;
247         rc = localFile.QueryInterface (nsIFile.NS_IFILE_IID, result);
248         if (rc != XPCOM.NS_OK) Mozilla.error (rc);
249         if (result[0] == 0) Mozilla.error (XPCOM.NS_ERROR_NULL_POINTER);
250
251         XPCOM.memmove (_retval, new int /*long*/[] {result[0]}, C.PTR_SIZEOF);
252         localFile.Release ();
253         return XPCOM.NS_OK;
254     }
255
256     return XPCOM.NS_ERROR_FAILURE;
257 }
258 }
259
Popular Tags