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

View File

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

View File

@ -4,6 +4,11 @@
TaskDownloader::TaskDownloader(QObject *parent) : TaskWorker(parent) { } TaskDownloader::TaskDownloader(QObject *parent) : TaskWorker(parent) { }
void TaskDownloader::setupDownloader()
{
m_downloadTryCount = 0;
}
void TaskDownloader::run() void TaskDownloader::run()
{ {
createDownloader(); createDownloader();
@ -30,7 +35,7 @@ void TaskDownloader::createDownloader()
m_downloader = new NetDownloader(this); m_downloader = new NetDownloader(this);
connect(m_downloader, &NetDownloader::finished, this, &TaskDownloader::downloadFinished); connect(m_downloader, &NetDownloader::finished, this, &TaskDownloader::onFinished);
} }
void TaskDownloader::deleteDownloader() void TaskDownloader::deleteDownloader()
@ -52,3 +57,15 @@ void TaskDownloader::startDownloader()
m_downloader->start(); 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; } NetDownloader *downloader() const { return m_downloader; }
protected: protected:
virtual void setupDownloader() = 0; void setDownloadMaxTryCount(quint16 v) { m_downloadMaxTryCount = v; }
void startDownloader(); virtual void setupDownloader();
public slots: public slots:
void run() override; void run() override;
@ -26,11 +26,18 @@ public slots:
protected slots: protected slots:
virtual void downloadFinished(const QByteArray &data, bool success) = 0; virtual void downloadFinished(const QByteArray &data, bool success) = 0;
void startDownloader();
private: private:
void onFinished(const QByteArray &data, bool success);
void createDownloader(); void createDownloader();
void deleteDownloader(); void deleteDownloader();
private: private:
quint16 m_downloadMaxTryCount = 0;
quint16 m_downloadTryCount = 0;
NetDownloader *m_downloader = nullptr; NetDownloader *m_downloader = nullptr;
}; };

View File

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

View File

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