From 15e3efdbb0f4062852be82bbbda4e83b07a0607e Mon Sep 17 00:00:00 2001 From: Nodir Temirkhodjaev Date: Sat, 15 Jun 2024 14:59:56 +0300 Subject: [PATCH] UI: ControlManager: Minor refactor --- src/ui/control/controlmanager.cpp | 310 ++++++++++++++++-------------- src/ui/control/controlmanager.h | 28 --- 2 files changed, 162 insertions(+), 176 deletions(-) diff --git a/src/ui/control/controlmanager.cpp b/src/ui/control/controlmanager.cpp index ac666ad8..a757df61 100644 --- a/src/ui/control/controlmanager.cpp +++ b/src/ui/control/controlmanager.cpp @@ -32,6 +32,168 @@ bool checkActionPassword() return IoC()->checkPassword(/*temporary=*/true); } +bool processCommandHome(const ProcessCommandArgs &p) +{ + const auto commandText = p.args.value(0).toString(); + + if (commandText == "show") { + return IoC()->exposeHomeWindow(); + } + + p.errorMessage = "Usage: home show"; + return false; +} + +enum ProgAction : quint32 { + ProgActionNone = 0, + ProgActionAdd = (1 << 0), + ProgActionDel = (1 << 1), + ProgActionAllow = (1 << 2), + ProgActionBlock = (1 << 3), + ProgActionKill = (1 << 4), +}; + +bool processCommandProgAction(ProgAction progAction, const QString &appPath) +{ + switch (progAction) { + case ProgActionAdd: { + return IoC()->showProgramEditForm(appPath); + } + case ProgActionDel: { + return IoC()->deleteAppPath(appPath); + } + case ProgActionAllow: + case ProgActionBlock: + case ProgActionKill: { + const bool blocked = (progAction != ProgActionAllow); + const bool killProcess = (progAction == ProgActionKill); + + return IoC()->addOrUpdateAppPath(appPath, blocked, killProcess); + } + default: + return false; + } +} + +bool checkProgActionPassword(ProgAction progAction) +{ + constexpr const quint32 passwordRequiredActions = + ProgActionDel | ProgActionAllow | ProgActionBlock | ProgActionKill; + + return (passwordRequiredActions & progAction) == 0 || checkActionPassword(); +} + +ProgAction progActionByText(const QString &commandText) +{ + if (commandText == "add") + return ProgActionAdd; + + if (commandText == "del") + return ProgActionDel; + + if (commandText == "allow") + return ProgActionAllow; + + if (commandText == "block") + return ProgActionBlock; + + if (commandText == "kill") + return ProgActionKill; + + return ProgActionNone; +} + +bool processCommandProg(const ProcessCommandArgs &p) +{ + const ProgAction progAction = progActionByText(p.args.value(0).toString()); + if (progAction == ProgActionNone) { + p.errorMessage = "Usage: prog add|del|allow|block|kill|show "; + return false; + } + + if (!checkProgActionPassword(progAction)) { + p.errorMessage = "Password required"; + return false; + } + + const QString appPath = p.args.value(1).toString(); + + return processCommandProgAction(progAction, appPath); +} + +enum ZoneAction : quint32 { + ZoneActionNone = 0, + ZoneActionUpdate = (1 << 0), +}; + +bool processCommandZoneAction(ZoneAction zoneAction) +{ + switch (zoneAction) { + case ZoneActionUpdate: { + IoC()->runTask(TaskInfo::ZoneDownloader); + return true; + } + default: + return false; + } +} + +bool checkZoneActionPassword(ZoneAction zoneAction) +{ + constexpr const quint32 passwordRequiredActions = ZoneActionUpdate; + + return (passwordRequiredActions & zoneAction) == 0 || checkActionPassword(); +} + +ZoneAction zoneActionByText(const QString &commandText) +{ + if (commandText == "update") + return ZoneActionUpdate; + + return ZoneActionNone; +} + +bool 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 processCommand(const ProcessCommandArgs &p) +{ + bool ok; + + switch (p.command) { + case Control::CommandHome: { + ok = processCommandHome(p); + } break; + case Control::CommandProg: { + ok = processCommandProg(p); + } break; + case Control::CommandZone: { + ok = processCommandZone(p); + } break; + default: + ok = IoC()->processCommandRpc(p); + } + + if (!ok && p.errorMessage.isEmpty()) { + p.errorMessage = "Invalid command"; + } + + return ok; +} + } ControlManager::ControlManager(QObject *parent) : QObject(parent) { } @@ -227,154 +389,6 @@ bool ControlManager::processRequest(Control::Command command, const QVariantList return success; } -bool ControlManager::processCommand(const ProcessCommandArgs &p) -{ - bool ok; - - switch (p.command) { - case Control::CommandHome: { - ok = processCommandHome(p); - } break; - case Control::CommandProg: { - ok = processCommandProg(p); - } break; - case Control::CommandZone: { - ok = processCommandZone(p); - } break; - default: - ok = IoC()->processCommandRpc(p); - } - - if (!ok && p.errorMessage.isEmpty()) { - p.errorMessage = "Invalid command"; - } - - return ok; -} - -bool ControlManager::processCommandHome(const ProcessCommandArgs &p) -{ - const auto commandText = p.args.value(0).toString(); - - if (commandText == "show") { - return IoC()->exposeHomeWindow(); - } - - p.errorMessage = "Usage: home show"; - return false; -} - -bool ControlManager::processCommandProg(const ProcessCommandArgs &p) -{ - const ProgAction progAction = progActionByText(p.args.value(0).toString()); - if (progAction == ProgActionNone) { - p.errorMessage = "Usage: prog add|del|allow|block|kill|show "; - return false; - } - - if (!checkProgActionPassword(progAction)) { - p.errorMessage = "Password required"; - return false; - } - - const QString appPath = p.args.value(1).toString(); - - return processCommandProgAction(progAction, appPath); -} - -bool ControlManager::processCommandProgAction(ProgAction progAction, const QString &appPath) -{ - switch (progAction) { - case ProgActionAdd: { - return IoC()->showProgramEditForm(appPath); - } - case ProgActionDel: { - return IoC()->deleteAppPath(appPath); - } - case ProgActionAllow: - case ProgActionBlock: - case ProgActionKill: { - const bool blocked = (progAction != ProgActionAllow); - const bool killProcess = (progAction == ProgActionKill); - - return IoC()->addOrUpdateAppPath(appPath, blocked, killProcess); - } - default: - return false; - } -} - -bool ControlManager::checkProgActionPassword(ProgAction progAction) -{ - constexpr const quint32 passwordRequiredActions = - ProgActionDel | ProgActionAllow | ProgActionBlock | ProgActionKill; - - return (passwordRequiredActions & progAction) == 0 || checkActionPassword(); -} - -ControlManager::ProgAction ControlManager::progActionByText(const QString &commandText) -{ - if (commandText == "add") - return ProgActionAdd; - - if (commandText == "del") - return ProgActionDel; - - if (commandText == "allow") - return ProgActionAllow; - - if (commandText == "block") - return ProgActionBlock; - - if (commandText == "kill") - return ProgActionKill; - - 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 609df228..af111a78 100644 --- a/src/ui/control/controlmanager.h +++ b/src/ui/control/controlmanager.h @@ -45,34 +45,6 @@ private slots: bool processRequest(Control::Command command, const QVariantList &args); private: - enum ProgAction : quint32 { - ProgActionNone = 0, - ProgActionAdd = (1 << 0), - ProgActionDel = (1 << 1), - ProgActionAllow = (1 << 2), - ProgActionBlock = (1 << 3), - 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); - 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: