1 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 42 supports = new XPCOMObject (new int[] {2, 0, 0}) { 43 public int method0 (int [] args) {return QueryInterface (args[0], args[1]);} 44 public int method1 (int [] args) {return AddRef ();} 45 public int method2 (int [] 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 method0 (int [] args) {return QueryInterface (args[0], args[1]);} 50 public int method1 (int [] args) {return AddRef ();} 51 public int method2 (int [] args) {return Release ();} 52 public int method3 (int [] args) {return Init (args[0], args[1], args[2], args[3], args[4], args[5], args[6]);} 53 public int method4 (int [] args) {return GetSource (args[0]);} 54 public int method5 (int [] args) {return GetTarget (args[0]);} 55 public int method6 (int [] args) {return GetPersist (args[0]);} 56 public int method7 (int [] args) {return GetPercentComplete (args[0]);} 57 public int method8 (int [] args) {return GetDisplayName (args[0]);} 58 public int method9 (int [] args) {return SetDisplayName (args[0]);} 59 public int method10 (int [] args) {return GetStartTime (args[0]);} 60 public int method11 (int [] args) {return GetMIMEInfo (args[0]);} 61 public int method12 (int [] args) {return GetListener (args[0]);} 62 public int method13 (int [] args) {return SetListener (args[0]);} 63 public int method14 (int [] args) {return GetObserver (args[0]);} 64 public int method15 (int [] 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 method0 (int [] args) {return QueryInterface (args[0], args[1]);} 69 public int method1 (int [] args) {return AddRef ();} 70 public int method2 (int [] args) {return Release ();} 71 public int method3 (int [] args) {return Init (args[0], args[1], args[2], args[3], args[4], args[5], args[6]);} 72 public int method4 (int [] args) {return GetSource (args[0]);} 73 public int method5 (int [] args) {return GetTarget (args[0]);} 74 public int method6 (int [] args) {return GetPersist (args[0]);} 75 public int method7 (int [] args) {return GetPercentComplete (args[0]);} 76 public int method8 (int [] args) {return GetDisplayName (args[0]);} 77 public int method9 (int [] args) {return SetDisplayName (args[0]);} 78 public int method10 (int [] args) {return GetStartTime (args[0]);} 79 public int method11 (int [] args) {return GetMIMEInfo (args[0]);} 80 public int method12 (int [] args) {return GetListener (args[0]);} 81 public int method13 (int [] args) {return SetListener (args[0]);} 82 public int method14 (int [] args) {return GetObserver (args[0]);} 83 public int method15 (int [] args) {return SetObserver (args[0]);} 84 public int method16 (int [] args) {return Open (args[0]);} 85 public int method17 (int [] args) {return GetCancelDownloadOnClose (args[0]);} 86 public int method18 (int [] args) {return SetCancelDownloadOnClose (args[0]);} 87 public int method19 (int [] args) {return GetDialog (args[0]);} 88 public int method20 (int [] args) {return SetDialog (args[0]);} 89 }; 90 91 webProgressListener = new XPCOMObject (new int[] {2, 0, 0, 4, 6, 3, 4, 3}) { 92 public int method0 (int [] args) {return QueryInterface (args[0], args[1]);} 93 public int method1 (int [] args) {return AddRef ();} 94 public int method2 (int [] args) {return Release ();} 95 public int method3 (int [] args) {return OnStateChange (args[0], args[1], args[2], args[3]);} 96 public int method4 (int [] args) {return OnProgressChange (args[0], args[1], args[2], args[3], args[4], args[5]);} 97 public int method5 (int [] args) {return OnLocationChange (args[0], args[1], args[2]);} 98 public int method6 (int [] args) {return OnStatusChange (args[0], args[1], args[2], args[3]);} 99 public int method7 (int [] 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 getAddress () { 123 return progressDialog.getAddress (); 124 } 125 126 int QueryInterface (int riid, int 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 [] {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 [] {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 [] {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 [] {webProgressListener.getAddress ()}, C.PTR_SIZEOF); 148 AddRef(); 149 return XPCOM.NS_OK; 150 } 151 XPCOM.memmove (ppvObject, new int [] {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 162 163 164 public int Init (int aSource, int aTarget, int aDisplayName, int aMIMEInfo, int startTime1, int startTime2, int aPersist) { 165 nsIURI source = new nsIURI (aSource); 166 int 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 buffer = XPCOM.nsEmbedCString_get (aSpec); 171 byte[] dest = new byte[length]; 172 XPCOM.memmove (dest, buffer, length); 173 XPCOM.nsEmbedCString_delete (aSpec); 174 String url = new String (dest); 175 176 181 String filename = null; 182 nsISupports supports = new nsISupports (aTarget); 183 int [] result = new int [1]; 184 rc = supports.QueryInterface (nsIURI.NS_IURI_IID, result); 185 if (rc == 0) { 186 nsIURI target = new nsIURI (result[0]); 187 result[0] = 0; 188 int 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 (dest); 197 int separator = filename.lastIndexOf (System.getProperty ("file.separator")); filename = filename.substring (separator + 1); 199 target.Release (); 200 } else { 201 nsILocalFile target = new nsILocalFile (aTarget); 202 int 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 (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 msg = Compatibility.getMessage ("SWT_Download_File", new Object [] {filename}); 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 [] {filename, url}); new Label (shell, SWT.SIMPLE).setText (msg); 236 status = new Label (shell, SWT.SIMPLE); 237 msg = Compatibility.getMessage ("SWT_Download_Started"); 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")); 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 GetSource (int aSource) { 257 return XPCOM.NS_ERROR_NOT_IMPLEMENTED; 258 } 259 260 public int GetTarget (int aTarget) { 261 return XPCOM.NS_ERROR_NOT_IMPLEMENTED; 262 } 263 264 public int GetPersist (int aPersist) { 265 return XPCOM.NS_ERROR_NOT_IMPLEMENTED; 266 } 267 268 public int GetPercentComplete (int aPercentComplete) { 269 return XPCOM.NS_ERROR_NOT_IMPLEMENTED; 270 } 271 272 public int GetDisplayName (int aDisplayName) { 273 return XPCOM.NS_ERROR_NOT_IMPLEMENTED; 274 } 275 276 public int SetDisplayName (int aDisplayName) { 277 return XPCOM.NS_ERROR_NOT_IMPLEMENTED; 278 } 279 280 public int GetStartTime (int aStartTime) { 281 return XPCOM.NS_ERROR_NOT_IMPLEMENTED; 282 } 283 284 public int GetMIMEInfo (int aMIMEInfo) { 285 return XPCOM.NS_ERROR_NOT_IMPLEMENTED; 286 } 287 288 public int GetListener (int aListener) { 289 return XPCOM.NS_ERROR_NOT_IMPLEMENTED; 290 } 291 292 public int SetListener (int aListener) { 293 return XPCOM.NS_ERROR_NOT_IMPLEMENTED; 294 } 295 296 public int GetObserver (int aObserver) { 297 return XPCOM.NS_ERROR_NOT_IMPLEMENTED; 298 } 299 300 public int SetObserver (int aObserver) { 301 if (aObserver != 0) { 302 nsISupports supports = new nsISupports (aObserver); 303 int [] result = new int [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 313 public int Open (int aParent) { 314 return XPCOM.NS_ERROR_NOT_IMPLEMENTED; 315 } 316 317 public int GetCancelDownloadOnClose (int aCancelDownloadOnClose) { 318 return XPCOM.NS_ERROR_NOT_IMPLEMENTED; 319 } 320 321 public int SetCancelDownloadOnClose (int aCancelDownloadOnClose) { 322 return XPCOM.NS_ERROR_NOT_IMPLEMENTED; 323 } 324 325 public int GetDialog (int aDialog) { 326 return XPCOM.NS_ERROR_NOT_IMPLEMENTED; 327 } 328 329 public int SetDialog (int aDialog) { 330 return XPCOM.NS_ERROR_NOT_IMPLEMENTED; 331 } 332 333 334 335 int OnStateChange (int aWebProgress, int aRequest, int aStateFlags, int 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 OnProgressChange (int aWebProgress, int aRequest, int aCurSelfProgress, int aMaxSelfProgress, int aCurTotalProgress, int aMaxTotalProgress) { 346 int currentKBytes = (int)aCurTotalProgress / 1024; 347 int totalKBytes = (int)aMaxTotalProgress / 1024; 348 if (shell != null & !shell.isDisposed ()) { 349 Object [] arguments = {new Integer (currentKBytes), new Integer (totalKBytes)}; 350 String statusMsg = Compatibility.getMessage ("SWT_Download_Status", arguments); status.setText (statusMsg); 352 shell.layout (true); 353 shell.getDisplay ().update (); 354 } 355 return XPCOM.NS_OK; 356 } 357 358 int OnLocationChange (int aWebProgress, int aRequest, int aLocation) { 359 return XPCOM.NS_OK; 360 } 361 362 int OnStatusChange (int aWebProgress, int aRequest, int aStatus, int aMessage) { 363 return XPCOM.NS_OK; 364 } 365 366 int OnSecurityChange (int aWebProgress, int aRequest, int state) { 367 return XPCOM.NS_OK; 368 } 369 } 370 | Popular Tags |