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.HelperAppLauncherDialog;
|
|
14
|
|
15 import java.lang.all;
|
|
16
|
48
|
17 version(Tango){
|
25
|
18 import Utf = tango.text.convert.Utf;
|
48
|
19 } else { // Phobos
|
|
20 }
|
25
|
21
|
|
22 import org.eclipse.swt.SWT;
|
|
23
|
|
24 import XPCOM = org.eclipse.swt.internal.mozilla.XPCOM;
|
|
25
|
|
26 import org.eclipse.swt.internal.mozilla.Common;
|
|
27 import org.eclipse.swt.internal.mozilla.nsEmbedString;
|
|
28 import org.eclipse.swt.internal.mozilla.nsID;
|
|
29 import org.eclipse.swt.internal.mozilla.nsIHelperAppLauncher;
|
|
30 import org.eclipse.swt.internal.mozilla.nsIHelperAppLauncherDialog;
|
|
31 import org.eclipse.swt.internal.mozilla.nsIHelperAppLauncher_1_8;
|
|
32 import org.eclipse.swt.internal.mozilla.nsISupports;
|
|
33 import org.eclipse.swt.internal.mozilla.nsILocalFile;
|
|
34 import org.eclipse.swt.internal.mozilla.nsStringAPI;
|
|
35
|
|
36 import org.eclipse.swt.browser.Mozilla;
|
|
37
|
|
38 import org.eclipse.swt.widgets.FileDialog;
|
|
39 import org.eclipse.swt.widgets.Shell;
|
|
40
|
|
41 /**
|
|
42 * This class implements the HelperAppLauncherDialog interface for mozilla
|
|
43 * versions 1.4 - 1.8.x. For mozilla versions >= 1.9 this interface is
|
|
44 * implemented by class HelperAppLauncherDialog_1_9. HelperAppLauncherDialogFactory
|
|
45 * determines at runtime which of these classes to instantiate.
|
|
46 */
|
|
47
|
|
48 class HelperAppLauncherDialog : nsIHelperAppLauncherDialog {
|
|
49 int refCount = 0;
|
|
50
|
|
51 this() {
|
|
52 }
|
|
53
|
|
54 extern(System)
|
|
55 nsrefcnt AddRef () {
|
|
56 refCount++;
|
|
57 return refCount;
|
|
58 }
|
|
59
|
|
60 extern(System)
|
53
|
61 nsresult QueryInterface (cnsID* riid, void** ppvObject) {
|
25
|
62 if (riid is null || ppvObject is null) return XPCOM.NS_ERROR_NO_INTERFACE;
|
|
63
|
|
64 if (*riid == nsISupports.IID) {
|
|
65 *ppvObject = cast(void*)cast(nsISupports)this;
|
|
66 AddRef ();
|
|
67 return XPCOM.NS_OK;
|
|
68 }
|
|
69 if (*riid == nsIHelperAppLauncherDialog.IID) {
|
|
70 *ppvObject = cast(void*)cast(nsIHelperAppLauncherDialog)this;
|
|
71 AddRef ();
|
|
72 return XPCOM.NS_OK;
|
|
73 }
|
|
74
|
|
75 *ppvObject = null;
|
|
76 return XPCOM.NS_ERROR_NO_INTERFACE;
|
|
77 }
|
|
78
|
|
79 extern(System)
|
|
80 nsrefcnt Release () {
|
|
81 refCount--;
|
|
82 /*
|
|
83 * Note. This instance lives as long as the download it is binded to.
|
|
84 * Its reference count is expected to go down to 0 when the download
|
|
85 * has completed or when it has been cancelled. E.g. when the user
|
|
86 * cancels the File Dialog, cancels or closes the Download Dialog
|
|
87 * and when the Download Dialog goes away after the download is completed.
|
|
88 */
|
|
89 if (refCount is 0) return 0;
|
|
90 return refCount;
|
|
91 }
|
|
92
|
|
93 /* nsIHelperAppLauncherDialog */
|
|
94
|
|
95 extern(System)
|
|
96 nsresult Show(nsIHelperAppLauncher aLauncher, nsISupports aContext, PRUint32 aReason) {
|
|
97 /*
|
|
98 * The interface for nsIHelperAppLauncher changed as of mozilla 1.8. Query the received
|
|
99 * nsIHelperAppLauncher for the new interface, and if it is not found then fall back to
|
|
100 * the old interface.
|
|
101 */
|
|
102
|
|
103 nsISupports supports = cast(nsISupports)aLauncher;
|
|
104 nsIHelperAppLauncher_1_8 helperAppLauncher;
|
|
105 nsresult rc = supports.QueryInterface (&nsIHelperAppLauncher_1_8.IID, cast(void**)&helperAppLauncher);
|
|
106 if (rc is 0) {
|
|
107 rc = helperAppLauncher.SaveToDisk (null, 0);
|
|
108 helperAppLauncher.Release ();
|
|
109 return rc;
|
|
110 }
|
|
111
|
|
112 /* < 1.8 */
|
|
113 return aLauncher.SaveToDisk (null, 0);
|
|
114 // no Release for this? -JJR
|
|
115 }
|
|
116
|
|
117 extern(System)
|
|
118 nsresult PromptForSaveToFile (nsIHelperAppLauncher aLauncher, nsISupports aWindowContext, PRUnichar* aDefaultFile, PRUnichar* aSuggestedFileExtension, nsILocalFile* _retval) {
|
|
119 bool hasLauncher = false;
|
|
120
|
|
121 /*
|
|
122 * The interface for nsIHelperAppLauncherDialog changed as of mozilla 1.5 when an
|
|
123 * extra argument was added to the PromptForSaveToFile method (this resulted in all
|
|
124 * subsequent arguments shifting right). The workaround is to provide an XPCOMObject
|
|
125 * that fits the newer API, and to use the first argument's type to infer whether
|
|
126 * the old or new nsIHelperAppLauncherDialog interface is being used (and by extension
|
|
127 * the ordering of the arguments). In mozilla >= 1.5 the first argument is an
|
|
128 * nsIHelperAppLauncher.
|
|
129 */
|
|
130 /*
|
|
131 * The interface for nsIHelperAppLauncher changed as of mozilla 1.8, so the first
|
|
132 * argument must be queried for both the old and new nsIHelperAppLauncher interfaces.
|
|
133 */
|
|
134 bool using_1_8 = false;
|
|
135 nsISupports support = cast(nsISupports)aLauncher;
|
|
136
|
|
137 if (aLauncher is null)
|
|
138 assert(0);
|
|
139
|
|
140 nsIHelperAppLauncher_1_8 helperAppLauncher1;
|
|
141 int rc = support.QueryInterface (&nsIHelperAppLauncher_1_8.IID, cast(void**)&helperAppLauncher1);
|
|
142 if (rc is XPCOM.NS_OK) {
|
|
143 using_1_8 = true;
|
|
144 hasLauncher = true;
|
|
145 helperAppLauncher1.Release ();
|
|
146 } else {
|
|
147 nsIHelperAppLauncher helperAppLauncher;
|
|
148 rc = support.QueryInterface (&nsIHelperAppLauncher.IID, cast(void**)&helperAppLauncher);
|
|
149 if (rc is XPCOM.NS_OK) {
|
|
150 hasLauncher = true;
|
|
151 helperAppLauncher.Release;
|
|
152 }
|
|
153 }
|
|
154
|
|
155 /+
|
|
156 // In D port, no suppport for version >1.4 yet
|
|
157 if (hasLauncher) { /* >= 1.5 */
|
|
158 aDefaultFile = arg2;
|
|
159 aSuggestedFileExtension = arg3;
|
|
160 _retval = arg4;
|
|
161 } else { /* 1.4 */
|
|
162 // This call conversion probablywon't work for non-Java
|
|
163 // and shouldn't get called; fix it later. -JJR
|
|
164 aDefaultFile = arg1;
|
|
165 aSuggestedFileExtension = arg2;
|
|
166 _retval = arg3;
|
|
167 }
|
|
168 +/
|
|
169 //int span = XPCOM.strlen_PRUnichar (aDefaultFile);
|
|
170 // XPCOM.memmove (dest, aDefaultFile, length * 2);
|
53
|
171 String defaultFile = String_valueOf(fromString16z(aDefaultFile));
|
25
|
172
|
|
173 //span = XPCOM.strlen_PRUnichar (aSuggestedFileExtension);
|
|
174 //dest = new char[length];
|
|
175 //XPCOM.memmove (dest, aSuggestedFileExtension, length * 2);
|
53
|
176 String suggestedFileExtension = String_valueOf(fromString16z(aSuggestedFileExtension));
|
25
|
177
|
|
178 Shell shell = new Shell ();
|
|
179 FileDialog fileDialog = new FileDialog (shell, SWT.SAVE);
|
|
180 fileDialog.setFileName (defaultFile);
|
|
181 String[] tmp;
|
|
182 tmp ~= suggestedFileExtension;
|
|
183 fileDialog.setFilterExtensions (tmp);
|
|
184 String name = fileDialog.open ();
|
|
185 shell.close ();
|
|
186 if (name is null) {
|
|
187 if (hasLauncher) {
|
|
188 if (using_1_8) {
|
|
189 rc = (cast(nsIHelperAppLauncher_1_8)aLauncher).Cancel (XPCOM.NS_BINDING_ABORTED);
|
|
190 } else {
|
|
191 rc = aLauncher.Cancel ();
|
|
192 }
|
|
193 if (rc !is XPCOM.NS_OK) Mozilla.error (rc,__FILE__,__LINE__);
|
|
194 return XPCOM.NS_OK;
|
|
195 }
|
|
196 return XPCOM.NS_ERROR_FAILURE;
|
|
197 }
|
|
198 scope auto path = new nsEmbedString (name.toWCharArray());
|
|
199 nsILocalFile localFile;
|
|
200 rc = XPCOM.NS_NewLocalFile (cast(nsAString*)path, 1, &localFile);
|
|
201 //path.dispose ();
|
|
202 if (rc !is XPCOM.NS_OK) Mozilla.error (rc,__FILE__,__LINE__);
|
|
203 if (localFile is null) Mozilla.error (XPCOM.NS_ERROR_NULL_POINTER,__FILE__,__LINE__);
|
|
204 /* Our own nsIDownload has been registered during the Browser initialization. It will be invoked by Mozilla. */
|
|
205 *_retval = localFile;
|
|
206 //XPCOM.memmove (_retval, result, C.PTR_SIZEOF);
|
|
207 return XPCOM.NS_OK;
|
|
208 }
|
|
209 }
|