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