Mercurial > projects > dwt-linux
comparison dwt/browser.old/AppFileLocProvider.d @ 288:4ee8c4237614
old branches... commit by mistake
author | John Reimer<terminal.node@gmail.com> |
---|---|
date | Tue, 05 Aug 2008 18:00:50 -0700 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
287:9cbe6285f746 | 288:4ee8c4237614 |
---|---|
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 * Port to the D programming language: | |
11 * John Reimer <terminal.node@gmail.com> | |
12 *******************************************************************************/ | |
13 | |
14 module dwt.browser.AppFileLocProvider; | |
15 | |
16 import dwt.dwthelper.utils; | |
17 | |
18 import dwt.internal.Compatibility; | |
19 | |
20 import dwt.internal.mozilla.nsEmbedString; | |
21 import dwt.internal.mozilla.nsID; | |
22 import dwt.internal.mozilla.nsIDirectoryService; | |
23 import dwt.internal.mozilla.nsIFile; | |
24 import dwt.internal.mozilla.nsILocalFile; | |
25 import dwt.internal.mozilla.nsISupports; | |
26 | |
27 class AppFileLocProvider : nsISupports, nsIDirectoryServiceProvider2 | |
28 { | |
29 // XPCOMObject supports; | |
30 // XPCOMObject directoryServiceProvider; | |
31 // XPCOMObject directoryServiceProvider2; | |
32 nsrefcnt _refCount = 0; | |
33 String mozillaPath, profilePath; | |
34 String[] pluginDirs; | |
35 bool isXULRunner; | |
36 | |
37 static String SEPARATOR_OS = System.getProperty ("file.separator"); //$NON-NLS-1$ | |
38 static String CHROME_DIR = "chrome"; //$NON-NLS-1$ | |
39 static String COMPONENTS_DIR = "components"; //$NON-NLS-1$ | |
40 static String HISTORY_FILE = "history.dat"; //$NON-NLS-1$ | |
41 static String LOCALSTORE_FILE = "localstore.rdf"; //$NON-NLS-1$ | |
42 static String MIMETYPES_FILE = "mimeTypes.rdf"; //$NON-NLS-1$ | |
43 static String PLUGINS_DIR = "plugins"; //$NON-NLS-1$ | |
44 static String USER_PLUGINS_DIR = ".mozilla" + SEPARATOR_OS + "plugins"; //$NON-NLS-1$ //$NON-NLS-2$ | |
45 static String PREFERENCES_FILE = "prefs.js"; //$NON-NLS-1$ | |
46 | |
47 AppFileLocProvider (String path) { | |
48 mozillaPath = path + SEPARATOR_OS; | |
49 createCOMInterfaces (); | |
50 } | |
51 | |
52 int AddRef () | |
53 { | |
54 refCount++; | |
55 return refCount; | |
56 } | |
57 | |
58 nsresult QueryInterface ( nsIID* riid, void** ppvObject) | |
59 { | |
60 if (riid is null || ppvObject is null) | |
61 return NS_ERROR_NO_INTERFACE; | |
62 | |
63 if (riid == nsISupports.IID) | |
64 { | |
65 *ppvObject =cast(void*)cast(nsISupports)this; | |
66 AddRef (); | |
67 return NS_OK; | |
68 } | |
69 | |
70 if (riid == nsIDirectoryServiceProvider.IID) | |
71 { | |
72 *ppvObject = cast(void*)cast(nsIDirectoryServiceProvider)this; | |
73 AddRef (); | |
74 return NS_OK; | |
75 } | |
76 | |
77 if (riid == nsDirectoryServiceProvider2.IID) { | |
78 *ppvObject = cast(void*)cast(nsIDirectoryServiceProvider2)this; | |
79 AddRef (); | |
80 return NS_OK; | |
81 } | |
82 | |
83 *ppvObject = null; | |
84 return NS_ERROR_NO_INTERFACE; | |
85 } | |
86 | |
87 nsrefcnt Release () | |
88 { | |
89 _refCount--; | |
90 if (_refCount is 0) | |
91 return 0; | |
92 return _refCount; | |
93 } | |
94 | |
95 void setProfilePath (String path) { | |
96 profilePath = path; | |
97 if (!Compatibility.fileExists (path, "")) { //$NON-NLS-1$ | |
98 int /*long*/[] result = new int /*long*/[1]; | |
99 nsEmbedString pathString = new nsEmbedString (path); | |
100 int rc = XPCOM.NS_NewLocalFile (pathString.getAddress (), 1, result); | |
101 if (rc !is XPCOM.NS_OK) Mozilla.error (rc); | |
102 if (result[0] is 0) Mozilla.error (XPCOM.NS_ERROR_NULL_POINTER); | |
103 pathString.dispose (); | |
104 | |
105 nsILocalFile file = new nsILocalFile (result [0]); | |
106 rc = file.Create (nsILocalFile.DIRECTORY_TYPE, 0700); | |
107 if (rc !is XPCOM.NS_OK) Mozilla.error (rc); | |
108 file.Release (); | |
109 } | |
110 } | |
111 | |
112 /* nsIDirectoryServiceProvider2 */ | |
113 | |
114 int getFiles (int /*long*/ prop, int /*long*/ _retval) { | |
115 int size = XPCOM.strlen (prop); | |
116 byte[] bytes = new byte[size]; | |
117 XPCOM.memmove (bytes, prop, size); | |
118 String propertyName = new String (MozillaDelegate.mbcsToWcs (null, bytes)); | |
119 String[] propertyValues = null; | |
120 | |
121 if (propertyName.equals (XPCOM.NS_APP_PLUGINS_DIR_LIST)) { | |
122 if (pluginDirs is null) { | |
123 int index = 0; | |
124 /* set the first value(s) to the MOZ_PLUGIN_PATH environment variable value if it's defined */ | |
125 int /*long*/ ptr = C.getenv (MozillaDelegate.wcsToMbcs (null, XPCOM.MOZILLA_PLUGIN_PATH, true)); | |
126 if (ptr !is 0) { | |
127 int length = C.strlen (ptr); | |
128 byte[] buffer = new byte[length]; | |
129 C.memmove (buffer, ptr, length); | |
130 String value = new String (MozillaDelegate.mbcsToWcs (null, buffer)); | |
131 if (value.length () > 0) { | |
132 String separator = System.getProperty ("path.separator"); // $NON-NLS-1$ | |
133 Vector segments = new Vector (); | |
134 int start, end = -1; | |
135 do { | |
136 start = end + 1; | |
137 end = value.indexOf (separator, start); | |
138 String segment; | |
139 if (end is -1) { | |
140 segment = value.substring (start); | |
141 } else { | |
142 segment = value.substring (start, end); | |
143 } | |
144 if (segment.length () > 0) segments.addElement (segment); | |
145 } while (end !is -1); | |
146 int segmentsSize = segments.size (); | |
147 pluginDirs = new String [segmentsSize + 2]; | |
148 for (index = 0; index < segmentsSize; index++) { | |
149 pluginDirs[index] = (String)segments.elementAt (index); | |
150 } | |
151 } | |
152 } | |
153 if (pluginDirs is null) { | |
154 pluginDirs = new String[2]; | |
155 } | |
156 | |
157 /* set the next value to the GRE path + "plugins" */ | |
158 pluginDirs[index++] = mozillaPath + PLUGINS_DIR; | |
159 | |
160 /* set the next value to the home directory + "/.mozilla/plugins" */ | |
161 pluginDirs[index++] = System.getProperty("user.home") + SEPARATOR_OS + USER_PLUGINS_DIR; | |
162 } | |
163 propertyValues = pluginDirs; | |
164 } | |
165 | |
166 XPCOM.memmove(_retval, new int /*long*/[] {0}, C.PTR_SIZEOF); | |
167 if (propertyValues !is null) { | |
168 int /*long*/[] result = new int /*long*/[1]; | |
169 nsISupports[] files = new nsISupports [propertyValues.length]; | |
170 int index = 0; | |
171 for (int i = 0; i < propertyValues.length; i++) { | |
172 nsEmbedString pathString = new nsEmbedString (propertyValues[i]); | |
173 int rc = XPCOM.NS_NewLocalFile (pathString.getAddress (), 1, result); | |
174 if (rc !is XPCOM.NS_ERROR_FILE_UNRECOGNIZED_PATH) { | |
175 /* value appears to be a valid pathname */ | |
176 if (rc !is XPCOM.NS_OK) Mozilla.error (rc); | |
177 if (result[0] is 0) Mozilla.error (XPCOM.NS_ERROR_NULL_POINTER); | |
178 | |
179 nsILocalFile localFile = new nsILocalFile (result[0]); | |
180 result[0] = 0; | |
181 rc = localFile.QueryInterface (nsIFile.NS_IFILE_IID, result); | |
182 if (rc !is XPCOM.NS_OK) Mozilla.error (rc); | |
183 if (result[0] is 0) Mozilla.error (XPCOM.NS_ERROR_NO_INTERFACE); | |
184 localFile.Release (); | |
185 | |
186 nsIFile file = new nsIFile (result[0]); | |
187 files[index++] = file; | |
188 } | |
189 pathString.dispose (); | |
190 result[0] = 0; | |
191 } | |
192 | |
193 if (index < propertyValues.length) { | |
194 /* there were some invalid values so remove the trailing empty array slots */ | |
195 nsISupports[] temp = new nsISupports [index]; | |
196 System.arraycopy (files, 0, temp, 0, index); | |
197 files = temp; | |
198 } | |
199 | |
200 SimpleEnumerator enumerator = new SimpleEnumerator (files); | |
201 enumerator.AddRef (); | |
202 XPCOM.memmove (_retval, new int /*long*/[] {enumerator.getAddress ()}, C.PTR_SIZEOF); | |
203 return XPCOM.NS_OK; | |
204 } | |
205 | |
206 return XPCOM.NS_ERROR_FAILURE; | |
207 } | |
208 | |
209 /* nsIDirectoryServiceProvider implementation */ | |
210 | |
211 int getFile(int /*long*/ prop, int /*long*/ persistent, int /*long*/ _retval) { | |
212 int size = XPCOM.strlen (prop); | |
213 byte[] bytes = new byte[size]; | |
214 XPCOM.memmove (bytes, prop, size); | |
215 String propertyName = new String (MozillaDelegate.mbcsToWcs (null, bytes)); | |
216 String propertyValue = null; | |
217 | |
218 if (propertyName.equals (XPCOM.NS_APP_HISTORY_50_FILE)) { | |
219 propertyValue = profilePath + HISTORY_FILE; | |
220 } else if (propertyName.equals (XPCOM.NS_APP_USER_MIMETYPES_50_FILE)) { | |
221 propertyValue = profilePath + MIMETYPES_FILE; | |
222 } else if (propertyName.equals (XPCOM.NS_APP_PREFS_50_FILE)) { | |
223 propertyValue = profilePath + PREFERENCES_FILE; | |
224 } else if (propertyName.equals (XPCOM.NS_APP_PREFS_50_DIR)) { | |
225 propertyValue = profilePath; | |
226 } else if (propertyName.equals (XPCOM.NS_APP_USER_CHROME_DIR)) { | |
227 propertyValue = profilePath + CHROME_DIR; | |
228 } else if (propertyName.equals (XPCOM.NS_APP_USER_PROFILE_50_DIR)) { | |
229 propertyValue = profilePath; | |
230 } else if (propertyName.equals (XPCOM.NS_APP_LOCALSTORE_50_FILE)) { | |
231 propertyValue = profilePath + LOCALSTORE_FILE; | |
232 } else if (propertyName.equals (XPCOM.NS_APP_CACHE_PARENT_DIR)) { | |
233 propertyValue = profilePath; | |
234 } else if (propertyName.equals (XPCOM.NS_OS_HOME_DIR)) { | |
235 propertyValue = System.getProperty("user.home"); //$NON-NLS-1$ | |
236 } else if (propertyName.equals (XPCOM.NS_OS_TEMP_DIR)) { | |
237 propertyValue = System.getProperty("java.io.tmpdir"); //$NON-NLS-1$ | |
238 } else if (propertyName.equals (XPCOM.NS_GRE_DIR)) { | |
239 propertyValue = mozillaPath; | |
240 } else if (propertyName.equals (XPCOM.NS_GRE_COMPONENT_DIR)) { | |
241 propertyValue = mozillaPath + COMPONENTS_DIR; | |
242 } else if (propertyName.equals (XPCOM.NS_XPCOM_INIT_CURRENT_PROCESS_DIR)) { | |
243 propertyValue = mozillaPath; | |
244 } else if (propertyName.equals (XPCOM.NS_OS_CURRENT_PROCESS_DIR)) { | |
245 propertyValue = mozillaPath; | |
246 } else if (propertyName.equals (XPCOM.NS_XPCOM_COMPONENT_DIR)) { | |
247 propertyValue = mozillaPath + COMPONENTS_DIR; | |
248 } else if (propertyName.equals (XPCOM.NS_XPCOM_CURRENT_PROCESS_DIR)) { | |
249 propertyValue = mozillaPath; | |
250 } else if (propertyName.equals (XPCOM.NS_APP_PREF_DEFAULTS_50_DIR)) { | |
251 /* | |
252 * Answering a value for this property causes problems in Mozilla versions | |
253 * < 1.7. Unfortunately this property is queried early enough in the | |
254 * Browser creation process that the Mozilla version being used is not | |
255 * yet determined. However it is known if XULRunner is being used or not. | |
256 * | |
257 * For now answer a value for this property iff XULRunner is the GRE. | |
258 * If the range of Mozilla versions supported by the Browser is changed | |
259 * in the future to be >= 1.7 then this value can always be answered. | |
260 */ | |
261 if (isXULRunner) propertyValue = profilePath; | |
262 } | |
263 | |
264 XPCOM.memmove (persistent, new int[] {1}, 4); /* PRBool */ | |
265 XPCOM.memmove (_retval, new int /*long*/[] {0}, C.PTR_SIZEOF); | |
266 if (propertyValue !is null && propertyValue.length () > 0) { | |
267 int /*long*/[] result = new int /*long*/[1]; | |
268 nsEmbedString pathString = new nsEmbedString (propertyValue); | |
269 int rc = XPCOM.NS_NewLocalFile (pathString.getAddress (), 1, result); | |
270 if (rc !is XPCOM.NS_OK) Mozilla.error (rc); | |
271 if (result[0] is 0) Mozilla.error (XPCOM.NS_ERROR_NULL_POINTER); | |
272 pathString.dispose (); | |
273 | |
274 nsILocalFile localFile = new nsILocalFile (result [0]); | |
275 result[0] = 0; | |
276 rc = localFile.QueryInterface (nsIFile.NS_IFILE_IID, result); | |
277 if (rc !is XPCOM.NS_OK) Mozilla.error (rc); | |
278 if (result[0] is 0) Mozilla.error (XPCOM.NS_ERROR_NO_INTERFACE); | |
279 | |
280 XPCOM.memmove (_retval, new int /*long*/[] {result[0]}, C.PTR_SIZEOF); | |
281 localFile.Release (); | |
282 return XPCOM.NS_OK; | |
283 } | |
284 | |
285 return XPCOM.NS_ERROR_FAILURE; | |
286 } | |
287 } |