|
1 /** |
|
2 This file is part of CWRT package ** |
|
3 |
|
4 Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). ** |
|
5 |
|
6 This program is free software: you can redistribute it and/or modify |
|
7 it under the terms of the GNU (Lesser) General Public License as |
|
8 published by the Free Software Foundation, version 2.1 of the License. |
|
9 This program is distributed in the hope that it will be useful, but |
|
10 WITHOUT ANY WARRANTY; without even the implied warranty of |
|
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
|
12 (Lesser) General Public License for more details. You should have |
|
13 received a copy of the GNU (Lesser) General Public License along |
|
14 with this program. If not, see <http://www.gnu.org/licenses/>. |
|
15 */ |
|
16 |
|
17 #include "downloadmanager.h" |
|
18 #include "backgrounddownloadmanager.h" |
|
19 #include "download.h" |
|
20 #include "backgrounddownload.h" |
|
21 #include "downloadmanagerclient.h" |
|
22 #include "downloadevent.h" |
|
23 #include "downloadinfo.h" |
|
24 #include <QHash> |
|
25 #include <QCoreApplication> |
|
26 #include <QNetworkReply> |
|
27 |
|
28 class BackgroundDownloadManagerPrivate |
|
29 { |
|
30 //declare public implementation |
|
31 DM_DECLARE_PUBLIC(BackgroundDownloadManager); |
|
32 public: |
|
33 BackgroundDownloadManagerPrivate(); |
|
34 ~BackgroundDownloadManagerPrivate(); |
|
35 |
|
36 QList<Download*> m_downloads; // list of downloads |
|
37 QString m_clientName; // client name |
|
38 QObject *m_receiver; // event reciever |
|
39 DownloadManager* m_downloadMgr; // not owned |
|
40 DownloadManagerClient* m_downloadMgrClient; // owned |
|
41 DownloadInfo* m_dlInfo; // not owned |
|
42 }; |
|
43 |
|
44 BackgroundDownloadManagerPrivate::BackgroundDownloadManagerPrivate() |
|
45 { |
|
46 m_clientName = ""; |
|
47 m_receiver = 0; |
|
48 m_downloadMgr = 0; |
|
49 m_downloadMgrClient = 0; |
|
50 } |
|
51 |
|
52 BackgroundDownloadManagerPrivate::~BackgroundDownloadManagerPrivate() |
|
53 { |
|
54 if (m_downloadMgrClient) { |
|
55 delete m_downloadMgrClient; |
|
56 m_downloadMgrClient = 0; |
|
57 } |
|
58 m_downloadMgr = 0; |
|
59 m_downloads.clear(); |
|
60 } |
|
61 |
|
62 BackgroundDownloadManager::BackgroundDownloadManager(DownloadManager* downloadMgr) |
|
63 { |
|
64 DM_INITIALIZE(BackgroundDownloadManager); |
|
65 priv->m_downloadMgr = downloadMgr; |
|
66 if (downloadMgr) { |
|
67 priv->m_clientName = downloadMgr->getAttribute(DlMgrClientName).toString(); |
|
68 priv->m_dlInfo = downloadMgr->downloadInfo(); |
|
69 } |
|
70 } |
|
71 |
|
72 // destructor for the download manager |
|
73 BackgroundDownloadManager::~BackgroundDownloadManager() |
|
74 { |
|
75 DM_UNINITIALIZE(BackgroundDownloadManager); |
|
76 } |
|
77 |
|
78 // initialises all the downloads which belongs to last download manager session |
|
79 void BackgroundDownloadManager::init() |
|
80 { |
|
81 //load all downloads which were created in the last session |
|
82 return; |
|
83 } |
|
84 |
|
85 // returns new download |
|
86 // url indicates download url |
|
87 Download* BackgroundDownloadManager::createDownload(const QString& url, DownloadType type) |
|
88 { |
|
89 if (url.isEmpty()) |
|
90 return NULL; |
|
91 |
|
92 DM_PRIVATE(BackgroundDownloadManager); |
|
93 // connection to server will be established whrn DownloadManagerClient object is created |
|
94 // so we delay the constructon of this object till client calls some function |
|
95 if (!priv->m_downloadMgrClient) |
|
96 priv->m_downloadMgrClient = new DownloadManagerClient(priv->m_downloadMgr); |
|
97 // create downlaod at server |
|
98 int downloadId = priv->m_downloadMgrClient->createDownload(url, type); |
|
99 if (downloadId == INVALID_DL_ID) |
|
100 return NULL; |
|
101 Download* dl = new BackgroundDownload(priv->m_downloadMgr, url, downloadId); |
|
102 if (dl) |
|
103 priv->m_downloads.append(dl); |
|
104 return dl; |
|
105 } |
|
106 |
|
107 // creates the background download from the persistantly stored download info |
|
108 Download* BackgroundDownloadManager::createDownload(int id) |
|
109 { |
|
110 DM_PRIVATE(BackgroundDownloadManager); |
|
111 |
|
112 if ((id == INVALID_DL_ID) || (!priv->m_dlInfo)) |
|
113 return NULL; |
|
114 |
|
115 // connection to server will be established when DownloadManagerClient object is created |
|
116 // so we delay the constructon of this object till client calls some function |
|
117 if (!priv->m_downloadMgrClient) |
|
118 priv->m_downloadMgrClient = new DownloadManagerClient(priv->m_downloadMgr); |
|
119 QString url; |
|
120 priv->m_dlInfo->getValue(id, DownloadInfo::EUrl, url); |
|
121 // create downlaod at server |
|
122 bool status = priv->m_downloadMgrClient->attachToDownload(id); |
|
123 if (status == false) { |
|
124 // this means, download no more exists in server side |
|
125 priv->m_dlInfo->remove(id); |
|
126 return NULL; |
|
127 } |
|
128 Download* dl = new BackgroundDownload(priv->m_downloadMgr, url, id); |
|
129 if (dl) |
|
130 priv->m_downloads.append(dl); |
|
131 return dl; |
|
132 } |
|
133 |
|
134 // sets the proxy |
|
135 // proxyServer indicates proxy server name |
|
136 // port indicates port number |
|
137 void BackgroundDownloadManager::setProxy(const QString& proxyServer, int port) |
|
138 { |
|
139 if (!proxyServer.isEmpty()) { |
|
140 DM_PRIVATE(BackgroundDownloadManager); |
|
141 // connection to server will be established whrn DownloadManagerClient object is created |
|
142 // so we delay the constructon of this object till client calls some function |
|
143 if (!priv->m_downloadMgrClient) |
|
144 priv->m_downloadMgrClient = new DownloadManagerClient(priv->m_downloadMgr); |
|
145 priv->m_downloadMgrClient->setProxy(proxyServer, port); |
|
146 } |
|
147 } |
|
148 |
|
149 // sets download manager attribute |
|
150 // attr indicates download manager attribute |
|
151 // value indicates value to be set |
|
152 int BackgroundDownloadManager::setAttribute(DownloadManagerAttribute attr, const QVariant& value) |
|
153 { |
|
154 // remove build warnings |
|
155 DM_PRIVATE(BackgroundDownloadManager); |
|
156 if (!priv->m_downloadMgrClient) |
|
157 return -1; |
|
158 int returnValue = priv->m_downloadMgrClient->setDownloadManagerAttribute(attr, value); |
|
159 return returnValue; |
|
160 } |
|
161 |
|
162 // fetches download manager attribute |
|
163 // attr indicates download manager attribute whos value to be returned |
|
164 QVariant BackgroundDownloadManager::getAttribute(DownloadManagerAttribute attr) |
|
165 { |
|
166 DM_PRIVATE(BackgroundDownloadManager); |
|
167 if (!priv->m_downloadMgrClient) |
|
168 return QVariant(); |
|
169 |
|
170 // if download manager error, return the last client-server error |
|
171 switch(attr) { |
|
172 case DlMgrServerError: |
|
173 return QVariant(priv->m_downloadMgrClient->serverError()); |
|
174 default: |
|
175 break; |
|
176 } |
|
177 |
|
178 QVariant value = priv->m_downloadMgrClient->getDownloadManagerAttribute(attr); |
|
179 return value; |
|
180 } |
|
181 |
|
182 // returns all the current downloads |
|
183 QList<Download*>& BackgroundDownloadManager::currentDownloads() |
|
184 { |
|
185 DM_PRIVATE(BackgroundDownloadManager); |
|
186 return priv->m_downloads; |
|
187 } |
|
188 |
|
189 // finds a download provided id if exists |
|
190 // id indicates identifier for download |
|
191 Download* BackgroundDownloadManager::findDownload(int dlId) |
|
192 { |
|
193 DM_PRIVATE(BackgroundDownloadManager); |
|
194 for (int i=0; i<priv->m_downloads.count(); ++i) { |
|
195 if (priv->m_downloads[i]->id() == dlId) { |
|
196 return priv->m_downloads[i]; |
|
197 } |
|
198 } |
|
199 return 0; |
|
200 } |
|
201 |
|
202 // cancels all the downloads |
|
203 void BackgroundDownloadManager::removeAll() |
|
204 { |
|
205 DM_PRIVATE(BackgroundDownloadManager); |
|
206 if (!priv->m_downloadMgrClient) |
|
207 return; |
|
208 |
|
209 // before removing all downloads, remove their downloadinfo |
|
210 for (int i=0; i<priv->m_downloads.count(); ++i) { |
|
211 BackgroundDownload* download = dynamic_cast<BackgroundDownload*>(priv->m_downloads[i]); |
|
212 if (download) |
|
213 download->removeDownloadInfo(); |
|
214 } |
|
215 |
|
216 priv->m_downloadMgrClient->removeAll(); |
|
217 for (int i=0; i<priv->m_downloads.count(); ++i) { |
|
218 delete (priv->m_downloads[i]); |
|
219 priv->m_downloads[i] = 0; |
|
220 } |
|
221 priv->m_downloads.clear(); |
|
222 } |
|
223 |
|
224 // cancels and removes the download |
|
225 // dl indicates the download to be canceled and removed |
|
226 void BackgroundDownloadManager::removeOne(Download* dl) |
|
227 { |
|
228 // check if download exists |
|
229 if (!dl) |
|
230 return; |
|
231 |
|
232 DM_PRIVATE(BackgroundDownloadManager); |
|
233 // return if download manager client does not exist yet |
|
234 if (!priv->m_downloadMgrClient) |
|
235 return; |
|
236 // if this is a background download, remove it |
|
237 if (priv->m_downloads.contains(dl)) { |
|
238 priv->m_downloadMgrClient->removeDownload(dl->id()); |
|
239 priv->m_downloads.removeOne(dl); |
|
240 } |
|
241 } |
|
242 |
|
243 // pauses all the downloads which are in progress |
|
244 void BackgroundDownloadManager::pauseAll() |
|
245 { |
|
246 DM_PRIVATE(BackgroundDownloadManager); |
|
247 if (priv->m_downloadMgrClient) |
|
248 priv->m_downloadMgrClient->pauseAll(); |
|
249 } |
|
250 |
|
251 // resumes all the downloads which are paused |
|
252 void BackgroundDownloadManager::resumeAll() |
|
253 { |
|
254 DM_PRIVATE(BackgroundDownloadManager); |
|
255 if (priv->m_downloadMgrClient) |
|
256 priv->m_downloadMgrClient->resumeAll(); |
|
257 } |
|
258 |
|
259 // get download manager client object |
|
260 DownloadManagerClient* BackgroundDownloadManager::downloadManagerClient() |
|
261 { |
|
262 DM_PRIVATE(BackgroundDownloadManager); |
|
263 return priv->m_downloadMgrClient; |
|
264 } |