KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > swt > browser > Download


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 package org.eclipse.swt.browser;
12
13 import org.eclipse.swt.SWT;
14 import org.eclipse.swt.internal.*;
15 import org.eclipse.swt.internal.mozilla.*;
16 import org.eclipse.swt.layout.*;
17 import org.eclipse.swt.widgets.*;
18
19 class Download {
20     XPCOMObject supports;
21     XPCOMObject download;
22     XPCOMObject progressDialog;
23     XPCOMObject webProgressListener;
24     nsIHelperAppLauncher helperAppLauncher;
25     int refCount = 0;
26
27     Shell shell;
28     Label status;
29     Button cancel;
30     
31 public Download () {
32     createCOMInterfaces ();
33 }
34
35 int AddRef () {
36     refCount++;
37     return refCount;
38 }
39
40 void createCOMInterfaces () {
41     /* Create each of the interfaces that this object implements */
42     supports = new XPCOMObject (new int[] {2, 0, 0}) {
43         public int /*long*/ method0 (int /*long*/[] args) {return QueryInterface (args[0], args[1]);}
44         public int /*long*/ method1 (int /*long*/[] args) {return AddRef ();}
45         public int /*long*/ method2 (int /*long*/[] args) {return Release ();}
46     };
47     
48     download = new XPCOMObject (new int[] {2, 0, 0, 7, 1, 1, 1, 1, 1, 1, 2, 1, 1, 1, 1, 1}) {
49         public int /*long*/ method0 (int /*long*/[] args) {return QueryInterface (args[0], args[1]);}
50         public int /*long*/ method1 (int /*long*/[] args) {return AddRef ();}
51         public int /*long*/ method2 (int /*long*/[] args) {return Release ();}
52         public int /*long*/ method3 (int /*long*/[] args) {return Init (args[0], args[1], args[2], args[3], args[4], args[5], args[6]);}
53         public int /*long*/ method4 (int /*long*/[] args) {return GetSource (args[0]);}
54         public int /*long*/ method5 (int /*long*/[] args) {return GetTarget (args[0]);}
55         public int /*long*/ method6 (int /*long*/[] args) {return GetPersist (args[0]);}
56         public int /*long*/ method7 (int /*long*/[] args) {return GetPercentComplete (args[0]);}
57         public int /*long*/ method8 (int /*long*/[] args) {return GetDisplayName (args[0]);}
58         public int /*long*/ method9 (int /*long*/[] args) {return SetDisplayName (args[0]);}
59         public int /*long*/ method10 (int /*long*/[] args) {return GetStartTime (args[0]);}
60         public int /*long*/ method11 (int /*long*/[] args) {return GetMIMEInfo (args[0]);}
61         public int /*long*/ method12 (int /*long*/[] args) {return GetListener (args[0]);}
62         public int /*long*/ method13 (int /*long*/[] args) {return SetListener (args[0]);}
63         public int /*long*/ method14 (int /*long*/[] args) {return GetObserver (args[0]);}
64         public int /*long*/ method15 (int /*long*/[] args) {return SetObserver (args[0]);}
65     };
66     
67     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}) {
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         public int /*long*/ method16 (int /*long*/[] args) {return Open (args[0]);}
85         public int /*long*/ method17 (int /*long*/[] args) {return GetCancelDownloadOnClose (args[0]);}
86         public int /*long*/ method18 (int /*long*/[] args) {return SetCancelDownloadOnClose (args[0]);}
87         public int /*long*/ method19 (int /*long*/[] args) {return GetDialog (args[0]);}
88         public int /*long*/ method20 (int /*long*/[] args) {return SetDialog (args[0]);}
89     };
90     
91     webProgressListener = new XPCOMObject (new int[] {2, 0, 0, 4, 6, 3, 4, 3}) {
92         public int /*long*/ method0 (int /*long*/[] args) {return QueryInterface (args[0], args[1]);}
93         public int /*long*/ method1 (int /*long*/[] args) {return AddRef ();}
94         public int /*long*/ method2 (int /*long*/[] args) {return Release ();}
95         public int /*long*/ method3 (int /*long*/[] args) {return OnStateChange (args[0], args[1], args[2], args[3]);}
96         public int /*long*/ method4 (int /*long*/[] args) {return OnProgressChange (args[0], args[1], args[2], args[3], args[4], args[5]);}
97         public int /*long*/ method5 (int /*long*/[] args) {return OnLocationChange (args[0], args[1], args[2]);}
98         public int /*long*/ method6 (int /*long*/[] args) {return OnStatusChange (args[0], args[1], args[2], args[3]);}
99         public int /*long*/ method7 (int /*long*/[] args) {return OnSecurityChange (args[0], args[1], args[2]);}
100     };
101 }
102
103 void disposeCOMInterfaces () {
104     if (supports != null) {
105         supports.dispose ();
106         supports = null;
107     }
108     if (download != null) {
109         download.dispose ();
110         download = null;
111     }
112     if (progressDialog != null) {
113         progressDialog.dispose ();
114         progressDialog = null;
115     }
116     if (webProgressListener != null) {
117         webProgressListener.dispose ();
118         webProgressListener = null;
119     }
120 }
121
122 int /*long*/ getAddress () {
123     return progressDialog.getAddress ();
124 }
125
126 int /*long*/ QueryInterface (int /*long*/ riid, int /*long*/ ppvObject) {
127     if (riid == 0 || ppvObject == 0) return XPCOM.NS_ERROR_NO_INTERFACE;
128     nsID guid = new nsID ();
129     XPCOM.memmove (guid, riid, nsID.sizeof);
130
131     if (guid.Equals (nsISupports.NS_ISUPPORTS_IID)) {
132         XPCOM.memmove (ppvObject, new int /*long*/[] {supports.getAddress ()}, C.PTR_SIZEOF);
133         AddRef();
134         return XPCOM.NS_OK;
135     }
136     if (guid.Equals (nsIDownload.NS_IDOWNLOAD_IID)) {
137         XPCOM.memmove (ppvObject, new int /*long*/[] {download.getAddress ()}, C.PTR_SIZEOF);
138         AddRef();
139         return XPCOM.NS_OK;
140     }
141     if (guid.Equals (nsIProgressDialog.NS_IPROGRESSDIALOG_IID)) {
142         XPCOM.memmove (ppvObject, new int /*long*/[] {progressDialog.getAddress ()}, C.PTR_SIZEOF);
143         AddRef();
144         return XPCOM.NS_OK;
145     }
146     if (guid.Equals (nsIWebProgressListener.NS_IWEBPROGRESSLISTENER_IID)) {
147         XPCOM.memmove (ppvObject, new int /*long*/[] {webProgressListener.getAddress ()}, C.PTR_SIZEOF);
148         AddRef();
149         return XPCOM.NS_OK;
150     }
151     XPCOM.memmove (ppvObject, new int /*long*/[] {0}, C.PTR_SIZEOF);
152     return XPCOM.NS_ERROR_NO_INTERFACE;
153 }
154             
155 int Release () {
156     refCount--;
157     if (refCount == 0) disposeCOMInterfaces ();
158     return refCount;
159 }
160
161 /* nsIDownload */
162
163 /* Note. The argument startTime is defined as a PRInt64. This translates into two java ints. */
164 public int /*long*/ Init (int /*long*/ aSource, int /*long*/ aTarget, int /*long*/ aDisplayName, int /*long*/ aMIMEInfo, int /*long*/ startTime1, int /*long*/ startTime2, int /*long*/ aPersist) {
165     nsIURI source = new nsIURI (aSource);
166     int /*long*/ aSpec = XPCOM.nsEmbedCString_new ();
167     int rc = source.GetHost (aSpec);
168     if (rc != XPCOM.NS_OK) Mozilla.error (rc);
169     int length = XPCOM.nsEmbedCString_Length (aSpec);
170     int /*long*/ buffer = XPCOM.nsEmbedCString_get (aSpec);
171     byte[] dest = new byte[length];
172     XPCOM.memmove (dest, buffer, length);
173     XPCOM.nsEmbedCString_delete (aSpec);
174     String JavaDoc url = new String JavaDoc (dest);
175
176     /*
177     * As of mozilla 1.7 the second argument of the nsIDownload interface's
178     * Init function changed from nsILocalFile to nsIURI. Detect which of
179     * these interfaces the second argument implements and act accordingly.
180     */

181     String JavaDoc filename = null;
182     nsISupports supports = new nsISupports (aTarget);
183     int /*long*/[] result = new int /*long*/[1];
184     rc = supports.QueryInterface (nsIURI.NS_IURI_IID, result);
185     if (rc == 0) { /* >= 1.7 */
186         nsIURI target = new nsIURI (result[0]);
187         result[0] = 0;
188         int /*long*/ aPath = XPCOM.nsEmbedCString_new ();
189         rc = target.GetPath (aPath);
190         if (rc != XPCOM.NS_OK) Mozilla.error (rc);
191         length = XPCOM.nsEmbedCString_Length (aPath);
192         buffer = XPCOM.nsEmbedCString_get (aPath);
193         dest = new byte[length];
194         XPCOM.memmove (dest, buffer, length);
195         XPCOM.nsEmbedCString_delete (aPath);
196         filename = new String JavaDoc (dest);
197         int separator = filename.lastIndexOf (System.getProperty ("file.separator")); //$NON-NLS-1$
198
filename = filename.substring (separator + 1);
199         target.Release ();
200     } else { /* < 1.7 */
201         nsILocalFile target = new nsILocalFile (aTarget);
202         int /*long*/ aNativeTarget = XPCOM.nsEmbedCString_new ();
203         rc = target.GetNativeLeafName (aNativeTarget);
204         if (rc != XPCOM.NS_OK) Mozilla.error (rc);
205         length = XPCOM.nsEmbedCString_Length (aNativeTarget);
206         buffer = XPCOM.nsEmbedCString_get (aNativeTarget);
207         dest = new byte[length];
208         XPCOM.memmove (dest, buffer, length);
209         XPCOM.nsEmbedCString_delete (aNativeTarget);
210         filename = new String JavaDoc (dest);
211     }
212
213     Listener listener = new Listener () {
214         public void handleEvent (Event event) {
215             if (event.widget == cancel) {
216                 shell.close ();
217             }
218             if (helperAppLauncher != null) {
219                 helperAppLauncher.Cancel ();
220                 helperAppLauncher.Release ();
221             }
222             shell = null;
223             helperAppLauncher = null;
224         }
225     };
226     shell = new Shell (SWT.DIALOG_TRIM);
227     String JavaDoc msg = Compatibility.getMessage ("SWT_Download_File", new Object JavaDoc[] {filename}); //$NON-NLS-1$
228
shell.setText (msg);
229     GridLayout gridLayout = new GridLayout ();
230     gridLayout.marginHeight = 15;
231     gridLayout.marginWidth = 15;
232     gridLayout.verticalSpacing = 20;
233     shell.setLayout(gridLayout);
234     msg = Compatibility.getMessage ("SWT_Download_Location", new Object JavaDoc[] {filename, url}); //$NON-NLS-1$
235
new Label (shell, SWT.SIMPLE).setText (msg);
236     status = new Label (shell, SWT.SIMPLE);
237     msg = Compatibility.getMessage ("SWT_Download_Started"); //$NON-NLS-1$
238
status.setText (msg);
239     GridData data = new GridData ();
240     data.grabExcessHorizontalSpace = true;
241     data.grabExcessVerticalSpace = true;
242     status.setLayoutData (data);
243
244     cancel = new Button (shell, SWT.PUSH);
245     cancel.setText (SWT.getMessage ("SWT_Cancel")); //$NON-NLS-1$
246
data = new GridData ();
247     data.horizontalAlignment = GridData.CENTER;
248     cancel.setLayoutData (data);
249     cancel.addListener (SWT.Selection, listener);
250     shell.addListener (SWT.Close, listener);
251     shell.pack ();
252     shell.open ();
253     return XPCOM.NS_OK;
254 }
255
256 public int /*long*/ GetSource (int /*long*/ aSource) {
257     return XPCOM.NS_ERROR_NOT_IMPLEMENTED;
258 }
259
260 public int /*long*/ GetTarget (int /*long*/ aTarget) {
261     return XPCOM.NS_ERROR_NOT_IMPLEMENTED;
262 }
263
264 public int /*long*/ GetPersist (int /*long*/ aPersist) {
265     return XPCOM.NS_ERROR_NOT_IMPLEMENTED;
266 }
267
268 public int /*long*/ GetPercentComplete (int /*long*/ aPercentComplete) {
269     return XPCOM.NS_ERROR_NOT_IMPLEMENTED;
270 }
271
272 public int /*long*/ GetDisplayName (int /*long*/ aDisplayName) {
273     return XPCOM.NS_ERROR_NOT_IMPLEMENTED;
274 }
275
276 public int /*long*/ SetDisplayName (int /*long*/ aDisplayName) {
277     return XPCOM.NS_ERROR_NOT_IMPLEMENTED;
278 }
279
280 public int /*long*/ GetStartTime (int /*long*/ aStartTime) {
281     return XPCOM.NS_ERROR_NOT_IMPLEMENTED;
282 }
283
284 public int /*long*/ GetMIMEInfo (int /*long*/ aMIMEInfo) {
285     return XPCOM.NS_ERROR_NOT_IMPLEMENTED;
286 }
287
288 public int /*long*/ GetListener (int /*long*/ aListener) {
289     return XPCOM.NS_ERROR_NOT_IMPLEMENTED;
290 }
291
292 public int /*long*/ SetListener (int /*long*/ aListener) {
293     return XPCOM.NS_ERROR_NOT_IMPLEMENTED;
294 }
295
296 public int /*long*/ GetObserver (int /*long*/ aObserver) {
297     return XPCOM.NS_ERROR_NOT_IMPLEMENTED;
298 }
299
300 public int /*long*/ SetObserver (int /*long*/ aObserver) {
301     if (aObserver != 0) {
302         nsISupports supports = new nsISupports (aObserver);
303         int /*long*/[] result = new int /*long*/[1];
304         int rc = supports.QueryInterface (nsIHelperAppLauncher.NS_IHELPERAPPLAUNCHER_IID, result);
305         if (rc != XPCOM.NS_OK) Mozilla.error (rc);
306         if (result[0] == 0) Mozilla.error (XPCOM.NS_ERROR_NO_INTERFACE);
307         helperAppLauncher = new nsIHelperAppLauncher (result[0]);
308     }
309     return XPCOM.NS_OK;
310 }
311
312 /* nsIProgressDialog */
313 public int /*long*/ Open (int /*long*/ aParent) {
314     return XPCOM.NS_ERROR_NOT_IMPLEMENTED;
315 }
316
317 public int /*long*/ GetCancelDownloadOnClose (int /*long*/ aCancelDownloadOnClose) {
318     return XPCOM.NS_ERROR_NOT_IMPLEMENTED;
319 }
320
321 public int /*long*/ SetCancelDownloadOnClose (int /*long*/ aCancelDownloadOnClose) {
322     return XPCOM.NS_ERROR_NOT_IMPLEMENTED;
323 }
324
325 public int /*long*/ GetDialog (int /*long*/ aDialog) {
326     return XPCOM.NS_ERROR_NOT_IMPLEMENTED;
327 }
328
329 public int /*long*/ SetDialog (int /*long*/ aDialog) {
330     return XPCOM.NS_ERROR_NOT_IMPLEMENTED;
331 }
332
333 /* nsIWebProgressListener */
334
335 int /*long*/ OnStateChange (int /*long*/ aWebProgress, int /*long*/ aRequest, int /*long*/ aStateFlags, int /*long*/ aStatus) {
336     if ((aStateFlags & nsIWebProgressListener.STATE_STOP) != 0) {
337         if (helperAppLauncher != null) helperAppLauncher.Release ();
338         helperAppLauncher = null;
339         if (shell != null && !shell.isDisposed ()) shell.dispose ();
340         shell = null;
341     }
342     return XPCOM.NS_OK;
343 }
344
345 int /*long*/ OnProgressChange (int /*long*/ aWebProgress, int /*long*/ aRequest, int /*long*/ aCurSelfProgress, int /*long*/ aMaxSelfProgress, int /*long*/ aCurTotalProgress, int /*long*/ aMaxTotalProgress) {
346     int currentKBytes = (int)/*64*/aCurTotalProgress / 1024;
347     int totalKBytes = (int)/*64*/aMaxTotalProgress / 1024;
348     if (shell != null & !shell.isDisposed ()) {
349         Object JavaDoc[] arguments = {new Integer JavaDoc (currentKBytes), new Integer JavaDoc (totalKBytes)};
350         String JavaDoc statusMsg = Compatibility.getMessage ("SWT_Download_Status", arguments); //$NON-NLS-1$
351
status.setText (statusMsg);
352         shell.layout (true);
353         shell.getDisplay ().update ();
354     }
355     return XPCOM.NS_OK;
356 }
357
358 int /*long*/ OnLocationChange (int /*long*/ aWebProgress, int /*long*/ aRequest, int /*long*/ aLocation) {
359     return XPCOM.NS_OK;
360 }
361
362 int /*long*/ OnStatusChange (int /*long*/ aWebProgress, int /*long*/ aRequest, int /*long*/ aStatus, int /*long*/ aMessage) {
363     return XPCOM.NS_OK;
364 }
365
366 int /*long*/ OnSecurityChange (int /*long*/ aWebProgress, int /*long*/ aRequest, int /*long*/ state) {
367     return XPCOM.NS_OK;
368 }
369 }
370
Popular Tags