Back to index

lightning-sunbird  0.9+nobinonly
nsDownloadManager.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>
00024  *   Ben Goodger <ben@netscape.com>
00025  *
00026  * Alternatively, the contents of this file may be used under the terms of
00027  * either of the GNU General Public License Version 2 or later (the "GPL"),
00028  * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00029  * in which case the provisions of the GPL or the LGPL are applicable instead
00030  * of those above. If you wish to allow use of your version of this file only
00031  * under the terms of either the GPL or the LGPL, and not to allow others to
00032  * use your version of this file under the terms of the MPL, indicate your
00033  * decision by deleting the provisions above and replace them with the notice
00034  * and other provisions required by the GPL or the LGPL. If you do not delete
00035  * the provisions above, a recipient may use your version of this file under
00036  * the terms of any one of the MPL, the GPL or the LGPL.
00037  *
00038  * ***** END LICENSE BLOCK ***** */
00039  
00040 #ifndef downloadmanager___h___
00041 #define downloadmanager___h___
00042 
00043 #include "nsIDownloadManager.h"
00044 #include "nsIDownloadProgressListener.h"
00045 #include "nsIDownload.h"
00046 #include "nsIRDFDataSource.h"
00047 #include "nsIRDFRemoteDataSource.h"
00048 #include "nsIRDFService.h"
00049 #include "nsIDOMDocument.h"
00050 #include "nsIDOMEventListener.h"
00051 #include "nsIRDFContainerUtils.h"
00052 #include "nsIWebProgressListener.h"
00053 #include "nsIURI.h"
00054 #include "nsILocalFile.h"
00055 #include "nsRefPtrHashtable.h"
00056 #include "nsIRequest.h"
00057 #include "nsIObserver.h"
00058 #include "nsIStringBundle.h"
00059 #include "nsIProgressDialog.h"
00060 #include "nsIMIMEInfo.h"
00061 #include "nsISound.h"
00062 #include "nsAutoPtr.h"
00063  
00064 enum DownloadState { NOTSTARTED = -1, DOWNLOADING, FINISHED, FAILED, CANCELED };
00065 
00066 class nsDownload;
00067 
00068 class nsDownloadManager : public nsIDownloadManager,
00069                           public nsIDOMEventListener,
00070                           public nsIObserver
00071 {
00072 public:
00073   NS_DECL_ISUPPORTS
00074   NS_DECL_NSIDOWNLOADMANAGER
00075   NS_DECL_NSIDOMEVENTLISTENER
00076   NS_DECL_NSIOBSERVER
00077 
00078   nsresult Init();
00079 
00080   nsDownloadManager();
00081   ~nsDownloadManager();
00082 
00083 protected:
00084   nsresult GetDownloadsContainer(nsIRDFContainer** aResult);
00085   nsresult GetProfileDownloadsFileURL(nsCString& aDownloadsFileURL);
00086   nsresult GetInternalListener(nsIDownloadProgressListener** aInternalListener);
00087   nsresult GetDataSource(nsIRDFDataSource** aDataSource);
00088   nsresult AssertProgressInfo();
00089   nsresult AssertProgressInfoFor(const nsACString& aTargetPath);
00090   nsresult DownloadStarted(const nsACString& aTargetPath);
00091   nsresult DownloadEnded(const nsACString& aTargetPath, const PRUnichar* aMessage);
00092   PRBool MustUpdateUI() { if (mDocument) return PR_TRUE; return PR_FALSE; }
00093 
00094   nsCOMPtr<nsISound> mSoundInterface;
00095 
00096 private:
00097   nsCOMPtr<nsIRDFDataSource> mDataSource;
00098   nsCOMPtr<nsIDOMDocument> mDocument;
00099   nsCOMPtr<nsIRDFContainer> mDownloadsContainer;
00100   nsCOMPtr<nsIDownloadProgressListener> mListener;
00101   nsCOMPtr<nsIRDFContainerUtils> mRDFContainerUtils;
00102   nsCOMPtr<nsIStringBundle> mBundle;
00103   PRInt32 mBatches;
00104   nsRefPtrHashtable<nsCStringHashKey, nsDownload> mCurrDownloads;
00105 
00106   friend class nsDownload;
00107 };
00108 
00109 class nsDownload : public nsIDownload_MOZILLA_1_8_BRANCH,
00110                    public nsIObserver
00111 {
00112 public:
00113   NS_DECL_NSIWEBPROGRESSLISTENER
00114   NS_DECL_NSIWEBPROGRESSLISTENER2
00115   NS_DECL_NSITRANSFER
00116   NS_DECL_NSIDOWNLOAD
00117   NS_DECL_NSIDOWNLOAD_MOZILLA_1_8_BRANCH
00118   NS_DECL_NSIOBSERVER
00119   NS_DECL_ISUPPORTS
00120 
00121   nsDownload(nsDownloadManager* aManager, nsIURI* aTarget, nsIURI* aSource,
00122              nsICancelable* aCancelable);
00123   ~nsDownload();
00124 
00125   nsresult Cancel();
00126   nsresult Suspend();
00127   nsresult SetDisplayName(const PRUnichar* aDisplayName);
00128   nsresult SetTempFile(nsILocalFile* aTempFile);
00129   nsresult Resume();
00130   void DisplayDownloadFinishedAlert();
00131 
00132   void SetDialogListener(nsIWebProgressListener2* aInternalListener) {
00133     mDialogListener = aInternalListener;
00134   }
00135   void SetDialog(nsIProgressDialog* aDialog) {
00136     mDialog = aDialog;
00137   }
00138   // May return null
00139   nsIProgressDialog* GetDialog() {
00140     return mDialog;
00141   }
00142 
00143   struct TransferInformation {
00144     PRUint64 mCurrBytes, mMaxBytes;
00145     TransferInformation(PRUint64 aCurr, PRUint64 aMax) :
00146       mCurrBytes(aCurr),
00147       mMaxBytes(aMax)
00148       {}
00149   };
00150 
00151   TransferInformation GetTransferInformation() {
00152     return TransferInformation(mCurrBytes, mMaxBytes);
00153   }
00154   DownloadState GetDownloadState() {
00155     return mDownloadState;
00156   }
00157   void SetDownloadState(DownloadState aState) {
00158     mDownloadState = aState;
00159   }
00160   void SetMIMEInfo(nsIMIMEInfo* aMIMEInfo) {
00161     mMIMEInfo = aMIMEInfo;
00162   }
00163   void SetStartTime(PRInt64 aStartTime) {
00164     mStartTime = aStartTime;
00165     mLastUpdate = aStartTime;
00166   }
00167 private:
00168   nsRefPtr<nsDownloadManager> mDownloadManager;
00169 
00170   nsString mDisplayName;
00171 
00172   nsCOMPtr<nsIURI> mTarget;
00173   nsCOMPtr<nsIURI> mSource;
00174   nsCOMPtr<nsIWebProgressListener2> mDialogListener;
00175   nsCOMPtr<nsICancelable> mCancelable;
00176   nsCOMPtr<nsIRequest> mRequest;
00177   nsCOMPtr<nsIProgressDialog> mDialog;
00178   nsCOMPtr<nsIMIMEInfo> mMIMEInfo;
00179   nsCOMPtr<nsILocalFile> mTempFile;
00180   DownloadState mDownloadState;
00181 
00182   PRInt32 mPercentComplete;
00183   PRUint64 mCurrBytes;
00184   PRUint64 mMaxBytes;
00185   PRTime mStartTime;
00186   PRTime mLastUpdate;
00187   double mSpeed;
00188 };
00189 
00190 #endif