From 35cbdb17e8f0e188e42c0a2c76ce2ff7580f5f93 Mon Sep 17 00:00:00 2001 From: Gres Date: Sat, 4 Apr 2020 16:23:26 +0300 Subject: [PATCH] Move update download handling to separate method --- src/service/updates.cpp | 70 +++++++++++++++++++++++------------------ src/service/updates.h | 2 ++ 2 files changed, 42 insertions(+), 30 deletions(-) diff --git a/src/service/updates.cpp b/src/service/updates.cpp index a1fdd0d..72b8827 100644 --- a/src/service/updates.cpp +++ b/src/service/updates.cpp @@ -54,14 +54,37 @@ Loader::Loader(const QUrl &updateUrl, QObject *parent) this, &Loader::handleReply); } +void Loader::handleReply(QNetworkReply *reply) +{ + if (reply->url() == updateUrl_) { + handleUpdateReply(reply); + } else { + handleComponentReply(reply); + } +} + void Loader::checkForUpdates() { auto reply = network_->get(QNetworkRequest(updateUrl_)); - if (reply->error() == QNetworkReply::NoError) - return; + if (reply->error() != QNetworkReply::NoError) + handleUpdateReply(reply); +} +void Loader::handleUpdateReply(QNetworkReply *reply) +{ reply->deleteLater(); - emit error(toError(*reply)); + + if (reply->error() != QNetworkReply::NoError) { + emit error(toError(*reply)); + return; + } + + const auto replyData = reply->readAll(); + + SOFT_ASSERT(model_, return ); + model_->parse(replyData); + if (model_->hasUpdates()) + emit updatesAvailable(); } QString Loader::toError(QNetworkReply &reply) const @@ -108,37 +131,12 @@ void Loader::applyUserActions() commitUpdate(); } -void Loader::finishUpdate(const QString &error) -{ - installer_.reset(); - for (const auto &i : componentReplyToPath_) i.first->deleteLater(); - componentReplyToPath_.clear(); - if (!error.isEmpty()) - emit this->error(error); - SOFT_ASSERT(model_, return ); - model_->updateStates(); -} - -void Loader::handleReply(QNetworkReply *reply) +void Loader::handleComponentReply(QNetworkReply *reply) { reply->deleteLater(); - const auto isUpdatesReply = reply->url() == updateUrl_; - if (reply->error() != QNetworkReply::NoError) { - emit error(toError(*reply)); - if (!isUpdatesReply) - finishUpdate(); - return; - } - - const auto replyData = reply->readAll(); - - if (isUpdatesReply) { - SOFT_ASSERT(model_, return ); - model_->parse(replyData); - if (model_->hasUpdates()) - emit updatesAvailable(); + finishUpdate(toError(*reply)); return; } @@ -159,6 +157,7 @@ void Loader::handleReply(QNetworkReply *reply) finishUpdate(error); return; } + const auto replyData = reply->readAll(); f.write(replyData); f.close(); @@ -168,6 +167,17 @@ void Loader::handleReply(QNetworkReply *reply) commitUpdate(); } +void Loader::finishUpdate(const QString &error) +{ + installer_.reset(); + for (const auto &i : componentReplyToPath_) i.first->deleteLater(); + componentReplyToPath_.clear(); + if (!error.isEmpty()) + emit this->error(error); + SOFT_ASSERT(model_, return ); + model_->updateStates(); +} + void Loader::commitUpdate() { SOFT_ASSERT(installer_, return ); diff --git a/src/service/updates.h b/src/service/updates.h index 97b71f0..2adb1a5 100644 --- a/src/service/updates.h +++ b/src/service/updates.h @@ -130,6 +130,8 @@ signals: private: void handleReply(QNetworkReply* reply); + void handleComponentReply(QNetworkReply* reply); + void handleUpdateReply(QNetworkReply* reply); QString toError(QNetworkReply& reply) const; void finishUpdate(const QString& error = {}); void commitUpdate();