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_checkLookupInfoFinished)
CASE_STRING(Rpc_AutoUpdateManager_startDownload)
CASE_STRING(Rpc_AutoUpdateManager_updateState)
CASE_STRING(Rpc_AutoUpdateManager_restartClients)
@ -124,6 +125,7 @@ RpcManager managerByCommand(Command cmd)
Rpc_AppInfoManager, // Rpc_AppInfoManager_lookupAppInfo,
Rpc_AppInfoManager, // Rpc_AppInfoManager_checkLookupFinished,
Rpc_AutoUpdateManager, // Rpc_AutoUpdateManager_startDownload,
Rpc_AutoUpdateManager, // Rpc_AutoUpdateManager_updateState,
Rpc_AutoUpdateManager, // Rpc_AutoUpdateManager_restartClients,
@ -206,6 +208,7 @@ bool commandRequiresValidation(Command cmd)
true, // Rpc_AppInfoManager_lookupAppInfo,
0, // Rpc_AppInfoManager_checkLookupFinished,
true, // Rpc_AutoUpdateManager_startDownload,
0, // Rpc_AutoUpdateManager_updateState,
0, // Rpc_AutoUpdateManager_restartClients,

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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