UI: AutoUpdateManager: Refactor re-downloads on error

This commit is contained in:
Nodir Temirkhodjaev 2024-10-18 13:10:36 +05:00
parent f0cd85d589
commit 63cee5624c
6 changed files with 34 additions and 13 deletions

View File

@ -21,13 +21,12 @@ namespace {
const QLoggingCategory LC("manager.autoUpdate");
constexpr int DownloadMaxTryCount = 3;
}
AutoUpdateManager::AutoUpdateManager(const QString &updatePath, QObject *parent) :
TaskDownloader(parent), m_updatePath(updatePath + "update/")
{
setDownloadMaxTryCount(3);
}
void AutoUpdateManager::setFlags(Flags v)
@ -136,6 +135,8 @@ bool AutoUpdateManager::startDownload()
void AutoUpdateManager::setupDownloader()
{
TaskDownloader::setupDownloader();
downloader()->setUrl(m_downloadUrl);
connect(downloader(), &NetDownloader::dataReceived, this,
@ -143,8 +144,6 @@ void AutoUpdateManager::setupDownloader()
setIsDownloaded(false);
setIsDownloading(true);
m_downloadTryCount = 0;
}
void AutoUpdateManager::downloadFinished(const QByteArray &data, bool success)
@ -153,11 +152,6 @@ void AutoUpdateManager::downloadFinished(const QByteArray &data, bool success)
success = saveInstaller(data);
setIsDownloaded(success);
} else {
if (++m_downloadTryCount < DownloadMaxTryCount) {
startDownloader();
return;
}
}
setIsDownloading(false);

View File

@ -112,7 +112,6 @@ private:
QString m_fileName;
QString m_downloadUrl;
int m_downloadSize = 0;
int m_downloadTryCount = 0;
};
Q_DECLARE_OPERATORS_FOR_FLAGS(AutoUpdateManager::Flags)

View File

@ -4,6 +4,11 @@
TaskDownloader::TaskDownloader(QObject *parent) : TaskWorker(parent) { }
void TaskDownloader::setupDownloader()
{
m_downloadTryCount = 0;
}
void TaskDownloader::run()
{
createDownloader();
@ -30,7 +35,7 @@ void TaskDownloader::createDownloader()
m_downloader = new NetDownloader(this);
connect(m_downloader, &NetDownloader::finished, this, &TaskDownloader::downloadFinished);
connect(m_downloader, &NetDownloader::finished, this, &TaskDownloader::onFinished);
}
void TaskDownloader::deleteDownloader()
@ -52,3 +57,15 @@ void TaskDownloader::startDownloader()
m_downloader->start();
}
}
void TaskDownloader::onFinished(const QByteArray &data, bool success)
{
if (!success) {
if (++m_downloadTryCount < m_downloadMaxTryCount) {
startDownloader();
return;
}
}
emit downloadFinished(data, success);
}

View File

@ -15,9 +15,9 @@ public:
NetDownloader *downloader() const { return m_downloader; }
protected:
virtual void setupDownloader() = 0;
void setDownloadMaxTryCount(quint16 v) { m_downloadMaxTryCount = v; }
void startDownloader();
virtual void setupDownloader();
public slots:
void run() override;
@ -26,11 +26,18 @@ public slots:
protected slots:
virtual void downloadFinished(const QByteArray &data, bool success) = 0;
void startDownloader();
private:
void onFinished(const QByteArray &data, bool success);
void createDownloader();
void deleteDownloader();
private:
quint16 m_downloadMaxTryCount = 0;
quint16 m_downloadTryCount = 0;
NetDownloader *m_downloader = nullptr;
};

View File

@ -53,6 +53,8 @@ QString TaskUpdateChecker::releaseText() const
void TaskUpdateChecker::setupDownloader()
{
TaskDownloader::setupDownloader();
downloader()->setUrl(APP_UPDATES_API_URL);
}

View File

@ -18,6 +18,8 @@ TaskZoneDownloader::TaskZoneDownloader(QObject *parent) : TaskDownloader(parent)
void TaskZoneDownloader::setupDownloader()
{
TaskDownloader::setupDownloader();
// Load addresses from text inline
if (url().isEmpty()) {
loadTextInline();