diff --git a/src/lib/qtlib/src/qtlib_networkresource.cpp b/src/lib/qtlib/src/qtlib_networkresource.cpp index 9b26e293f220433255d49a86fc4a5448e7ebf742..e7d415eb683cea798ff25c418af5e384deb70234 100644 --- a/src/lib/qtlib/src/qtlib_networkresource.cpp +++ b/src/lib/qtlib/src/qtlib_networkresource.cpp @@ -216,6 +216,16 @@ void NetworkResource::replyFinished() emit finished(this); } +void NetworkResource::replyDownloadProgress(qint64 bytesReceived, qint64 bytesTotal) +{ + emit downloadProgress(id(), bytesReceived, bytesTotal); +} + +void NetworkResource::replyUploadProgress(qint64 bytesSent, qint64 bytesTotal) +{ + emit uploadProgress(id(), bytesSent, bytesTotal); +} + void NetworkResource::setManager(QNetworkAccessManager *manager) { manager_ = manager; @@ -266,8 +276,8 @@ NetworkResource *NetworkResource::send(const QUrl &url, bool async) Q_ASSERT(false); } connect(reply(), &QNetworkReply::finished, this, &NetworkResource::replyFinished); - connect(reply(), &QNetworkReply::downloadProgress, this, &NetworkResource::downloadProgress); - connect(reply(), &QNetworkReply::uploadProgress, this, &NetworkResource::uploadProgress); + connect(reply(), &QNetworkReply::downloadProgress, this, &NetworkResource::replyDownloadProgress); + connect(reply(), &QNetworkReply::uploadProgress, this, &NetworkResource::replyUploadProgress); if (!async) { QEventLoop eventLoop; connect(this, &NetworkResource::finished, &eventLoop, &QEventLoop::quit); diff --git a/src/lib/qtlib/src/qtlib_networkresource.h b/src/lib/qtlib/src/qtlib_networkresource.h index 038f96ebc7d1814b62fe97163f921c75cbcfd5c4..beae8fc20949f57e58a87a9049e3e4553e65950f 100644 --- a/src/lib/qtlib/src/qtlib_networkresource.h +++ b/src/lib/qtlib/src/qtlib_networkresource.h @@ -56,14 +56,16 @@ public: signals: void finished(NetworkResource *resource); - void downloadProgress(qint64 bytesReceived, qint64 bytesTotal); - void uploadProgress(qint64 bytesSent, qint64 bytesTotal); + void downloadProgress(QString id, qint64 bytesReceived, qint64 bytesTotal); + void uploadProgress(QString id, qint64 bytesSent, qint64 bytesTotal); public slots: void abort(); private slots: void replyFinished(); + void replyDownloadProgress(qint64 bytesReceived, qint64 bytesTotal); + void replyUploadProgress(qint64 bytesSent, qint64 bytesTotal); private: void setManager(QNetworkAccessManager *manager); diff --git a/src/lib/qtlib/test/main.cpp b/src/lib/qtlib/test/main.cpp index c43cf3da08a8038723a9a53df285cf95839ee5f8..9c2ab9447cdfc88ef2d0d1bb073afeed420107dd 100644 --- a/src/lib/qtlib/test/main.cpp +++ b/src/lib/qtlib/test/main.cpp @@ -16,7 +16,8 @@ public: Test() {} virtual ~Test() {} - void start() { + void start() + { qDebug() << "Start"; qtlib::NetworkResource *resource = new qtlib::NetworkResource( @@ -24,27 +25,35 @@ public: QUrl("https://api.opensource.org/license/LGPL-3.0"), false, this); + connect(resource, &qtlib::NetworkResource::downloadProgress, this, &Test::downloadProgress); QJsonObject result = qtlib::Json(resource->get()->readData()).toObject(); qDebug() << resource->id() << ":" << result["name"].toString(); + connect(resource, &qtlib::NetworkResource::finished, this, &Test::finished); + resource->setId(result["name"].toString()); resource->setUrl(QUrl(result["text"].toArray()[0].toObject()["url"].toString())); resource->setAsync(true); - connect(resource, &qtlib::NetworkResource::finished, this, &Test::finished); resource->get(); } public slots: - void finished(qtlib::NetworkResource *resource) { + void finished(qtlib::NetworkResource *resource) + { QString path = qtlib::Dir::tempPath() + "/" + resource->url().fileName(); resource->saveData(path); - resource->deleteLater(); qDebug() << "Downloaded" << resource->id() << ":" << path; qDebug() << "Finished"; + resource->deleteLater(); QCoreApplication::exit(); } + + void downloadProgress(QString id, qint64 bytesReceived, qint64 bytesTotal) + { + qDebug() << "Progress" << id << ":" << bytesReceived << "/" << bytesTotal; + } }; int main(int argc, char *argv[])