From 0f0a3c10e245d4ccb2085e3bafdd5cbbd0b7ca99 Mon Sep 17 00:00:00 2001 From: Nodir Temirkhodjaev Date: Thu, 7 Nov 2024 12:23:33 +0500 Subject: [PATCH] UI: Add "-c block none|all|internet" command-line argument --- src/ui/control/control.cpp | 3 ++ src/ui/control/control_types.h | 1 + src/ui/control/controlmanager.cpp | 52 +++++++++++++++++++++++++++++++ 3 files changed, 56 insertions(+) diff --git a/src/ui/control/control.cpp b/src/ui/control/control.cpp index 690f5728..851fd874 100644 --- a/src/ui/control/control.cpp +++ b/src/ui/control/control.cpp @@ -10,6 +10,7 @@ static const QHash g_commandNames = { CASE_STRING(CommandNone), CASE_STRING(CommandHome), + CASE_STRING(CommandBlock), CASE_STRING(CommandProg), CASE_STRING(CommandBackup), CASE_STRING(CommandZone), @@ -121,6 +122,7 @@ static const RpcManager g_commandManagers[] = { Rpc_NoneManager, // CommandNone = 0, Rpc_NoneManager, // CommandHome, + Rpc_NoneManager, // CommandBlock, Rpc_NoneManager, // CommandProg, Rpc_NoneManager, // CommandBackup, Rpc_NoneManager, // CommandZone, @@ -212,6 +214,7 @@ static const qint8 g_commandValidations[] = { 0, // CommandNone = 0, 0, // CommandHome, + 0, // CommandBlock, 0, // CommandProg, 0, // CommandBackup, 0, // CommandZone, diff --git a/src/ui/control/control_types.h b/src/ui/control/control_types.h index 89f1ae53..4b917af9 100644 --- a/src/ui/control/control_types.h +++ b/src/ui/control/control_types.h @@ -12,6 +12,7 @@ enum Command : qint8 { CommandNone = 0, CommandHome, + CommandBlock, CommandProg, CommandBackup, CommandZone, diff --git a/src/ui/control/controlmanager.cpp b/src/ui/control/controlmanager.cpp index 3e224edb..c1516bd5 100644 --- a/src/ui/control/controlmanager.cpp +++ b/src/ui/control/controlmanager.cpp @@ -47,6 +47,53 @@ bool processCommandHome(const ProcessCommandArgs &p) return false; } +enum BlockAction : qint8 { + BlockActionInvalid = -1, + BlockActionNone = 0, + BlockActionAll, + BlockActionInet, +}; + +bool processCommandProgBlock(BlockAction blockAction) +{ + auto confManager = IoC(); + + auto conf = confManager->conf(); + conf->setBlockTrafficIndex(blockAction); + + return confManager->saveFlags(); +} + +BlockAction blockActionByText(const QString &commandText) +{ + if (commandText == "none") + return BlockActionNone; + + if (commandText == "all") + return BlockActionAll; + + if (commandText == "internet") + return BlockActionInet; + + return BlockActionInvalid; +} + +bool processCommandBlock(const ProcessCommandArgs &p) +{ + const BlockAction blockAction = blockActionByText(p.args.value(0).toString()); + if (blockAction == BlockActionInvalid) { + p.errorMessage = "Usage: block none|all|internet"; + return false; + } + + if (!checkActionPassword(blockAction)) { + p.errorMessage = "Password required"; + return false; + } + + return processCommandProgBlock(blockAction); +} + enum ProgAction : quint32 { ProgActionNone = 0, ProgActionAdd = (1 << 0), @@ -214,6 +261,9 @@ bool processCommand(const ProcessCommandArgs &p) case Control::CommandHome: { ok = processCommandHome(p); } break; + case Control::CommandBlock: { + ok = processCommandBlock(p); + } break; case Control::CommandProg: { ok = processCommandProg(p); } break; @@ -314,6 +364,8 @@ bool ControlManager::processCommandClient() Control::Command command; if (settings->controlCommand() == "home") { command = Control::CommandHome; + } else if (settings->controlCommand() == "block") { + command = Control::CommandBlock; } else if (settings->controlCommand() == "prog") { command = Control::CommandProg; } else if (settings->controlCommand() == "backup") {