From d732bd0f74225828455c5863444b0211faf586d0 Mon Sep 17 00:00:00 2001 From: Louie S Date: Fri, 15 Mar 2024 15:41:31 -0400 Subject: Add backend for rules dialog --- src/rulesDialog.cpp | 28 +++++++++++++++++++++++++++- 1 file changed, 27 insertions(+), 1 deletion(-) (limited to 'src/rulesDialog.cpp') diff --git a/src/rulesDialog.cpp b/src/rulesDialog.cpp index b856ebb..be8b9d3 100644 --- a/src/rulesDialog.cpp +++ b/src/rulesDialog.cpp @@ -59,7 +59,6 @@ void RulesDialog::drawRules() { } void RulesDialog::addRule() { - // TODO handle in db backend to insert instead of updating if id is 0 Rule *new_rule = new Rule(0, this->entry_id, Rule::before); RuleLayout *new_layout = new RuleLayout(*new_rule); @@ -76,6 +75,33 @@ void RulesDialog::deleteRule(int i) { if(i >= this->rules.size()) return; this->updateRulesList(); + // mark as needing to be deleted from database if id > 0 + if(this->rules[i]->id > 0) + this->deleted_rules.append(*this->rules[i]); this->rules.removeAt(i); this->drawRules(); } + +void RulesDialog::accept() { + int i; + BackendDB database; + + this->updateRulesList(); + + // update rules in database + for(i = 0; i < this->rules.size(); ++i) { + // insert - this is a new rule + if(this->rules[i]->id == 0) + database.insertRule(*this->rules[i]); + // update - this is an existing rule + else + database.updateRule(*this->rules[i]); + } + + // delete deleted rules in database + for(i = 0; i < this->deleted_rules.size(); ++i) { + database.removeRule(this->deleted_rules[i]); + } + + QDialog::accept(); +} -- cgit