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 org.eclipse.swt.browser.Download_1_8;
14 
15 import java.lang.all;
16 
17 import org.eclipse.swt.SWT;
18 
19 import XPCOM = org.eclipse.swt.internal.mozilla.XPCOM;
20 
21 import org.eclipse.swt.internal.mozilla.Common;
22 import org.eclipse.swt.internal.mozilla.prtime;
23 import org.eclipse.swt.internal.mozilla.nsICancelable;
24 import org.eclipse.swt.internal.mozilla.nsID;
25 import org.eclipse.swt.internal.mozilla.nsIDownload_1_8;
26 import org.eclipse.swt.internal.mozilla.nsIProgressDialog_1_8;
27 import org.eclipse.swt.internal.mozilla.nsISupports;
28 import org.eclipse.swt.internal.mozilla.nsIURI;
29 import org.eclipse.swt.internal.mozilla.nsIWebProgressListener;
30 import org.eclipse.swt.internal.mozilla.nsIMIMEInfo;
31 import org.eclipse.swt.internal.mozilla.nsIObserver;
32 import org.eclipse.swt.internal.mozilla.nsIDOMWindow;
33 import org.eclipse.swt.internal.mozilla.nsIWebProgress;
34 import org.eclipse.swt.internal.mozilla.nsIRequest;
35 import org.eclipse.swt.internal.mozilla.nsILocalFile;
36 import org.eclipse.swt.internal.mozilla.nsStringAPI;
37 import org.eclipse.swt.internal.mozilla.nsEmbedString;
38 
39 import org.eclipse.swt.browser.Mozilla;
40 
41 import org.eclipse.swt.layout.GridData;
42 import org.eclipse.swt.layout.GridLayout;
43 import org.eclipse.swt.widgets.Button;
44 import org.eclipse.swt.widgets.Event;
45 import org.eclipse.swt.widgets.Label;
46 import org.eclipse.swt.widgets.Listener;
47 import org.eclipse.swt.widgets.Shell;
48 
49 class Download_1_8 : nsIProgressDialog_1_8 {
50     nsICancelable cancelable;
51     int refCount = 0;
52 
53     Shell shell;
54     Label status;
55     Button cancel;
56 
57     //static const bool is32 = C.PTR_SIZEOF is 4; //determine if 32 or 64 bit platform?
58 
59 this () {
60 }
61 
62 extern(System)
63 nsrefcnt AddRef () {
64     refCount++;
65     return refCount;
66 }
67 
68 extern(System)
69 nsresult QueryInterface ( in cnsID* riid, void** ppvObject) {
70     if (riid is null || ppvObject is null) return XPCOM.NS_ERROR_NO_INTERFACE;
71    
72     if (*riid == nsISupports.IID) {
73         *ppvObject = cast(void*)cast(nsISupports)this;
74         AddRef();
75         return XPCOM.NS_OK;
76     }
77     if (*riid == nsIDownload_1_8.IID) {
78         *ppvObject = cast(void*)cast(nsIDownload_1_8)this;
79         AddRef();
80         return XPCOM.NS_OK;
81     }
82     if (*riid == nsIProgressDialog_1_8.IID) {
83         *ppvObject = cast(void*)cast(nsIProgressDialog_1_8)this;
84         AddRef();
85         return XPCOM.NS_OK;
86     }
87     if (*riid == nsIWebProgressListener.IID) {
88         *ppvObject = cast(void*)cast(nsIWebProgressListener)this;
89         AddRef();
90         return XPCOM.NS_OK;
91     }
92     *ppvObject = null;
93     return XPCOM.NS_ERROR_NO_INTERFACE;
94 }
95 
96 extern(System)
97 nsrefcnt Release () {
98     refCount--;
99     if (refCount is 0) return 0;
100     return refCount;
101 }
102 
103 /* nsIDownload */
104 
105 /* Note. The argument startTime is defined as a PRInt64. This translates into two java ints. */
106 extern(System)
107 nsresult Init_32 (nsIURI aSource, nsIURI aTarget, nsAString* aDisplayName, nsIMIMEInfo aMIMEInfo, PRInt32 startTime1, PRInt32 startTime2, nsILocalFile aTempFile, nsICancelable aCancelable) {
108     long startTime = (startTime2 << 32) + startTime1;
109     return Init (aSource, aTarget, aDisplayName, aMIMEInfo, startTime, aTempFile, aCancelable);
110 }
111 
112 // FIXME: I've hardcoded the string values in place of Compatibility.getMessage calls in 
113 // the Init method; this will need fixing in future releases. -JJR
114 extern(System)
115 nsresult Init (nsIURI aSource, nsIURI aTarget, nsAString* aDisplayName, nsIMIMEInfo aMIMEInfo, PRTime startTime, nsILocalFile aTempFile, nsICancelable aCancelable) {
116     cancelable = aCancelable;
117     // nsIURI source = new nsIURI (aSource);
118     scope auto aSpec = new nsEmbedCString;
119     int rc = aSource.GetHost (cast(nsACString*)aSpec);
120     if (rc !is XPCOM.NS_OK) Mozilla.error(rc,__FILE__,__LINE__);
121     //int length = XPCOM.nsEmbedCString_Length (aSpec);
122     //ptrdiff_t buffer = XPCOM.nsEmbedCString_get (aSpec);
123     //byte[] dest = new byte[length];
124     //XPCOM.memmove (dest, buffer, length);
125     //XPCOM.nsEmbedCString_delete (aSpec);
126     String url = aSpec.toString;
127 
128     //nsIURI target = new nsIURI (aTarget);
129     scope auto aPath = new nsEmbedCString;
130     rc = aTarget.GetPath (cast(nsACString*)aPath);
131     if (rc !is XPCOM.NS_OK) Mozilla.error (rc);
132     //length = XPCOM.nsEmbedCString_Length (aPath);
133     //buffer = XPCOM.nsEmbedCString_get (aPath);
134     //dest = new byte[length];
135     //XPCOM.memmove (dest, buffer, length);
136     //XPCOM.nsEmbedCString_delete (aPath);
137     String filename = aPath.toString;
138     //int separator = locatePrior(filename, System.getProperty ("file.separator"));
139     int separator = filename.lastIndexOf (System.getProperty ("file.separator"));   //$NON-NLS-1$
140     // NOTE: Not sure if this is correct; watch out for bugs here. -JJR
141     filename = filename.substring (separator + 1);
142 
143     Listener listener = new class() Listener {
144         public void handleEvent (Event event) {
145             if (event.widget is cancel) {
146                 shell.close ();
147             }
148             if (cancelable !is null) {
149                 int rc = cancelable.Cancel (XPCOM.NS_BINDING_ABORTED);
150                 if (rc !is XPCOM.NS_OK) Mozilla.error (rc,__FILE__,__LINE__);
151             }
152             shell = null;
153             cancelable = null;
154         }
155     };
156     shell = new Shell (SWT.DIALOG_TRIM);
157 // FIXME: A working Compatibility.getMessage has not been ported yet
158 // Strings hardcoded for now.
159     //String msg = Compatibility.getMessage ("SWT_Download_File", new Object[] {filename}); //$NON-NLS-1$
160     shell.setText ("Download: " ~ filename);
161     GridLayout gridLayout = new GridLayout ();
162     gridLayout.marginHeight = 15;
163     gridLayout.marginWidth = 15;
164     gridLayout.verticalSpacing = 20;
165     shell.setLayout (gridLayout);
166     //msg = Compatibility.getMessage ("SWT_Download_Location", new Object[] {filename, url}); //$NON-NLS-1$
167     auto lbl = new Label (shell, SWT.SIMPLE);
168     lbl.setText ("Saving " ~ filename ~ " from " ~ url );
169     status = new Label (shell, SWT.SIMPLE);
170     //msg = Compatibility.getMessage ("SWT_Download_Started"); //$NON-NLS-1$
171     status.setText ("Downloading...");
172     GridData data = new GridData ();
173     data.grabExcessHorizontalSpace = true;
174     data.grabExcessVerticalSpace = true;
175     status.setLayoutData (data);
176     
177     cancel = new Button (shell, SWT.PUSH);
178     cancel.setText( "Cancel" ); 
179     //cancel.setText (SWT.getMessage ("SWT_Cancel")); //$NON-NLS-1$
180     data = new GridData ();
181     data.horizontalAlignment = GridData.CENTER;
182     cancel.setLayoutData (data);
183     cancel.addListener (SWT.Selection, listener);
184     shell.addListener (SWT.Close, listener);
185     shell.pack ();
186     shell.open ();
187     return XPCOM.NS_OK;
188 }
189 
190 extern(System)
191 nsresult GetAmountTransferred (PRUint64* arg0) {
192     return XPCOM.NS_ERROR_NOT_IMPLEMENTED;
193 }
194 
195 extern(System)
196 nsresult GetCancelable (nsICancelable* arg0) {
197     return XPCOM.NS_ERROR_NOT_IMPLEMENTED;
198 }
199 
200 extern(System)
201 nsresult GetDisplayName (PRUnichar** aDisplayName) {
202     return XPCOM.NS_ERROR_NOT_IMPLEMENTED;
203 }
204 
205 extern(System)
206 nsresult GetMIMEInfo (nsIMIMEInfo* aMIMEInfo) {
207     return XPCOM.NS_ERROR_NOT_IMPLEMENTED;
208 }
209 
210 extern(System)
211 nsresult GetPercentComplete (PRInt32* aPercentComplete) {
212     return XPCOM.NS_ERROR_NOT_IMPLEMENTED;
213 }
214 
215 extern(System)
216 nsresult GetSize (PRUint64* arg0) {
217     return XPCOM.NS_ERROR_NOT_IMPLEMENTED;
218 }
219 
220 extern(System)
221 nsresult GetSource (nsIURI* aSource) {
222     return XPCOM.NS_ERROR_NOT_IMPLEMENTED;
223 }
224 
225 extern(System)
226 nsresult GetStartTime (PRInt64* aStartTime) {
227     return XPCOM.NS_ERROR_NOT_IMPLEMENTED;
228 }
229 
230 extern(System)
231 nsresult GetTarget (nsIURI* aTarget) {
232     return XPCOM.NS_ERROR_NOT_IMPLEMENTED;
233 }
234 
235 extern(System)
236 nsresult GetTargetFile (nsILocalFile* arg0) {
237     return XPCOM.NS_ERROR_NOT_IMPLEMENTED;
238 }
239 
240 /* nsIProgressDialog */
241 
242 extern(System)
243 nsresult GetCancelDownloadOnClose (PRBool* aCancelDownloadOnClose) {
244     return XPCOM.NS_ERROR_NOT_IMPLEMENTED;
245 }
246 
247 extern(System)
248 nsresult GetDialog (nsIDOMWindow* aDialog) {
249     return XPCOM.NS_ERROR_NOT_IMPLEMENTED;
250 }
251 
252 extern(System)
253 nsresult GetObserver (nsIObserver* aObserver) {
254     return XPCOM.NS_ERROR_NOT_IMPLEMENTED;
255 }
256 
257 extern(System)
258 nsresult Open (nsIDOMWindow aParent) {
259     return XPCOM.NS_ERROR_NOT_IMPLEMENTED;
260 }
261 
262 extern(System)
263 nsresult SetCancelDownloadOnClose (PRBool aCancelDownloadOnClose) {
264     return XPCOM.NS_ERROR_NOT_IMPLEMENTED;
265 }
266 
267 extern(System)
268 nsresult SetDialog (nsIDOMWindow aDialog) {
269     return XPCOM.NS_ERROR_NOT_IMPLEMENTED;
270 }
271 
272 extern(System)
273 nsresult SetObserver (nsIObserver aObserver) {
274     return XPCOM.NS_ERROR_NOT_IMPLEMENTED;
275 }
276 
277 /* nsIWebProgressListener */
278 
279 extern(System)
280 nsresult OnLocationChange (nsIWebProgress aWebProgress, nsIRequest aRequest, nsIURI aLocation) {
281     return XPCOM.NS_OK;
282 }
283 
284 extern(System)
285 nsresult OnProgressChange (nsIWebProgress aWebProgress, nsIRequest aRequest, PRInt32 aCurSelfProgress, PRInt32 aMaxSelfProgress, PRInt32 aCurTotalProgress, PRInt32 aMaxTotalProgress) {
286     return OnProgressChange64 (aWebProgress, aRequest, aCurSelfProgress, aMaxSelfProgress, aCurTotalProgress, aMaxTotalProgress);
287 }
288 /++
289 /* Note. The last 4 args in the original interface are defined as PRInt64. These each translate into two java ints. */
290 nsresult OnProgressChange64_32 (ptrdiff_t aWebProgress, ptrdiff_t aRequest, ptrdiff_t aCurSelfProgress1, ptrdiff_t aCurSelfProgress2, ptrdiff_t aMaxSelfProgress1, ptrdiff_t aMaxSelfProgress2, ptrdiff_t aCurTotalProgress1, ptrdiff_t aCurTotalProgress2, ptrdiff_t aMaxTotalProgress1, ptrdiff_t aMaxTotalProgress2) {
291     long aCurSelfProgress = (aCurSelfProgress2 << 32) + aCurSelfProgress1;
292     long aMaxSelfProgress = (aMaxSelfProgress2 << 32) + aMaxSelfProgress1;
293     long aCurTotalProgress = (aCurTotalProgress2 << 32) + aCurTotalProgress1;
294     long aMaxTotalProgress = (aMaxTotalProgress2 << 32) + aMaxTotalProgress1;
295     return OnProgressChange64 (aWebProgress, aRequest, aCurSelfProgress, aMaxSelfProgress, aCurTotalProgress, aMaxTotalProgress);
296 }
297 ++/
298 extern(System)
299 nsresult OnProgressChange64 (nsIWebProgress aWebProgress, nsIRequest aRequest, PRInt64 aCurSelfProgress, PRInt64 aMaxSelfProgress, PRInt64 aCurTotalProgress, PRInt64 aMaxTotalProgress) {
300     long currentKBytes = aCurTotalProgress / 1024;
301     long totalKBytes = aMaxTotalProgress / 1024;
302     if (shell !is null && !shell.isDisposed ()) {
303         //Object[] arguments = {new Long (currentKBytes), new Long (totalKBytes)};
304         //String statusMsg = Compatibility.getMessage ("SWT_Download_Status", arguments); //$NON-NLS-1$
305         String statusMsg = Format("Download:  {0} KB of {1} KB", currentKBytes, totalKBytes); 
306         status.setText (statusMsg);
307         shell.layout (true);
308         shell.getDisplay ().update ();
309     }
310     return XPCOM.NS_OK;
311 }
312 
313 extern(System)
314 nsresult OnSecurityChange (nsIWebProgress aWebProgress, nsIRequest aRequest, PRUint32 state) {
315     return XPCOM.NS_OK;
316 }
317 
318 extern(System)
319 nsresult OnStateChange (nsIWebProgress aWebProgress, nsIRequest aRequest, PRUint32 aStateFlags, nsresult aStatus) {
320     if ((aStateFlags & nsIWebProgressListener.STATE_STOP) !is 0) {
321         cancelable = null;
322         if (shell !is null && !shell.isDisposed ()) shell.dispose ();
323         shell = null;
324     }
325     return XPCOM.NS_OK;
326 }   
327 
328 extern(System)
329 nsresult OnStatusChange (nsIWebProgress aWebProgress, nsIRequest aRequest, nsresult aStatus, PRUnichar* aMessage) {
330     return XPCOM.NS_OK;
331 }       
332 }