UI: Add "-c zone update" command line argument

This commit is contained in:
Nodir Temirkhodjaev 2024-06-13 12:12:14 +03:00
parent f9ca5c7dee
commit 3bc831ce51
4 changed files with 79 additions and 11 deletions

View File

@ -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,

View File

@ -13,6 +13,7 @@ enum Command : qint8 {
CommandHome,
CommandProg,
CommandZone,
Rpc_Result_Ok,
Rpc_Result_Error,

View File

@ -13,6 +13,8 @@
#include <fortsettings.h>
#include <manager/windowmanager.h>
#include <rpc/rpcmanager.h>
#include <task/taskinfozonedownloader.h>
#include <task/taskmanager.h>
#include <util/fileutil.h>
#include <util/ioc/ioccontainer.h>
#include <util/osutil.h>
@ -25,6 +27,11 @@ constexpr int maxClientsCount = 9;
const QLoggingCategory LC("control");
bool checkActionPassword()
{
return IoC<WindowManager>()->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<RpcManager>()->processCommandRpc(p))
return true;
ok = IoC<RpcManager>()->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<WindowManager>()->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<TaskManager>()->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";

View File

@ -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: