UI: Rules: Prepare Rule Set storing

This commit is contained in:
Nodir Temirkhodjaev 2024-03-30 15:06:57 +03:00
parent 8f5c9f448b
commit 0e94b61f45
9 changed files with 122 additions and 58 deletions

View File

@ -47,7 +47,16 @@ const char *const sqlDeleteAppRule = "UPDATE app"
" SET rule_id = NULL"
" WHERE rule_id = ?1;";
const char *const sqlDeleteRuleSet = "DELETE rule_set WHERE rule_id = ?1 OR sub_rule_id = ?1;";
const char *const sqlInsertRuleSet = "INSERT INTO rule_set(rule_id, sub_rule_id, order_index)"
" VALUES(?1, ?2, ?3);";
const char *const sqlDeleteRuleSet = "DELETE rule_set WHERE rule_id = ?1;";
const char *const sqlDeleteRuleSetSub = "DELETE rule_set WHERE sub_rule_id = ?1;";
const char *const sqlSelectRuleSet = "SELECT sub_rule_id FROM rule_set"
" WHERE rule_id = ?1"
" ORDER BY order_index;";
const char *const sqlUpdateRuleName = "UPDATE rule SET name = ?2 WHERE rule_id = ?1;";
@ -90,6 +99,44 @@ void ConfRuleManager::setUp()
m_confManager = IoCPinned()->setUpDependency<ConfManager>();
}
void ConfRuleManager::loadRuleSet(Rule &rule)
{
rule.ruleSetEdited = false;
rule.ruleSet.clear();
SqliteStmt stmt;
if (!DbQuery(sqliteDb()).sql(sqlSelectRuleSet).vars({ rule.ruleId }).prepare(stmt))
return;
while (stmt.step() == SqliteStmt::StepRow) {
const int subRuleId = stmt.columnInt();
rule.ruleSet.append(subRuleId);
}
}
void ConfRuleManager::saveRuleSet(Rule &rule)
{
if (!rule.ruleSetEdited)
return;
DbQuery(sqliteDb()).sql(sqlDeleteRuleSet).vars({ rule.ruleId }).executeOk();
SqliteStmt stmt;
if (!DbQuery(sqliteDb()).sql(sqlInsertRuleSet).prepare(stmt))
return;
stmt.bindInt(1, rule.ruleId);
int orderIndex = 0;
for (const auto subRuleId : rule.ruleSet) {
stmt.bindInt(2, subRuleId);
stmt.bindInt(3, ++orderIndex);
stmt.step();
}
}
bool ConfRuleManager::addOrUpdateRule(Rule &rule)
{
bool ok = true;
@ -120,6 +167,8 @@ bool ConfRuleManager::addOrUpdateRule(Rule &rule)
if (ok) {
DbQuery(sqliteDb(), &ok).sql(isNew ? sqlInsertRule : sqlUpdateRule).vars(vars).executeOk();
saveRuleSet(rule);
}
commitTransaction(ok);
@ -151,6 +200,7 @@ bool ConfRuleManager::deleteRule(int ruleId)
// Delete the Preset Rule from Rules
DbQuery(sqliteDb()).sql(sqlDeleteRuleSet).vars(vars).executeOk();
DbQuery(sqliteDb()).sql(sqlDeleteRuleSetSub).vars(vars).executeOk();
// Put the Rule Id back to the free list
putFreeRuleId(ruleId);

View File

@ -24,6 +24,9 @@ public:
void setUp() override;
void loadRuleSet(Rule &rule);
void saveRuleSet(Rule &rule);
virtual bool addOrUpdateRule(Rule &rule);
virtual bool deleteRule(int ruleId);
virtual bool updateRuleName(int ruleId, const QString &ruleName);

View File

@ -3,12 +3,7 @@
#include <QDateTime>
#include <QObject>
struct RuleIdRange
{
int minId;
int maxId;
};
#include <QVector>
class Rule
{
@ -28,6 +23,7 @@ public:
bool enabled : 1 = true;
bool blocked : 1 = false;
bool exclusive : 1 = false;
bool ruleSetEdited : 1 = false;
RuleType ruleType = AppRule;
@ -41,6 +37,8 @@ public:
QString ruleText;
QDateTime modTime;
QVector<quint16> ruleSet;
};
#endif // RULE_H

View File

@ -10,11 +10,11 @@
#include <QRadioButton>
#include <QVBoxLayout>
#include <conf/confrulemanager.h>
#include <form/controls/controlutil.h>
#include <form/controls/plaintextedit.h>
#include <form/controls/zonesselector.h>
#include <manager/windowmanager.h>
#include <model/rulelistmodel.h>
#include <util/iconcache.h>
#include <util/net/netutil.h>
@ -27,15 +27,17 @@ RuleEditDialog::RuleEditDialog(RulesController *ctrl, QWidget *parent) :
setupController();
}
RuleListModel *RuleEditDialog::ruleListModel() const
ConfRuleManager *RuleEditDialog::confRuleManager() const
{
return ctrl()->ruleListModel();
return ctrl()->confRuleManager();
}
void RuleEditDialog::initialize(const RuleRow &ruleRow)
{
m_ruleRow = ruleRow;
confRuleManager()->loadRuleSet(m_ruleRow);
retranslateUi();
m_editName->setText(ruleRow.ruleName);

View File

@ -12,9 +12,9 @@ QT_FORWARD_DECLARE_CLASS(QLineEdit)
QT_FORWARD_DECLARE_CLASS(QPushButton)
QT_FORWARD_DECLARE_CLASS(QRadioButton)
class ConfRuleManager;
class PlainTextEdit;
class Rule;
class RuleListModel;
class RulesController;
class ZonesSelector;
@ -26,7 +26,7 @@ public:
explicit RuleEditDialog(RulesController *ctrl, QWidget *parent = nullptr);
RulesController *ctrl() const { return m_ctrl; }
RuleListModel *ruleListModel() const;
ConfRuleManager *confRuleManager() const;
bool isEmpty() const { return m_ruleRow.ruleId == 0; }

View File

@ -37,7 +37,10 @@ bool processConfAppManager_updateAppName(
bool processConfAppManager_deleteApps(
ConfAppManager *confAppManager, const ProcessCommandArgs &p, QVariantList & /*resArgs*/)
{
return confAppManager->deleteApps(VariantUtil::listToVector(p.args.value(0).toList()));
QVector<qint64> appIdList;
VariantUtil::listToVector(p.args.value(0).toList(), appIdList);
return confAppManager->deleteApps(appIdList);
}
bool processConfAppManager_purgeApps(ConfAppManager *confAppManager,
@ -49,8 +52,11 @@ bool processConfAppManager_purgeApps(ConfAppManager *confAppManager,
bool processConfAppManager_updateAppsBlocked(
ConfAppManager *confAppManager, const ProcessCommandArgs &p, QVariantList & /*resArgs*/)
{
return confAppManager->updateAppsBlocked(VariantUtil::listToVector(p.args.value(0).toList()),
p.args.value(1).toBool(), p.args.value(2).toBool());
QVector<qint64> appIdList;
VariantUtil::listToVector(p.args.value(0).toList(), appIdList);
return confAppManager->updateAppsBlocked(
appIdList, p.args.value(1).toBool(), p.args.value(2).toBool());
}
using processConfAppManager_func = bool (*)(
@ -98,7 +104,8 @@ bool ConfAppManagerRpc::updateAppName(qint64 appId, const QString &appName)
bool ConfAppManagerRpc::deleteApps(const QVector<qint64> &appIdList)
{
const QVariantList appIdVarList = VariantUtil::vectorToList(appIdList);
QVariantList appIdVarList;
VariantUtil::vectorToList(appIdList, appIdVarList);
QVariantList args;
VariantUtil::addToList(args, appIdVarList);
@ -114,7 +121,8 @@ bool ConfAppManagerRpc::purgeApps()
bool ConfAppManagerRpc::updateAppsBlocked(
const QVector<qint64> &appIdList, bool blocked, bool killProcess)
{
const QVariantList appIdVarList = VariantUtil::vectorToList(appIdList);
QVariantList appIdVarList;
VariantUtil::vectorToList(appIdList, appIdVarList);
QVariantList args;
VariantUtil::addToList(args, appIdVarList);

View File

@ -5,6 +5,7 @@
#include <control/controlworker.h>
#include <rpc/rpcmanager.h>
#include <util/ioc/ioccontainer.h>
#include <util/variantutil.h>
namespace {
@ -94,8 +95,12 @@ bool ConfRuleManagerRpc::updateRuleEnabled(int ruleId, bool enabled)
QVariantList ConfRuleManagerRpc::ruleToVarList(const Rule &rule)
{
return { rule.enabled, rule.blocked, rule.exclusive, rule.ruleType, rule.ruleId,
rule.acceptZones, rule.rejectZones, rule.ruleName, rule.notes, rule.ruleText };
QVariantList ruleSetList;
VariantUtil::vectorToList(rule.ruleSet, ruleSetList);
return { rule.enabled, rule.blocked, rule.exclusive, rule.ruleSetEdited, rule.ruleType,
rule.ruleId, rule.acceptZones, rule.rejectZones, rule.ruleName, rule.notes, rule.ruleText,
ruleSetList };
}
Rule ConfRuleManagerRpc::varListToRule(const QVariantList &v)
@ -104,13 +109,15 @@ Rule ConfRuleManagerRpc::varListToRule(const QVariantList &v)
rule.enabled = v.value(0).toBool();
rule.blocked = v.value(1).toBool();
rule.exclusive = v.value(2).toBool();
rule.ruleType = Rule::RuleType(v.value(3).toInt());
rule.ruleId = v.value(4).toInt();
rule.acceptZones = v.value(5).toUInt();
rule.rejectZones = v.value(6).toUInt();
rule.ruleName = v.value(7).toString();
rule.notes = v.value(8).toString();
rule.ruleText = v.value(9).toString();
rule.ruleSetEdited = v.value(3).toBool();
rule.ruleType = Rule::RuleType(v.value(4).toInt());
rule.ruleId = v.value(5).toInt();
rule.acceptZones = v.value(6).toUInt();
rule.rejectZones = v.value(7).toUInt();
rule.ruleName = v.value(8).toString();
rule.notes = v.value(9).toString();
rule.ruleText = v.value(10).toString();
VariantUtil::listToVector(v.value(11).toList(), rule.ruleSet);
return rule;
}

View File

@ -1,30 +1 @@
#include "variantutil.h"
QVariantList VariantUtil::vectorToList(const QVector<qint64> &array)
{
QVariantList list;
list.reserve(array.size());
for (const qint64 v : array) {
list.append(v);
}
return list;
}
QVector<qint64> VariantUtil::listToVector(const QVariantList &list)
{
QVector<qint64> array;
array.reserve(list.size());
for (const QVariant &v : list) {
array.append(v.toLongLong());
}
return array;
}
void VariantUtil::addToList(QVariantList &list, const QVariant &v)
{
list.push_back(v); // append() merges the list, does not insert
}

View File

@ -8,11 +8,36 @@
class VariantUtil
{
public:
static QVariantList vectorToList(const QVector<qint64> &array);
template<typename T>
static void vectorToList(const QVector<T> &array, QVariantList &list);
static QVector<qint64> listToVector(const QVariantList &list);
template<typename T>
static void listToVector(const QVariantList &list, QVector<T> &array);
static void addToList(QVariantList &list, const QVariant &v);
inline static void addToList(QVariantList &list, const QVariant &v)
{
list.push_back(v); // append() merges the list, does not insert
}
};
template<typename T>
void VariantUtil::vectorToList(const QVector<T> &array, QVariantList &list)
{
list.reserve(array.size());
for (const T v : array) {
list.append(v);
}
}
template<typename T>
void VariantUtil::listToVector(const QVariantList &list, QVector<T> &array)
{
array.reserve(list.size());
for (const QVariant &v : list) {
array.append(v.value<T>());
}
}
#endif // VARIANTUTIL_H