From 46bdfeb27a072a44ce05affb0ccc3ca9c330197d Mon Sep 17 00:00:00 2001 From: "Danilo M." Date: Fri, 14 Aug 2026 11:27:56 +0200 Subject: feat(rules): seed the rules dialog even when it is open The dialog is non-modal and single-instance, so a second Create tagging rule reaches one that is already up. Seeding it beats dropping the request, which would read as a broken menu item. Co-Authored-By: Claude Opus 5 --- src/mainwindow.cpp | 9 +++++++-- src/mainwindow.h | 7 ++++++- tests/test_tagrules.cpp | 44 ++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 57 insertions(+), 3 deletions(-) diff --git a/src/mainwindow.cpp b/src/mainwindow.cpp index 14e4202..f9c9c65 100644 --- a/src/mainwindow.cpp +++ b/src/mainwindow.cpp @@ -1322,18 +1322,23 @@ void MainWindow::onDatabaseStatsReady(const DatabaseStats &stats, number(stats.tags))); } -void MainWindow::showTagRulesDialog() +void MainWindow::showTagRulesDialog(const TagRule &seed) { // One dialog. A second would edit a stale copy and the last Save would // silently win, which is the lost-edit case the atomic write cannot help // with because both writers are this process. if (m_tagRulesDialog) { + // Seeded into the dialog already up rather than dropped: the menu item + // must do something visible, and a second dialog would edit a stale + // copy whose Save would silently win. + if (!seed.query.isEmpty()) + m_tagRulesDialog->seedRule(seed); m_tagRulesDialog->raise(); m_tagRulesDialog->activateWindow(); return; } - auto *dialog = new TagRulesDialog(this); + auto *dialog = new TagRulesDialog(seed, this); dialog->setAttribute(Qt::WA_DeleteOnClose); m_tagRulesDialog = dialog; diff --git a/src/mainwindow.h b/src/mainwindow.h index 6e8501a..a68fcd9 100644 --- a/src/mainwindow.h +++ b/src/mainwindow.h @@ -34,6 +34,9 @@ // Included rather than forward-declared: SyncPhaseTracker is held by value, so // its size must be known here. MailSync itself stays a forward declaration. #include "mailsync.h" +// Complete type, not a forward declaration: showTagRulesDialog() defaults its +// seed to TagRule(). +#include "tagrules.h" #include "syncmonitor.h" #include "tagcolors.h" #include "types.h" @@ -376,7 +379,9 @@ private slots: void onRulePreviewRequested(const QString &query); /// Opens the auto-tagging rules editor, or raises the one already open. - void showTagRulesDialog(); + /// `seed` is an optional rule to open on, used by Create tagging rule on a + /// saved query. A default-constructed TagRule (empty query) means none. + void showTagRulesDialog(const TagRule &seed = TagRule()); /// Message counts for the rules dialog's queries, in the order it asked /// for them. Does nothing if the dialog has since closed, or if a newer diff --git a/tests/test_tagrules.cpp b/tests/test_tagrules.cpp index 3954d80..1779245 100644 --- a/tests/test_tagrules.cpp +++ b/tests/test_tagrules.cpp @@ -47,6 +47,7 @@ private slots: void aDismissedWarningComesBackWhenThereIsSomethingNewToSay(); void aSeededDialogOpensOnTheNewRuleWithoutWritingIt(); void aSeededIdThatCollidesDoesNotReplaceTheRuleItMatches(); + void seedingTwiceAddsTwoRulesRatherThanReplacingOne(); void aNameTypedWithSpacesIsSanitisedInTheField(); void aRuleAddedAndNamedInTheDialogSurvivesAReopen(); void unknownFieldsSurviveASave(); @@ -1011,6 +1012,49 @@ void TestTagRules::aSeededIdThatCollidesDoesNotReplaceTheRuleItMatches() QStringLiteral("from:vendor.example.org")); } +void TestTagRules::seedingTwiceAddsTwoRulesRatherThanReplacingOne() +{ + // The dialog is non-modal and single-instance, so a second Create tagging + // rule arrives at a dialog that is already up. It must append, not replace + // the first seed and not be dropped. + QTemporaryDir configHome; + QVERIFY(configHome.isValid()); + qputenv("XDG_CONFIG_HOME", configHome.path().toUtf8()); + QVERIFY(QDir().mkpath(configHome.filePath(QStringLiteral("mailrules")))); + + const QString stored = configHome.filePath( + QStringLiteral("mailrules/rules.json")); + QFile out(stored); + QVERIFY(out.open(QIODevice::WriteOnly)); + out.write(R"({ + "version": 1, + "rules": [ + {"id": "vendor", "query": "from:vendor.example.org", + "add": ["vendor"], "stage": 50, "enabled": true} + ] + })"); + out.close(); + + TagRule first; + first.id = QStringLiteral("first-seed"); + first.query = QStringLiteral("from:one.example.org"); + + TagRulesDialog dialog(first); + QCOMPARE(dialog.ruleCountForTest(), 2); + + TagRule second; + second.id = QStringLiteral("second-seed"); + second.query = QStringLiteral("from:two.example.org"); + dialog.seedRule(second); + + QCOMPARE(dialog.ruleCountForTest(), 3); + QCOMPARE(dialog.nameLineForTest(), QStringLiteral("second-seed")); + + // The first seed survived rather than being overwritten. + dialog.selectRuleForTest(1); + QCOMPARE(dialog.nameLineForTest(), QStringLiteral("first-seed")); +} + void TestTagRules::aFolderRowUsesTheDropdownAndKeepsItsSuffix() { // A path: without its suffix matches nothing and notmuch says nothing -- cgit v1.2.3