3
|
1 |
/*
|
|
2 |
* Copyright (c) 2010 Nokia Corporation and/or its subsidiary(-ies).
|
|
3 |
* All rights reserved.
|
|
4 |
*
|
|
5 |
* This program is free software: you can redistribute it and/or modify
|
|
6 |
* it under the terms of the GNU Lesser General Public License as published by
|
|
7 |
* the Free Software Foundation, version 2.1 of the License.
|
|
8 |
*
|
|
9 |
* This program is distributed in the hope that it will be useful,
|
|
10 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
11 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
12 |
* GNU Lesser General Public License for more details.
|
|
13 |
*
|
|
14 |
* You should have received a copy of the GNU Lesser General Public License
|
|
15 |
* along with this program. If not,
|
|
16 |
* see "http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html/".
|
|
17 |
*
|
|
18 |
* Description:
|
|
19 |
*
|
|
20 |
*/
|
|
21 |
|
|
22 |
#include "downloadproxy_p.h"
|
|
23 |
|
|
24 |
#include <QNetworkReply>
|
|
25 |
#include <QtDebug>
|
|
26 |
|
|
27 |
#ifdef USE_DOWNLOAD_MANAGER
|
|
28 |
#include "download.h"
|
|
29 |
#include "downloadmanager.h"
|
|
30 |
#endif // USE_DOWNLOAD_MANAGER
|
|
31 |
|
|
32 |
DownloadProxyData::DownloadProxyData(Download * download)
|
|
33 |
: m_download(download)
|
|
34 |
{}
|
|
35 |
|
|
36 |
DownloadProxyData::~DownloadProxyData()
|
|
37 |
{
|
|
38 |
// Nothing to do, Download * is owned by DownloadManager.
|
|
39 |
}
|
|
40 |
|
|
41 |
#ifdef USE_DOWNLOAD_MANAGER
|
|
42 |
|
|
43 |
// Helper functions for translating various download attribute enum values.
|
|
44 |
|
|
45 |
static const char * downloadState(int state)
|
|
46 |
{
|
|
47 |
switch (state) {
|
|
48 |
case DlNone:
|
|
49 |
return "None";
|
|
50 |
case DlCreated:
|
|
51 |
return "Created";
|
|
52 |
case DlStarted:
|
|
53 |
return "Started";
|
|
54 |
case DlInprogress:
|
|
55 |
return "InProgress";
|
|
56 |
case DlPaused:
|
|
57 |
return "Paused";
|
|
58 |
case DlCompleted:
|
|
59 |
return "Completed";
|
|
60 |
case DlFailed:
|
|
61 |
return "Failed";
|
|
62 |
case DlCancelled:
|
|
63 |
return "Cancelled";
|
|
64 |
case DlDescriptorUpdated:
|
|
65 |
return "DescriptorUpdated";
|
|
66 |
default:
|
|
67 |
return "???";
|
|
68 |
}
|
|
69 |
}
|
|
70 |
|
|
71 |
static const char * downloadError(QNetworkReply::NetworkError error)
|
|
72 |
{
|
|
73 |
switch (error) {
|
|
74 |
case QNetworkReply::NoError:
|
|
75 |
return "QNetworkReply::NoError";
|
|
76 |
case QNetworkReply::ConnectionRefusedError:
|
|
77 |
return "QNetworkReply::ConnectionRefusedError";
|
|
78 |
case QNetworkReply::RemoteHostClosedError:
|
|
79 |
return "QNetworkReply::RemoteHostClosedError";
|
|
80 |
case QNetworkReply::HostNotFoundError:
|
|
81 |
return "QNetworkReply::HostNotFoundError";
|
|
82 |
case QNetworkReply::TimeoutError:
|
|
83 |
return "QNetworkReply::TimeoutError";
|
|
84 |
case QNetworkReply::OperationCanceledError:
|
|
85 |
return "QNetworkReply::OperationCanceledError";
|
|
86 |
case QNetworkReply::SslHandshakeFailedError:
|
|
87 |
return "QNetworkReply::SslHandshakeFailedError";
|
|
88 |
case QNetworkReply::ProxyConnectionRefusedError:
|
|
89 |
return "QNetworkReply::ProxyConnectionRefusedError";
|
|
90 |
case QNetworkReply::ProxyConnectionClosedError:
|
|
91 |
return "QNetworkReply::ProxyConnectionClosedError";
|
|
92 |
case QNetworkReply::ProxyNotFoundError:
|
|
93 |
return "QNetworkReply::ProxyNotFoundError";
|
|
94 |
case QNetworkReply::ProxyTimeoutError:
|
|
95 |
return "QNetworkReply::ProxyTimeoutError";
|
|
96 |
case QNetworkReply::ProxyAuthenticationRequiredError:
|
|
97 |
return "QNetworkReply::ProxyAuthenticationRequiredError";
|
|
98 |
case QNetworkReply::ContentAccessDenied:
|
|
99 |
return "QNetworkReply::ContentAccessDenied";
|
|
100 |
case QNetworkReply::ContentOperationNotPermittedError:
|
|
101 |
return "QNetworkReply::ContentOperationNotPermittedError";
|
|
102 |
case QNetworkReply::ContentNotFoundError:
|
|
103 |
return "QNetworkReply::ContentNotFoundError";
|
|
104 |
case QNetworkReply::AuthenticationRequiredError:
|
|
105 |
return "QNetworkReply::AuthenticationRequiredError";
|
|
106 |
case QNetworkReply::ContentReSendError:
|
|
107 |
return "QNetworkReply::ContentReSendError";
|
|
108 |
case QNetworkReply::ProtocolUnknownError:
|
|
109 |
return "QNetworkReply::ProtocolUnknownError";
|
|
110 |
case QNetworkReply::ProtocolInvalidOperationError:
|
|
111 |
return "QNetworkReply::ProtocolInvalidOperationError";
|
|
112 |
case QNetworkReply::UnknownNetworkError:
|
|
113 |
return "QNetworkReply::UnknownNetworkError";
|
|
114 |
case QNetworkReply::UnknownProxyError:
|
|
115 |
return "QNetworkReply::UnknownProxyError";
|
|
116 |
case QNetworkReply::UnknownContentError:
|
|
117 |
return "QNetworkReply::UnknownContentError";
|
|
118 |
case QNetworkReply::ProtocolFailure:
|
|
119 |
return "QNetworkReply::ProtocolFailure";
|
|
120 |
default:
|
|
121 |
return "???";
|
|
122 |
}
|
|
123 |
}
|
|
124 |
|
|
125 |
// Helper functions to get download attribute of a particular type.
|
|
126 |
|
|
127 |
static int intAttribute(Download * download, DownloadAttribute which)
|
|
128 |
{
|
|
129 |
return download->getAttribute(which).toInt();
|
|
130 |
}
|
|
131 |
|
|
132 |
static uint uintAttribute(Download * download, DownloadAttribute which)
|
|
133 |
{
|
|
134 |
return download->getAttribute(which).toUInt();
|
|
135 |
}
|
|
136 |
|
|
137 |
static QString stringAttribute(Download * download, DownloadAttribute which)
|
|
138 |
{
|
|
139 |
return download->getAttribute(which).toString();
|
|
140 |
}
|
|
141 |
|
|
142 |
// Helper functions for reporting download attributes.
|
|
143 |
|
|
144 |
template<typename T>
|
|
145 |
void debugDownloadAttribute(
|
|
146 |
Download * download,
|
|
147 |
char const * name,
|
|
148 |
const T & value)
|
|
149 |
{
|
|
150 |
qDebug() << "DL" << download->id() << name << value;
|
|
151 |
}
|
|
152 |
|
|
153 |
static void debugDownloadInt(
|
|
154 |
Download * download,
|
|
155 |
DownloadAttribute which,
|
|
156 |
const char * name)
|
|
157 |
{
|
|
158 |
int value = intAttribute(download, which);
|
|
159 |
|
|
160 |
debugDownloadAttribute(download, name, value);
|
|
161 |
}
|
|
162 |
|
|
163 |
static void debugDownloadUInt(
|
|
164 |
Download * download,
|
|
165 |
DownloadAttribute which,
|
|
166 |
const char * name)
|
|
167 |
{
|
|
168 |
uint value = uintAttribute(download, which);
|
|
169 |
|
|
170 |
debugDownloadAttribute(download, name, value);
|
|
171 |
}
|
|
172 |
|
|
173 |
static void debugDownloadStr(
|
|
174 |
Download * download,
|
|
175 |
DownloadAttribute which,
|
|
176 |
const char * name)
|
|
177 |
{
|
|
178 |
QString value = stringAttribute(download, which);
|
|
179 |
|
|
180 |
debugDownloadAttribute(download, name, value);
|
|
181 |
}
|
|
182 |
|
|
183 |
static void debugDownloadState(Download * download)
|
|
184 |
{
|
|
185 |
int num = intAttribute(download, DlDownloadState);
|
|
186 |
|
|
187 |
const char * state = downloadState(num);
|
|
188 |
|
|
189 |
debugDownloadAttribute(download, "DownloadState", state);
|
|
190 |
|
|
191 |
}
|
|
192 |
|
|
193 |
static void debugDownloadError(Download * download)
|
|
194 |
{
|
|
195 |
int num = intAttribute(download, DlLastError);
|
|
196 |
|
|
197 |
const char * error = downloadError(static_cast<QNetworkReply::NetworkError>(num));
|
|
198 |
|
|
199 |
debugDownloadAttribute(download, "DownloadError", error);
|
|
200 |
}
|
|
201 |
|
|
202 |
// Helper function for implementing DownloadProxyData::debug().
|
|
203 |
|
|
204 |
static void debugDownload(Download * download)
|
|
205 |
{
|
|
206 |
debugDownloadState(download);
|
|
207 |
|
|
208 |
debugDownloadError(download);
|
|
209 |
|
|
210 |
debugDownloadStr(download,
|
|
211 |
DlLastErrorString,
|
|
212 |
"LastErrorString");
|
|
213 |
|
|
214 |
debugDownloadStr(download,
|
|
215 |
DlSourceUrl,
|
|
216 |
"SourceUrl");
|
|
217 |
|
|
218 |
debugDownloadStr(download,
|
|
219 |
DlContentType,
|
|
220 |
"ContentType");
|
|
221 |
|
|
222 |
debugDownloadStr(download,
|
|
223 |
DlDestPath,
|
|
224 |
"DestPath");
|
|
225 |
|
|
226 |
debugDownloadStr(download,
|
|
227 |
DlFileName,
|
|
228 |
"FileName");
|
|
229 |
|
|
230 |
debugDownloadInt(download,
|
|
231 |
DlDownloadedSize,
|
|
232 |
"DownloadedSize");
|
|
233 |
|
|
234 |
debugDownloadInt(download,
|
|
235 |
DlTotalSize,
|
|
236 |
"TotalSize");
|
|
237 |
|
|
238 |
debugDownloadInt(download,
|
|
239 |
DlLastPausedSize,
|
|
240 |
"LastPausedSize");
|
|
241 |
|
|
242 |
debugDownloadInt(download,
|
|
243 |
DlPercentage,
|
|
244 |
"Percentage");
|
|
245 |
|
|
246 |
debugDownloadStr(download,
|
|
247 |
DlStartTime,
|
|
248 |
"StartTime");
|
|
249 |
|
|
250 |
debugDownloadStr(download,
|
|
251 |
DlEndTime,
|
|
252 |
"EndTime");
|
|
253 |
|
|
254 |
debugDownloadUInt(download,
|
|
255 |
DlElapsedTime,
|
|
256 |
"ElapsedTime");
|
|
257 |
|
|
258 |
debugDownloadStr(download,
|
|
259 |
DlRemainingTime,
|
|
260 |
"RemainingTime");
|
|
261 |
|
|
262 |
debugDownloadStr(download,
|
|
263 |
DlSpeed,
|
|
264 |
"Speed");
|
|
265 |
|
|
266 |
debugDownloadInt(download,
|
|
267 |
DlProgressInterval,
|
|
268 |
"ProgressInterval");
|
|
269 |
}
|
|
270 |
|
|
271 |
#endif // USE_DOWNLOAD_MANAGER
|
|
272 |
|
|
273 |
void DownloadProxyData::debug()
|
|
274 |
{
|
|
275 |
#ifdef USE_DOWNLOAD_MANAGER
|
|
276 |
if (m_download == 0) {
|
|
277 |
return;
|
|
278 |
}
|
|
279 |
|
|
280 |
int id = m_download->id();
|
|
281 |
|
|
282 |
DownloadManager * manager = m_download->downloadManager();
|
|
283 |
|
|
284 |
Download * download = manager->findDownload(id);
|
|
285 |
|
|
286 |
if (download != 0) {
|
|
287 |
debugDownload(download);
|
|
288 |
}
|
|
289 |
#endif // USE_DOWNLOAD_MANAGER
|
|
290 |
}
|
|
291 |
|
|
292 |
void DownloadProxyData::remove()
|
|
293 |
{
|
|
294 |
#ifdef USE_DOWNLOAD_MANAGER
|
|
295 |
if (m_download == 0) {
|
|
296 |
return;
|
|
297 |
}
|
|
298 |
|
|
299 |
DownloadManager * manager = m_download->downloadManager();
|
|
300 |
|
|
301 |
manager->removeOne(m_download);
|
|
302 |
|
|
303 |
// Download is no longer valid.
|
|
304 |
|
|
305 |
m_download = 0;
|
|
306 |
#endif // USE_DOWNLOAD_MANAGER
|
|
307 |
}
|
|
308 |
|
|
309 |
QString DownloadProxyData::fileName()
|
|
310 |
{
|
|
311 |
#ifdef USE_DOWNLOAD_MANAGER
|
|
312 |
if (m_download == 0) {
|
|
313 |
return QString();
|
|
314 |
}
|
|
315 |
|
|
316 |
return stringAttribute(m_download, DlFileName);
|
|
317 |
#else // USE_DOWNLOAD_MANAGER
|
|
318 |
return QString();
|
|
319 |
#endif // USE_DOWNLOAD_MANAGER
|
|
320 |
}
|