From 0e94b61f45572385326248fffed73b8e0b434dd1 Mon Sep 17 00:00:00 2001 From: Nodir Temirkhodjaev Date: Sat, 30 Mar 2024 15:06:57 +0300 Subject: [PATCH] UI: Rules: Prepare Rule Set storing --- src/ui/conf/confrulemanager.cpp | 52 ++++++++++++++++++++++++++++- src/ui/conf/confrulemanager.h | 3 ++ src/ui/conf/rule.h | 10 +++--- src/ui/form/rule/ruleeditdialog.cpp | 8 +++-- src/ui/form/rule/ruleeditdialog.h | 4 +-- src/ui/rpc/confappmanagerrpc.cpp | 18 +++++++--- src/ui/rpc/confrulemanagerrpc.cpp | 25 +++++++++----- src/ui/util/variantutil.cpp | 29 ---------------- src/ui/util/variantutil.h | 31 +++++++++++++++-- 9 files changed, 122 insertions(+), 58 deletions(-) diff --git a/src/ui/conf/confrulemanager.cpp b/src/ui/conf/confrulemanager.cpp index 6e50a778..1b3c49d8 100644 --- a/src/ui/conf/confrulemanager.cpp +++ b/src/ui/conf/confrulemanager.cpp @@ -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(); } +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); diff --git a/src/ui/conf/confrulemanager.h b/src/ui/conf/confrulemanager.h index 6dcdadd2..e89585f6 100644 --- a/src/ui/conf/confrulemanager.h +++ b/src/ui/conf/confrulemanager.h @@ -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); diff --git a/src/ui/conf/rule.h b/src/ui/conf/rule.h index a611e382..5957f743 100644 --- a/src/ui/conf/rule.h +++ b/src/ui/conf/rule.h @@ -3,12 +3,7 @@ #include #include - -struct RuleIdRange -{ - int minId; - int maxId; -}; +#include 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 ruleSet; }; #endif // RULE_H diff --git a/src/ui/form/rule/ruleeditdialog.cpp b/src/ui/form/rule/ruleeditdialog.cpp index 6fd9ccfe..1b085296 100644 --- a/src/ui/form/rule/ruleeditdialog.cpp +++ b/src/ui/form/rule/ruleeditdialog.cpp @@ -10,11 +10,11 @@ #include #include +#include #include
#include #include #include -#include #include #include @@ -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); diff --git a/src/ui/form/rule/ruleeditdialog.h b/src/ui/form/rule/ruleeditdialog.h index 7c48b05d..29058987 100644 --- a/src/ui/form/rule/ruleeditdialog.h +++ b/src/ui/form/rule/ruleeditdialog.h @@ -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; } diff --git a/src/ui/rpc/confappmanagerrpc.cpp b/src/ui/rpc/confappmanagerrpc.cpp index e06cb091..3d125356 100644 --- a/src/ui/rpc/confappmanagerrpc.cpp +++ b/src/ui/rpc/confappmanagerrpc.cpp @@ -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 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 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 &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 &appIdList, bool blocked, bool killProcess) { - const QVariantList appIdVarList = VariantUtil::vectorToList(appIdList); + QVariantList appIdVarList; + VariantUtil::vectorToList(appIdList, appIdVarList); QVariantList args; VariantUtil::addToList(args, appIdVarList); diff --git a/src/ui/rpc/confrulemanagerrpc.cpp b/src/ui/rpc/confrulemanagerrpc.cpp index 15b93d0b..5c591912 100644 --- a/src/ui/rpc/confrulemanagerrpc.cpp +++ b/src/ui/rpc/confrulemanagerrpc.cpp @@ -5,6 +5,7 @@ #include #include #include +#include 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; } diff --git a/src/ui/util/variantutil.cpp b/src/ui/util/variantutil.cpp index 70d3142b..02ffa1f7 100644 --- a/src/ui/util/variantutil.cpp +++ b/src/ui/util/variantutil.cpp @@ -1,30 +1 @@ #include "variantutil.h" - -QVariantList VariantUtil::vectorToList(const QVector &array) -{ - QVariantList list; - list.reserve(array.size()); - - for (const qint64 v : array) { - list.append(v); - } - - return list; -} - -QVector VariantUtil::listToVector(const QVariantList &list) -{ - QVector 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 -} diff --git a/src/ui/util/variantutil.h b/src/ui/util/variantutil.h index 6f85c311..80f6839b 100644 --- a/src/ui/util/variantutil.h +++ b/src/ui/util/variantutil.h @@ -8,11 +8,36 @@ class VariantUtil { public: - static QVariantList vectorToList(const QVector &array); + template + static void vectorToList(const QVector &array, QVariantList &list); - static QVector listToVector(const QVariantList &list); + template + static void listToVector(const QVariantList &list, QVector &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 +void VariantUtil::vectorToList(const QVector &array, QVariantList &list) +{ + list.reserve(array.size()); + + for (const T v : array) { + list.append(v); + } +} + +template +void VariantUtil::listToVector(const QVariantList &list, QVector &array) +{ + array.reserve(list.size()); + + for (const QVariant &v : list) { + array.append(v.value()); + } +} + #endif // VARIANTUTIL_H