|
1 /**************************************************************************** |
|
2 ** |
|
3 ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). |
|
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 ** |
|
9 ** $QT_BEGIN_LICENSE:LGPL$ |
|
10 ** No Commercial Usage |
|
11 ** This file contains pre-release code and may not be distributed. |
|
12 ** You may use this file in accordance with the terms and conditions |
|
13 ** contained in the Technology Preview License Agreement accompanying |
|
14 ** this package. |
|
15 ** |
|
16 ** GNU Lesser General Public License Usage |
|
17 ** Alternatively, this file may be used under the terms of the GNU Lesser |
|
18 ** General Public License version 2.1 as published by the Free Software |
|
19 ** Foundation and appearing in the file LICENSE.LGPL included in the |
|
20 ** packaging of this file. Please review the following information to |
|
21 ** ensure the GNU Lesser General Public License version 2.1 requirements |
|
22 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. |
|
23 ** |
|
24 ** In addition, as a special exception, Nokia gives you certain additional |
|
25 ** rights. These rights are described in the Nokia Qt LGPL Exception |
|
26 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. |
|
27 ** |
|
28 ** If you have questions regarding the use of this file, please contact |
|
29 ** Nokia at qt-info@nokia.com. |
|
30 ** |
|
31 ** |
|
32 ** |
|
33 ** |
|
34 ** |
|
35 ** |
|
36 ** |
|
37 ** |
|
38 ** $QT_END_LICENSE$ |
|
39 ** |
|
40 ****************************************************************************/ |
|
41 |
|
42 #include "downloadmanager.h" |
|
43 |
|
44 #include <QFileInfo> |
|
45 #include <QNetworkRequest> |
|
46 #include <QNetworkReply> |
|
47 #include <QString> |
|
48 #include <QStringList> |
|
49 #include <QTimer> |
|
50 #include <stdio.h> |
|
51 |
|
52 DownloadManager::DownloadManager(QObject *parent) |
|
53 : QObject(parent), downloadedCount(0), totalCount(0) |
|
54 { |
|
55 } |
|
56 |
|
57 void DownloadManager::append(const QStringList &urlList) |
|
58 { |
|
59 foreach (QString url, urlList) |
|
60 append(QUrl::fromEncoded(url.toLocal8Bit())); |
|
61 |
|
62 if (downloadQueue.isEmpty()) |
|
63 QTimer::singleShot(0, this, SIGNAL(finished())); |
|
64 } |
|
65 |
|
66 void DownloadManager::append(const QUrl &url) |
|
67 { |
|
68 if (downloadQueue.isEmpty()) |
|
69 QTimer::singleShot(0, this, SLOT(startNextDownload())); |
|
70 |
|
71 downloadQueue.enqueue(url); |
|
72 ++totalCount; |
|
73 } |
|
74 |
|
75 QString DownloadManager::saveFileName(const QUrl &url) |
|
76 { |
|
77 QString path = url.path(); |
|
78 QString basename = QFileInfo(path).fileName(); |
|
79 |
|
80 if (basename.isEmpty()) |
|
81 basename = "download"; |
|
82 |
|
83 if (QFile::exists(basename)) { |
|
84 // already exists, don't overwrite |
|
85 int i = 0; |
|
86 basename += '.'; |
|
87 while (QFile::exists(basename + QString::number(i))) |
|
88 ++i; |
|
89 |
|
90 basename += QString::number(i); |
|
91 } |
|
92 |
|
93 return basename; |
|
94 } |
|
95 |
|
96 void DownloadManager::startNextDownload() |
|
97 { |
|
98 if (downloadQueue.isEmpty()) { |
|
99 printf("%d/%d files downloaded successfully\n", downloadedCount, totalCount); |
|
100 emit finished(); |
|
101 return; |
|
102 } |
|
103 |
|
104 QUrl url = downloadQueue.dequeue(); |
|
105 |
|
106 QString filename = saveFileName(url); |
|
107 output.setFileName(filename); |
|
108 if (!output.open(QIODevice::WriteOnly)) { |
|
109 fprintf(stderr, "Problem opening save file '%s' for download '%s': %s\n", |
|
110 qPrintable(filename), url.toEncoded().constData(), |
|
111 qPrintable(output.errorString())); |
|
112 |
|
113 startNextDownload(); |
|
114 return; // skip this download |
|
115 } |
|
116 |
|
117 QNetworkRequest request(url); |
|
118 currentDownload = manager.get(request); |
|
119 connect(currentDownload, SIGNAL(downloadProgress(qint64,qint64)), |
|
120 SLOT(downloadProgress(qint64,qint64))); |
|
121 connect(currentDownload, SIGNAL(finished()), |
|
122 SLOT(downloadFinished())); |
|
123 connect(currentDownload, SIGNAL(readyRead()), |
|
124 SLOT(downloadReadyRead())); |
|
125 |
|
126 // prepare the output |
|
127 printf("Downloading %s...\n", url.toEncoded().constData()); |
|
128 downloadTime.start(); |
|
129 } |
|
130 |
|
131 void DownloadManager::downloadProgress(qint64 bytesReceived, qint64 bytesTotal) |
|
132 { |
|
133 progressBar.setStatus(bytesReceived, bytesTotal); |
|
134 |
|
135 // calculate the download speed |
|
136 double speed = bytesReceived * 1000.0 / downloadTime.elapsed(); |
|
137 QString unit; |
|
138 if (speed < 1024) { |
|
139 unit = "bytes/sec"; |
|
140 } else if (speed < 1024*1024) { |
|
141 speed /= 1024; |
|
142 unit = "kB/s"; |
|
143 } else { |
|
144 speed /= 1024*1024; |
|
145 unit = "MB/s"; |
|
146 } |
|
147 |
|
148 progressBar.setMessage(QString::fromLatin1("%1 %2") |
|
149 .arg(speed, 3, 'f', 1).arg(unit)); |
|
150 progressBar.update(); |
|
151 } |
|
152 |
|
153 void DownloadManager::downloadFinished() |
|
154 { |
|
155 progressBar.clear(); |
|
156 output.close(); |
|
157 |
|
158 if (currentDownload->error()) { |
|
159 // download failed |
|
160 fprintf(stderr, "Failed: %s\n", qPrintable(currentDownload->errorString())); |
|
161 } else { |
|
162 printf("Succeeded.\n"); |
|
163 ++downloadedCount; |
|
164 } |
|
165 |
|
166 currentDownload->deleteLater(); |
|
167 startNextDownload(); |
|
168 } |
|
169 |
|
170 void DownloadManager::downloadReadyRead() |
|
171 { |
|
172 output.write(currentDownload->readAll()); |
|
173 } |