Mercurial > projects > dwt-mac
comparison dwt/browser/HelperAppLauncherDialog.d @ 125:5583f8eeee6c
Synced mozilla with dwt-linux
author | Jacob Carlborg <doob@me.com> |
---|---|
date | Fri, 16 Jan 2009 12:49:08 +0100 |
parents | d8635bb48c7c |
children |
comparison
equal
deleted
inserted
replaced
124:540fa4e9974a | 125:5583f8eeee6c |
---|---|
1 /******************************************************************************* | 1 /******************************************************************************* |
2 * Copyright (c) 2003, 2008 IBM Corporation and others. | 2 * Copyright (c) 2003, 2007 IBM Corporation and others. |
3 * All rights reserved. This program and the accompanying materials | 3 * All rights reserved. This program and the accompanying materials |
4 * are made available under the terms of the Eclipse Public License v1.0 | 4 * are made available under the terms of the Eclipse Public License v1.0 |
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 |
17 import dwt.internal.mozilla.XPCOM; | 21 import XPCOM = dwt.internal.mozilla.XPCOM; |
18 import dwt.internal.mozilla.XPCOMObject; | 22 |
23 import dwt.internal.mozilla.Common; | |
19 import dwt.internal.mozilla.nsEmbedString; | 24 import dwt.internal.mozilla.nsEmbedString; |
20 import dwt.internal.mozilla.nsID; | 25 import dwt.internal.mozilla.nsID; |
21 import dwt.internal.mozilla.nsIHelperAppLauncher; | 26 import dwt.internal.mozilla.nsIHelperAppLauncher; |
22 import dwt.internal.mozilla.nsIHelperAppLauncherDialog; | 27 import dwt.internal.mozilla.nsIHelperAppLauncherDialog; |
23 import dwt.internal.mozilla.nsIHelperAppLauncher_1_8; | 28 import dwt.internal.mozilla.nsIHelperAppLauncher_1_8; |
24 import dwt.internal.mozilla.nsISupports; | 29 import dwt.internal.mozilla.nsISupports; |
30 import dwt.internal.mozilla.nsILocalFile; | |
31 import dwt.internal.mozilla.nsStringAPI; | |
32 | |
33 import dwt.browser.Mozilla; | |
34 | |
25 import dwt.widgets.FileDialog; | 35 import dwt.widgets.FileDialog; |
26 import dwt.widgets.Shell; | 36 import dwt.widgets.Shell; |
27 | 37 |
28 /** | 38 /** |
29 * This class implements the nsIHelperAppLauncherDialog interface for mozilla | 39 * This class implements the HelperAppLauncherDialog interface for mozilla |
30 * versions 1.4 - 1.8.x. For mozilla versions >= 1.9 this interface is | 40 * versions 1.4 - 1.8.x. For mozilla versions >= 1.9 this interface is |
31 * implemented by class HelperAppLauncherDialog_1_9. HelperAppLauncherDialogFactory | 41 * implemented by class HelperAppLauncherDialog_1_9. HelperAppLauncherDialogFactory |
32 * determines at runtime which of these classes to instantiate. | 42 * determines at runtime which of these classes to instantiate. |
33 */ | 43 */ |
34 class HelperAppLauncherDialog { | 44 |
35 XPCOMObject supports; | 45 class HelperAppLauncherDialog : nsIHelperAppLauncherDialog { |
36 XPCOMObject helperAppLauncherDialog; | |
37 int refCount = 0; | 46 int refCount = 0; |
38 | 47 |
39 this () { | 48 this() { |
40 createCOMInterfaces (); | 49 } |
41 } | 50 |
42 | 51 extern(System) |
43 int AddRef () { | 52 nsrefcnt AddRef () { |
44 refCount++; | 53 refCount++; |
45 return refCount; | 54 return refCount; |
46 } | 55 } |
47 | 56 |
48 void createCOMInterfaces () { | 57 extern(System) |
49 /* Create each of the interfaces that this object implements */ | 58 nsresult QueryInterface (nsID* riid, void** ppvObject) { |
50 supports = new XPCOMObject (new int[] {2, 0, 0}) { | 59 if (riid is null || ppvObject is null) return XPCOM.NS_ERROR_NO_INTERFACE; |
51 public int /*long*/ method0 (int /*long*/[] args) {return QueryInterface (args[0], args[1]);} | |
52 public int /*long*/ method1 (int /*long*/[] args) {return AddRef ();} | |
53 public int /*long*/ method2 (int /*long*/[] args) {return Release ();} | |
54 }; | |
55 | 60 |
56 helperAppLauncherDialog = new XPCOMObject (new int[] {2, 0, 0, 3, 5}) { | 61 if (*riid == nsISupports.IID) { |
57 public int /*long*/ method0 (int /*long*/[] args) {return QueryInterface (args[0], args[1]);} | 62 *ppvObject = cast(void*)cast(nsISupports)this; |
58 public int /*long*/ method1 (int /*long*/[] args) {return AddRef ();} | |
59 public int /*long*/ method2 (int /*long*/[] args) {return Release ();} | |
60 public int /*long*/ method3 (int /*long*/[] args) {return Show (args[0], args[1], cast(int)/*64*/args[2]);} | |
61 public int /*long*/ method4 (int /*long*/[] args) {return PromptForSaveToFile (args[0], args[1], args[2], args[3], args[4]);} | |
62 }; | |
63 } | |
64 | |
65 void disposeCOMInterfaces () { | |
66 if (supports !is null) { | |
67 supports.dispose (); | |
68 supports = null; | |
69 } | |
70 if (helperAppLauncherDialog !is null) { | |
71 helperAppLauncherDialog.dispose (); | |
72 helperAppLauncherDialog = null; | |
73 } | |
74 } | |
75 | |
76 int /*long*/ getAddress () { | |
77 return helperAppLauncherDialog.getAddress (); | |
78 } | |
79 | |
80 int QueryInterface (int /*long*/ riid, int /*long*/ ppvObject) { | |
81 if (riid is 0 || ppvObject is 0) return XPCOM.NS_ERROR_NO_INTERFACE; | |
82 nsID guid = new nsID (); | |
83 XPCOM.memmove (guid, riid, nsID.sizeof); | |
84 | |
85 if (guid.Equals (nsISupports.NS_ISUPPORTS_IID)) { | |
86 XPCOM.memmove (ppvObject, new int /*long*/[] {supports.getAddress ()}, C.PTR_SIZEOF); | |
87 AddRef (); | 63 AddRef (); |
88 return XPCOM.NS_OK; | 64 return XPCOM.NS_OK; |
89 } | 65 } |
90 if (guid.Equals (nsIHelperAppLauncherDialog.NS_IHELPERAPPLAUNCHERDIALOG_IID)) { | 66 if (*riid == nsIHelperAppLauncherDialog.IID) { |
91 XPCOM.memmove (ppvObject, new int /*long*/[] {helperAppLauncherDialog.getAddress ()}, C.PTR_SIZEOF); | 67 *ppvObject = cast(void*)cast(nsIHelperAppLauncherDialog)this; |
92 AddRef (); | 68 AddRef (); |
93 return XPCOM.NS_OK; | 69 return XPCOM.NS_OK; |
94 } | 70 } |
95 | 71 |
96 XPCOM.memmove (ppvObject, new int /*long*/[] {0}, C.PTR_SIZEOF); | 72 *ppvObject = null; |
97 return XPCOM.NS_ERROR_NO_INTERFACE; | 73 return XPCOM.NS_ERROR_NO_INTERFACE; |
98 } | 74 } |
99 | 75 |
100 int Release () { | 76 extern(System) |
77 nsrefcnt Release () { | |
101 refCount--; | 78 refCount--; |
102 /* | 79 /* |
103 * Note. This instance lives as long as the download it is binded to. | 80 * Note. This instance lives as long as the download it is binded to. |
104 * Its reference count is expected to go down to 0 when the download | 81 * Its reference count is expected to go down to 0 when the download |
105 * has completed or when it has been cancelled. E.g. when the user | 82 * has completed or when it has been cancelled. E.g. when the user |
106 * cancels the File Dialog, cancels or closes the Download Dialog | 83 * cancels the File Dialog, cancels or closes the Download Dialog |
107 * and when the Download Dialog goes away after the download is completed. | 84 * and when the Download Dialog goes away after the download is completed. |
108 */ | 85 */ |
109 if (refCount is 0) disposeCOMInterfaces (); | 86 if (refCount is 0) return 0; |
110 return refCount; | 87 return refCount; |
111 } | 88 } |
112 | 89 |
113 /* nsIHelperAppLauncherDialog */ | 90 /* nsIHelperAppLauncherDialog */ |
114 | 91 |
115 int Show (int /*long*/ aLauncher, int /*long*/ aContext, int aReason) { | 92 extern(System) |
116 /* | 93 nsresult Show(nsIHelperAppLauncher aLauncher, nsISupports aContext, PRUint32 aReason) { |
94 /* | |
117 * The interface for nsIHelperAppLauncher changed as of mozilla 1.8. Query the received | 95 * The interface for nsIHelperAppLauncher changed as of mozilla 1.8. Query the received |
118 * nsIHelperAppLauncher for the new interface, and if it is not found then fall back to | 96 * nsIHelperAppLauncher for the new interface, and if it is not found then fall back to |
119 * the old interface. | 97 * the old interface. |
120 */ | 98 */ |
121 nsISupports supports = new nsISupports (aLauncher); | 99 |
122 int /*long*/[] result = new int /*long*/[1]; | 100 nsISupports supports = cast(nsISupports)aLauncher; |
123 int rc = supports.QueryInterface (nsIHelperAppLauncher_1_8.NS_IHELPERAPPLAUNCHER_IID, result); | 101 nsIHelperAppLauncher_1_8 helperAppLauncher; |
124 if (rc is XPCOM.NS_OK) { /* >= 1.8 */ | 102 nsresult rc = supports.QueryInterface (&nsIHelperAppLauncher_1_8.IID, cast(void**)&helperAppLauncher); |
125 nsIHelperAppLauncher_1_8 helperAppLauncher = new nsIHelperAppLauncher_1_8 (aLauncher); | 103 if (rc is 0) { |
126 rc = helperAppLauncher.SaveToDisk (0, 0); | 104 rc = helperAppLauncher.SaveToDisk (null, 0); |
127 helperAppLauncher.Release (); | 105 helperAppLauncher.Release (); |
128 return rc; | 106 return rc; |
129 } | 107 } |
130 nsIHelperAppLauncher helperAppLauncher = new nsIHelperAppLauncher (aLauncher); /* < 1.8 */ | 108 |
131 return helperAppLauncher.SaveToDisk (0, 0); | 109 /* < 1.8 */ |
132 } | 110 return aLauncher.SaveToDisk (null, 0); |
133 | 111 // no Release for this? -JJR |
134 int PromptForSaveToFile (int /*long*/ arg0, int /*long*/ arg1, int /*long*/ arg2, int /*long*/ arg3, int /*long*/ arg4) { | 112 } |
135 int /*long*/ aDefaultFile, aSuggestedFileExtension, _retval; | 113 |
114 extern(System) | |
115 nsresult PromptForSaveToFile (nsIHelperAppLauncher aLauncher, nsISupports aWindowContext, PRUnichar* aDefaultFile, PRUnichar* aSuggestedFileExtension, nsILocalFile* _retval) { | |
136 bool hasLauncher = false; | 116 bool hasLauncher = false; |
137 | 117 |
138 /* | 118 /* |
139 * The interface for nsIHelperAppLauncherDialog changed as of mozilla 1.5 when an | 119 * The interface for nsIHelperAppLauncherDialog changed as of mozilla 1.5 when an |
140 * extra argument was added to the PromptForSaveToFile method (this resulted in all | 120 * extra argument was added to the PromptForSaveToFile method (this resulted in all |
147 /* | 127 /* |
148 * The interface for nsIHelperAppLauncher changed as of mozilla 1.8, so the first | 128 * The interface for nsIHelperAppLauncher changed as of mozilla 1.8, so the first |
149 * argument must be queried for both the old and new nsIHelperAppLauncher interfaces. | 129 * argument must be queried for both the old and new nsIHelperAppLauncher interfaces. |
150 */ | 130 */ |
151 bool using_1_8 = false; | 131 bool using_1_8 = false; |
152 nsISupports support = new nsISupports (arg0); | 132 nsISupports support = cast(nsISupports)aLauncher; |
153 int /*long*/[] result = new int /*long*/[1]; | 133 |
154 int rc = support.QueryInterface (nsIHelperAppLauncher_1_8.NS_IHELPERAPPLAUNCHER_IID, result); | 134 if (aLauncher is null) |
135 assert(0); | |
136 | |
137 nsIHelperAppLauncher_1_8 helperAppLauncher1; | |
138 int rc = support.QueryInterface (&nsIHelperAppLauncher_1_8.IID, cast(void**)&helperAppLauncher1); | |
155 if (rc is XPCOM.NS_OK) { | 139 if (rc is XPCOM.NS_OK) { |
156 using_1_8 = true; | 140 using_1_8 = true; |
157 hasLauncher = true; | 141 hasLauncher = true; |
158 new nsISupports (result[0]).Release (); | 142 helperAppLauncher1.Release (); |
159 } else { | 143 } else { |
160 result[0] = 0; | 144 nsIHelperAppLauncher helperAppLauncher; |
161 rc = support.QueryInterface (nsIHelperAppLauncher.NS_IHELPERAPPLAUNCHER_IID, result); | 145 rc = support.QueryInterface (&nsIHelperAppLauncher.IID, cast(void**)&helperAppLauncher); |
162 if (rc is XPCOM.NS_OK) { | 146 if (rc is XPCOM.NS_OK) { |
163 hasLauncher = true; | 147 hasLauncher = true; |
164 new nsISupports (result[0]).Release (); | 148 helperAppLauncher.Release; |
165 } | 149 } |
166 } | 150 } |
167 result[0] = 0; | 151 |
168 | 152 /+ |
153 // In D port, no suppport for version >1.4 yet | |
169 if (hasLauncher) { /* >= 1.5 */ | 154 if (hasLauncher) { /* >= 1.5 */ |
170 aDefaultFile = arg2; | 155 aDefaultFile = arg2; |
171 aSuggestedFileExtension = arg3; | 156 aSuggestedFileExtension = arg3; |
172 _retval = arg4; | 157 _retval = arg4; |
173 } else { /* 1.4 */ | 158 } else { /* 1.4 */ |
159 // This call conversion probablywon't work for non-Java | |
160 // and shouldn't get called; fix it later. -JJR | |
174 aDefaultFile = arg1; | 161 aDefaultFile = arg1; |
175 aSuggestedFileExtension = arg2; | 162 aSuggestedFileExtension = arg2; |
176 _retval = arg3; | 163 _retval = arg3; |
177 } | 164 } |
178 | 165 +/ |
179 int length = XPCOM.strlen_PRUnichar (aDefaultFile); | 166 //int span = XPCOM.strlen_PRUnichar (aDefaultFile); |
180 char[] dest = new char[length]; | 167 // XPCOM.memmove (dest, aDefaultFile, length * 2); |
181 XPCOM.memmove (dest, aDefaultFile, length * 2); | 168 String defaultFile = Utf.toString(fromString16z(aDefaultFile)); |
182 String defaultFile = new String (dest); | 169 |
183 | 170 //span = XPCOM.strlen_PRUnichar (aSuggestedFileExtension); |
184 length = XPCOM.strlen_PRUnichar (aSuggestedFileExtension); | 171 //dest = new char[length]; |
185 dest = new char[length]; | 172 //XPCOM.memmove (dest, aSuggestedFileExtension, length * 2); |
186 XPCOM.memmove (dest, aSuggestedFileExtension, length * 2); | 173 String suggestedFileExtension = Utf.toString(fromString16z(aSuggestedFileExtension)); |
187 String suggestedFileExtension = new String (dest); | |
188 | 174 |
189 Shell shell = new Shell (); | 175 Shell shell = new Shell (); |
190 FileDialog fileDialog = new FileDialog (shell, DWT.SAVE); | 176 FileDialog fileDialog = new FileDialog (shell, DWT.SAVE); |
191 fileDialog.setFileName (defaultFile); | 177 fileDialog.setFileName (defaultFile); |
192 fileDialog.setFilterExtensions (new String[] {suggestedFileExtension}); | 178 String[] tmp; |
179 tmp ~= suggestedFileExtension; | |
180 fileDialog.setFilterExtensions (tmp); | |
193 String name = fileDialog.open (); | 181 String name = fileDialog.open (); |
194 shell.close (); | 182 shell.close (); |
195 if (name is null) { | 183 if (name is null) { |
196 if (hasLauncher) { | 184 if (hasLauncher) { |
197 if (using_1_8) { | 185 if (using_1_8) { |
198 nsIHelperAppLauncher_1_8 launcher = new nsIHelperAppLauncher_1_8 (arg0); | 186 rc = (cast(nsIHelperAppLauncher_1_8)aLauncher).Cancel (XPCOM.NS_BINDING_ABORTED); |
199 rc = launcher.Cancel (XPCOM.NS_BINDING_ABORTED); | |
200 } else { | 187 } else { |
201 nsIHelperAppLauncher launcher = new nsIHelperAppLauncher (arg0); | 188 rc = aLauncher.Cancel (); |
202 rc = launcher.Cancel (); | |
203 } | 189 } |
204 if (rc !is XPCOM.NS_OK) Mozilla.error (rc); | 190 if (rc !is XPCOM.NS_OK) Mozilla.error (rc,__FILE__,__LINE__); |
205 return XPCOM.NS_OK; | 191 return XPCOM.NS_OK; |
206 } | 192 } |
207 return XPCOM.NS_ERROR_FAILURE; | 193 return XPCOM.NS_ERROR_FAILURE; |
208 } | 194 } |
209 nsEmbedString path = new nsEmbedString (name); | 195 scope auto path = new nsEmbedString (name.toString16()); |
210 rc = XPCOM.NS_NewLocalFile (path.getAddress (), 1, result); | 196 nsILocalFile localFile; |
211 path.dispose (); | 197 rc = XPCOM.NS_NewLocalFile (cast(nsAString*)path, 1, &localFile); |
212 if (rc !is XPCOM.NS_OK) Mozilla.error (rc); | 198 //path.dispose (); |
213 if (result[0] is 0) Mozilla.error (XPCOM.NS_ERROR_NULL_POINTER); | 199 if (rc !is XPCOM.NS_OK) Mozilla.error (rc,__FILE__,__LINE__); |
200 if (localFile is null) Mozilla.error (XPCOM.NS_ERROR_NULL_POINTER,__FILE__,__LINE__); | |
214 /* Our own nsIDownload has been registered during the Browser initialization. It will be invoked by Mozilla. */ | 201 /* Our own nsIDownload has been registered during the Browser initialization. It will be invoked by Mozilla. */ |
215 XPCOM.memmove (_retval, result, C.PTR_SIZEOF); | 202 *_retval = localFile; |
203 //XPCOM.memmove (_retval, result, C.PTR_SIZEOF); | |
216 return XPCOM.NS_OK; | 204 return XPCOM.NS_OK; |
217 } | 205 } |
218 } | 206 } |