From 12e47d242574a3bc91d0c4a0c96fa1c93a613c7e Mon Sep 17 00:00:00 2001 From: Akira Ohgaki <akiraohgaki@gmail.com> Date: Tue, 13 Dec 2016 08:06:38 +0900 Subject: [PATCH] Update qtlib --- src/lib/qtlib/src/qtlib_networkresource.cpp | 14 ++++++++++++-- src/lib/qtlib/src/qtlib_networkresource.h | 6 ++++-- src/lib/qtlib/test/main.cpp | 17 +++++++++++++---- 3 files changed, 29 insertions(+), 8 deletions(-) diff --git a/src/lib/qtlib/src/qtlib_networkresource.cpp b/src/lib/qtlib/src/qtlib_networkresource.cpp index 9b26e29..e7d415e 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 038f96e..beae8fc 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 c43cf3d..9c2ab94 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[]) -- GitLab