106
swisssurfer/src/downloadmanager.hxx
Normal file
106
swisssurfer/src/downloadmanager.hxx
Normal file
@@ -0,0 +1,106 @@
|
||||
/*! @file
|
||||
|
||||
@id $Id$
|
||||
*/
|
||||
// 1 2 3 4 5 6 7 8
|
||||
// 45678901234567890123456789012345678901234567890123456789012345678901234567890
|
||||
|
||||
#include <QtNetwork/QNetworkReply>
|
||||
#include <map>
|
||||
|
||||
#include <cassert>
|
||||
#include <QtCore/QDebug>
|
||||
#ifndef LOG
|
||||
#define LOG qDebug()<<__PRETTY_FUNCTION__
|
||||
#endif
|
||||
|
||||
class DownloadManager: public QObject {
|
||||
Q_OBJECT;
|
||||
public:
|
||||
|
||||
DownloadManager& operator+=(QNetworkReply* reply) {
|
||||
LOG;
|
||||
add(reply);
|
||||
return *this;
|
||||
}
|
||||
|
||||
Q_SIGNALS:
|
||||
|
||||
void progress(qint64 done, qint64 total);
|
||||
void started();
|
||||
void finished();
|
||||
|
||||
public Q_SLOTS:
|
||||
|
||||
void add(QNetworkReply* reply) {
|
||||
LOG<<reply->url().toString();
|
||||
_downloads[reply] = Progress(0, 0);
|
||||
assert(connect(reply, SIGNAL(downloadProgress(qint64, qint64)),
|
||||
SLOT(downloadProgress(qint64, qint64))));
|
||||
assert(connect(reply, SIGNAL(error(QNetworkReply::NetworkError)),
|
||||
SLOT(error(QNetworkReply::NetworkError))));
|
||||
assert(connect(reply, SIGNAL(finished()),
|
||||
SLOT(slotFinished())));
|
||||
assert(connect(reply, SIGNAL(metaDataChanged()),
|
||||
SLOT(metaDataChanged())));
|
||||
assert(connect(reply, SIGNAL(sslErrors(const QList<QSslError>&)),
|
||||
SLOT(sslErrors(const QList<QSslError>&))));
|
||||
assert(connect(reply, SIGNAL(uploadProgress(qint64, qint64)),
|
||||
SLOT(uploadProgress(qint64, qint64))));
|
||||
if (_downloads.size()==1) started();
|
||||
}
|
||||
|
||||
void abort() {
|
||||
while (_downloads.size()) _downloads.begin()->first->abort();
|
||||
}
|
||||
|
||||
private:
|
||||
|
||||
void calcProgress() {
|
||||
//LOG;
|
||||
qint64 done(0);
|
||||
qint64 total(0);
|
||||
for (Downloads::iterator it(_downloads.begin());
|
||||
it!=_downloads.end(); ++it) {
|
||||
done += it->second.first;
|
||||
total += it->second.second;
|
||||
}
|
||||
progress(done, total);
|
||||
}
|
||||
|
||||
private Q_SLOTS:
|
||||
|
||||
void downloadProgress(qint64 bytesReceived, qint64 bytesTotal) {
|
||||
//LOG<<bytesReceived<<bytesTotal;
|
||||
_downloads[qobject_cast<QNetworkReply*>(sender())].first = bytesReceived;
|
||||
_downloads[qobject_cast<QNetworkReply*>(sender())].second = bytesTotal;
|
||||
calcProgress();
|
||||
}
|
||||
void error(QNetworkReply::NetworkError code) {
|
||||
LOG;
|
||||
}
|
||||
void slotFinished() {
|
||||
LOG;
|
||||
_downloads.erase(qobject_cast<QNetworkReply*>(sender()));
|
||||
if (_downloads.size()==0) finished();
|
||||
}
|
||||
void metaDataChanged() {
|
||||
LOG;
|
||||
}
|
||||
void sslErrors(const QList<QSslError> & errors) {
|
||||
LOG;
|
||||
}
|
||||
void uploadProgress(qint64 bytesSent, qint64 bytesTotal) {
|
||||
//LOG<<bytesSent<<bytesTotal;
|
||||
_downloads[qobject_cast<QNetworkReply*>(sender())].first = bytesSent;
|
||||
_downloads[qobject_cast<QNetworkReply*>(sender())].second = bytesTotal;
|
||||
calcProgress();
|
||||
}
|
||||
|
||||
private:
|
||||
|
||||
typedef std::pair<qint64, qint64> Progress;
|
||||
typedef std::map<QNetworkReply*, Progress> Downloads;
|
||||
|
||||
Downloads _downloads;
|
||||
};
|
Reference in New Issue
Block a user