comparison dwt/browser/HelperAppLauncherDialog.d @ 298:eec6ddb07873

More xpcom/mozilla port
author John Reimer<terminal.node@gmail.com>
date Sun, 10 Aug 2008 22:25:43 -0700
parents 93409d9838c5
children 942da4b6558a
comparison
equal deleted inserted replaced
297:2f204a4aebc6 298:eec6ddb07873
5 * which accompanies this distribution, and is available at 5 * which accompanies this distribution, and is available at
6 * http://www.eclipse.org/legal/epl-v10.html 6 * http://www.eclipse.org/legal/epl-v10.html
7 * 7 *
8 * Contributors: 8 * Contributors:
9 * IBM Corporation - initial API and implementation 9 * IBM Corporation - initial API and implementation
10 * Port to the D programming language:
11 * John Reimer <terminal.node@gmail.com>
10 *******************************************************************************/ 12 *******************************************************************************/
11 module dwt.browser.HelperAppLauncherDialog; 13 module dwt.browser.HelperAppLauncherDialog;
12 14
13 import dwt.dwthelper.utils; 15 import dwt.dwthelper.utils;
14 16
17 import Utf = tango.text.convert.Utf;
18
15 import dwt.DWT; 19 import dwt.DWT;
16 import dwt.internal.C; 20 //import dwt.internal.C;
17 import dwt.internal.mozilla.XPCOM; 21 import dwt.internal.mozilla.XPCOM;
18 import dwt.internal.mozilla.XPCOMObject; 22 //import dwt.internal.mozilla.XPCOMObject;
19 import dwt.internal.mozilla.nsEmbedString; 23 import dwt.internal.mozilla.nsEmbedString;
20 import dwt.internal.mozilla.nsID; 24 import dwt.internal.mozilla.nsID;
21 import dwt.internal.mozilla.nsIHelperAppLauncher; 25 import dwt.internal.mozilla.nsIHelperAppLauncher;
22 import dwt.internal.mozilla.nsIHelperAppLauncherDialog; 26 import dwt.internal.mozilla.nsIHelperAppLauncherDialog;
23 import dwt.internal.mozilla.nsIHelperAppLauncher_1_8; 27 import dwt.internal.mozilla.nsIHelperAppLauncher_1_8;
24 import dwt.internal.mozilla.nsIHelperAppLauncher_1_9; 28 import dwt.internal.mozilla.nsIHelperAppLauncher_1_9;
25 import dwt.internal.mozilla.nsISupports; 29 import dwt.internal.mozilla.nsISupports;
26 import dwt.widgets.FileDialog; 30 import dwt.widgets.FileDialog;
27 import dwt.widgets.Shell; 31 import dwt.widgets.Shell;
28 32
29 class HelperAppLauncherDialog { 33 class HelperAppLauncherDialog : nsHelperAppLauncherDialog {
30 XPCOMObject supports; 34 //XPCOMObject supports;
31 XPCOMObject helperAppLauncherDialog; 35 //XPCOMObject helperAppLauncherDialog;
32 int refCount = 0; 36 int refCount = 0;
33 37
34 HelperAppLauncherDialog () { 38 this() {
35 createCOMInterfaces (); 39 //createCOMInterfaces ();
36 } 40 }
37 41
38 int AddRef () { 42 nsrefcnt AddRef () {
39 refCount++; 43 refCount++;
40 return refCount; 44 return refCount;
41 } 45 }
42 46 /+
43 void createCOMInterfaces () { 47 void createCOMInterfaces () {
44 /* Create each of the interfaces that this object implements */ 48 /* Create each of the interfaces that this object implements */
45 supports = new XPCOMObject (new int[] {2, 0, 0}) { 49 supports = new XPCOMObject (new int[] {2, 0, 0}) {
46 public int /*long*/ method0 (int /*long*/[] args) {return QueryInterface (args[0], args[1]);} 50 public int /*long*/ method0 (int /*long*/[] args) {return QueryInterface (args[0], args[1]);}
47 public int /*long*/ method1 (int /*long*/[] args) {return AddRef ();} 51 public int /*long*/ method1 (int /*long*/[] args) {return AddRef ();}
69 } 73 }
70 74
71 int /*long*/ getAddress () { 75 int /*long*/ getAddress () {
72 return helperAppLauncherDialog.getAddress (); 76 return helperAppLauncherDialog.getAddress ();
73 } 77 }
74 78 +/
75 int QueryInterface (int /*long*/ riid, int /*long*/ ppvObject) { 79 nsresult QueryInterface (nsID* riid, void** ppvObject) {
76 if (riid is 0 || ppvObject is 0) return XPCOM.NS_ERROR_NO_INTERFACE; 80 if (riid is null || ppvObject is null) return XPCOM.NS_ERROR_NO_INTERFACE;
77 nsID guid = new nsID (); 81 //nsID guid = new nsID ();
78 XPCOM.memmove (guid, riid, nsID.sizeof); 82 //XPCOM.memmove (guid, riid, nsID.sizeof);
79 83
80 if (guid.Equals (nsISupports.NS_ISUPPORTS_IID)) { 84 if (*riid == nsISupports.IID) {
81 XPCOM.memmove (ppvObject, new int /*long*/[] {supports.getAddress ()}, C.PTR_SIZEOF); 85 *ppvObject = cast(void*)cast(nsISupports)this;
82 AddRef (); 86 AddRef ();
83 return XPCOM.NS_OK; 87 return XPCOM.NS_OK;
84 } 88 }
85 if (guid.Equals (nsIHelperAppLauncherDialog.NS_IHELPERAPPLAUNCHERDIALOG_IID)) { 89 if (*riid == nsIHelperAppLauncherDialog.IID) {
86 XPCOM.memmove (ppvObject, new int /*long*/[] {helperAppLauncherDialog.getAddress ()}, C.PTR_SIZEOF); 90 *ppvObject = cast(void*)cast(nsIHelperAppLauncherDialog)this;
87 AddRef (); 91 AddRef ();
88 return XPCOM.NS_OK; 92 return XPCOM.NS_OK;
89 } 93 }
90 94
91 XPCOM.memmove (ppvObject, new int /*long*/[] {0}, C.PTR_SIZEOF); 95 *ppvObject = null;
92 return XPCOM.NS_ERROR_NO_INTERFACE; 96 return XPCOM.NS_ERROR_NO_INTERFACE;
93 } 97 }
94 98
95 int Release () { 99 nsrefcnt Release () {
96 refCount--; 100 refCount--;
97 /* 101 /*
98 * Note. This instance lives as long as the download it is binded to. 102 * Note. This instance lives as long as the download it is binded to.
99 * Its reference count is expected to go down to 0 when the download 103 * Its reference count is expected to go down to 0 when the download
100 * has completed or when it has been cancelled. E.g. when the user 104 * has completed or when it has been cancelled. E.g. when the user
101 * cancels the File Dialog, cancels or closes the Download Dialog 105 * cancels the File Dialog, cancels or closes the Download Dialog
102 * and when the Download Dialog goes away after the download is completed. 106 * and when the Download Dialog goes away after the download is completed.
103 */ 107 */
104 if (refCount is 0) disposeCOMInterfaces (); 108 //if (refCount is 0) disposeCOMInterfaces ();
105 return refCount; 109 return refCount;
106 } 110 }
107 111
108 /* nsIHelperAppLauncherDialog */ 112 /* nsIHelperAppLauncherDialog */
109 113
110 int Show (int /*long*/ aLauncher, int /*long*/ aContext, int aReason) { 114 nsresult Show (nsIHelperAppLauncher aLauncher, nsISupports aContext, PRUint32 aReason) {
111 /* 115 /*
112 * The interface for nsIHelperAppLauncher changed in GRE versions 1.8 and 1.9. Query for 116 * The interface for nsIHelperAppLauncher changed in GRE versions 1.8 and 1.9. Query for
113 * each of these interfaces in turn until one is found. 117 * each of these interfaces in turn until one is found.
114 */ 118 */
115 nsISupports supports = new nsISupports (aLauncher); 119 // FIXME: Not sure if this will work because of different versions of
116 int /*long*/[] result = new int /*long*/[1]; 120 // nsIHelperAppLauncher. Remember to check. -JJR
117 int rc = supports.QueryInterface (nsIHelperAppLauncher_1_9.NS_IHELPERAPPLAUNCHER_IID, result); 121 nsIHelperAppLauncher_1_9 helperAppLauncher1;
122 int rc = aLauncher.QueryInterface (nsIHelperAppLauncher_1_9.IID, cast(void*)&helperAppLauncher1);
118 if (rc is 0) { 123 if (rc is 0) {
119 nsIHelperAppLauncher_1_9 helperAppLauncher = new nsIHelperAppLauncher_1_9 (aLauncher); 124 // nsIHelperAppLauncher_1_9 helperAppLauncher = new nsIHelperAppLauncher_1_9 (aLauncher);
120 rc = helperAppLauncher.SaveToDisk (0, 0); 125 rc = helperAppLauncher1.SaveToDisk (0, 0);
121 helperAppLauncher.Release (); 126 helperAppLauncher1.Release ();
122 return rc; 127 return rc;
123 } 128 }
124 129
125 result[0] = 0; 130 //result[0] = 0;
126 rc = supports.QueryInterface (nsIHelperAppLauncher_1_8.NS_IHELPERAPPLAUNCHER_IID, result); 131 nsIHelperAppLauncher_1_8 helperAppLauncher2;
132 rc = aLauncher.QueryInterface (nsIHelperAppLauncher_1_8.IID, cast(void*)&helperAppLauncher2);
127 if (rc is 0) { 133 if (rc is 0) {
128 nsIHelperAppLauncher_1_8 helperAppLauncher = new nsIHelperAppLauncher_1_8 (aLauncher); 134 //nsIHelperAppLauncher_1_8 helperAppLauncher = new nsIHelperAppLauncher_1_8 (aLauncher);
129 rc = helperAppLauncher.SaveToDisk (0, 0); 135 rc = helperAppLauncher2.SaveToDisk (0, 0);
130 helperAppLauncher.Release (); 136 helperAppLauncher2.Release ();
131 return rc; 137 return rc;
132 } 138 }
133 139
134 nsIHelperAppLauncher helperAppLauncher = new nsIHelperAppLauncher (aLauncher); /* < 1.8 */ 140 //nsIHelperAppLauncher helperAppLauncher = new nsIHelperAppLauncher (aLauncher); /* < 1.8 */
135 return helperAppLauncher.SaveToDisk (0, 0); 141 return aLauncher.SaveToDisk (0, 0);
136 } 142 }
137 143
138 int PromptForSaveToFile (int /*long*/ arg0, int /*long*/ arg1, int /*long*/ arg2, int /*long*/ arg3, int /*long*/ arg4) { 144 int PromptForSaveToFile (nsIHelperAppLauncher arg0, nsISupports arg1, PRUnichar* arg2, PRUnichar* arg3, nsILocalFile* arg4) {
139 int /*long*/ aDefaultFile, aSuggestedFileExtension, _retval; 145 PRUnichar* aDefaultFile;
146 PRUnichar* aSuggestedFileExtension;
147 nsILocalFile* _retval;
140 bool hasLauncher = false; 148 bool hasLauncher = false;
141 149
142 /* 150 /*
143 * The interface for nsIHelperAppLauncherDialog changed as of mozilla 1.5 when an 151 * The interface for nsIHelperAppLauncherDialog changed as of mozilla 1.5 when an
144 * extra argument was added to the PromptForSaveToFile method (this resulted in all 152 * extra argument was added to the PromptForSaveToFile method (this resulted in all
151 /* 159 /*
152 * The interface for nsIHelperAppLauncher changed as of mozilla 1.8, so the first 160 * The interface for nsIHelperAppLauncher changed as of mozilla 1.8, so the first
153 * argument must be queried for both the old and new nsIHelperAppLauncher interfaces. 161 * argument must be queried for both the old and new nsIHelperAppLauncher interfaces.
154 */ 162 */
155 bool using_1_8 = false, using_1_9 = false; 163 bool using_1_8 = false, using_1_9 = false;
156 nsISupports support = new nsISupports (arg0); 164 // technically this should have an AddRef? -JJR
157 int /*long*/[] result = new int /*long*/[1]; 165 nsISupports support = cast(nsISupports)arg0;
158 int rc = support.QueryInterface (nsIHelperAppLauncher_1_8.NS_IHELPERAPPLAUNCHER_IID, result); 166 //int /*long*/[] result = new int /*long*/[1];
167 if (arg0 is null)
168 assert(0);
169
170 nsIHelperAppLauncher_1_8 helperAppLauncher1;
171 int rc = support.QueryInterface (nsIHelperAppLauncher_1_8.IID, cast(void*)&helperAppLauncher1);
159 if (rc is 0) { 172 if (rc is 0) {
160 using_1_8 = true; 173 using_1_8 = true;
161 hasLauncher = true; 174 hasLauncher = true;
162 new nsISupports (result[0]).Release (); 175 helperAppLauncher1.Release ();
163 } else { 176 } else {
164 result[0] = 0; 177 nsIHelperAppLauncher_1_9 helperAppLauncher2;
165 rc = support.QueryInterface (nsIHelperAppLauncher_1_9.NS_IHELPERAPPLAUNCHER_IID, result); 178 rc = support.QueryInterface (nsIHelperAppLauncher_1_9.IID, cast(void*)&helperAppLauncher2);
166 if (rc is 0) { 179 if (rc is 0) {
167 using_1_9 = true; 180 using_1_9 = true;
168 hasLauncher = true; 181 hasLauncher = true;
169 new nsISupports (result[0]).Release (); 182 helperAppLauncher2.Release;
170 } else { 183 } else {
171 result[0] = 0; 184 nsIHelperAppLauncher helperAppLauncher;
172 rc = support.QueryInterface (nsIHelperAppLauncher.NS_IHELPERAPPLAUNCHER_IID, result); 185 rc = support.QueryInterface (nsIHelperAppLauncher.IID, cast(void*)&helperAppLauncher);
173 if (rc is 0) { 186 if (rc is 0) {
174 hasLauncher = true; 187 hasLauncher = true;
175 new nsISupports (result[0]).Release (); 188 helperAppLauncher.Release;
176 } 189 }
177 } 190 }
178 } 191 }
179 result[0] = 0; 192 //result[0] = 0;
180 193
181 if (hasLauncher) { /* >= 1.5 */ 194 if (hasLauncher) { /* >= 1.5 */
182 aDefaultFile = arg2; 195 aDefaultFile = arg2;
183 aSuggestedFileExtension = arg3; 196 aSuggestedFileExtension = arg3;
184 _retval = arg4; 197 _retval = arg4;
185 } else { /* 1.4 */ 198 } else { /* 1.4 */
199 // This call conversion probablywon't work for non-Java
200 // and shouldn't get called; fix it later. -JJR
186 aDefaultFile = arg1; 201 aDefaultFile = arg1;
187 aSuggestedFileExtension = arg2; 202 aSuggestedFileExtension = arg2;
188 _retval = arg3; 203 _retval = arg3;
189 } 204 }
190 205
191 int length = XPCOM.strlen_PRUnichar (aDefaultFile); 206 int span = XPCOM.strlen_PRUnichar (aDefaultFile);
192 char[] dest = new char[length]; 207 // XPCOM.memmove (dest, aDefaultFile, length * 2);
193 XPCOM.memmove (dest, aDefaultFile, length * 2); 208 String defaultFile = Utf.toString (aDefaultFile[0 .. span]));
194 String defaultFile = new String (dest); 209
195 210 span = XPCOM.strlen_PRUnichar (aSuggestedFileExtension);
196 length = XPCOM.strlen_PRUnichar (aSuggestedFileExtension); 211 //dest = new char[length];
197 dest = new char[length]; 212 //XPCOM.memmove (dest, aSuggestedFileExtension, length * 2);
198 XPCOM.memmove (dest, aSuggestedFileExtension, length * 2); 213 String suggestedFileExtension = Utf.toString (aSuggestedFileExtension[0 .. span]);
199 String suggestedFileExtension = new String (dest);
200 214
201 Shell shell = new Shell (); 215 Shell shell = new Shell ();
202 FileDialog fileDialog = new FileDialog (shell, DWT.SAVE); 216 FileDialog fileDialog = new FileDialog (shell, DWT.SAVE);
203 fileDialog.setFileName (defaultFile); 217 fileDialog.setFileName (defaultFile);
204 fileDialog.setFilterExtensions (new String[] {suggestedFileExtension}); 218 String[] tmp ~= suggestedFileExtension;
219 fileDialog.setFilterExtensions (tmp);
205 String name = fileDialog.open (); 220 String name = fileDialog.open ();
206 shell.close (); 221 shell.close ();
207 if (name is null) { 222 if (name is null) {
208 if (hasLauncher) { 223 if (hasLauncher) {
224 // not sure if these produce correct interface alignment. Check. -JJR
209 if (using_1_8) { 225 if (using_1_8) {
210 nsIHelperAppLauncher_1_8 launcher = new nsIHelperAppLauncher_1_8 (arg0); 226 auto launcher = cast(nsIHelperAppLauncher_1_8)cast(void*)arg0;
211 rc = launcher.Cancel (XPCOM.NS_BINDING_ABORTED); 227 rc = launcher.Cancel (XPCOM.NS_BINDING_ABORTED);
212 } else if (using_1_9) { 228 } else if (using_1_9) {
213 nsIHelperAppLauncher_1_9 launcher = new nsIHelperAppLauncher_1_9 (arg0); 229 auto launcher = cast(nsIHelperAppLauncher_1_9)cast(void*)arg0;
214 rc = launcher.Cancel (XPCOM.NS_BINDING_ABORTED); 230 rc = launcher.Cancel (XPCOM.NS_BINDING_ABORTED);
215 } else { 231 } else {
216 nsIHelperAppLauncher launcher = new nsIHelperAppLauncher (arg0); 232 //nsIHelperAppLauncher launcher = new nsIHelperAppLauncher (arg0);
217 rc = launcher.Cancel (); 233 rc = arg0.Cancel ();
218 } 234 }
219 if (rc !is XPCOM.NS_OK) Mozilla.error (rc); 235 if (rc !is XPCOM.NS_OK) Mozilla.error (rc);
220 return XPCOM.NS_OK; 236 return XPCOM.NS_OK;
221 } 237 }
222 return XPCOM.NS_ERROR_FAILURE; 238 return XPCOM.NS_ERROR_FAILURE;
223 } 239 }
224 nsEmbedString path = new nsEmbedString (name); 240 scope auto path = new nsEmbedString (name);
225 rc = XPCOM.NS_NewLocalFile (path.getAddress (), 1, result); 241 nsILocalFile localFile;
226 path.dispose (); 242 rc = XPCOM.NS_NewLocalFile (cast(nsAString*)path, 1, cast(void*)&localFile);
243 //path.dispose ();
227 if (rc !is XPCOM.NS_OK) Mozilla.error (rc); 244 if (rc !is XPCOM.NS_OK) Mozilla.error (rc);
228 if (result[0] is 0) Mozilla.error (XPCOM.NS_ERROR_NULL_POINTER); 245 if (localFile is null) Mozilla.error (XPCOM.NS_ERROR_NULL_POINTER);
229 /* Our own nsIDownload has been registered during the Browser initialization. It will be invoked by Mozilla. */ 246 /* Our own nsIDownload has been registered during the Browser initialization. It will be invoked by Mozilla. */
230 XPCOM.memmove (_retval, result, C.PTR_SIZEOF); 247 *_retval = cast(void*)&localFile;
248 //XPCOM.memmove (_retval, result, C.PTR_SIZEOF);
231 return XPCOM.NS_OK; 249 return XPCOM.NS_OK;
232 } 250 }
233 } 251 }