comparison dwt/browser/HelperAppLauncherDialog.d @ 278:93409d9838c5

Commit more browser/xpcom updates, including still uncoverted source.
author John Reimer<terminal.node@gmail.com>
date Thu, 31 Jul 2008 19:17:51 -0700
parents
children eec6ddb07873
comparison
equal deleted inserted replaced
277:687f261028b8 278:93409d9838c5
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 module dwt.browser.HelperAppLauncherDialog;
12
13 import dwt.dwthelper.utils;
14
15 import dwt.DWT;
16 import dwt.internal.C;
17 import dwt.internal.mozilla.XPCOM;
18 import dwt.internal.mozilla.XPCOMObject;
19 import dwt.internal.mozilla.nsEmbedString;
20 import dwt.internal.mozilla.nsID;
21 import dwt.internal.mozilla.nsIHelperAppLauncher;
22 import dwt.internal.mozilla.nsIHelperAppLauncherDialog;
23 import dwt.internal.mozilla.nsIHelperAppLauncher_1_8;
24 import dwt.internal.mozilla.nsIHelperAppLauncher_1_9;
25 import dwt.internal.mozilla.nsISupports;
26 import dwt.widgets.FileDialog;
27 import dwt.widgets.Shell;
28
29 class HelperAppLauncherDialog {
30 XPCOMObject supports;
31 XPCOMObject helperAppLauncherDialog;
32 int refCount = 0;
33
34 HelperAppLauncherDialog () {
35 createCOMInterfaces ();
36 }
37
38 int AddRef () {
39 refCount++;
40 return refCount;
41 }
42
43 void createCOMInterfaces () {
44 /* Create each of the interfaces that this object implements */
45 supports = new XPCOMObject (new int[] {2, 0, 0}) {
46 public int /*long*/ method0 (int /*long*/[] args) {return QueryInterface (args[0], args[1]);}
47 public int /*long*/ method1 (int /*long*/[] args) {return AddRef ();}
48 public int /*long*/ method2 (int /*long*/[] args) {return Release ();}
49 };
50
51 helperAppLauncherDialog = new XPCOMObject (new int[] {2, 0, 0, 3, 5}) {
52 public int /*long*/ method0 (int /*long*/[] args) {return QueryInterface (args[0], args[1]);}
53 public int /*long*/ method1 (int /*long*/[] args) {return AddRef ();}
54 public int /*long*/ method2 (int /*long*/[] args) {return Release ();}
55 public int /*long*/ method3 (int /*long*/[] args) {return Show (args[0], args[1], (int)/*64*/args[2]);}
56 public int /*long*/ method4 (int /*long*/[] args) {return PromptForSaveToFile (args[0], args[1], args[2], args[3], args[4]);}
57 };
58 }
59
60 void disposeCOMInterfaces () {
61 if (supports !is null) {
62 supports.dispose ();
63 supports = null;
64 }
65 if (helperAppLauncherDialog !is null) {
66 helperAppLauncherDialog.dispose ();
67 helperAppLauncherDialog = null;
68 }
69 }
70
71 int /*long*/ getAddress () {
72 return helperAppLauncherDialog.getAddress ();
73 }
74
75 int QueryInterface (int /*long*/ riid, int /*long*/ ppvObject) {
76 if (riid is 0 || ppvObject is 0) return XPCOM.NS_ERROR_NO_INTERFACE;
77 nsID guid = new nsID ();
78 XPCOM.memmove (guid, riid, nsID.sizeof);
79
80 if (guid.Equals (nsISupports.NS_ISUPPORTS_IID)) {
81 XPCOM.memmove (ppvObject, new int /*long*/[] {supports.getAddress ()}, C.PTR_SIZEOF);
82 AddRef ();
83 return XPCOM.NS_OK;
84 }
85 if (guid.Equals (nsIHelperAppLauncherDialog.NS_IHELPERAPPLAUNCHERDIALOG_IID)) {
86 XPCOM.memmove (ppvObject, new int /*long*/[] {helperAppLauncherDialog.getAddress ()}, C.PTR_SIZEOF);
87 AddRef ();
88 return XPCOM.NS_OK;
89 }
90
91 XPCOM.memmove (ppvObject, new int /*long*/[] {0}, C.PTR_SIZEOF);
92 return XPCOM.NS_ERROR_NO_INTERFACE;
93 }
94
95 int Release () {
96 refCount--;
97 /*
98 * 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
100 * has completed or when it has been cancelled. E.g. when the user
101 * cancels the File Dialog, cancels or closes the Download Dialog
102 * and when the Download Dialog goes away after the download is completed.
103 */
104 if (refCount is 0) disposeCOMInterfaces ();
105 return refCount;
106 }
107
108 /* nsIHelperAppLauncherDialog */
109
110 int Show (int /*long*/ aLauncher, int /*long*/ aContext, int aReason) {
111 /*
112 * 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.
114 */
115 nsISupports supports = new nsISupports (aLauncher);
116 int /*long*/[] result = new int /*long*/[1];
117 int rc = supports.QueryInterface (nsIHelperAppLauncher_1_9.NS_IHELPERAPPLAUNCHER_IID, result);
118 if (rc is 0) {
119 nsIHelperAppLauncher_1_9 helperAppLauncher = new nsIHelperAppLauncher_1_9 (aLauncher);
120 rc = helperAppLauncher.SaveToDisk (0, 0);
121 helperAppLauncher.Release ();
122 return rc;
123 }
124
125 result[0] = 0;
126 rc = supports.QueryInterface (nsIHelperAppLauncher_1_8.NS_IHELPERAPPLAUNCHER_IID, result);
127 if (rc is 0) {
128 nsIHelperAppLauncher_1_8 helperAppLauncher = new nsIHelperAppLauncher_1_8 (aLauncher);
129 rc = helperAppLauncher.SaveToDisk (0, 0);
130 helperAppLauncher.Release ();
131 return rc;
132 }
133
134 nsIHelperAppLauncher helperAppLauncher = new nsIHelperAppLauncher (aLauncher); /* < 1.8 */
135 return helperAppLauncher.SaveToDisk (0, 0);
136 }
137
138 int PromptForSaveToFile (int /*long*/ arg0, int /*long*/ arg1, int /*long*/ arg2, int /*long*/ arg3, int /*long*/ arg4) {
139 int /*long*/ aDefaultFile, aSuggestedFileExtension, _retval;
140 bool hasLauncher = false;
141
142 /*
143 * 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
145 * subsequent arguments shifting right). The workaround is to provide an XPCOMObject
146 * that fits the newer API, and to use the first argument's type to infer whether
147 * the old or new nsIHelperAppLauncherDialog interface is being used (and by extension
148 * the ordering of the arguments). In mozilla >= 1.5 the first argument is an
149 * nsIHelperAppLauncher.
150 */
151 /*
152 * 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.
154 */
155 bool using_1_8 = false, using_1_9 = false;
156 nsISupports support = new nsISupports (arg0);
157 int /*long*/[] result = new int /*long*/[1];
158 int rc = support.QueryInterface (nsIHelperAppLauncher_1_8.NS_IHELPERAPPLAUNCHER_IID, result);
159 if (rc is 0) {
160 using_1_8 = true;
161 hasLauncher = true;
162 new nsISupports (result[0]).Release ();
163 } else {
164 result[0] = 0;
165 rc = support.QueryInterface (nsIHelperAppLauncher_1_9.NS_IHELPERAPPLAUNCHER_IID, result);
166 if (rc is 0) {
167 using_1_9 = true;
168 hasLauncher = true;
169 new nsISupports (result[0]).Release ();
170 } else {
171 result[0] = 0;
172 rc = support.QueryInterface (nsIHelperAppLauncher.NS_IHELPERAPPLAUNCHER_IID, result);
173 if (rc is 0) {
174 hasLauncher = true;
175 new nsISupports (result[0]).Release ();
176 }
177 }
178 }
179 result[0] = 0;
180
181 if (hasLauncher) { /* >= 1.5 */
182 aDefaultFile = arg2;
183 aSuggestedFileExtension = arg3;
184 _retval = arg4;
185 } else { /* 1.4 */
186 aDefaultFile = arg1;
187 aSuggestedFileExtension = arg2;
188 _retval = arg3;
189 }
190
191 int length = XPCOM.strlen_PRUnichar (aDefaultFile);
192 char[] dest = new char[length];
193 XPCOM.memmove (dest, aDefaultFile, length * 2);
194 String defaultFile = new String (dest);
195
196 length = XPCOM.strlen_PRUnichar (aSuggestedFileExtension);
197 dest = new char[length];
198 XPCOM.memmove (dest, aSuggestedFileExtension, length * 2);
199 String suggestedFileExtension = new String (dest);
200
201 Shell shell = new Shell ();
202 FileDialog fileDialog = new FileDialog (shell, DWT.SAVE);
203 fileDialog.setFileName (defaultFile);
204 fileDialog.setFilterExtensions (new String[] {suggestedFileExtension});
205 String name = fileDialog.open ();
206 shell.close ();
207 if (name is null) {
208 if (hasLauncher) {
209 if (using_1_8) {
210 nsIHelperAppLauncher_1_8 launcher = new nsIHelperAppLauncher_1_8 (arg0);
211 rc = launcher.Cancel (XPCOM.NS_BINDING_ABORTED);
212 } else if (using_1_9) {
213 nsIHelperAppLauncher_1_9 launcher = new nsIHelperAppLauncher_1_9 (arg0);
214 rc = launcher.Cancel (XPCOM.NS_BINDING_ABORTED);
215 } else {
216 nsIHelperAppLauncher launcher = new nsIHelperAppLauncher (arg0);
217 rc = launcher.Cancel ();
218 }
219 if (rc !is XPCOM.NS_OK) Mozilla.error (rc);
220 return XPCOM.NS_OK;
221 }
222 return XPCOM.NS_ERROR_FAILURE;
223 }
224 nsEmbedString path = new nsEmbedString (name);
225 rc = XPCOM.NS_NewLocalFile (path.getAddress (), 1, result);
226 path.dispose ();
227 if (rc !is XPCOM.NS_OK) Mozilla.error (rc);
228 if (result[0] is 0) Mozilla.error (XPCOM.NS_ERROR_NULL_POINTER);
229 /* Our own nsIDownload has been registered during the Browser initialization. It will be invoked by Mozilla. */
230 XPCOM.memmove (_retval, result, C.PTR_SIZEOF);
231 return XPCOM.NS_OK;
232 }
233 }