diff options
Diffstat (limited to 'src')
| -rw-r--r-- | src/CMakeLists.txt | 1 | ||||
| -rw-r--r-- | src/keymap.cpp | 2 | ||||
| -rw-r--r-- | src/mainwindow.cpp | 41 | ||||
| -rw-r--r-- | src/mainwindow.h | 6 | ||||
| -rw-r--r-- | src/tagdialog.cpp | 237 | ||||
| -rw-r--r-- | src/tagdialog.h | 102 |
6 files changed, 389 insertions, 0 deletions
diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index de19c8f..5214f42 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -8,6 +8,7 @@ add_library(qtmaildir_lib STATIC notmuchworker.cpp tagchip.cpp tagcolors.cpp + tagdialog.cpp tagstrip.cpp threadlistmodel.cpp mailsync.cpp diff --git a/src/keymap.cpp b/src/keymap.cpp index b18f5d5..0901cfb 100644 --- a/src/keymap.cpp +++ b/src/keymap.cpp @@ -31,6 +31,7 @@ QStringList KeyMap::knownActions() QStringLiteral("delete"), QStringLiteral("spam"), QStringLiteral("toggle_unread"), + QStringLiteral("edit_tags"), QStringLiteral("flag"), QStringLiteral("focus_query"), QStringLiteral("complete_query"), @@ -65,6 +66,7 @@ QList<QPair<QString, QString>> KeyMap::defaultBindings() { QStringLiteral("Ctrl+Shift+S"), QStringLiteral("spam") }, { QStringLiteral("Ctrl+U"), QStringLiteral("toggle_unread") }, { QStringLiteral("Ctrl+I"), QStringLiteral("flag") }, + { QStringLiteral("Ctrl+T"), QStringLiteral("edit_tags") }, { QStringLiteral("Ctrl+L"), QStringLiteral("focus_query") }, // Ctrl+Space is the completion idiom users already carry over from // shells and editors, and it is a named key rather than a symbol, so diff --git a/src/mainwindow.cpp b/src/mainwindow.cpp index d8fba3e..000be85 100644 --- a/src/mainwindow.cpp +++ b/src/mainwindow.cpp @@ -52,6 +52,7 @@ #include "notmuchworker.h" #include "querycompleter.h" #include "tagchip.h" +#include "tagdialog.h" #include "threadlistmodel.h" #include "version.h" @@ -548,6 +549,10 @@ void MainWindow::registerActions() else tagSelected({ QStringLiteral("unread") }, {}, tr("Mark unread")); }); + addAction(QStringLiteral("edit_tags"), tr("Edit &tags..."), + tr("Add or remove any tag on the selected threads"), [this]() { + editTagsOnSelection(); + }); addAction(QStringLiteral("toggle_html"), tr("Toggle &HTML"), tr("Switch the thread between HTML and plain text"), [this]() { m_messageView->toggleHtml(); @@ -634,6 +639,7 @@ void MainWindow::buildMenus() messageMenu->addAction(m_actions.value(QStringLiteral("spam"))); messageMenu->addSeparator(); messageMenu->addAction(m_actions.value(QStringLiteral("toggle_unread"))); + messageMenu->addAction(m_actions.value(QStringLiteral("edit_tags"))); messageMenu->addAction(m_actions.value(QStringLiteral("flag"))); auto *viewMenu = menuBar()->addMenu(tr("&View")); @@ -1152,6 +1158,41 @@ void MainWindow::markCurrentThreadRead() tr("Mark read")); } +void MainWindow::editTagsOnSelection() +{ + const QModelIndexList rows = + m_threadView->selectionModel()->selectedRows(); + if (rows.isEmpty()) { + m_statusLabel->setText(tr("Select a thread first")); + return; + } + + // How many of the selected threads carry each tag, which is what tells a + // tag that is on all of them from one that is on some. + QHash<QString, int> counts; + for (const QModelIndex &index : rows) { + const ThreadSummary thread = m_model->threadAt(index.row()); + for (const QString &tag : thread.tags) + counts[tag] += 1; + } + + // m_knownTags is the same list the query completer uses, so the dialog + // offers every tag in the database without a round trip. + TagDialog dialog(m_knownTags, counts, rows.size(), this); + if (dialog.exec() != QDialog::Accepted) + return; + + const QStringList add = dialog.tagsToAdd(); + const QStringList remove = dialog.tagsToRemove(); + if (add.isEmpty() && remove.isEmpty()) + return; // Applied with nothing changed. + + // Straight through tagSelected(), so this inherits undo, the optimistic + // model update, the one-query multi-row resolution, and the completer + // refresh for a tag that did not exist before. + tagSelected(add, remove, tr("Edit tags")); +} + void MainWindow::tagSelected(const QStringList &add, const QStringList &remove, const QString &description) { diff --git a/src/mainwindow.h b/src/mainwindow.h index b0a4cfd..7897038 100644 --- a/src/mainwindow.h +++ b/src/mainwindow.h @@ -134,6 +134,12 @@ private: /// Redraws the unsynced-edits indicator from m_pendingEdits. void updatePendingIndicator(); + /// Opens the tag dialog on the current selection and applies its result. + /// + /// The only route to an arbitrary tag: every other tag action writes a + /// hardcoded name. + void editTagsOnSelection(); + /// Set once the user has answered the exit prompt, or once a sync started /// for exit has finished. Stops closeEvent asking a second time, and is /// what lets the deferred close through. diff --git a/src/tagdialog.cpp b/src/tagdialog.cpp new file mode 100644 index 0000000..94d2e05 --- /dev/null +++ b/src/tagdialog.cpp @@ -0,0 +1,237 @@ +/* + * qtmaildir - a Qt6 mail client for notmuch-indexed Maildirs + * Copyright (C) 2026 Danilo M. <danix@danix.xyz> + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License version 2 as + * published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + */ + +#include "tagdialog.h" + +#include <QCompleter> +#include <QCoreApplication> +#include <QDialogButtonBox> +#include <QFormLayout> +#include <QHash> +#include <QLabel> +#include <QLineEdit> +#include <QListWidget> +#include <QMessageBox> +#include <QPushButton> +#include <QVBoxLayout> + +TagNameProblem validateTagName(const QString &tag) +{ + const QString trimmed = tag.trimmed(); + + if (trimmed.isEmpty()) + return TagNameProblem::Empty; + + // notmuch's CLI reads "-tag" as an instruction to remove that tag, so a tag + // actually named "-inbox" is a trap the user cannot easily undo later. + if (trimmed.startsWith(QLatin1Char('-'))) + return TagNameProblem::LeadingDash; + + for (const QChar c : trimmed) { + // Checked before the general control-character test, since space is not + // a control character but splits a tag just as effectively. + if (c.isSpace()) + return TagNameProblem::ContainsSpace; + if (c.category() == QChar::Other_Control) + return TagNameProblem::ControlChar; + } + + return TagNameProblem::Ok; +} + +QString tagNameProblemText(TagNameProblem problem, const QString &tag) +{ + switch (problem) { + case TagNameProblem::Ok: + return {}; + case TagNameProblem::Empty: + return QCoreApplication::translate("TagDialog", + "A tag name cannot be empty."); + case TagNameProblem::LeadingDash: + return QCoreApplication::translate( + "TagDialog", + "'%1' cannot start with '-': notmuch reads a leading dash as an " + "instruction to remove a tag.").arg(tag); + case TagNameProblem::ContainsSpace: + return QCoreApplication::translate( + "TagDialog", "'%1' cannot contain spaces.").arg(tag); + case TagNameProblem::ControlChar: + return QCoreApplication::translate( + "TagDialog", "'%1' contains a character that cannot be typed " + "back.").arg(tag); + } + return {}; +} + +namespace { + +/// Splits a line edit's contents into tag names, dropping empties. +/// +/// Comma-separated, so several tags can be applied in one pass. A tag name +/// cannot contain a comma once validateTagName() has run, which is what makes +/// this unambiguous. +QStringList splitTags(const QString &text) +{ + QStringList tags; + const QStringList parts = text.split(QLatin1Char(','), Qt::SkipEmptyParts); + for (const QString &part : parts) { + const QString trimmed = part.trimmed(); + if (!trimmed.isEmpty()) + tags.append(trimmed); + } + return tags; +} + +} // namespace + +TagDialog::TagDialog(const QStringList &knownTags, + const QHash<QString, int> ¤tTags, + int threadCount, + QWidget *parent) + : QDialog(parent), m_threadCount(threadCount) +{ + setWindowTitle(tr("Edit tags")); + + auto *layout = new QVBoxLayout(this); + + auto *heading = new QLabel(tr("%n selected thread(s)", "", threadCount), + this); + layout->addWidget(heading); + + auto *form = new QFormLayout; + + m_addEdit = new QLineEdit(this); + m_addEdit->setPlaceholderText(tr("tag, or several separated by commas")); + m_removeEdit = new QLineEdit(this); + m_removeEdit->setPlaceholderText(tr("tag, or several separated by commas")); + + // Completion is a guard against typos, never a whitelist: a tag absent from + // this list is exactly what the dialog exists to create, so the completer + // suggests and does not constrain. + for (QLineEdit *edit : { m_addEdit, m_removeEdit }) { + auto *completer = new QCompleter(knownTags, edit); + completer->setCaseSensitivity(Qt::CaseInsensitive); + // Hierarchies are the reason this matters: typing "amazon" should find + // "shopping/amazon". + completer->setFilterMode(Qt::MatchContains); + edit->setCompleter(completer); + } + + form->addRow(tr("Add:"), m_addEdit); + form->addRow(tr("Remove:"), m_removeEdit); + layout->addLayout(form); + + // The tags already on the selection, so removing one does not require + // remembering its name. Tri-state, because with several threads selected a + // tag can be on some and not others. + m_currentList = new QListWidget(this); + QStringList sorted = currentTags.keys(); + sorted.sort(); + for (const QString &tag : sorted) { + const int count = currentTags.value(tag); + + auto *item = new QListWidgetItem(tag, m_currentList); + if (count >= threadCount) { + item->setCheckState(Qt::Checked); + m_fullyTagged.append(tag); + } else { + item->setCheckState(Qt::PartiallyChecked); + // Say what "partial" means in numbers, rather than leaving the user + // to infer it from a shaded box. + item->setText(tr("%1 (on %2 of %3)") + .arg(tag).arg(count).arg(threadCount)); + item->setData(Qt::UserRole, tag); + } + if (item->data(Qt::UserRole).isNull()) + item->setData(Qt::UserRole, tag); + } + + if (m_currentList->count() > 0) { + layout->addWidget(new QLabel(tr("Tags on the selection:"), this)); + layout->addWidget(m_currentList); + } else { + m_currentList->hide(); + } + + auto *buttons = new QDialogButtonBox(QDialogButtonBox::Ok + | QDialogButtonBox::Cancel, + this); + buttons->button(QDialogButtonBox::Ok)->setText(tr("Apply")); + connect(buttons, &QDialogButtonBox::accepted, this, &TagDialog::accept); + connect(buttons, &QDialogButtonBox::rejected, this, &QDialog::reject); + layout->addWidget(buttons); + + m_addEdit->setFocus(); +} + +void TagDialog::accept() +{ + QStringList add = splitTags(m_addEdit->text()); + QStringList remove = splitTags(m_removeEdit->text()); + + // Validate before applying anything: a partial change is worse than none, + // since the user cannot tell which half landed. + for (const QStringList &list : { add, remove }) { + for (const QString &tag : list) { + const TagNameProblem problem = validateTagName(tag); + if (problem != TagNameProblem::Ok) { + QMessageBox::warning(this, tr("Invalid tag"), + tagNameProblemText(problem, tag)); + return; // Stay open, with the text still there to fix. + } + } + } + + // Then the checkbox list. An item whose state the user did not touch must + // change nothing, which is why PartiallyChecked is skipped entirely: it is + // the "leave this alone" state for a tag that is on some threads only. + for (int row = 0; row < m_currentList->count(); ++row) { + QListWidgetItem *item = m_currentList->item(row); + const QString tag = item->data(Qt::UserRole).toString(); + + switch (item->checkState()) { + case Qt::Unchecked: + // Was on at least one thread, since it is in this list at all. + if (!remove.contains(tag)) + remove.append(tag); + break; + case Qt::Checked: + // Only meaningful when it was NOT already on every thread; a tag + // already everywhere and left checked is not a change. + if (!add.contains(tag) && !m_fullyTagged.contains(tag)) + add.append(tag); + break; + case Qt::PartiallyChecked: + break; // Untouched: do nothing, deliberately. + } + } + + m_add = add; + m_remove = remove; + QDialog::accept(); +} + +QStringList TagDialog::tagsToAdd() const +{ + return m_add; +} + +QStringList TagDialog::tagsToRemove() const +{ + return m_remove; +} diff --git a/src/tagdialog.h b/src/tagdialog.h new file mode 100644 index 0000000..40eb089 --- /dev/null +++ b/src/tagdialog.h @@ -0,0 +1,102 @@ +/* + * qtmaildir - a Qt6 mail client for notmuch-indexed Maildirs + * Copyright (C) 2026 Danilo M. <danix@danix.xyz> + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License version 2 as + * published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + */ + +#pragma once + +#include <QDialog> +#include <QStringList> + +class QLineEdit; +class QListWidget; + +/// Why a tag name was refused, or Ok when it was not. +/// +/// A reason rather than a bool: a rejected tag has to say what was wrong with +/// it, since silently dropping one leaves the user believing they tagged +/// something they did not. +enum class TagNameProblem { + Ok, + Empty, ///< Nothing, or only whitespace. + LeadingDash, ///< notmuch reads a leading '-' as "remove this tag". + ContainsSpace, ///< Splits into two tags, or fails the write outright. + ControlChar, ///< Not typeable, and unreadable once stored. +}; + +/// Whether `tag` is safe to hand to notmuch. +/// +/// A free function so the rules can be tested without a widget. notmuch itself +/// accepts a great deal, so this is deliberately narrow: it rejects only what +/// produces a failed write or a tag the user cannot see they created. +TagNameProblem validateTagName(const QString &tag); + +/// The message for a rejected tag, ready to show. Empty for Ok. +QString tagNameProblemText(TagNameProblem problem, const QString &tag); + +/// Adds and removes tags across the selected threads. +/// +/// One dialog rather than separate add and remove actions: the natural +/// operation is "make these threads look like this", and filing something under +/// a new tag while removing inbox is one thought, not two. +/// +/// Pure UI. It contacts no worker and holds no database handle; it is handed +/// the vocabulary and the current state, and returns two lists. That is what +/// lets it be unit-tested without a notmuch database. +class TagDialog : public QDialog +{ + Q_OBJECT +public: + /// `knownTags` is the completion vocabulary, usually every tag in the + /// database. `currentTags` maps a tag to how many of the selected threads + /// carry it, which is what drives the tri-state checkboxes. + TagDialog(const QStringList &knownTags, + const QHash<QString, int> ¤tTags, + int threadCount, + QWidget *parent = nullptr); + + /// Tags to add. Empty when the user asked for nothing. + QStringList tagsToAdd() const; + + /// Tags to remove. + QStringList tagsToRemove() const; + + /// Reads both line edits and the checkbox list into the two lists, + /// reporting the first invalid name rather than applying a partial change. + /// + /// Public because QDialog::accept() is: a test drives it directly rather + /// than clicking a button, since which button carries the AcceptRole is + /// not what this class is for. + void accept() override; + +private: + + QStringList m_add; + QStringList m_remove; + + /// Tags that were already on EVERY selected thread when the dialog opened. + /// + /// Needed to tell "the user checked this box" apart from "this box was + /// checked all along": the first is an instruction, the second is not a + /// change and must not be sent as one. + QStringList m_fullyTagged; + + int m_threadCount = 0; + + QLineEdit *m_addEdit = nullptr; + QLineEdit *m_removeEdit = nullptr; + QListWidget *m_currentList = nullptr; +}; |
