From b64cd45ebb6ca88f6f57acba22c5705899e0eca1 Mon Sep 17 00:00:00 2001 From: "Danilo M." Date: Mon, 16 Nov 2026 12:24:23 +0100 Subject: feat(rules): Move to in the rules dialog TagRule gains move, read and written by TagRules and chosen from a Move to combo whose item data is the wire-format role. An unknown value from the file is kept raw in unknown and the rule still loads, so a save writes it back instead of deleting the rule; a role picked in the dialog replaces it. A move-only rule is valid. Item 214. Co-Authored-By: Claude Opus 5.5 --- src/tagrules.cpp | 49 +++++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 43 insertions(+), 6 deletions(-) (limited to 'src/tagrules.cpp') diff --git a/src/tagrules.cpp b/src/tagrules.cpp index 0049278..d88a70f 100644 --- a/src/tagrules.cpp +++ b/src/tagrules.cpp @@ -41,7 +41,7 @@ bool isKnownKey(const QString &key) QStringLiteral("id"), QStringLiteral("stage"), QStringLiteral("enabled"), QStringLiteral("add"), QStringLiteral("remove"), QStringLiteral("query"), - QStringLiteral("note"), + QStringLiteral("note"), QStringLiteral("move"), }; return known.contains(key); } @@ -58,6 +58,12 @@ QStringList stringsOf(const QJsonValue &value) return out; } +/// What `move` may say. mailrules.py and qtmaildirconf.py carry the same two. +bool isMoveRole(const QString &role) +{ + return role == QStringLiteral("spam") || role == QStringLiteral("trash"); +} + /// The id pattern, in one place. mailrules.py enforces the same one; see /// "Changing the shared rule format" in CLAUDE.md before touching it. const QRegularExpression &idPattern() @@ -141,8 +147,12 @@ QStringList TagRules::validate(const QList &rules) if (rule.query.trimmed().isEmpty()) problems.append(QObject::tr("'%1': no query").arg(where)); - if (rule.add.isEmpty() && rule.remove.isEmpty()) - problems.append(QObject::tr("'%1': adds and removes nothing") + if (!rule.move.isEmpty() && !isMoveRole(rule.move)) { + problems.append(QObject::tr("'%1': move '%2' is not spam or trash") + .arg(where, rule.move)); + } + if (rule.add.isEmpty() && rule.remove.isEmpty() && rule.move.isEmpty()) + problems.append(QObject::tr("'%1': adds, removes and moves nothing") .arg(where)); } return problems; @@ -247,8 +257,27 @@ void TagRules::load(const QString &path) rule.add = stringsOf(object.value(QStringLiteral("add"))); rule.remove = stringsOf(object.value(QStringLiteral("remove"))); - if (rule.add.isEmpty() && rule.remove.isEmpty()) { - m_warnings.append(QObject::tr("Rule '%1': adds and removes " + // A move this version cannot act on is kept, raw, in `unknown`, so a + // save writes it back rather than deleting it; and a rule whose only + // action it was still loads, so the dialog shows it and validate() + // refuses the save until it is fixed. Same reasoning as the repaired + // id above. A non-string value (`"move": 5`) is a bad move too. + const QJsonValue moveValue = object.value(QStringLiteral("move")); + const bool badMove = !moveValue.isUndefined() + && moveValue != QJsonValue(QString()) + && !isMoveRole(moveValue.toString()); + if (badMove) { + m_warnings.append(QObject::tr("Rule '%1': move '%2' is not spam " + "or trash; ignored") + .arg(rule.id, + moveValue.toVariant().toString())); + rule.unknown.insert(QStringLiteral("move"), moveValue); + } else { + rule.move = moveValue.toString(); + } + if (!badMove && rule.add.isEmpty() && rule.remove.isEmpty() + && rule.move.isEmpty()) { + m_warnings.append(QObject::tr("Rule '%1': adds, removes and moves " "nothing; dropped").arg(rule.id)); continue; } @@ -283,10 +312,18 @@ bool TagRules::save(const QString &path) const QJsonArray::fromStringList(rule.add)); object.insert(QStringLiteral("remove"), QJsonArray::fromStringList(rule.remove)); + // Only when set: a file with no moves stays byte-identical on save. + if (!rule.move.isEmpty()) + object.insert(QStringLiteral("move"), rule.move); object.insert(QStringLiteral("query"), rule.query); object.insert(QStringLiteral("note"), rule.note); - for (auto it = rule.unknown.begin(); it != rule.unknown.end(); ++it) + for (auto it = rule.unknown.begin(); it != rule.unknown.end(); ++it) { + // A bad move kept from load must not overwrite the role the user + // has since chosen in the dialog. + if (it.key() == QStringLiteral("move") && !rule.move.isEmpty()) + continue; object.insert(it.key(), it.value()); + } array.append(object); } -- cgit v1.2.3