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