Mercurial > projects > dwt-mac
comparison dwt/browser/Download.d @ 0:380af2bdd8e5
Upload of whole dwt tree
author | Jacob Carlborg <doob@me.com> <jacob.carlborg@gmail.com> |
---|---|
date | Sat, 09 Aug 2008 17:00:02 +0200 |
parents | |
children | 1a8b3cb347e0 |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:380af2bdd8e5 |
---|---|
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.Download; | |
12 | |
13 import dwt.dwthelper.utils; | |
14 | |
15 import dwt.DWT; | |
16 import dwt.internal.C; | |
17 import dwt.internal.Compatibility; | |
18 import dwt.internal.mozilla.XPCOM; | |
19 import dwt.internal.mozilla.XPCOMObject; | |
20 import dwt.internal.mozilla.nsID; | |
21 import dwt.internal.mozilla.nsIDownload; | |
22 import dwt.internal.mozilla.nsIHelperAppLauncher; | |
23 import dwt.internal.mozilla.nsILocalFile; | |
24 import dwt.internal.mozilla.nsIProgressDialog; | |
25 import dwt.internal.mozilla.nsISupports; | |
26 import dwt.internal.mozilla.nsIURI; | |
27 import dwt.internal.mozilla.nsIWebProgressListener; | |
28 import dwt.layout.GridData; | |
29 import dwt.layout.GridLayout; | |
30 import dwt.widgets.Button; | |
31 import dwt.widgets.Event; | |
32 import dwt.widgets.Label; | |
33 import dwt.widgets.Listener; | |
34 import dwt.widgets.Shell; | |
35 | |
36 class Download { | |
37 XPCOMObject supports; | |
38 XPCOMObject download; | |
39 XPCOMObject progressDialog; | |
40 XPCOMObject webProgressListener; | |
41 nsIHelperAppLauncher helperAppLauncher; | |
42 int refCount = 0; | |
43 | |
44 Shell shell; | |
45 Label status; | |
46 Button cancel; | |
47 | |
48 Download () { | |
49 createCOMInterfaces (); | |
50 } | |
51 | |
52 int AddRef () { | |
53 refCount++; | |
54 return refCount; | |
55 } | |
56 | |
57 void createCOMInterfaces () { | |
58 /* Create each of the interfaces that this object implements */ | |
59 supports = new XPCOMObject (new int[] {2, 0, 0}) { | |
60 public int /*long*/ method0 (int /*long*/[] args) {return QueryInterface (args[0], args[1]);} | |
61 public int /*long*/ method1 (int /*long*/[] args) {return AddRef ();} | |
62 public int /*long*/ method2 (int /*long*/[] args) {return Release ();} | |
63 }; | |
64 | |
65 download = new XPCOMObject (new int[] {2, 0, 0, 7, 1, 1, 1, 1, 1, 1, 2, 1, 1, 1, 1, 1}) { | |
66 public int /*long*/ method0 (int /*long*/[] args) {return QueryInterface (args[0], args[1]);} | |
67 public int /*long*/ method1 (int /*long*/[] args) {return AddRef ();} | |
68 public int /*long*/ method2 (int /*long*/[] args) {return Release ();} | |
69 public int /*long*/ method3 (int /*long*/[] args) {return Init (args[0], args[1], args[2], args[3], args[4], args[5], args[6]);} | |
70 public int /*long*/ method4 (int /*long*/[] args) {return GetSource (args[0]);} | |
71 public int /*long*/ method5 (int /*long*/[] args) {return GetTarget (args[0]);} | |
72 public int /*long*/ method6 (int /*long*/[] args) {return GetPersist (args[0]);} | |
73 public int /*long*/ method7 (int /*long*/[] args) {return GetPercentComplete (args[0]);} | |
74 public int /*long*/ method8 (int /*long*/[] args) {return GetDisplayName (args[0]);} | |
75 public int /*long*/ method9 (int /*long*/[] args) {return SetDisplayName (args[0]);} | |
76 public int /*long*/ method10 (int /*long*/[] args) {return GetStartTime (args[0]);} | |
77 public int /*long*/ method11 (int /*long*/[] args) {return GetMIMEInfo (args[0]);} | |
78 public int /*long*/ method12 (int /*long*/[] args) {return GetListener (args[0]);} | |
79 public int /*long*/ method13 (int /*long*/[] args) {return SetListener (args[0]);} | |
80 public int /*long*/ method14 (int /*long*/[] args) {return GetObserver (args[0]);} | |
81 public int /*long*/ method15 (int /*long*/[] args) {return SetObserver (args[0]);} | |
82 }; | |
83 | |
84 progressDialog = new XPCOMObject (new int[] {2, 0, 0, 7, 1, 1, 1, 1, 1, 1, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1}) { | |
85 public int /*long*/ method0 (int /*long*/[] args) {return QueryInterface (args[0], args[1]);} | |
86 public int /*long*/ method1 (int /*long*/[] args) {return AddRef ();} | |
87 public int /*long*/ method2 (int /*long*/[] args) {return Release ();} | |
88 public int /*long*/ method3 (int /*long*/[] args) {return Init (args[0], args[1], args[2], args[3], args[4], args[5], args[6]);} | |
89 public int /*long*/ method4 (int /*long*/[] args) {return GetSource (args[0]);} | |
90 public int /*long*/ method5 (int /*long*/[] args) {return GetTarget (args[0]);} | |
91 public int /*long*/ method6 (int /*long*/[] args) {return GetPersist (args[0]);} | |
92 public int /*long*/ method7 (int /*long*/[] args) {return GetPercentComplete (args[0]);} | |
93 public int /*long*/ method8 (int /*long*/[] args) {return GetDisplayName (args[0]);} | |
94 public int /*long*/ method9 (int /*long*/[] args) {return SetDisplayName (args[0]);} | |
95 public int /*long*/ method10 (int /*long*/[] args) {return GetStartTime (args[0]);} | |
96 public int /*long*/ method11 (int /*long*/[] args) {return GetMIMEInfo (args[0]);} | |
97 public int /*long*/ method12 (int /*long*/[] args) {return GetListener (args[0]);} | |
98 public int /*long*/ method13 (int /*long*/[] args) {return SetListener (args[0]);} | |
99 public int /*long*/ method14 (int /*long*/[] args) {return GetObserver (args[0]);} | |
100 public int /*long*/ method15 (int /*long*/[] args) {return SetObserver (args[0]);} | |
101 public int /*long*/ method16 (int /*long*/[] args) {return Open (args[0]);} | |
102 public int /*long*/ method17 (int /*long*/[] args) {return GetCancelDownloadOnClose (args[0]);} | |
103 public int /*long*/ method18 (int /*long*/[] args) {return SetCancelDownloadOnClose ((int)/*64*/args[0]);} | |
104 public int /*long*/ method19 (int /*long*/[] args) {return GetDialog (args[0]);} | |
105 public int /*long*/ method20 (int /*long*/[] args) {return SetDialog (args[0]);} | |
106 }; | |
107 | |
108 webProgressListener = new XPCOMObject (new int[] {2, 0, 0, 4, 6, 3, 4, 3}) { | |
109 public int /*long*/ method0 (int /*long*/[] args) {return QueryInterface (args[0], args[1]);} | |
110 public int /*long*/ method1 (int /*long*/[] args) {return AddRef ();} | |
111 public int /*long*/ method2 (int /*long*/[] args) {return Release ();} | |
112 public int /*long*/ method3 (int /*long*/[] args) {return OnStateChange (args[0], args[1], (int)/*64*/args[2], (int)/*64*/args[3]);} | |
113 public int /*long*/ method4 (int /*long*/[] args) {return OnProgressChange (args[0], args[1], (int)/*64*/args[2], (int)/*64*/args[3], (int)/*64*/args[4], (int)/*64*/args[5]);} | |
114 public int /*long*/ method5 (int /*long*/[] args) {return OnLocationChange (args[0], args[1], args[2]);} | |
115 public int /*long*/ method6 (int /*long*/[] args) {return OnStatusChange (args[0], args[1], (int)/*64*/args[2], args[3]);} | |
116 public int /*long*/ method7 (int /*long*/[] args) {return OnSecurityChange (args[0], args[1], (int)/*64*/args[2]);} | |
117 }; | |
118 } | |
119 | |
120 void disposeCOMInterfaces () { | |
121 if (supports !is null) { | |
122 supports.dispose (); | |
123 supports = null; | |
124 } | |
125 if (download !is null) { | |
126 download.dispose (); | |
127 download = null; | |
128 } | |
129 if (progressDialog !is null) { | |
130 progressDialog.dispose (); | |
131 progressDialog = null; | |
132 } | |
133 if (webProgressListener !is null) { | |
134 webProgressListener.dispose (); | |
135 webProgressListener = null; | |
136 } | |
137 } | |
138 | |
139 int /*long*/ getAddress () { | |
140 return progressDialog.getAddress (); | |
141 } | |
142 | |
143 int QueryInterface (int /*long*/ riid, int /*long*/ ppvObject) { | |
144 if (riid is 0 || ppvObject is 0) return XPCOM.NS_ERROR_NO_INTERFACE; | |
145 nsID guid = new nsID (); | |
146 XPCOM.memmove (guid, riid, nsID.sizeof); | |
147 | |
148 if (guid.Equals (nsISupports.NS_ISUPPORTS_IID)) { | |
149 XPCOM.memmove (ppvObject, new int /*long*/[] {supports.getAddress ()}, C.PTR_SIZEOF); | |
150 AddRef(); | |
151 return XPCOM.NS_OK; | |
152 } | |
153 if (guid.Equals (nsIDownload.NS_IDOWNLOAD_IID)) { | |
154 XPCOM.memmove (ppvObject, new int /*long*/[] {download.getAddress ()}, C.PTR_SIZEOF); | |
155 AddRef(); | |
156 return XPCOM.NS_OK; | |
157 } | |
158 if (guid.Equals (nsIProgressDialog.NS_IPROGRESSDIALOG_IID)) { | |
159 XPCOM.memmove (ppvObject, new int /*long*/[] {progressDialog.getAddress ()}, C.PTR_SIZEOF); | |
160 AddRef(); | |
161 return XPCOM.NS_OK; | |
162 } | |
163 if (guid.Equals (nsIWebProgressListener.NS_IWEBPROGRESSLISTENER_IID)) { | |
164 XPCOM.memmove (ppvObject, new int /*long*/[] {webProgressListener.getAddress ()}, C.PTR_SIZEOF); | |
165 AddRef(); | |
166 return XPCOM.NS_OK; | |
167 } | |
168 XPCOM.memmove (ppvObject, new int /*long*/[] {0}, C.PTR_SIZEOF); | |
169 return XPCOM.NS_ERROR_NO_INTERFACE; | |
170 } | |
171 | |
172 int Release () { | |
173 refCount--; | |
174 if (refCount is 0) disposeCOMInterfaces (); | |
175 return refCount; | |
176 } | |
177 | |
178 /* nsIDownload */ | |
179 | |
180 /* Note. The argument startTime is defined as a PRInt64. This translates into two java ints. */ | |
181 int Init (int /*long*/ aSource, int /*long*/ aTarget, int /*long*/ aDisplayName, int /*long*/ aMIMEInfo, int /*long*/ startTime1, int /*long*/ startTime2, int /*long*/ aPersist) { | |
182 nsIURI source = new nsIURI (aSource); | |
183 int /*long*/ aSpec = XPCOM.nsEmbedCString_new (); | |
184 int rc = source.GetHost (aSpec); | |
185 if (rc !is XPCOM.NS_OK) Mozilla.error (rc); | |
186 int length = XPCOM.nsEmbedCString_Length (aSpec); | |
187 int /*long*/ buffer = XPCOM.nsEmbedCString_get (aSpec); | |
188 byte[] dest = new byte[length]; | |
189 XPCOM.memmove (dest, buffer, length); | |
190 XPCOM.nsEmbedCString_delete (aSpec); | |
191 String url = new String (dest); | |
192 | |
193 /* | |
194 * As of mozilla 1.7 the second argument of the nsIDownload interface's | |
195 * Init function changed from nsILocalFile to nsIURI. Detect which of | |
196 * these interfaces the second argument implements and act accordingly. | |
197 */ | |
198 String filename = null; | |
199 nsISupports supports = new nsISupports (aTarget); | |
200 int /*long*/[] result = new int /*long*/[1]; | |
201 rc = supports.QueryInterface (nsIURI.NS_IURI_IID, result); | |
202 if (rc is 0) { /* >= 1.7 */ | |
203 nsIURI target = new nsIURI (result[0]); | |
204 result[0] = 0; | |
205 int /*long*/ aPath = XPCOM.nsEmbedCString_new (); | |
206 rc = target.GetPath (aPath); | |
207 if (rc !is XPCOM.NS_OK) Mozilla.error (rc); | |
208 length = XPCOM.nsEmbedCString_Length (aPath); | |
209 buffer = XPCOM.nsEmbedCString_get (aPath); | |
210 dest = new byte[length]; | |
211 XPCOM.memmove (dest, buffer, length); | |
212 XPCOM.nsEmbedCString_delete (aPath); | |
213 filename = new String (dest); | |
214 int separator = filename.lastIndexOf (System.getProperty ("file.separator")); //$NON-NLS-1$ | |
215 filename = filename.substring (separator + 1); | |
216 target.Release (); | |
217 } else { /* < 1.7 */ | |
218 nsILocalFile target = new nsILocalFile (aTarget); | |
219 int /*long*/ aNativeTarget = XPCOM.nsEmbedCString_new (); | |
220 rc = target.GetNativeLeafName (aNativeTarget); | |
221 if (rc !is XPCOM.NS_OK) Mozilla.error (rc); | |
222 length = XPCOM.nsEmbedCString_Length (aNativeTarget); | |
223 buffer = XPCOM.nsEmbedCString_get (aNativeTarget); | |
224 dest = new byte[length]; | |
225 XPCOM.memmove (dest, buffer, length); | |
226 XPCOM.nsEmbedCString_delete (aNativeTarget); | |
227 filename = new String (dest); | |
228 } | |
229 | |
230 Listener listener = new Listener () { | |
231 public void handleEvent (Event event) { | |
232 if (event.widget is cancel) { | |
233 shell.close (); | |
234 } | |
235 if (helperAppLauncher !is null) { | |
236 helperAppLauncher.Cancel (); | |
237 helperAppLauncher.Release (); | |
238 } | |
239 shell = null; | |
240 helperAppLauncher = null; | |
241 } | |
242 }; | |
243 shell = new Shell (DWT.DIALOG_TRIM); | |
244 String msg = Compatibility.getMessage ("SWT_Download_File", new Object[] {filename}); //$NON-NLS-1$ | |
245 shell.setText (msg); | |
246 GridLayout gridLayout = new GridLayout (); | |
247 gridLayout.marginHeight = 15; | |
248 gridLayout.marginWidth = 15; | |
249 gridLayout.verticalSpacing = 20; | |
250 shell.setLayout(gridLayout); | |
251 msg = Compatibility.getMessage ("SWT_Download_Location", new Object[] {filename, url}); //$NON-NLS-1$ | |
252 new Label (shell, DWT.SIMPLE).setText (msg); | |
253 status = new Label (shell, DWT.SIMPLE); | |
254 msg = Compatibility.getMessage ("SWT_Download_Started"); //$NON-NLS-1$ | |
255 status.setText (msg); | |
256 GridData data = new GridData (); | |
257 data.grabExcessHorizontalSpace = true; | |
258 data.grabExcessVerticalSpace = true; | |
259 status.setLayoutData (data); | |
260 | |
261 cancel = new Button (shell, DWT.PUSH); | |
262 cancel.setText (DWT.getMessage ("SWT_Cancel")); //$NON-NLS-1$ | |
263 data = new GridData (); | |
264 data.horizontalAlignment = GridData.CENTER; | |
265 cancel.setLayoutData (data); | |
266 cancel.addListener (DWT.Selection, listener); | |
267 shell.addListener (DWT.Close, listener); | |
268 shell.pack (); | |
269 shell.open (); | |
270 return XPCOM.NS_OK; | |
271 } | |
272 | |
273 int GetSource (int /*long*/ aSource) { | |
274 return XPCOM.NS_ERROR_NOT_IMPLEMENTED; | |
275 } | |
276 | |
277 int GetTarget (int /*long*/ aTarget) { | |
278 return XPCOM.NS_ERROR_NOT_IMPLEMENTED; | |
279 } | |
280 | |
281 int GetPersist (int /*long*/ aPersist) { | |
282 return XPCOM.NS_ERROR_NOT_IMPLEMENTED; | |
283 } | |
284 | |
285 int GetPercentComplete (int /*long*/ aPercentComplete) { | |
286 return XPCOM.NS_ERROR_NOT_IMPLEMENTED; | |
287 } | |
288 | |
289 int GetDisplayName (int /*long*/ aDisplayName) { | |
290 return XPCOM.NS_ERROR_NOT_IMPLEMENTED; | |
291 } | |
292 | |
293 int SetDisplayName (int /*long*/ aDisplayName) { | |
294 return XPCOM.NS_ERROR_NOT_IMPLEMENTED; | |
295 } | |
296 | |
297 int GetStartTime (int /*long*/ aStartTime) { | |
298 return XPCOM.NS_ERROR_NOT_IMPLEMENTED; | |
299 } | |
300 | |
301 int GetMIMEInfo (int /*long*/ aMIMEInfo) { | |
302 return XPCOM.NS_ERROR_NOT_IMPLEMENTED; | |
303 } | |
304 | |
305 int GetListener (int /*long*/ aListener) { | |
306 return XPCOM.NS_ERROR_NOT_IMPLEMENTED; | |
307 } | |
308 | |
309 int SetListener (int /*long*/ aListener) { | |
310 return XPCOM.NS_ERROR_NOT_IMPLEMENTED; | |
311 } | |
312 | |
313 int GetObserver (int /*long*/ aObserver) { | |
314 return XPCOM.NS_ERROR_NOT_IMPLEMENTED; | |
315 } | |
316 | |
317 int SetObserver (int /*long*/ aObserver) { | |
318 if (aObserver !is 0) { | |
319 nsISupports supports = new nsISupports (aObserver); | |
320 int /*long*/[] result = new int /*long*/[1]; | |
321 int rc = supports.QueryInterface (nsIHelperAppLauncher.NS_IHELPERAPPLAUNCHER_IID, result); | |
322 if (rc !is XPCOM.NS_OK) Mozilla.error (rc); | |
323 if (result[0] is 0) Mozilla.error (XPCOM.NS_ERROR_NO_INTERFACE); | |
324 helperAppLauncher = new nsIHelperAppLauncher (result[0]); | |
325 } | |
326 return XPCOM.NS_OK; | |
327 } | |
328 | |
329 /* nsIProgressDialog */ | |
330 int Open (int /*long*/ aParent) { | |
331 return XPCOM.NS_ERROR_NOT_IMPLEMENTED; | |
332 } | |
333 | |
334 int GetCancelDownloadOnClose (int /*long*/ aCancelDownloadOnClose) { | |
335 return XPCOM.NS_ERROR_NOT_IMPLEMENTED; | |
336 } | |
337 | |
338 int SetCancelDownloadOnClose (int aCancelDownloadOnClose) { | |
339 return XPCOM.NS_ERROR_NOT_IMPLEMENTED; | |
340 } | |
341 | |
342 int GetDialog (int /*long*/ aDialog) { | |
343 return XPCOM.NS_ERROR_NOT_IMPLEMENTED; | |
344 } | |
345 | |
346 int SetDialog (int /*long*/ aDialog) { | |
347 return XPCOM.NS_ERROR_NOT_IMPLEMENTED; | |
348 } | |
349 | |
350 /* nsIWebProgressListener */ | |
351 | |
352 int OnStateChange (int /*long*/ aWebProgress, int /*long*/ aRequest, int aStateFlags, int aStatus) { | |
353 if ((aStateFlags & nsIWebProgressListener.STATE_STOP) !is 0) { | |
354 if (helperAppLauncher !is null) helperAppLauncher.Release (); | |
355 helperAppLauncher = null; | |
356 if (shell !is null && !shell.isDisposed ()) shell.dispose (); | |
357 shell = null; | |
358 } | |
359 return XPCOM.NS_OK; | |
360 } | |
361 | |
362 int OnProgressChange (int /*long*/ aWebProgress, int /*long*/ aRequest, int aCurSelfProgress, int aMaxSelfProgress, int aCurTotalProgress, int aMaxTotalProgress) { | |
363 int currentKBytes = aCurTotalProgress / 1024; | |
364 int totalKBytes = aMaxTotalProgress / 1024; | |
365 if (shell !is null && !shell.isDisposed ()) { | |
366 Object[] arguments = {new Integer (currentKBytes), new Integer (totalKBytes)}; | |
367 String statusMsg = Compatibility.getMessage ("SWT_Download_Status", arguments); //$NON-NLS-1$ | |
368 status.setText (statusMsg); | |
369 shell.layout (true); | |
370 shell.getDisplay ().update (); | |
371 } | |
372 return XPCOM.NS_OK; | |
373 } | |
374 | |
375 int OnLocationChange (int /*long*/ aWebProgress, int /*long*/ aRequest, int /*long*/ aLocation) { | |
376 return XPCOM.NS_OK; | |
377 } | |
378 | |
379 int OnStatusChange (int /*long*/ aWebProgress, int /*long*/ aRequest, int aStatus, int /*long*/ aMessage) { | |
380 return XPCOM.NS_OK; | |
381 } | |
382 | |
383 int OnSecurityChange (int /*long*/ aWebProgress, int /*long*/ aRequest, int state) { | |
384 return XPCOM.NS_OK; | |
385 } | |
386 } |