From 3bc831ce510da5efd800120885745778803bd528 Mon Sep 17 00:00:00 2001 From: Nodir Temirkhodjaev Date: Thu, 13 Jun 2024 12:12:14 +0300 Subject: [PATCH] UI: Add "-c zone update" command line argument --- src/ui/control/control.cpp | 3 ++ src/ui/control/control_types.h | 1 + src/ui/control/controlmanager.cpp | 74 ++++++++++++++++++++++++++----- src/ui/control/controlmanager.h | 12 ++++- 4 files changed, 79 insertions(+), 11 deletions(-) diff --git a/src/ui/control/control.cpp b/src/ui/control/control.cpp index 3dca4eae..6378c1e2 100644 --- a/src/ui/control/control.cpp +++ b/src/ui/control/control.cpp @@ -13,6 +13,7 @@ const char *const commandString(Command cmd) CASE_STRING(CommandHome) CASE_STRING(CommandProg) + CASE_STRING(CommandZone) CASE_STRING(Rpc_Result_Ok) CASE_STRING(Rpc_Result_Error) @@ -120,6 +121,7 @@ RpcManager managerByCommand(Command cmd) Rpc_NoneManager, // CommandHome, Rpc_NoneManager, // CommandProg, + Rpc_NoneManager, // CommandZone, Rpc_NoneManager, // Rpc_Result_Ok, Rpc_NoneManager, // Rpc_Result_Error, @@ -206,6 +208,7 @@ bool commandRequiresValidation(Command cmd) 0, // CommandHome, 0, // CommandProg, + 0, // CommandZone, 0, // Rpc_Result_Ok, 0, // Rpc_Result_Error, diff --git a/src/ui/control/control_types.h b/src/ui/control/control_types.h index c42ed4f1..d12447d3 100644 --- a/src/ui/control/control_types.h +++ b/src/ui/control/control_types.h @@ -13,6 +13,7 @@ enum Command : qint8 { CommandHome, CommandProg, + CommandZone, Rpc_Result_Ok, Rpc_Result_Error, diff --git a/src/ui/control/controlmanager.cpp b/src/ui/control/controlmanager.cpp index 5257932d..ac666ad8 100644 --- a/src/ui/control/controlmanager.cpp +++ b/src/ui/control/controlmanager.cpp @@ -13,6 +13,8 @@ #include #include #include +#include +#include #include #include #include @@ -25,6 +27,11 @@ constexpr int maxClientsCount = 9; const QLoggingCategory LC("control"); +bool checkActionPassword() +{ + return IoC()->checkPassword(/*temporary=*/true); +} + } ControlManager::ControlManager(QObject *parent) : QObject(parent) { } @@ -107,6 +114,8 @@ bool ControlManager::processCommandClient() command = Control::CommandHome; } else if (settings->controlCommand() == "prog") { command = Control::CommandProg; + } else if (settings->controlCommand() == "zone") { + command = Control::CommandZone; } else { qCWarning(LC) << "Unknown control command:" << settings->controlCommand(); return false; @@ -220,24 +229,27 @@ bool ControlManager::processRequest(Control::Command command, const QVariantList bool ControlManager::processCommand(const ProcessCommandArgs &p) { + bool ok; + switch (p.command) { case Control::CommandHome: { - if (processCommandHome(p)) - return true; + ok = processCommandHome(p); } break; case Control::CommandProg: { - if (processCommandProg(p)) - return true; + ok = processCommandProg(p); + } break; + case Control::CommandZone: { + ok = processCommandZone(p); } break; default: - if (IoC()->processCommandRpc(p)) - return true; + ok = IoC()->processCommandRpc(p); } - if (p.errorMessage.isEmpty()) { + if (!ok && p.errorMessage.isEmpty()) { p.errorMessage = "Invalid command"; } - return false; + + return ok; } bool ControlManager::processCommandHome(const ProcessCommandArgs &p) @@ -297,8 +309,7 @@ bool ControlManager::checkProgActionPassword(ProgAction progAction) constexpr const quint32 passwordRequiredActions = ProgActionDel | ProgActionAllow | ProgActionBlock | ProgActionKill; - return (passwordRequiredActions & progAction) == 0 - || IoC()->checkPassword(/*temporary=*/true); + return (passwordRequiredActions & progAction) == 0 || checkActionPassword(); } ControlManager::ProgAction ControlManager::progActionByText(const QString &commandText) @@ -321,6 +332,49 @@ ControlManager::ProgAction ControlManager::progActionByText(const QString &comma return ProgActionNone; } +bool ControlManager::processCommandZone(const ProcessCommandArgs &p) +{ + const ZoneAction zoneAction = zoneActionByText(p.args.value(0).toString()); + if (zoneAction == ZoneActionNone) { + p.errorMessage = "Usage: zone update"; + return false; + } + + if (!checkZoneActionPassword(zoneAction)) { + p.errorMessage = "Password required"; + return false; + } + + return processCommandZoneAction(zoneAction); +} + +bool ControlManager::processCommandZoneAction(ZoneAction zoneAction) +{ + switch (zoneAction) { + case ZoneActionUpdate: { + IoC()->runTask(TaskInfo::ZoneDownloader); + return true; + } + default: + return false; + } +} + +bool ControlManager::checkZoneActionPassword(ZoneAction zoneAction) +{ + constexpr const quint32 passwordRequiredActions = ZoneActionUpdate; + + return (passwordRequiredActions & zoneAction) == 0 || checkActionPassword(); +} + +ControlManager::ZoneAction ControlManager::zoneActionByText(const QString &commandText) +{ + if (commandText == "update") + return ZoneActionUpdate; + + return ZoneActionNone; +} + QString ControlManager::getServerName(bool isService) { return QLatin1String(APP_BASE) + (isService ? "Svc" : OsUtil::userName()) + "Pipe"; diff --git a/src/ui/control/controlmanager.h b/src/ui/control/controlmanager.h index 85958637..609df228 100644 --- a/src/ui/control/controlmanager.h +++ b/src/ui/control/controlmanager.h @@ -54,15 +54,25 @@ private: ProgActionKill = (1 << 4), }; + enum ZoneAction : quint32 { + ZoneActionNone = 0, + ZoneActionUpdate = (1 << 0), + }; + bool processCommand(const ProcessCommandArgs &p); bool processCommandHome(const ProcessCommandArgs &p); bool processCommandProg(const ProcessCommandArgs &p); - bool processCommandProgAction(ProgAction progAction, const QString &appPath); + static bool processCommandProgAction(ProgAction progAction, const QString &appPath); static bool checkProgActionPassword(ProgAction progAction); static ProgAction progActionByText(const QString &commandText); + bool processCommandZone(const ProcessCommandArgs &p); + static bool processCommandZoneAction(ZoneAction zoneAction); + static bool checkZoneActionPassword(ZoneAction zoneAction); + static ZoneAction zoneActionByText(const QString &commandText); + static QString getServerName(bool isService = false); private: