From a97a915bc8675dac8bbbf22cb3e8efa04ca47a87 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gr=C3=A9gory=20Soutad=C3=A9?= Date: Tue, 22 Feb 2022 20:58:32 +0100 Subject: [PATCH] Rework HTTP request loop events (Thanks Milian) and display download progression --- include/libgourou_common.h | 2 +- utils/drmprocessorclientimpl.cpp | 28 ++++++++++++++++++++++++---- 2 files changed, 25 insertions(+), 5 deletions(-) diff --git a/include/libgourou_common.h b/include/libgourou_common.h index 6d4ed08..d9747c7 100644 --- a/include/libgourou_common.h +++ b/include/libgourou_common.h @@ -110,7 +110,7 @@ namespace gourou CLIENT_ZIP_ERROR, CLIENT_GENERIC_EXCEPTION, CLIENT_NETWORK_ERROR, - CLIENT_INVALID_PKCS8, + CLIENT_INVALID_PKCS8 }; enum DRM_REMOVAL_ERROR { diff --git a/utils/drmprocessorclientimpl.cpp b/utils/drmprocessorclientimpl.cpp index b0244be..089b31a 100644 --- a/utils/drmprocessorclientimpl.cpp +++ b/utils/drmprocessorclientimpl.cpp @@ -88,6 +88,20 @@ void DRMProcessorClientImpl::randBytes(unsigned char* bytesOut, unsigned int len } /* HTTP interface */ +#define DISPLAY_THRESHOLD 10*1024 // Threshold to display download progression + +static void downloadProgress(qint64 bytesReceived, qint64 bytesTotal) { + // For "big" files only + if (bytesTotal >= DISPLAY_THRESHOLD && gourou::logLevel >= gourou::WARN) + { + int percent = 0; + if (bytesTotal) + percent = (bytesReceived * 100) / bytesTotal; + + std::cout << "\rDownload " << percent << "%" << std::flush; + } +} + std::string DRMProcessorClientImpl::sendHTTPRequest(const std::string& URL, const std::string& POSTData, const std::string& contentType, std::map* responseHeaders) { QNetworkRequest request(QUrl(URL.c_str())); @@ -112,11 +126,15 @@ std::string DRMProcessorClientImpl::sendHTTPRequest(const std::string& URL, cons else reply = networkManager.get(request); - QCoreApplication* app = QCoreApplication::instance(); - networkManager.moveToThread(app->thread()); - while (!reply->isFinished()) - app->processEvents(); + QEventLoop loop; + QObject::connect(reply, &QNetworkReply::finished, &loop, &QEventLoop::quit); + // Handled just below + QObject::connect(reply, &QNetworkReply::errorOccurred, &loop, &QEventLoop::quit); + QObject::connect(reply, &QNetworkReply::downloadProgress, &loop, downloadProgress); + + loop.exec(); + QByteArray location = reply->rawHeader("Location"); if (location.size() != 0) { @@ -136,6 +154,8 @@ std::string DRMProcessorClientImpl::sendHTTPRequest(const std::string& URL, cons } replyData = reply->readAll(); + if (replyData.size() >= DISPLAY_THRESHOLD && gourou::logLevel >= gourou::WARN) + std::cout << std::endl; if (reply->rawHeader("Content-Type") == "application/vnd.adobe.adept+xml") { GOUROU_LOG(gourou::DEBUG, ">>> " << std::endl << replyData.data());