25
|
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 module org.eclipse.swt.browser.AppFileLocProvider;
|
|
14
|
48
|
15 version(Tango){
|
25
|
16 import tango.sys.Environment;
|
|
17 import tango.text.Util;
|
48
|
18 } else { // Phobos
|
|
19 }
|
25
|
20
|
|
21 import java.lang.all;
|
|
22 import org.eclipse.swt.browser.Mozilla;
|
|
23 import org.eclipse.swt.browser.SimpleEnumerator;
|
|
24
|
|
25 import org.eclipse.swt.internal.Compatibility;
|
|
26
|
|
27 import XPCOM = org.eclipse.swt.internal.mozilla.XPCOM;
|
|
28
|
|
29 import org.eclipse.swt.internal.mozilla.Common;
|
|
30 import org.eclipse.swt.internal.mozilla.nsISimpleEnumerator;
|
|
31 import org.eclipse.swt.internal.mozilla.nsEmbedString;
|
|
32 import org.eclipse.swt.internal.mozilla.nsID;
|
|
33 import org.eclipse.swt.internal.mozilla.nsIDirectoryService;
|
|
34 import org.eclipse.swt.internal.mozilla.nsIFile;
|
|
35 import org.eclipse.swt.internal.mozilla.nsILocalFile;
|
|
36 import org.eclipse.swt.internal.mozilla.nsISupports;
|
|
37 import org.eclipse.swt.internal.mozilla.nsStringAPI;
|
|
38
|
|
39 class AppFileLocProvider : nsIDirectoryServiceProvider2 {
|
|
40 int refCount = 0;
|
|
41 String mozillaPath, profilePath;
|
|
42 String[] pluginDirs;
|
|
43 bool isXULRunner;
|
|
44
|
|
45 static String SEPARATOR_OS;
|
|
46 static final String CHROME_DIR = "chrome"; //$NON-NLS-1$
|
|
47 static final String COMPONENTS_DIR = "components"; //$NON-NLS-1$
|
|
48 static final String HISTORY_FILE = "history.dat"; //$NON-NLS-1$
|
|
49 static final String LOCALSTORE_FILE = "localstore.rdf"; //$NON-NLS-1$
|
|
50 static final String MIMETYPES_FILE = "mimeTypes.rdf"; //$NON-NLS-1$
|
|
51 static final String PLUGINS_DIR = "plugins"; //$NON-NLS-1$
|
|
52 static String USER_PLUGINS_DIR;
|
|
53 static final String PREFERENCES_FILE = "prefs.js"; //$NON-NLS-1$
|
|
54
|
|
55 static this () {
|
|
56 SEPARATOR_OS = System.getProperty ("file.separator");
|
|
57 USER_PLUGINS_DIR = ".mozilla" ~ SEPARATOR_OS ~ "plugins";
|
|
58 }
|
|
59
|
|
60 this (String path) {
|
|
61 mozillaPath = path ~ SEPARATOR_OS;
|
|
62 }
|
|
63
|
|
64 extern(System)
|
|
65 nsrefcnt AddRef () {
|
|
66 refCount++;
|
|
67 return refCount;
|
|
68 }
|
|
69
|
|
70 extern(System)
|
|
71 nsresult QueryInterface (nsID* riid, void** ppvObject) {
|
|
72 if (riid is null || ppvObject is null) return XPCOM.NS_ERROR_NO_INTERFACE;
|
|
73
|
|
74 if (*riid == nsISupports.IID) {
|
|
75 *ppvObject = cast(void*)cast(nsISupports)this;
|
|
76 AddRef ();
|
|
77 return XPCOM.NS_OK;
|
|
78 }
|
|
79 if (*riid == nsIDirectoryServiceProvider.IID) {
|
|
80 *ppvObject = cast(void*)cast(nsIDirectoryServiceProvider)this;
|
|
81 AddRef ();
|
|
82 return XPCOM.NS_OK;
|
|
83 }
|
|
84 if (*riid == nsIDirectoryServiceProvider2.IID) {
|
|
85 *ppvObject = cast(void*)cast(nsIDirectoryServiceProvider2)this;
|
|
86 AddRef ();
|
|
87 return XPCOM.NS_OK;
|
|
88 }
|
|
89
|
|
90 *ppvObject = null;
|
|
91 return XPCOM.NS_ERROR_NO_INTERFACE;
|
|
92 }
|
|
93
|
|
94 extern(System)
|
|
95 nsrefcnt Release () {
|
|
96 refCount--;
|
|
97 if (refCount is 0) return 0;
|
|
98 return refCount;
|
|
99 }
|
|
100
|
|
101 void setProfilePath (String path) {
|
|
102 profilePath = path;
|
|
103 if (!Compatibility.fileExists (path, "")) { //$NON-NLS-1$
|
|
104 nsILocalFile file;
|
|
105 scope auto nsEmbedString pathString = new nsEmbedString (toWCharArray(path));
|
|
106 int rc = XPCOM.NS_NewLocalFile (cast(nsAString*)pathString, 1, &file);
|
|
107 if (rc !is XPCOM.NS_OK) Mozilla.error (rc);
|
|
108 if (file is null) Mozilla.error (XPCOM.NS_ERROR_NULL_POINTER);
|
|
109
|
|
110 rc = file.Create (nsILocalFile.DIRECTORY_TYPE, 0700);
|
|
111 if (rc !is XPCOM.NS_OK) Mozilla.error (rc);
|
|
112 file.Release ();
|
|
113 }
|
|
114 }
|
|
115
|
|
116 /* nsIDirectoryServiceProvider2 */
|
|
117 extern(System)
|
|
118 nsresult GetFiles (char* prop, nsISimpleEnumerator* _retval) {
|
|
119 String propertyName = fromStringz(prop);
|
|
120 String[] propertyValues = null;
|
|
121
|
|
122 if (propertyName == XPCOM.NS_APP_PLUGINS_DIR_LIST) {
|
|
123 if (pluginDirs is null) {
|
|
124 int index = 0;
|
|
125 /* set the first value(s) to the MOZ_PLUGIN_PATH environment variable value if it's defined */
|
|
126 String value = Environment.get (XPCOM.MOZILLA_PLUGIN_PATH);
|
|
127 if (value !is null) {
|
|
128 if (value.length > 0) {
|
|
129 String separator = System.getProperty ("file.separator"); // $NON-NLS-1$
|
|
130 foreach (segment; delimiters(value, separator))
|
|
131 pluginDirs ~= segment;
|
|
132 }
|
|
133 }
|
|
134 if (pluginDirs is null) {
|
|
135 pluginDirs = new String[2];
|
|
136 }
|
|
137
|
|
138 /* set the next value to the GRE path + "plugins" */
|
|
139 pluginDirs ~= mozillaPath ~ PLUGINS_DIR;
|
|
140
|
|
141 /* set the next value to the home directory + "/.mozilla/plugins" */
|
|
142 pluginDirs ~= System.getProperty("user.home") ~ SEPARATOR_OS ~ USER_PLUGINS_DIR;
|
|
143 }
|
|
144 propertyValues = pluginDirs;
|
|
145 }
|
|
146
|
|
147 *_retval = null;
|
|
148 //XPCOM.memmove(_retval, new int /*long*/[] {0}, C.PTR_SIZEOF);
|
|
149 if (propertyValues !is null) {
|
|
150 nsILocalFile localFile;
|
|
151 nsIFile file;
|
|
152 nsISupports[] files = new nsISupports [propertyValues.length];
|
|
153 int index = 0;
|
|
154 for (int i = 0; i < propertyValues.length; i++) {
|
|
155 scope auto pathString = new nsEmbedString (toWCharArray(propertyValues[i]));
|
|
156 int rc = XPCOM.NS_NewLocalFile (cast(nsAString*)pathString, 1, &localFile);
|
|
157 if (rc !is XPCOM.NS_ERROR_FILE_UNRECOGNIZED_PATH) {
|
|
158 /* value appears to be a valid pathname */
|
|
159 if (rc !is XPCOM.NS_OK) Mozilla.error (rc);
|
|
160 if (localFile is null) Mozilla.error (XPCOM.NS_ERROR_NULL_POINTER);
|
|
161
|
|
162 rc = localFile.QueryInterface (&nsIFile.IID, cast(void**)&file);
|
|
163 if (rc !is XPCOM.NS_OK) Mozilla.error (rc);
|
|
164 if (file is null) Mozilla.error (XPCOM.NS_ERROR_NO_INTERFACE);
|
|
165 localFile.Release ();
|
|
166
|
|
167 files[index++] = cast(nsISupports)file;
|
|
168 }
|
|
169 }
|
|
170
|
|
171 if (index < propertyValues.length) {
|
|
172 /* there were some invalid values so remove the trailing empty array slots */
|
|
173 files = files[0..index];
|
|
174 }
|
|
175
|
|
176 auto enumerator = new SimpleEnumerator (files);
|
|
177 enumerator.AddRef ();
|
|
178 *_retval = cast(nsISimpleEnumerator)enumerator;
|
|
179 return XPCOM.NS_OK;
|
|
180 }
|
|
181
|
|
182 return XPCOM.NS_ERROR_FAILURE;
|
|
183 }
|
|
184
|
|
185 /* nsIDirectoryServiceProvider implementation */
|
|
186 extern(System)
|
|
187 nsresult GetFile(char* prop, PRBool* persistent, nsIFile* _retval) {
|
|
188 String propertyName = tango.stdc.stringz.fromStringz( prop );
|
|
189 String propertyValue = null;
|
|
190
|
|
191 if (propertyName == (XPCOM.NS_APP_HISTORY_50_FILE)) {
|
|
192 propertyValue = profilePath ~ HISTORY_FILE;
|
|
193 } else if (propertyName == (XPCOM.NS_APP_USER_MIMETYPES_50_FILE)) {
|
|
194 propertyValue = profilePath ~ MIMETYPES_FILE;
|
|
195 } else if (propertyName == (XPCOM.NS_APP_PREFS_50_FILE)) {
|
|
196 propertyValue = profilePath ~ PREFERENCES_FILE;
|
|
197 } else if (propertyName == (XPCOM.NS_APP_PREFS_50_DIR)) {
|
|
198 propertyValue = profilePath;
|
|
199 } else if (propertyName == (XPCOM.NS_APP_USER_CHROME_DIR)) {
|
|
200 propertyValue = profilePath ~ CHROME_DIR;
|
|
201 } else if (propertyName == (XPCOM.NS_APP_USER_PROFILE_50_DIR)) {
|
|
202 propertyValue = profilePath;
|
|
203 } else if (propertyName == (XPCOM.NS_APP_LOCALSTORE_50_FILE)) {
|
|
204 propertyValue = profilePath ~ LOCALSTORE_FILE;
|
|
205 } else if (propertyName == (XPCOM.NS_APP_CACHE_PARENT_DIR)) {
|
|
206 propertyValue = profilePath;
|
|
207 } else if (propertyName == (XPCOM.NS_OS_HOME_DIR)) {
|
|
208 propertyValue = System.getProperty("user.home"); //$NON-NLS-1$
|
|
209 } else if (propertyName == (XPCOM.NS_OS_TEMP_DIR)) {
|
|
210 propertyValue = System.getProperty("java.io.tmpdir"); //$NON-NLS-1$
|
|
211 } else if (propertyName == (XPCOM.NS_GRE_DIR)) {
|
|
212 propertyValue = mozillaPath;
|
|
213 } else if (propertyName == (XPCOM.NS_GRE_COMPONENT_DIR)) {
|
|
214 propertyValue = mozillaPath ~ COMPONENTS_DIR;
|
|
215 } else if (propertyName == (XPCOM.NS_XPCOM_INIT_CURRENT_PROCESS_DIR)) {
|
|
216 propertyValue = mozillaPath;
|
|
217 } else if (propertyName == (XPCOM.NS_OS_CURRENT_PROCESS_DIR)) {
|
|
218 propertyValue = mozillaPath;
|
|
219 } else if (propertyName == (XPCOM.NS_XPCOM_COMPONENT_DIR)) {
|
|
220 propertyValue = mozillaPath ~ COMPONENTS_DIR;
|
|
221 } else if (propertyName == (XPCOM.NS_XPCOM_CURRENT_PROCESS_DIR)) {
|
|
222 propertyValue = mozillaPath;
|
|
223 } else if (propertyName == (XPCOM.NS_APP_PREF_DEFAULTS_50_DIR)) {
|
|
224 /*
|
|
225 * Answering a value for this property causes problems in Mozilla versions
|
|
226 * < 1.7. Unfortunately this property is queried early enough in the
|
|
227 * Browser creation process that the Mozilla version being used is not
|
|
228 * yet determined. However it is known if XULRunner is being used or not.
|
|
229 *
|
|
230 * For now answer a value for this property iff XULRunner is the GRE.
|
|
231 * If the range of Mozilla versions supported by the Browser is changed
|
|
232 * in the future to be >= 1.7 then this value can always be answered.
|
|
233 */
|
|
234 if (isXULRunner) propertyValue = profilePath;
|
|
235 }
|
|
236
|
|
237 *persistent = true; /* PRBool */
|
|
238 *_retval = null;
|
|
239 if (propertyValue !is null && propertyValue.length > 0) {
|
|
240 nsILocalFile localFile;
|
|
241 scope auto pathString = new nsEmbedString (propertyValue.toWCharArray());
|
|
242 int rc = XPCOM.NS_NewLocalFile (cast(nsAString*)pathString, 1, &localFile);
|
|
243 if (rc !is XPCOM.NS_OK) Mozilla.error (rc);
|
|
244 if (localFile is null) Mozilla.error (XPCOM.NS_ERROR_NULL_POINTER);
|
|
245
|
|
246 nsIFile file;
|
|
247 rc = localFile.QueryInterface (&nsIFile.IID, cast(void**)&file);
|
|
248 if (rc !is XPCOM.NS_OK) Mozilla.error (rc);
|
|
249 if (file is null) Mozilla.error (XPCOM.NS_ERROR_NO_INTERFACE);
|
|
250
|
|
251 *_retval = file;
|
|
252 localFile.Release ();
|
|
253 return XPCOM.NS_OK;
|
|
254 }
|
|
255
|
|
256 return XPCOM.NS_ERROR_FAILURE;
|
|
257 }
|
|
258 }
|