From 73ef69526d00ffe09f9c24de43e4895d22c87a9b Mon Sep 17 00:00:00 2001 From: "Vladimir Golovnev (Glassez)" Date: Sat, 14 Sep 2019 11:11:29 +0300 Subject: [PATCH] Allow to cancel download request --- src/base/net/downloadmanager.cpp | 13 +++++++++++++ src/base/net/downloadmanager.h | 5 ++++- 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/src/base/net/downloadmanager.cpp b/src/base/net/downloadmanager.cpp index d35cdd6bc..ab002e4cd 100644 --- a/src/base/net/downloadmanager.cpp +++ b/src/base/net/downloadmanager.cpp @@ -121,6 +121,8 @@ namespace explicit DownloadHandlerImpl(const Net::DownloadRequest &downloadRequest, QObject *parent); ~DownloadHandlerImpl() override; + void cancel() override; + QString url() const; const Net::DownloadRequest downloadRequest() const; @@ -416,6 +418,17 @@ namespace delete m_reply; } + void DownloadHandlerImpl::cancel() + { + if (m_reply) { + m_reply->abort(); + } + else { + setError(errorCodeToString(QNetworkReply::OperationCanceledError)); + finish(); + } + } + void DownloadHandlerImpl::assignNetworkReply(QNetworkReply *reply) { Q_ASSERT(reply); diff --git a/src/base/net/downloadmanager.h b/src/base/net/downloadmanager.h index a4fd7b2ac..ceba46010 100644 --- a/src/base/net/downloadmanager.h +++ b/src/base/net/downloadmanager.h @@ -104,6 +104,8 @@ namespace Net public: using QObject::QObject; + virtual void cancel() = 0; + signals: void finished(const DownloadResult &result); }; @@ -118,6 +120,8 @@ namespace Net static void freeInstance(); static DownloadManager *instance(); + DownloadHandler *download(const DownloadRequest &downloadRequest); + template void download(const DownloadRequest &downloadRequest, Context context, Func &&slot); @@ -137,7 +141,6 @@ namespace Net private: explicit DownloadManager(QObject *parent = nullptr); - DownloadHandler *download(const DownloadRequest &downloadRequest); void applyProxySettings(); void handleReplyFinished(const QNetworkReply *reply);