UI: Control: Simplify long methods

This commit is contained in:
Nodir Temirkhodjaev 2024-06-13 12:49:40 +03:00
parent 71dc4e8fe7
commit 8a060e935c

View File

@ -1,121 +1,118 @@
#include "control.h" #include "control.h"
#include <QHash>
namespace Control { namespace Control {
#define CASE_STRING(id) \ #define CASE_STRING(id) { id, #id }
case (id): \
return #id; static const QHash<Command, const char *> g_commandNames = {
CASE_STRING(CommandNone),
CASE_STRING(CommandHome),
CASE_STRING(CommandProg),
CASE_STRING(CommandZone),
CASE_STRING(Rpc_Result_Ok),
CASE_STRING(Rpc_Result_Error),
CASE_STRING(Rpc_RpcManager_initClient),
CASE_STRING(Rpc_AppInfoManager_lookupAppInfo),
CASE_STRING(Rpc_AppInfoManager_checkLookupInfoFinished),
CASE_STRING(Rpc_AutoUpdateManager_startDownload),
CASE_STRING(Rpc_AutoUpdateManager_runInstaller),
CASE_STRING(Rpc_AutoUpdateManager_updateState),
CASE_STRING(Rpc_AutoUpdateManager_restartClients),
CASE_STRING(Rpc_ConfManager_saveVariant),
CASE_STRING(Rpc_ConfManager_exportMasterBackup),
CASE_STRING(Rpc_ConfManager_importMasterBackup),
CASE_STRING(Rpc_ConfManager_checkPassword),
CASE_STRING(Rpc_ConfManager_confChanged),
CASE_STRING(Rpc_ConfAppManager_addOrUpdateAppPath),
CASE_STRING(Rpc_ConfAppManager_deleteAppPath),
CASE_STRING(Rpc_ConfAppManager_addOrUpdateApp),
CASE_STRING(Rpc_ConfAppManager_updateApp),
CASE_STRING(Rpc_ConfAppManager_updateAppName),
CASE_STRING(Rpc_ConfAppManager_deleteApps),
CASE_STRING(Rpc_ConfAppManager_purgeApps),
CASE_STRING(Rpc_ConfAppManager_updateAppsBlocked),
CASE_STRING(Rpc_ConfAppManager_appAlerted),
CASE_STRING(Rpc_ConfAppManager_appsChanged),
CASE_STRING(Rpc_ConfAppManager_appUpdated),
CASE_STRING(Rpc_ConfRuleManager_addOrUpdateRule),
CASE_STRING(Rpc_ConfRuleManager_deleteRule),
CASE_STRING(Rpc_ConfRuleManager_updateRuleName),
CASE_STRING(Rpc_ConfRuleManager_updateRuleEnabled),
CASE_STRING(Rpc_ConfRuleManager_ruleAdded),
CASE_STRING(Rpc_ConfRuleManager_ruleRemoved),
CASE_STRING(Rpc_ConfRuleManager_ruleUpdated),
CASE_STRING(Rpc_ConfZoneManager_addOrUpdateZone),
CASE_STRING(Rpc_ConfZoneManager_deleteZone),
CASE_STRING(Rpc_ConfZoneManager_updateZoneName),
CASE_STRING(Rpc_ConfZoneManager_updateZoneEnabled),
CASE_STRING(Rpc_ConfZoneManager_zoneAdded),
CASE_STRING(Rpc_ConfZoneManager_zoneRemoved),
CASE_STRING(Rpc_ConfZoneManager_zoneUpdated),
CASE_STRING(Rpc_DriverManager_updateState),
CASE_STRING(Rpc_QuotaManager_alert),
CASE_STRING(Rpc_StatManager_deleteStatApp),
CASE_STRING(Rpc_StatManager_resetAppTrafTotals),
CASE_STRING(Rpc_StatManager_clearTraffic),
CASE_STRING(Rpc_StatManager_trafficCleared),
CASE_STRING(Rpc_StatManager_appStatRemoved),
CASE_STRING(Rpc_StatManager_appCreated),
CASE_STRING(Rpc_StatManager_trafficAdded),
CASE_STRING(Rpc_StatManager_appTrafTotalsResetted),
CASE_STRING(Rpc_StatBlockManager_deleteConn),
CASE_STRING(Rpc_StatBlockManager_connChanged),
CASE_STRING(Rpc_ServiceInfoManager_trackService),
CASE_STRING(Rpc_ServiceInfoManager_revertService),
CASE_STRING(Rpc_TaskManager_runTask),
CASE_STRING(Rpc_TaskManager_abortTask),
CASE_STRING(Rpc_TaskManager_taskStarted),
CASE_STRING(Rpc_TaskManager_taskFinished),
CASE_STRING(Rpc_TaskManager_appVersionDownloaded),
CASE_STRING(Rpc_TaskManager_zonesDownloaded),
};
const char *const commandString(Command cmd) const char *const commandString(Command cmd)
{ {
switch (cmd) { return g_commandNames.value(cmd);
CASE_STRING(CommandNone)
CASE_STRING(CommandHome)
CASE_STRING(CommandProg)
CASE_STRING(CommandZone)
CASE_STRING(Rpc_Result_Ok)
CASE_STRING(Rpc_Result_Error)
CASE_STRING(Rpc_RpcManager_initClient)
CASE_STRING(Rpc_AppInfoManager_lookupAppInfo)
CASE_STRING(Rpc_AppInfoManager_checkLookupInfoFinished)
CASE_STRING(Rpc_AutoUpdateManager_startDownload)
CASE_STRING(Rpc_AutoUpdateManager_runInstaller)
CASE_STRING(Rpc_AutoUpdateManager_updateState)
CASE_STRING(Rpc_AutoUpdateManager_restartClients)
CASE_STRING(Rpc_ConfManager_saveVariant)
CASE_STRING(Rpc_ConfManager_exportMasterBackup)
CASE_STRING(Rpc_ConfManager_importMasterBackup)
CASE_STRING(Rpc_ConfManager_checkPassword)
CASE_STRING(Rpc_ConfManager_confChanged)
CASE_STRING(Rpc_ConfAppManager_addOrUpdateAppPath)
CASE_STRING(Rpc_ConfAppManager_deleteAppPath)
CASE_STRING(Rpc_ConfAppManager_addOrUpdateApp)
CASE_STRING(Rpc_ConfAppManager_updateApp)
CASE_STRING(Rpc_ConfAppManager_updateAppName)
CASE_STRING(Rpc_ConfAppManager_deleteApps)
CASE_STRING(Rpc_ConfAppManager_purgeApps)
CASE_STRING(Rpc_ConfAppManager_updateAppsBlocked)
CASE_STRING(Rpc_ConfAppManager_appAlerted)
CASE_STRING(Rpc_ConfAppManager_appsChanged)
CASE_STRING(Rpc_ConfAppManager_appUpdated)
CASE_STRING(Rpc_ConfRuleManager_addOrUpdateRule)
CASE_STRING(Rpc_ConfRuleManager_deleteRule)
CASE_STRING(Rpc_ConfRuleManager_updateRuleName)
CASE_STRING(Rpc_ConfRuleManager_updateRuleEnabled)
CASE_STRING(Rpc_ConfRuleManager_ruleAdded)
CASE_STRING(Rpc_ConfRuleManager_ruleRemoved)
CASE_STRING(Rpc_ConfRuleManager_ruleUpdated)
CASE_STRING(Rpc_ConfZoneManager_addOrUpdateZone)
CASE_STRING(Rpc_ConfZoneManager_deleteZone)
CASE_STRING(Rpc_ConfZoneManager_updateZoneName)
CASE_STRING(Rpc_ConfZoneManager_updateZoneEnabled)
CASE_STRING(Rpc_ConfZoneManager_zoneAdded)
CASE_STRING(Rpc_ConfZoneManager_zoneRemoved)
CASE_STRING(Rpc_ConfZoneManager_zoneUpdated)
CASE_STRING(Rpc_DriverManager_updateState)
CASE_STRING(Rpc_QuotaManager_alert)
CASE_STRING(Rpc_StatManager_deleteStatApp)
CASE_STRING(Rpc_StatManager_resetAppTrafTotals)
CASE_STRING(Rpc_StatManager_clearTraffic)
CASE_STRING(Rpc_StatManager_trafficCleared)
CASE_STRING(Rpc_StatManager_appStatRemoved)
CASE_STRING(Rpc_StatManager_appCreated)
CASE_STRING(Rpc_StatManager_trafficAdded)
CASE_STRING(Rpc_StatManager_appTrafTotalsResetted)
CASE_STRING(Rpc_StatBlockManager_deleteConn)
CASE_STRING(Rpc_StatBlockManager_connChanged)
CASE_STRING(Rpc_ServiceInfoManager_trackService)
CASE_STRING(Rpc_ServiceInfoManager_revertService)
CASE_STRING(Rpc_TaskManager_runTask)
CASE_STRING(Rpc_TaskManager_abortTask)
CASE_STRING(Rpc_TaskManager_taskStarted)
CASE_STRING(Rpc_TaskManager_taskFinished)
CASE_STRING(Rpc_TaskManager_appVersionDownloaded)
CASE_STRING(Rpc_TaskManager_zonesDownloaded)
default:
return nullptr;
}
} }
static const QHash<RpcManager, const char *> g_managerNames = {
CASE_STRING(Rpc_NoneManager),
CASE_STRING(Rpc_AppInfoManager),
CASE_STRING(Rpc_AutoUpdateManager),
CASE_STRING(Rpc_ConfManager),
CASE_STRING(Rpc_ConfAppManager),
CASE_STRING(Rpc_ConfRuleManager),
CASE_STRING(Rpc_ConfZoneManager),
CASE_STRING(Rpc_DriverManager),
CASE_STRING(Rpc_QuotaManager),
CASE_STRING(Rpc_StatManager),
CASE_STRING(Rpc_StatBlockManager),
CASE_STRING(Rpc_ServiceInfoManager),
CASE_STRING(Rpc_TaskManager),
};
const char *const rpcManagerString(RpcManager rpcManager) const char *const rpcManagerString(RpcManager rpcManager)
{ {
switch (rpcManager) { return g_managerNames.value(rpcManager);
CASE_STRING(Rpc_NoneManager)
CASE_STRING(Rpc_AppInfoManager)
CASE_STRING(Rpc_AutoUpdateManager)
CASE_STRING(Rpc_ConfManager)
CASE_STRING(Rpc_ConfAppManager)
CASE_STRING(Rpc_ConfRuleManager)
CASE_STRING(Rpc_ConfZoneManager)
CASE_STRING(Rpc_DriverManager)
CASE_STRING(Rpc_QuotaManager)
CASE_STRING(Rpc_StatManager)
CASE_STRING(Rpc_StatBlockManager)
CASE_STRING(Rpc_ServiceInfoManager)
CASE_STRING(Rpc_TaskManager)
default:
return nullptr;
}
} }
RpcManager managerByCommand(Command cmd)
{
static const RpcManager g_commandManagers[] = { static const RpcManager g_commandManagers[] = {
Rpc_NoneManager, // CommandNone = 0, Rpc_NoneManager, // CommandNone = 0,
@ -198,11 +195,11 @@ RpcManager managerByCommand(Command cmd)
Rpc_TaskManager, // Rpc_TaskManager_zonesDownloaded, Rpc_TaskManager, // Rpc_TaskManager_zonesDownloaded,
}; };
RpcManager managerByCommand(Command cmd)
{
return g_commandManagers[cmd]; return g_commandManagers[cmd];
} }
bool commandRequiresValidation(Command cmd)
{
static const qint8 g_commandValidations[] = { static const qint8 g_commandValidations[] = {
0, // CommandNone = 0, 0, // CommandNone = 0,
@ -282,6 +279,8 @@ bool commandRequiresValidation(Command cmd)
0, // Rpc_TaskManager_zonesDownloaded, 0, // Rpc_TaskManager_zonesDownloaded,
}; };
bool commandRequiresValidation(Command cmd)
{
return g_commandValidations[cmd]; return g_commandValidations[cmd];
} }