aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorDanilo M. <danix@danix.xyz>2026-11-16 12:24:23 +0100
committerDanilo M. <danix@danix.xyz>2026-11-16 12:24:23 +0100
commitb64cd45ebb6ca88f6f57acba22c5705899e0eca1 (patch)
tree8f4d9addf8920142e29727c0f758f1ddca2301bb /src
parent227258a199283089ec16e4798988277cf5062e67 (diff)
downloadqtmaildir-b64cd45ebb6ca88f6f57acba22c5705899e0eca1.tar.gz
qtmaildir-b64cd45ebb6ca88f6f57acba22c5705899e0eca1.zip
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 <noreply@anthropic.com>
Diffstat (limited to 'src')
-rw-r--r--src/tagrules.cpp49
-rw-r--r--src/tagrules.h4
-rw-r--r--src/tagrulesdialog.cpp25
-rw-r--r--src/tagrulesdialog.h3
4 files changed, 75 insertions, 6 deletions
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<TagRule> &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);
}
diff --git a/src/tagrules.h b/src/tagrules.h
index 4d3f0ab..946329a 100644
--- a/src/tagrules.h
+++ b/src/tagrules.h
@@ -37,6 +37,10 @@ struct TagRule
QString note; ///< Why the rule is shaped this way. Shown in the dialog.
QStringList add;
QStringList remove;
+ /// "spam" or "trash", or empty. An account ROLE, never a folder name: the
+ /// hook resolves it per message to that account's configured folder, so
+ /// a typo cannot create a folder on the server (item 103, item 214).
+ QString move;
int stage = 50; ///< Ascending. Account tags 10, topic rules 50.
bool enabled = true;
diff --git a/src/tagrulesdialog.cpp b/src/tagrulesdialog.cpp
index e037e37..2e00841 100644
--- a/src/tagrulesdialog.cpp
+++ b/src/tagrulesdialog.cpp
@@ -162,6 +162,16 @@ TagRulesDialog::TagRulesDialog(const TagRule &seed, QWidget *parent)
m_add->setPlaceholderText(tr("comma separated"));
m_remove = new QLineEdit(this);
m_remove->setPlaceholderText(tr("comma separated"));
+ // The stored value is the item DATA, the wire-format role. The label is
+ // translated, and nothing may match on it ("Translating a string that
+ // something MATCHES on" in AGENTS.md).
+ m_move = new QComboBox(this);
+ m_move->addItem(tr("Don't move"), QString());
+ m_move->addItem(tr("Spam folder"), QStringLiteral("spam"));
+ m_move->addItem(tr("Trash folder"), QStringLiteral("trash"));
+ m_move->setToolTip(tr("Moves matching new mail into the spam or trash "
+ "folder of the account it arrived in, and takes it "
+ "out of the inbox. It stays unread."));
m_query = new QLineEdit(this);
// Query syntax is wire format, never translated. Only the prose is.
m_query->setPlaceholderText(QStringLiteral("from:sender@example.com"));
@@ -173,6 +183,7 @@ TagRulesDialog::TagRulesDialog(const TagRule &seed, QWidget *parent)
form->addRow(QString(), m_enabled);
form->addRow(tr("Add tags"), m_add);
form->addRow(tr("Remove tags"), m_remove);
+ form->addRow(tr("Move to"), m_move);
m_builder = new QWidget(this);
auto *builderLayout = new QVBoxLayout(m_builder);
builderLayout->setContentsMargins(0, 0, 0, 0);
@@ -352,6 +363,8 @@ TagRulesDialog::TagRulesDialog(const TagRule &seed, QWidget *parent)
this, &TagRulesDialog::applyEditsToCurrentRule);
connect(m_remove, &QLineEdit::editingFinished,
this, &TagRulesDialog::applyEditsToCurrentRule);
+ connect(m_move, &QComboBox::currentIndexChanged,
+ this, &TagRulesDialog::applyEditsToCurrentRule);
connect(m_query, &QLineEdit::editingFinished,
this, &TagRulesDialog::applyEditsToCurrentRule);
connect(m_stage, &QSpinBox::editingFinished,
@@ -644,6 +657,7 @@ void TagRulesDialog::onSelectionChanged()
m_enabled->setChecked(rule.enabled);
m_add->setText(rule.add.join(QStringLiteral(", ")));
m_remove->setText(rule.remove.join(QStringLiteral(", ")));
+ m_move->setCurrentIndex(qMax(0, m_move->findData(rule.move)));
m_query->setText(rule.query);
m_note->setPlainText(rule.note);
@@ -700,6 +714,7 @@ void TagRulesDialog::applyEditsToCurrentRule()
rule.enabled = m_enabled->isChecked();
rule.add = splitTags(m_add->text());
rule.remove = splitTags(m_remove->text());
+ rule.move = m_move->currentData().toString();
if (m_textMode->isChecked()) {
rule.query = m_query->text().trimmed();
} else {
@@ -922,6 +937,16 @@ void TagRulesDialog::setTagsForTest(const QString &tags)
emit m_add->editingFinished();
}
+void TagRulesDialog::setMoveForTest(const QString &role)
+{
+ m_move->setCurrentIndex(m_move->findData(role));
+}
+
+QString TagRulesDialog::moveForTest() const
+{
+ return m_move->currentData().toString();
+}
+
bool TagRulesDialog::textModeToggleIsReachableForTest() const
{
// isVisibleTo rather than isVisible: nothing is isVisible() on a dialog
diff --git a/src/tagrulesdialog.h b/src/tagrulesdialog.h
index 3bfb3d5..0148f38 100644
--- a/src/tagrulesdialog.h
+++ b/src/tagrulesdialog.h
@@ -128,6 +128,8 @@ public:
bool currentRuleEnabledForTest() const;
void addRuleForTest() { onAddRule(); }
void setTagsForTest(const QString &tags);
+ void setMoveForTest(const QString &role);
+ QString moveForTest() const;
/// Whether the text-mode toggle would be on screen. A toggle that hides
/// itself when switched on is a one-way trip, and asserting only on the
@@ -294,6 +296,7 @@ private:
QLineEdit *m_id = nullptr;
QLineEdit *m_add = nullptr;
QLineEdit *m_remove = nullptr;
+ QComboBox *m_move = nullptr;
QLineEdit *m_query = nullptr;
QPlainTextEdit *m_note = nullptr;
QSpinBox *m_stage = nullptr;