author | Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com> |
Mon, 04 Oct 2010 01:19:32 +0300 | |
changeset 37 | 758a864f9613 |
parent 30 | 5dc02b23752f |
permissions | -rw-r--r-- |
0 | 1 |
/**************************************************************************** |
2 |
** |
|
18
2f34d5167611
Revision: 201011
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
3 |
** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies). |
0 | 4 |
** All rights reserved. |
5 |
** Contact: Nokia Corporation (qt-info@nokia.com) |
|
6 |
** |
|
7 |
** This file is part of the examples of the Qt Toolkit. |
|
8 |
** |
|
30
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
9 |
** $QT_BEGIN_LICENSE:BSD$ |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
10 |
** You may use this file under the terms of the BSD license as follows: |
0 | 11 |
** |
30
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
12 |
** "Redistribution and use in source and binary forms, with or without |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
13 |
** modification, are permitted provided that the following conditions are |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
14 |
** met: |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
15 |
** * Redistributions of source code must retain the above copyright |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
16 |
** notice, this list of conditions and the following disclaimer. |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
17 |
** * Redistributions in binary form must reproduce the above copyright |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
18 |
** notice, this list of conditions and the following disclaimer in |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
19 |
** the documentation and/or other materials provided with the |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
20 |
** distribution. |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
21 |
** * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
22 |
** the names of its contributors may be used to endorse or promote |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
23 |
** products derived from this software without specific prior written |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
24 |
** permission. |
0 | 25 |
** |
30
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
26 |
** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
27 |
** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
28 |
** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
29 |
** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
30 |
** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
31 |
** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
32 |
** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
33 |
** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
34 |
** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
35 |
** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
36 |
** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." |
0 | 37 |
** $QT_END_LICENSE$ |
38 |
** |
|
39 |
****************************************************************************/ |
|
40 |
||
41 |
#include "downloadmanager.h" |
|
42 |
||
43 |
#include <QFileInfo> |
|
44 |
#include <QNetworkRequest> |
|
45 |
#include <QNetworkReply> |
|
46 |
#include <QString> |
|
47 |
#include <QStringList> |
|
48 |
#include <QTimer> |
|
49 |
#include <stdio.h> |
|
50 |
||
51 |
DownloadManager::DownloadManager(QObject *parent) |
|
52 |
: QObject(parent), downloadedCount(0), totalCount(0) |
|
53 |
{ |
|
54 |
} |
|
55 |
||
56 |
void DownloadManager::append(const QStringList &urlList) |
|
57 |
{ |
|
58 |
foreach (QString url, urlList) |
|
59 |
append(QUrl::fromEncoded(url.toLocal8Bit())); |
|
60 |
||
61 |
if (downloadQueue.isEmpty()) |
|
62 |
QTimer::singleShot(0, this, SIGNAL(finished())); |
|
63 |
} |
|
64 |
||
65 |
void DownloadManager::append(const QUrl &url) |
|
66 |
{ |
|
67 |
if (downloadQueue.isEmpty()) |
|
68 |
QTimer::singleShot(0, this, SLOT(startNextDownload())); |
|
69 |
||
70 |
downloadQueue.enqueue(url); |
|
71 |
++totalCount; |
|
72 |
} |
|
73 |
||
74 |
QString DownloadManager::saveFileName(const QUrl &url) |
|
75 |
{ |
|
76 |
QString path = url.path(); |
|
77 |
QString basename = QFileInfo(path).fileName(); |
|
78 |
||
79 |
if (basename.isEmpty()) |
|
80 |
basename = "download"; |
|
81 |
||
82 |
if (QFile::exists(basename)) { |
|
83 |
// already exists, don't overwrite |
|
84 |
int i = 0; |
|
85 |
basename += '.'; |
|
86 |
while (QFile::exists(basename + QString::number(i))) |
|
87 |
++i; |
|
88 |
||
89 |
basename += QString::number(i); |
|
90 |
} |
|
91 |
||
92 |
return basename; |
|
93 |
} |
|
94 |
||
95 |
void DownloadManager::startNextDownload() |
|
96 |
{ |
|
97 |
if (downloadQueue.isEmpty()) { |
|
98 |
printf("%d/%d files downloaded successfully\n", downloadedCount, totalCount); |
|
99 |
emit finished(); |
|
100 |
return; |
|
101 |
} |
|
102 |
||
103 |
QUrl url = downloadQueue.dequeue(); |
|
104 |
||
105 |
QString filename = saveFileName(url); |
|
106 |
output.setFileName(filename); |
|
107 |
if (!output.open(QIODevice::WriteOnly)) { |
|
108 |
fprintf(stderr, "Problem opening save file '%s' for download '%s': %s\n", |
|
109 |
qPrintable(filename), url.toEncoded().constData(), |
|
110 |
qPrintable(output.errorString())); |
|
111 |
||
112 |
startNextDownload(); |
|
113 |
return; // skip this download |
|
114 |
} |
|
115 |
||
116 |
QNetworkRequest request(url); |
|
117 |
currentDownload = manager.get(request); |
|
118 |
connect(currentDownload, SIGNAL(downloadProgress(qint64,qint64)), |
|
119 |
SLOT(downloadProgress(qint64,qint64))); |
|
120 |
connect(currentDownload, SIGNAL(finished()), |
|
121 |
SLOT(downloadFinished())); |
|
122 |
connect(currentDownload, SIGNAL(readyRead()), |
|
123 |
SLOT(downloadReadyRead())); |
|
124 |
||
125 |
// prepare the output |
|
126 |
printf("Downloading %s...\n", url.toEncoded().constData()); |
|
127 |
downloadTime.start(); |
|
128 |
} |
|
129 |
||
130 |
void DownloadManager::downloadProgress(qint64 bytesReceived, qint64 bytesTotal) |
|
131 |
{ |
|
132 |
progressBar.setStatus(bytesReceived, bytesTotal); |
|
133 |
||
134 |
// calculate the download speed |
|
135 |
double speed = bytesReceived * 1000.0 / downloadTime.elapsed(); |
|
136 |
QString unit; |
|
137 |
if (speed < 1024) { |
|
138 |
unit = "bytes/sec"; |
|
139 |
} else if (speed < 1024*1024) { |
|
140 |
speed /= 1024; |
|
141 |
unit = "kB/s"; |
|
142 |
} else { |
|
143 |
speed /= 1024*1024; |
|
144 |
unit = "MB/s"; |
|
145 |
} |
|
146 |
||
147 |
progressBar.setMessage(QString::fromLatin1("%1 %2") |
|
148 |
.arg(speed, 3, 'f', 1).arg(unit)); |
|
149 |
progressBar.update(); |
|
150 |
} |
|
151 |
||
152 |
void DownloadManager::downloadFinished() |
|
153 |
{ |
|
154 |
progressBar.clear(); |
|
155 |
output.close(); |
|
156 |
||
157 |
if (currentDownload->error()) { |
|
158 |
// download failed |
|
159 |
fprintf(stderr, "Failed: %s\n", qPrintable(currentDownload->errorString())); |
|
160 |
} else { |
|
161 |
printf("Succeeded.\n"); |
|
162 |
++downloadedCount; |
|
163 |
} |
|
164 |
||
165 |
currentDownload->deleteLater(); |
|
166 |
startNextDownload(); |
|
167 |
} |
|
168 |
||
169 |
void DownloadManager::downloadReadyRead() |
|
170 |
{ |
|
171 |
output.write(currentDownload->readAll()); |
|
172 |
} |