Back to index

lightning-sunbird  0.9+nobinonly
nsDownloadProxy.h
Go to the documentation of this file.
00001 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
00002 /* ***** BEGIN LICENSE BLOCK *****
00003  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00004  *
00005  * The contents of this file are subject to the Mozilla Public License Version
00006  * 1.1 (the "License"); you may not use this file except in compliance with
00007  * the License. You may obtain a copy of the License at
00008  * http://www.mozilla.org/MPL/
00009  *
00010  * Software distributed under the License is distributed on an "AS IS" basis,
00011  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00012  * for the specific language governing rights and limitations under the
00013  * License.
00014  *
00015  * The Original Code is mozilla.org code.
00016  *
00017  * The Initial Developer of the Original Code is
00018  * Netscape Communications Corporation.
00019  * Portions created by the Initial Developer are Copyright (C) 1998
00020  * the Initial Developer. All Rights Reserved.
00021  *
00022  * Contributor(s):
00023  *   Blake Ross <blaker@netscape.com> (Original Author)
00024  *
00025  * Alternatively, the contents of this file may be used under the terms of
00026  * either the GNU General Public License Version 2 or later (the "GPL"), or
00027  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00028  * in which case the provisions of the GPL or the LGPL are applicable instead
00029  * of those above. If you wish to allow use of your version of this file only
00030  * under the terms of either the GPL or the LGPL, and not to allow others to
00031  * use your version of this file under the terms of the MPL, indicate your
00032  * decision by deleting the provisions above and replace them with the notice
00033  * and other provisions required by the GPL or the LGPL. If you do not delete
00034  * the provisions above, a recipient may use your version of this file under
00035  * the terms of any one of the MPL, the GPL or the LGPL.
00036  *
00037  * ***** END LICENSE BLOCK ***** */
00038  
00039 #ifndef downloadproxy___h___
00040 #define downloadproxy___h___
00041 
00042 #include "nsIDownload.h"
00043 #include "nsIDownloadManager.h"
00044 #include "nsIPrefBranch.h"
00045 #include "nsIPrefService.h"
00046 #include "nsIMIMEInfo.h"
00047 #include "nsIFileURL.h"
00048 
00049 #define PREF_BDM_SHOWWHENSTARTING "browser.download.manager.showWhenStarting"
00050 #define PREF_BDM_USEWINDOW "browser.download.manager.useWindow"
00051 
00052 class nsDownloadProxy : public nsIDownload
00053 {
00054 public:
00055 
00056   nsDownloadProxy() { }
00057   virtual ~nsDownloadProxy() { }
00058 
00059   NS_DECL_ISUPPORTS
00060 
00061   NS_IMETHODIMP Init(nsIURI* aSource,
00062                      nsIURI* aTarget,
00063                      const nsAString& aDisplayName,
00064                      nsIMIMEInfo *aMIMEInfo,
00065                      PRTime aStartTime,
00066                      nsILocalFile* aTempFile,
00067                      nsICancelable* aCancelable) {
00068     nsresult rv;
00069     nsCOMPtr<nsIDownloadManager> dm = do_GetService("@mozilla.org/download-manager;1", &rv);
00070     if (NS_FAILED(rv))
00071       return rv;
00072     
00073     rv = dm->AddDownload(nsIDownloadManager::DOWNLOAD_TYPE_DOWNLOAD, aSource,
00074                          aTarget, aDisplayName, EmptyString(), aMIMEInfo,
00075                          aStartTime, aTempFile, aCancelable,
00076                          getter_AddRefs(mInner));
00077     if (NS_FAILED(rv)) return rv;
00078 
00079     nsCOMPtr<nsIPrefService> prefs = do_GetService("@mozilla.org/preferences-service;1", &rv);
00080     if (NS_FAILED(rv)) return rv;
00081     nsCOMPtr<nsIPrefBranch> branch = do_QueryInterface(prefs);
00082 
00083     PRBool showDM = PR_TRUE;
00084     branch->GetBoolPref(PREF_BDM_SHOWWHENSTARTING , &showDM);
00085 
00086     PRBool useWindow = PR_TRUE;
00087     branch->GetBoolPref(PREF_BDM_USEWINDOW, &useWindow);
00088     if (showDM && useWindow) {
00089       nsAutoString path;
00090 
00091       nsCOMPtr<nsIFileURL> fileURL = do_QueryInterface(aTarget, &rv);
00092       if (NS_FAILED(rv)) return rv;
00093 
00094       nsCOMPtr<nsIFile> file;
00095       rv = fileURL->GetFile(getter_AddRefs(file));
00096       if (NS_FAILED(rv)) return rv;
00097 
00098       rv = file->GetPath(path);
00099       if (NS_FAILED(rv)) return rv;
00100 
00101       return dm->Open(nsnull, path.get());
00102     }
00103     return rv;
00104   }
00105 
00106  
00107   NS_IMETHODIMP GetDisplayName(PRUnichar** aDisplayName)
00108   {
00109     return mInner->GetDisplayName(aDisplayName);
00110   }
00111   
00112   NS_IMETHODIMP GetMIMEInfo(nsIMIMEInfo** aMIMEInfo)
00113   {
00114     return mInner->GetMIMEInfo(aMIMEInfo);
00115   }
00116   
00117   NS_IMETHODIMP GetSource(nsIURI** aSource)
00118   {
00119     return mInner->GetSource(aSource);
00120   }
00121   
00122   NS_IMETHODIMP GetTarget(nsIURI** aTarget)
00123   {
00124     return mInner->GetTarget(aTarget);
00125   }
00126   
00127   NS_IMETHODIMP GetStartTime(PRInt64* aStartTime)
00128   {
00129     return mInner->GetStartTime(aStartTime);
00130   }
00131 
00132   NS_IMETHODIMP GetPercentComplete(PRInt32* aPercentComplete)
00133   {
00134     return mInner->GetPercentComplete(aPercentComplete);
00135   }
00136 
00137   NS_IMETHODIMP GetAmountTransferred(PRUint64* aAmountTransferred)
00138   {
00139     return mInner->GetAmountTransferred(aAmountTransferred);
00140   }
00141   
00142   NS_IMETHODIMP GetSize(PRUint64* aSize)
00143   {
00144     return mInner->GetSize(aSize);
00145   }
00146 
00147   NS_IMETHODIMP GetCancelable(nsICancelable** aCancelable)
00148   {
00149     return mInner->GetCancelable(aCancelable);
00150   }
00151 
00152   NS_IMETHODIMP GetTargetFile(nsILocalFile** aTargetFile)
00153   {
00154     return mInner->GetTargetFile(aTargetFile);
00155   }
00156 
00157   NS_IMETHODIMP OnStateChange(nsIWebProgress* aWebProgress,
00158                               nsIRequest* aRequest, PRUint32 aStateFlags,
00159                               PRUint32 aStatus)
00160   {
00161     nsCOMPtr<nsIWebProgressListener> listener = do_QueryInterface(mInner);
00162     if (listener)
00163       return listener->OnStateChange(aWebProgress, aRequest, aStateFlags, aStatus);
00164     return NS_OK;
00165   }
00166   
00167   NS_IMETHODIMP OnStatusChange(nsIWebProgress *aWebProgress,
00168                                nsIRequest *aRequest, nsresult aStatus,
00169                                const PRUnichar *aMessage)
00170   {
00171     nsCOMPtr<nsIWebProgressListener> listener = do_QueryInterface(mInner);
00172     if (listener)
00173       return listener->OnStatusChange(aWebProgress, aRequest, aStatus, aMessage);
00174     return NS_OK;
00175   }
00176 
00177   NS_IMETHODIMP OnLocationChange(nsIWebProgress *aWebProgress,
00178                                  nsIRequest *aRequest, nsIURI *aLocation)
00179   {
00180     nsCOMPtr<nsIWebProgressListener> listener = do_QueryInterface(mInner);
00181     if (listener)
00182       return listener->OnLocationChange(aWebProgress, aRequest, aLocation);
00183     return NS_OK;
00184   }
00185   
00186   NS_IMETHODIMP OnProgressChange(nsIWebProgress *aWebProgress,
00187                                  nsIRequest *aRequest,
00188                                  PRInt32 aCurSelfProgress,
00189                                  PRInt32 aMaxSelfProgress,
00190                                  PRInt32 aCurTotalProgress,
00191                                  PRInt32 aMaxTotalProgress)
00192   {
00193     nsCOMPtr<nsIWebProgressListener> listener = do_QueryInterface(mInner);
00194     if (listener)
00195       return listener->OnProgressChange(aWebProgress, aRequest,
00196                                         aCurSelfProgress,
00197                                         aMaxSelfProgress,
00198                                         aCurTotalProgress,
00199                                         aMaxTotalProgress);
00200     return NS_OK;
00201   }
00202 
00203   NS_IMETHODIMP OnProgressChange64(nsIWebProgress *aWebProgress,
00204                                    nsIRequest *aRequest,
00205                                    PRInt64 aCurSelfProgress,
00206                                    PRInt64 aMaxSelfProgress,
00207                                    PRInt64 aCurTotalProgress,
00208                                    PRInt64 aMaxTotalProgress)
00209   {
00210     nsCOMPtr<nsIWebProgressListener2> listener = do_QueryInterface(mInner);
00211     if (listener)
00212       return listener->OnProgressChange64(aWebProgress, aRequest,
00213                                           aCurSelfProgress,
00214                                           aMaxSelfProgress,
00215                                           aCurTotalProgress,
00216                                           aMaxTotalProgress);
00217     return NS_OK;
00218   }
00219 
00220 
00221 
00222   NS_IMETHODIMP OnSecurityChange(nsIWebProgress *aWebProgress,
00223                                  nsIRequest *aRequest, PRUint32 aState)
00224   {
00225     nsCOMPtr<nsIWebProgressListener> listener = do_QueryInterface(mInner);
00226     if (listener)
00227       return listener->OnSecurityChange(aWebProgress, aRequest, aState);
00228     return NS_OK;
00229   }
00230 
00231 private:
00232   nsCOMPtr<nsIDownload> mInner;
00233 };
00234 
00235 NS_IMPL_ISUPPORTS4(nsDownloadProxy, nsIDownload, nsITransfer,
00236                    nsIWebProgressListener, nsIWebProgressListener2)
00237 
00238 #endif