1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 : /* ***** BEGIN LICENSE BLOCK *****
3 : * Version: MPL 1.1/GPL 2.0/LGPL 2.1
4 : *
5 : * The contents of this file are subject to the Mozilla Public License Version
6 : * 1.1 (the "License"); you may not use this file except in compliance with
7 : * the License. You may obtain a copy of the License at
8 : * http://www.mozilla.org/MPL/
9 : *
10 : * Software distributed under the License is distributed on an "AS IS" basis,
11 : * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12 : * for the specific language governing rights and limitations under the
13 : * License.
14 : *
15 : * The Original Code is mozilla.org code.
16 : *
17 : * The Initial Developer of the Original Code is
18 : * Netscape Communications Corporation.
19 : * Portions created by the Initial Developer are Copyright (C) 1998
20 : * the Initial Developer. All Rights Reserved.
21 : *
22 : * Contributor(s):
23 : * Blake Ross <blaker@netscape.com> (Original Author)
24 : *
25 : * Alternatively, the contents of this file may be used under the terms of
26 : * either the GNU General Public License Version 2 or later (the "GPL"), or
27 : * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
28 : * in which case the provisions of the GPL or the LGPL are applicable instead
29 : * of those above. If you wish to allow use of your version of this file only
30 : * under the terms of either the GPL or the LGPL, and not to allow others to
31 : * use your version of this file under the terms of the MPL, indicate your
32 : * decision by deleting the provisions above and replace them with the notice
33 : * and other provisions required by the GPL or the LGPL. If you do not delete
34 : * the provisions above, a recipient may use your version of this file under
35 : * the terms of any one of the MPL, the GPL or the LGPL.
36 : *
37 : * ***** END LICENSE BLOCK ***** */
38 :
39 : #ifndef downloadproxy___h___
40 : #define downloadproxy___h___
41 :
42 : #include "nsIDownloadManager.h"
43 : #include "nsIPrefBranch.h"
44 : #include "nsIPrefService.h"
45 : #include "nsIMIMEInfo.h"
46 : #include "nsIFileURL.h"
47 : #include "nsIDownloadManagerUI.h"
48 :
49 : #define PREF_BDM_SHOWWHENSTARTING "browser.download.manager.showWhenStarting"
50 : #define PREF_BDM_FOCUSWHENSTARTING "browser.download.manager.focusWhenStarting"
51 :
52 : class nsDownloadProxy : public nsITransfer
53 : {
54 : public:
55 :
56 6 : nsDownloadProxy() { }
57 24 : virtual ~nsDownloadProxy() { }
58 :
59 : NS_DECL_ISUPPORTS
60 :
61 6 : NS_IMETHODIMP Init(nsIURI* aSource,
62 : nsIURI* aTarget,
63 : const nsAString& aDisplayName,
64 : nsIMIMEInfo *aMIMEInfo,
65 : PRTime aStartTime,
66 : nsILocalFile* aTempFile,
67 : nsICancelable* aCancelable) {
68 : nsresult rv;
69 12 : nsCOMPtr<nsIDownloadManager> dm = do_GetService("@mozilla.org/download-manager;1", &rv);
70 6 : NS_ENSURE_SUCCESS(rv, rv);
71 :
72 6 : rv = dm->AddDownload(nsIDownloadManager::DOWNLOAD_TYPE_DOWNLOAD, aSource,
73 : aTarget, aDisplayName, aMIMEInfo, aStartTime,
74 6 : aTempFile, aCancelable, getter_AddRefs(mInner));
75 6 : NS_ENSURE_SUCCESS(rv, rv);
76 :
77 12 : nsCOMPtr<nsIPrefService> prefs = do_GetService("@mozilla.org/preferences-service;1", &rv);
78 6 : NS_ENSURE_SUCCESS(rv, rv);
79 12 : nsCOMPtr<nsIPrefBranch> branch = do_QueryInterface(prefs);
80 :
81 6 : bool showDM = true;
82 6 : if (branch)
83 6 : branch->GetBoolPref(PREF_BDM_SHOWWHENSTARTING, &showDM);
84 :
85 6 : if (showDM) {
86 : PRUint32 id;
87 0 : mInner->GetId(&id);
88 :
89 : nsCOMPtr<nsIDownloadManagerUI> dmui =
90 0 : do_GetService("@mozilla.org/download-manager-ui;1", &rv);
91 0 : NS_ENSURE_SUCCESS(rv, rv);
92 :
93 : bool visible;
94 0 : rv = dmui->GetVisible(&visible);
95 0 : NS_ENSURE_SUCCESS(rv, rv);
96 :
97 0 : bool focusWhenStarting = true;
98 0 : if (branch)
99 0 : (void)branch->GetBoolPref(PREF_BDM_FOCUSWHENSTARTING, &focusWhenStarting);
100 :
101 0 : if (visible && !focusWhenStarting)
102 0 : return NS_OK;
103 :
104 0 : return dmui->Show(nsnull, id, nsIDownloadManagerUI::REASON_NEW_DOWNLOAD);
105 : }
106 6 : return rv;
107 : }
108 :
109 8 : NS_IMETHODIMP OnStateChange(nsIWebProgress* aWebProgress,
110 : nsIRequest* aRequest, PRUint32 aStateFlags,
111 : PRUint32 aStatus)
112 : {
113 8 : NS_ENSURE_TRUE(mInner, NS_ERROR_NOT_INITIALIZED);
114 8 : return mInner->OnStateChange(aWebProgress, aRequest, aStateFlags, aStatus);
115 : }
116 :
117 0 : NS_IMETHODIMP OnStatusChange(nsIWebProgress *aWebProgress,
118 : nsIRequest *aRequest, nsresult aStatus,
119 : const PRUnichar *aMessage)
120 : {
121 0 : NS_ENSURE_TRUE(mInner, NS_ERROR_NOT_INITIALIZED);
122 0 : return mInner->OnStatusChange(aWebProgress, aRequest, aStatus, aMessage);
123 : }
124 :
125 0 : NS_IMETHODIMP OnLocationChange(nsIWebProgress *aWebProgress,
126 : nsIRequest *aRequest, nsIURI *aLocation,
127 : PRUint32 aFlags)
128 : {
129 0 : NS_ENSURE_TRUE(mInner, NS_ERROR_NOT_INITIALIZED);
130 0 : return mInner->OnLocationChange(aWebProgress, aRequest, aLocation, aFlags);
131 : }
132 :
133 0 : NS_IMETHODIMP OnProgressChange(nsIWebProgress *aWebProgress,
134 : nsIRequest *aRequest,
135 : PRInt32 aCurSelfProgress,
136 : PRInt32 aMaxSelfProgress,
137 : PRInt32 aCurTotalProgress,
138 : PRInt32 aMaxTotalProgress)
139 : {
140 0 : NS_ENSURE_TRUE(mInner, NS_ERROR_NOT_INITIALIZED);
141 0 : return mInner->OnProgressChange(aWebProgress, aRequest,
142 : aCurSelfProgress,
143 : aMaxSelfProgress,
144 : aCurTotalProgress,
145 0 : aMaxTotalProgress);
146 : }
147 :
148 8 : NS_IMETHODIMP OnProgressChange64(nsIWebProgress *aWebProgress,
149 : nsIRequest *aRequest,
150 : PRInt64 aCurSelfProgress,
151 : PRInt64 aMaxSelfProgress,
152 : PRInt64 aCurTotalProgress,
153 : PRInt64 aMaxTotalProgress)
154 : {
155 8 : NS_ENSURE_TRUE(mInner, NS_ERROR_NOT_INITIALIZED);
156 8 : return mInner->OnProgressChange64(aWebProgress, aRequest,
157 : aCurSelfProgress,
158 : aMaxSelfProgress,
159 : aCurTotalProgress,
160 8 : aMaxTotalProgress);
161 : }
162 :
163 0 : NS_IMETHODIMP OnRefreshAttempted(nsIWebProgress *aWebProgress,
164 : nsIURI *aUri,
165 : PRInt32 aDelay,
166 : bool aSameUri,
167 : bool *allowRefresh)
168 : {
169 0 : *allowRefresh = true;
170 0 : return NS_OK;
171 : }
172 :
173 0 : NS_IMETHODIMP OnSecurityChange(nsIWebProgress *aWebProgress,
174 : nsIRequest *aRequest, PRUint32 aState)
175 : {
176 0 : NS_ENSURE_TRUE(mInner, NS_ERROR_NOT_INITIALIZED);
177 0 : return mInner->OnSecurityChange(aWebProgress, aRequest, aState);
178 : }
179 :
180 : private:
181 : nsCOMPtr<nsIDownload> mInner;
182 : };
183 :
184 78 : NS_IMPL_ISUPPORTS3(nsDownloadProxy, nsITransfer,
185 : nsIWebProgressListener, nsIWebProgressListener2)
186 :
187 : #endif
|