16
|
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 "backgrounddownload.h"
|
|
18 |
#include "download.h"
|
|
19 |
#include "downloadmanager.h"
|
|
20 |
#include "backgrounddownloadmanager.h"
|
|
21 |
#include "downloadmanagerclient.h"
|
|
22 |
#include "downloadevent.h"
|
|
23 |
#include "downloadinfo.h"
|
|
24 |
#include <QNetworkReply>
|
|
25 |
#include <QCoreApplication>
|
|
26 |
|
|
27 |
// private implementation
|
|
28 |
class BackgroundDownloadPrivate
|
|
29 |
{
|
|
30 |
DM_DECLARE_PUBLIC(BackgroundDownload);
|
|
31 |
public:
|
|
32 |
BackgroundDownloadPrivate();
|
|
33 |
~BackgroundDownloadPrivate();
|
|
34 |
int m_downloadId;
|
|
35 |
QString m_url;
|
|
36 |
DownloadManager *m_downloadManager; // not owned
|
|
37 |
BackgroundDownloadManager* m_backgroundMgr; // not owned
|
|
38 |
EventReceiverList m_eventReceiverList; // list of event listeners
|
|
39 |
DownloadManagerClient* m_downloadMgrClient; // not owned
|
|
40 |
DownloadInfo *m_dlInfo; // not owned
|
|
41 |
};
|
|
42 |
|
|
43 |
BackgroundDownloadPrivate::BackgroundDownloadPrivate()
|
|
44 |
{
|
|
45 |
m_downloadId = INVALID_DL_ID;
|
|
46 |
m_downloadManager = 0;
|
|
47 |
m_backgroundMgr = 0;
|
|
48 |
m_downloadMgrClient = 0;
|
|
49 |
}
|
|
50 |
|
|
51 |
BackgroundDownloadPrivate::~BackgroundDownloadPrivate()
|
|
52 |
{
|
|
53 |
}
|
|
54 |
|
|
55 |
/*!
|
|
56 |
* \class BackgroundDownload
|
|
57 |
*
|
|
58 |
* \brief The public APIs for managing a background download
|
|
59 |
*
|
|
60 |
* This class has the public APIs for managing a single background download
|
|
61 |
*/
|
|
62 |
|
|
63 |
BackgroundDownload::BackgroundDownload(DownloadManager *mgr, const QString& url, int dlId)
|
|
64 |
{
|
|
65 |
DM_INITIALIZE(BackgroundDownload);
|
|
66 |
priv->m_downloadId = dlId;
|
|
67 |
priv->m_url = url;
|
|
68 |
priv->m_downloadManager = mgr;
|
|
69 |
priv->m_backgroundMgr = mgr->backgroundManager();
|
|
70 |
priv->m_downloadMgrClient = priv->m_backgroundMgr->downloadManagerClient();
|
|
71 |
if (mgr)
|
|
72 |
priv->m_dlInfo = mgr->downloadInfo();
|
|
73 |
if (priv->m_dlInfo) {
|
|
74 |
priv->m_dlInfo->setValue(priv->m_downloadId, DownloadInfo::EUrl, (const QString&)url);
|
|
75 |
priv->m_dlInfo->setValue(priv->m_downloadId, DownloadInfo::EScope, (long)Background);
|
|
76 |
}
|
|
77 |
}
|
|
78 |
|
|
79 |
BackgroundDownload::~BackgroundDownload()
|
|
80 |
{
|
|
81 |
DM_UNINITIALIZE(BackgroundDownload);
|
|
82 |
}
|
|
83 |
|
|
84 |
/*!
|
|
85 |
starts the background download, returns the success status
|
|
86 |
*/
|
|
87 |
int BackgroundDownload::start()
|
|
88 |
{
|
|
89 |
DM_PRIVATE(BackgroundDownload);
|
|
90 |
if (priv->m_downloadMgrClient)
|
|
91 |
return priv->m_downloadMgrClient->startDownload(priv->m_downloadId);
|
|
92 |
return -1;
|
|
93 |
}
|
|
94 |
|
|
95 |
/*!
|
|
96 |
returns id of the background download
|
|
97 |
*/
|
|
98 |
int BackgroundDownload::id()
|
|
99 |
{
|
|
100 |
DM_PRIVATE(BackgroundDownload);
|
|
101 |
return priv->m_downloadId;
|
|
102 |
}
|
|
103 |
|
|
104 |
/*!
|
|
105 |
sets the attribute for the background download
|
|
106 |
\a attr indicates attribute
|
|
107 |
\a value indicates value for the download
|
|
108 |
*/
|
|
109 |
int BackgroundDownload::setAttribute(DownloadAttribute attr, const QVariant& value)
|
|
110 |
{
|
|
111 |
DM_PRIVATE(BackgroundDownload);
|
|
112 |
if (priv->m_downloadMgrClient)
|
|
113 |
return priv->m_downloadMgrClient->setDownloadAttribute(priv->m_downloadId, attr, value);
|
|
114 |
return -1;
|
|
115 |
}
|
|
116 |
|
|
117 |
/*!
|
|
118 |
fetches the attribute of the background download
|
|
119 |
\a attr indicates download attribute
|
|
120 |
*/
|
|
121 |
QVariant BackgroundDownload::getAttribute(DownloadAttribute attr)
|
|
122 |
{
|
|
123 |
DM_PRIVATE(BackgroundDownload);
|
|
124 |
if (priv->m_downloadMgrClient)
|
|
125 |
return priv->m_downloadMgrClient->getDownloadAttribute(priv->m_downloadId, attr);
|
|
126 |
return QVariant();
|
|
127 |
}
|
|
128 |
|
|
129 |
/*!
|
|
130 |
pauses the background download
|
|
131 |
*/
|
|
132 |
int BackgroundDownload::pause()
|
|
133 |
{
|
|
134 |
DM_PRIVATE(BackgroundDownload);
|
|
135 |
if (priv->m_downloadMgrClient)
|
|
136 |
return priv->m_downloadMgrClient->pauseDownload(priv->m_downloadId);
|
|
137 |
return -1;
|
|
138 |
}
|
|
139 |
|
|
140 |
/*!
|
|
141 |
resumes the background download
|
|
142 |
*/
|
|
143 |
int BackgroundDownload::resume()
|
|
144 |
{
|
|
145 |
DM_PRIVATE(BackgroundDownload);
|
|
146 |
if (priv->m_downloadMgrClient)
|
|
147 |
return priv->m_downloadMgrClient->resumeDownload(priv->m_downloadId);
|
|
148 |
return -1;
|
|
149 |
}
|
|
150 |
|
|
151 |
/*!
|
|
152 |
cancels the background download
|
|
153 |
*/
|
|
154 |
int BackgroundDownload::cancel()
|
|
155 |
{
|
|
156 |
DM_PRIVATE(BackgroundDownload);
|
|
157 |
if (priv->m_downloadMgrClient)
|
|
158 |
return priv->m_downloadMgrClient->cancelDownload(priv->m_downloadId);
|
|
159 |
return -1;
|
|
160 |
}
|
|
161 |
|
|
162 |
/*!
|
|
163 |
registers receiver for the background download events
|
|
164 |
\a reciever indicates reciever which listen to download events
|
|
165 |
*/
|
|
166 |
void BackgroundDownload::registerEventReceiver(QObject *receiver)
|
|
167 |
{
|
|
168 |
DM_PRIVATE(BackgroundDownload);
|
|
169 |
if (receiver)
|
|
170 |
if (!priv->m_eventReceiverList.contains(receiver))
|
|
171 |
priv->m_eventReceiverList.append(receiver);
|
|
172 |
}
|
|
173 |
|
|
174 |
/*!
|
|
175 |
unregisters the event listener
|
|
176 |
\a receiver indicates listener which will be unregistered
|
|
177 |
*/
|
|
178 |
void BackgroundDownload::unregisterEventReceiver(QObject *receiver)
|
|
179 |
{
|
|
180 |
DM_PRIVATE(BackgroundDownload);
|
|
181 |
priv->m_eventReceiverList.removeOne(receiver);
|
|
182 |
}
|
|
183 |
|
|
184 |
/*!
|
|
185 |
returns download manager
|
|
186 |
*/
|
|
187 |
DownloadManager* BackgroundDownload::downloadManager()
|
|
188 |
{
|
|
189 |
DM_PRIVATE(BackgroundDownload);
|
|
190 |
return priv->m_downloadManager;
|
|
191 |
}
|
|
192 |
|
|
193 |
/*!
|
|
194 |
returns the child downloads i.e if download has any media objects
|
|
195 |
\a list indicates list of child downloads
|
|
196 |
*/
|
|
197 |
void BackgroundDownload::getChildren(QList<Download*>& /*list*/)
|
|
198 |
{
|
|
199 |
// This feature is not supported at present
|
|
200 |
return;
|
|
201 |
}
|
|
202 |
|
|
203 |
// posts the event to recievers event loop
|
|
204 |
void BackgroundDownload::postEvent(DEventType type, DlEventAttributeMap* attrMap)
|
|
205 |
{
|
|
206 |
DM_PRIVATE(BackgroundDownload);
|
|
207 |
if((type == Failed) || (type == Completed) || (type == Cancelled)) {
|
|
208 |
if (!(type == Completed && InActive == priv->m_downloadManager->getAttribute(DlMgrPersistantMode)))
|
|
209 |
removeDownloadInfo();
|
|
210 |
}
|
|
211 |
EventReceiverList list = eventReceivers();
|
|
212 |
for (int i=0; i<list.size(); i++) {
|
|
213 |
if (list[i]) {
|
|
214 |
DownloadEvent *event = new DownloadEvent(type, attrMap, priv->m_downloadId);
|
|
215 |
QCoreApplication::postEvent(list[i], event);
|
|
216 |
}
|
|
217 |
}
|
|
218 |
}
|
|
219 |
|
|
220 |
// returns the event listeners
|
|
221 |
EventReceiverList& BackgroundDownload::eventReceivers()
|
|
222 |
{
|
|
223 |
DM_PRIVATE(BackgroundDownload);
|
|
224 |
return priv->m_eventReceiverList;
|
|
225 |
}
|
|
226 |
|
|
227 |
// remove info of this download
|
|
228 |
void BackgroundDownload::removeDownloadInfo()
|
|
229 |
{
|
|
230 |
DM_PRIVATE(BackgroundDownload);
|
|
231 |
priv->m_dlInfo->remove(priv->m_downloadId);
|
|
232 |
}
|