Move update download handling to separate method

This commit is contained in:
Gres 2020-04-04 16:23:26 +03:00
parent 0df0f2ef20
commit 35cbdb17e8
2 changed files with 42 additions and 30 deletions

View File

@ -54,14 +54,37 @@ Loader::Loader(const QUrl &updateUrl, QObject *parent)
this, &Loader::handleReply); this, &Loader::handleReply);
} }
void Loader::handleReply(QNetworkReply *reply)
{
if (reply->url() == updateUrl_) {
handleUpdateReply(reply);
} else {
handleComponentReply(reply);
}
}
void Loader::checkForUpdates() void Loader::checkForUpdates()
{ {
auto reply = network_->get(QNetworkRequest(updateUrl_)); auto reply = network_->get(QNetworkRequest(updateUrl_));
if (reply->error() == QNetworkReply::NoError) if (reply->error() != QNetworkReply::NoError)
return; handleUpdateReply(reply);
}
void Loader::handleUpdateReply(QNetworkReply *reply)
{
reply->deleteLater(); 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 QString Loader::toError(QNetworkReply &reply) const
@ -108,37 +131,12 @@ void Loader::applyUserActions()
commitUpdate(); commitUpdate();
} }
void Loader::finishUpdate(const QString &error) void Loader::handleComponentReply(QNetworkReply *reply)
{
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)
{ {
reply->deleteLater(); reply->deleteLater();
const auto isUpdatesReply = reply->url() == updateUrl_;
if (reply->error() != QNetworkReply::NoError) { if (reply->error() != QNetworkReply::NoError) {
emit error(toError(*reply)); finishUpdate(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();
return; return;
} }
@ -159,6 +157,7 @@ void Loader::handleReply(QNetworkReply *reply)
finishUpdate(error); finishUpdate(error);
return; return;
} }
const auto replyData = reply->readAll();
f.write(replyData); f.write(replyData);
f.close(); f.close();
@ -168,6 +167,17 @@ void Loader::handleReply(QNetworkReply *reply)
commitUpdate(); 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() void Loader::commitUpdate()
{ {
SOFT_ASSERT(installer_, return ); SOFT_ASSERT(installer_, return );

View File

@ -130,6 +130,8 @@ signals:
private: private:
void handleReply(QNetworkReply* reply); void handleReply(QNetworkReply* reply);
void handleComponentReply(QNetworkReply* reply);
void handleUpdateReply(QNetworkReply* reply);
QString toError(QNetworkReply& reply) const; QString toError(QNetworkReply& reply) const;
void finishUpdate(const QString& error = {}); void finishUpdate(const QString& error = {});
void commitUpdate(); void commitUpdate();