UI: AutoUpdateManager: Add "isDownloaded" flag

This commit is contained in:
Nodir Temirkhodjaev 2024-04-23 20:12:15 +03:00
parent 42d461dc53
commit bcd1aef9cb
8 changed files with 120 additions and 43 deletions

View File

@ -21,6 +21,7 @@ const char *const commandString(Command cmd)
CASE_STRING(Rpc_AppInfoManager_lookupAppInfo) CASE_STRING(Rpc_AppInfoManager_lookupAppInfo)
CASE_STRING(Rpc_AppInfoManager_checkLookupInfoFinished) CASE_STRING(Rpc_AppInfoManager_checkLookupInfoFinished)
CASE_STRING(Rpc_AutoUpdateManager_startDownload)
CASE_STRING(Rpc_AutoUpdateManager_updateState) CASE_STRING(Rpc_AutoUpdateManager_updateState)
CASE_STRING(Rpc_AutoUpdateManager_restartClients) CASE_STRING(Rpc_AutoUpdateManager_restartClients)
@ -124,6 +125,7 @@ RpcManager managerByCommand(Command cmd)
Rpc_AppInfoManager, // Rpc_AppInfoManager_lookupAppInfo, Rpc_AppInfoManager, // Rpc_AppInfoManager_lookupAppInfo,
Rpc_AppInfoManager, // Rpc_AppInfoManager_checkLookupFinished, Rpc_AppInfoManager, // Rpc_AppInfoManager_checkLookupFinished,
Rpc_AutoUpdateManager, // Rpc_AutoUpdateManager_startDownload,
Rpc_AutoUpdateManager, // Rpc_AutoUpdateManager_updateState, Rpc_AutoUpdateManager, // Rpc_AutoUpdateManager_updateState,
Rpc_AutoUpdateManager, // Rpc_AutoUpdateManager_restartClients, Rpc_AutoUpdateManager, // Rpc_AutoUpdateManager_restartClients,
@ -206,6 +208,7 @@ bool commandRequiresValidation(Command cmd)
true, // Rpc_AppInfoManager_lookupAppInfo, true, // Rpc_AppInfoManager_lookupAppInfo,
0, // Rpc_AppInfoManager_checkLookupFinished, 0, // Rpc_AppInfoManager_checkLookupFinished,
true, // Rpc_AutoUpdateManager_startDownload,
0, // Rpc_AutoUpdateManager_updateState, 0, // Rpc_AutoUpdateManager_updateState,
0, // Rpc_AutoUpdateManager_restartClients, 0, // Rpc_AutoUpdateManager_restartClients,

View File

@ -21,6 +21,7 @@ enum Command : qint8 {
Rpc_AppInfoManager_lookupAppInfo, Rpc_AppInfoManager_lookupAppInfo,
Rpc_AppInfoManager_checkLookupInfoFinished, Rpc_AppInfoManager_checkLookupInfoFinished,
Rpc_AutoUpdateManager_startDownload,
Rpc_AutoUpdateManager_updateState, Rpc_AutoUpdateManager_updateState,
Rpc_AutoUpdateManager_restartClients, Rpc_AutoUpdateManager_restartClients,

View File

@ -1,5 +1,6 @@
#include "autoupdatemanager.h" #include "autoupdatemanager.h"
#include <QFileInfo>
#include <QProcess> #include <QProcess>
#include <fortsettings.h> #include <fortsettings.h>
@ -27,9 +28,17 @@ int AutoUpdateManager::bytesReceived() const
void AutoUpdateManager::setUp() void AutoUpdateManager::setUp()
{ {
clearUpdateDir(); auto taskManager = IoCDependency<TaskManager>();
auto taskInfo = taskManager->taskInfoUpdateChecker();
setupTaskInfo(); connect(taskManager, &TaskManager::appVersionDownloaded, this,
[=, this] { setupByTaskInfo(taskInfo); });
setupByTaskInfo(taskInfo);
if (!isDownloaded()) {
clearUpdateDir();
}
} }
void AutoUpdateManager::tearDown() void AutoUpdateManager::tearDown()
@ -37,21 +46,21 @@ void AutoUpdateManager::tearDown()
finish(); finish();
} }
void AutoUpdateManager::setupTaskInfo() bool AutoUpdateManager::startDownload()
{ {
auto taskManager = IoCDependency<TaskManager>(); if (downloader())
return false;
m_taskInfo = taskManager->taskInfoUpdateChecker(); run();
connect(taskManager, &TaskManager::appVersionDownloaded, this, return true;
&AutoUpdateManager::checkAutoUpdate);
checkAutoUpdate();
} }
void AutoUpdateManager::setupDownloader() void AutoUpdateManager::setupDownloader()
{ {
downloader()->setUrl(m_taskInfo->downloadUrl()); setIsDownloaded(false);
downloader()->setUrl(m_downloadUrl);
connect(downloader(), &NetDownloader::startedChanged, this, connect(downloader(), &NetDownloader::startedChanged, this,
&AutoUpdateManager::isDownloadingChanged); &AutoUpdateManager::isDownloadingChanged);
@ -63,22 +72,23 @@ void AutoUpdateManager::downloadFinished(bool success)
{ {
if (success) { if (success) {
success = saveInstaller(); success = saveInstaller();
setIsDownloaded(success);
} }
finish(success); finish(success);
} }
void AutoUpdateManager::checkAutoUpdate() void AutoUpdateManager::setupByTaskInfo(TaskInfoUpdateChecker *taskInfo)
{ {
if (downloader()) m_downloadUrl = taskInfo->downloadUrl();
if (m_downloadUrl.isEmpty())
return; return;
const QString downloadUrl = m_taskInfo->downloadUrl(); m_fileName = QUrl(m_downloadUrl).fileName();
if (!downloadUrl.isEmpty() && m_taskInfo->isNewVersion()) { m_downloadSize = taskInfo->downloadSize();
m_fileName = QUrl(downloadUrl).fileName();
run(); const QFileInfo fi(installerPath());
} setIsDownloaded(fi.size() == m_downloadSize);
} }
void AutoUpdateManager::clearUpdateDir() void AutoUpdateManager::clearUpdateDir()
@ -93,7 +103,7 @@ void AutoUpdateManager::clearUpdateDir()
bool AutoUpdateManager::saveInstaller() bool AutoUpdateManager::saveInstaller()
{ {
const QByteArray fileData = downloader()->takeBuffer(); const QByteArray fileData = downloader()->takeBuffer();
if (fileData.size() != m_taskInfo->downloadSize()) if (fileData.size() != m_downloadSize)
return false; return false;
return FileUtil::writeFileData(installerPath(), fileData); return FileUtil::writeFileData(installerPath(), fileData);

View File

@ -15,32 +15,36 @@ class AutoUpdateManager : public TaskDownloader, public IocService
public: public:
explicit AutoUpdateManager(const QString &cachePath, QObject *parent = nullptr); explicit AutoUpdateManager(const QString &cachePath, QObject *parent = nullptr);
bool isDownloaded() const { return m_isDownloaded; }
virtual bool isDownloading() const; virtual bool isDownloading() const;
virtual int bytesReceived() const; virtual int bytesReceived() const;
void setUp() override; void setUp() override;
void tearDown() override; void tearDown() override;
public slots:
virtual bool startDownload();
bool runInstaller();
signals: signals:
void isDownloadingChanged(); void isDownloadingChanged();
void bytesReceivedChanged(); void bytesReceivedChanged();
void restartClients(); void restartClients();
public slots:
bool runInstaller();
protected: protected:
void setupTaskInfo(); void setIsDownloaded(bool v) { m_isDownloaded = v; }
void setupDownloader() override; void setupDownloader() override;
protected slots: protected slots:
void downloadFinished(bool success) override; void downloadFinished(bool success) override;
void checkAutoUpdate();
private: private:
void setupByTaskInfo(TaskInfoUpdateChecker *taskInfo);
void clearUpdateDir(); void clearUpdateDir();
bool saveInstaller(); bool saveInstaller();
@ -50,10 +54,13 @@ private:
static QString getDownloadUrl(); static QString getDownloadUrl();
private: private:
QString m_updatePath; bool m_isDownloaded = false;
QString m_fileName;
TaskInfoUpdateChecker *m_taskInfo = nullptr; QString m_updatePath;
QString m_fileName;
QString m_downloadUrl;
int m_downloadSize = 0;
}; };
#endif // AUTOUPDATEMANAGER_H #endif // AUTOUPDATEMANAGER_H

View File

@ -5,6 +5,51 @@
#include <util/ioc/ioccontainer.h> #include <util/ioc/ioccontainer.h>
#include <util/osutil.h> #include <util/osutil.h>
namespace {
inline bool processAutoUpdateManager_updateState(
AutoUpdateManager *autoUpdateManager, const ProcessCommandArgs &p)
{
if (auto aum = qobject_cast<AutoUpdateManagerRpc *>(autoUpdateManager)) {
aum->updateState(
p.args.value(0).toBool(), p.args.value(1).toBool(), p.args.value(2).toInt());
}
return true;
}
inline bool processAutoUpdateManager_restartClients(
AutoUpdateManager *autoUpdateManager, const ProcessCommandArgs & /*p*/)
{
QMetaObject::invokeMethod(
autoUpdateManager, [] { OsUtil::restartClient(); }, Qt::QueuedConnection);
return true;
}
bool processAutoUpdateManager_startDownload(AutoUpdateManager *autoUpdateManager,
const ProcessCommandArgs & /*p*/, QVariantList & /*resArgs*/)
{
return autoUpdateManager->startDownload();
}
using processAutoUpdateManager_func = bool (*)(
AutoUpdateManager *autoUpdateManager, const ProcessCommandArgs &p, QVariantList &resArgs);
static processAutoUpdateManager_func processAutoUpdateManager_funcList[] = {
&processAutoUpdateManager_startDownload, // Rpc_AutoUpdateManager_startDownload,
};
inline bool processAutoUpdateManagerRpcResult(
AutoUpdateManager *autoUpdateManager, const ProcessCommandArgs &p, QVariantList &resArgs)
{
const processAutoUpdateManager_func func = RpcManager::getProcessFunc(p.command,
processAutoUpdateManager_funcList, Control::Rpc_AutoUpdateManager_startDownload,
Control::Rpc_AutoUpdateManager_startDownload);
return func ? func(autoUpdateManager, p, resArgs) : false;
}
}
AutoUpdateManagerRpc::AutoUpdateManagerRpc(const QString &cachePath, QObject *parent) : AutoUpdateManagerRpc::AutoUpdateManagerRpc(const QString &cachePath, QObject *parent) :
AutoUpdateManager(cachePath, parent) AutoUpdateManager(cachePath, parent)
{ {
@ -33,8 +78,9 @@ void AutoUpdateManagerRpc::setUp()
setupClientSignals(); setupClientSignals();
} }
void AutoUpdateManagerRpc::updateState(bool isDownloading, int bytesReceived) void AutoUpdateManagerRpc::updateState(bool isDownloaded, bool isDownloading, int bytesReceived)
{ {
setIsDownloaded(isDownloaded);
setIsDownloading(isDownloading); setIsDownloading(isDownloading);
setBytesReceived(bytesReceived); setBytesReceived(bytesReceived);
} }
@ -43,7 +89,11 @@ QVariantList AutoUpdateManagerRpc::updateState_args()
{ {
auto autoUpdateManager = IoC<AutoUpdateManager>(); auto autoUpdateManager = IoC<AutoUpdateManager>();
return { autoUpdateManager->isDownloading(), autoUpdateManager->bytesReceived() }; return {
autoUpdateManager->isDownloaded(),
autoUpdateManager->isDownloading(),
autoUpdateManager->bytesReceived(),
};
} }
bool AutoUpdateManagerRpc::processInitClient(ControlWorker *w) bool AutoUpdateManagerRpc::processInitClient(ControlWorker *w)
@ -51,25 +101,23 @@ bool AutoUpdateManagerRpc::processInitClient(ControlWorker *w)
return w->sendCommand(Control::Rpc_AutoUpdateManager_updateState, updateState_args()); return w->sendCommand(Control::Rpc_AutoUpdateManager_updateState, updateState_args());
} }
bool AutoUpdateManagerRpc::processServerCommand(const ProcessCommandArgs &p, bool AutoUpdateManagerRpc::processServerCommand(
QVariantList & /*resArgs*/, bool & /*ok*/, bool & /*isSendResult*/) const ProcessCommandArgs &p, QVariantList &resArgs, bool &ok, bool &isSendResult)
{ {
auto autoUpdateManager = IoC<AutoUpdateManager>(); auto autoUpdateManager = IoC<AutoUpdateManager>();
switch (p.command) { switch (p.command) {
case Control::Rpc_AutoUpdateManager_updateState: { case Control::Rpc_AutoUpdateManager_updateState: {
if (auto aum = qobject_cast<AutoUpdateManagerRpc *>(autoUpdateManager)) { return processAutoUpdateManager_updateState(autoUpdateManager, p);
aum->updateState(p.args.value(0).toBool(), p.args.value(1).toInt());
}
return true;
} }
case Control::Rpc_AutoUpdateManager_restartClients: { case Control::Rpc_AutoUpdateManager_restartClients: {
QMetaObject::invokeMethod( return processAutoUpdateManager_restartClients(autoUpdateManager, p);
autoUpdateManager, [] { OsUtil::restartClient(); }, Qt::QueuedConnection); }
default: {
ok = processAutoUpdateManagerRpcResult(autoUpdateManager, p, resArgs);
isSendResult = true;
return true; return true;
} }
default:
return false;
} }
} }
@ -91,6 +139,11 @@ void AutoUpdateManagerRpc::setupServerSignals(RpcManager *rpcManager)
[=] { rpcManager->invokeOnClients(Control::Rpc_AutoUpdateManager_restartClients); }); [=] { rpcManager->invokeOnClients(Control::Rpc_AutoUpdateManager_restartClients); });
} }
bool AutoUpdateManagerRpc::startDownload()
{
return IoC<RpcManager>()->doOnServer(Control::Rpc_AutoUpdateManager_startDownload);
}
void AutoUpdateManagerRpc::setupClientSignals() void AutoUpdateManagerRpc::setupClientSignals()
{ {
auto rpcManager = IoCDependency<RpcManager>(); auto rpcManager = IoCDependency<RpcManager>();

View File

@ -23,7 +23,7 @@ public:
void setUp() override; void setUp() override;
void updateState(bool isDownloading, int bytesReceived); void updateState(bool isDownloaded, bool isDownloading, int bytesReceived);
static QVariantList updateState_args(); static QVariantList updateState_args();
@ -34,6 +34,9 @@ public:
static void setupServerSignals(RpcManager *rpcManager); static void setupServerSignals(RpcManager *rpcManager);
public slots:
bool startDownload() override;
private: private:
void setupClientSignals(); void setupClientSignals();

View File

@ -49,6 +49,9 @@ bool processConfManager_checkPassword(
return ok; return ok;
} }
using processConfManager_func = bool (*)(
ConfManager *confManager, const ProcessCommandArgs &p, QVariantList &resArgs);
static processConfManager_func processConfManager_funcList[] = { static processConfManager_func processConfManager_funcList[] = {
&processConfManager_saveVariant, // Rpc_ConfManager_saveVariant, &processConfManager_saveVariant, // Rpc_ConfManager_saveVariant,
&processConfManager_exportMasterBackup, // Rpc_ConfManager_exportMasterBackup, &processConfManager_exportMasterBackup, // Rpc_ConfManager_exportMasterBackup,

View File

@ -8,9 +8,6 @@ class TaskManager;
struct ProcessCommandArgs; struct ProcessCommandArgs;
using processConfManager_func = bool (*)(
ConfManager *confManager, const ProcessCommandArgs &p, QVariantList &resArgs);
class ConfManagerRpc : public ConfManager class ConfManagerRpc : public ConfManager
{ {
Q_OBJECT Q_OBJECT