/*! @file @id $Id$ */ // 1 2 3 4 5 6 7 8 // 45678901234567890123456789012345678901234567890123456789012345678901234567890 #include #include #include #include #include #ifndef LOG #define LOG qDebug()<<__PRETTY_FUNCTION__ #endif class DownloadManager: public QObject { Q_OBJECT; public: DownloadManager& operator+=(QNetworkReply* reply) { LOG; add(reply); return *this; } static QString networkError(QNetworkReply::NetworkError err) { LOG<Unknown network error (code: %1).") .arg(err); } } Q_SIGNALS: void progress(qint64 done, qint64 total); void started(); void finished(); public Q_SLOTS: void add(QNetworkReply* reply) { LOG<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&)), SLOT(sslErrors(const QList&)))); 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<(sender())].first = bytesReceived; _downloads[qobject_cast(sender())].second = bytesTotal; calcProgress(); } void error(QNetworkReply::NetworkError code) { LOG<<"Status:"<(sender())); if (_downloads.size()==0) finished(); } void metaDataChanged() { LOG; } void sslErrors(const QList & errors) { LOG; for (QList::const_iterator err(errors.begin()); err!=errors.end(); ++err) LOG<<"SSL-Error: "<errorString(); } void uploadProgress(qint64 bytesSent, qint64 bytesTotal) { //LOG<(sender())].first = bytesSent; _downloads[qobject_cast(sender())].second = bytesTotal; calcProgress(); } private: typedef std::pair Progress; typedef std::map Downloads; Downloads _downloads; };