summaryrefslogtreecommitdiffstats
path: root/src/notmuchworker.cpp
diff options
context:
space:
mode:
authorDanilo M. <danix@danix.xyz>2026-08-11 12:41:14 +0200
committerDanilo M. <danix@danix.xyz>2026-08-11 12:41:14 +0200
commit44d62143a83af8acbd1c1d14653d39da37e5de4a (patch)
tree360b8fae46487055fbd65bb1601f78345db7e27f /src/notmuchworker.cpp
parent694ec02eb652fcfdbf65c27f68f4607f88615f76 (diff)
downloadqtmaildir-44d62143a83af8acbd1c1d14653d39da37e5de4a.tar.gz
qtmaildir-44d62143a83af8acbd1c1d14653d39da37e5de4a.zip
feat(sent): add a Sent view, flat and by recipient
Adds a `sent` key to [account.*] naming that account's sent folder, and a Sent button beside the saved queries that composes its query from every account carrying one. An account without the key is omitted silently, as a real account may keep no sent mail locally. With no account selected the button spans all of them; selecting one narrows it through the existing scope wrap rather than a second path. Composed at run time rather than shipped as a [queries] entry. A saved query is one fixed string: it cannot narrow to the selected account, and it goes stale the moment an account is added or a provider renames a folder. The design and the measurements behind it are in docs/superpowers/specs/2026-08-11-sent-mail-design.md. Three things there are worth repeating here. The composed path is QUOTED, and that is load-bearing. A real provider nests its sent folder under a bracketed parent, and "[" and "]" are Xapian syntax: unquoted, the query parses rather than matches and returns nothing while looking entirely plausible. Composition happens in one place so there is one chance to get it right, and a bracketed path is pinned in a test. Recipients are opt-in per query, which is a performance contract rather than a preference. notmuch_message_get_header(m, "To") is not served from the index, it reads the message file: folding every thread of a 4411-thread inbox took 38.2 seconds against 251 ms for the 601-thread sent view. The worker skips the walk entirely unless asked, and the refresh path carries the same flag so a background sync cannot blank the column mid-read. Always folding is mutation-tested: the data would be right and only the cost wrong, which nothing else here would notice. The messages reached through the thread are owned by it and freed with it, so recipientsOf() holds them raw and finishes while the thread is alive, exactly as walkReplies does. An NmMessage wrapper there is a double-free. Sent mail is presented flat, and the pane follows. A message you sent otherwise drags in the replies you received, so a view labelled Sent shows conversations rather than what you sent. ThreadListModel::setFlatMode() makes hasChildren() and ReplyCountRole answer differently and changes nothing else; runQuery() sets it on EVERY run, so any other query restores the tree on its way through and the flag cannot outlive the button that set it. The pane needed its own fix for the same reason: the single-message path depends on a field only filled when a thread is expanded, which never happens in a flat list, so loadThread() gained matchedOnly and drops the messages that did not match instead of rendering them as stubs. Recipients replace the sender through the existing SendersRole rather than a new one, so the delegate needs no branch and cannot disagree with the model about which name a row shows. It falls back to the sender when a To header is absent or unparseable, since a blank where a name belongs reads as a rendering fault. Address parsing uses GMime: a display name may contain a comma, so "Rossi, Mario" <m@example.org>, info@example.net is two addresses and splitting reports three. internet_address_list_parse returns NULL for an empty string, which is a crash if unguarded. Backlog item 63.
Diffstat (limited to 'src/notmuchworker.cpp')
-rw-r--r--src/notmuchworker.cpp57
1 files changed, 55 insertions, 2 deletions
diff --git a/src/notmuchworker.cpp b/src/notmuchworker.cpp
index d34c032..24f3fd9 100644
--- a/src/notmuchworker.cpp
+++ b/src/notmuchworker.cpp
@@ -24,6 +24,7 @@
#include <cstdlib>
+#include "mimeparser.h"
#include "nmraii.h"
namespace {
@@ -46,6 +47,45 @@ QStringList tagsOf(notmuch_thread_t *thread)
return result;
}
+/// Who a thread's messages were addressed to, summarised for one line.
+///
+/// EXPENSIVE, and only called when a query asks. "To" is not served from
+/// notmuch's index, so every call here reads message FILES: 8.7 ms per thread
+/// measured against a real database, which is 38 seconds over a 4411-thread
+/// inbox. See ThreadSummary::recipients.
+///
+/// The messages come from the THREAD and are owned by it, freed when it is
+/// freed (notmuch.h:1637). They are therefore held raw and never wrapped in
+/// NmMessage, which would call notmuch_message_destroy on memory the thread
+/// frees again, and the walk finishes before the caller drops the thread. This
+/// is the same rule walkReplies follows, and getting it wrong is a double-free
+/// rather than a leak.
+QString recipientsOf(notmuch_thread_t *thread)
+{
+ // The first message with a usable To wins. A thread is one conversation,
+ // and the alternative, folding every message's recipients together, is the
+ // participants-list problem item 2 rejected: it produces a union that
+ // misdescribes itself the moment a thread has replies going both ways.
+ notmuch_messages_t *messages = notmuch_thread_get_messages(thread);
+ for (; notmuch_messages_valid(messages);
+ notmuch_messages_move_to_next(messages)) {
+ notmuch_message_t *message = notmuch_messages_get(messages);
+ if (!message)
+ continue;
+
+ // Returns "" for a missing header and NULL on error, and the two mean
+ // different things only to notmuch: both are "nothing to show" here.
+ const char *to = notmuch_message_get_header(message, "To");
+ if (!to || !*to)
+ continue;
+
+ const QString summary = recipientSummary(QString::fromUtf8(to));
+ if (!summary.isEmpty())
+ return summary;
+ }
+ return QString();
+}
+
/// Collects the message ids a query matches. Returns false if the query could
/// not be run at all, which is different from a query that matched nothing.
bool collectMessageIds(notmuch_database_t *db, const QString &query,
@@ -187,7 +227,7 @@ void NotmuchWorker::close()
}
void NotmuchWorker::runQuery(const QString &query, quint64 generation,
- SortOrder sort)
+ SortOrder sort, bool withRecipients)
{
if (!openReadOnly())
return;
@@ -231,6 +271,8 @@ void NotmuchWorker::runQuery(const QString &query, quint64 generation,
summary.totalCount = notmuch_thread_get_total_messages(thread.get());
summary.matchedCount = notmuch_thread_get_matched_messages(thread.get());
summary.tags = tagsOf(thread.get());
+ if (withRecipients)
+ summary.recipients = recipientsOf(thread.get());
batch.append(summary);
++total;
@@ -250,7 +292,7 @@ void NotmuchWorker::runQuery(const QString &query, quint64 generation,
void NotmuchWorker::loadThread(const QString &threadId,
const QString &matchQuery,
- quint64 generation)
+ quint64 generation, bool matchedOnly)
{
if (!openReadOnly())
return;
@@ -303,6 +345,17 @@ void NotmuchWorker::loadThread(const QString &threadId,
ref.filePath = QString::fromUtf8(notmuch_message_get_filename(message.get()));
ref.tags = tagsOf(message.get());
ref.matched = !haveMatchSet || matchedIds.contains(ref.messageId);
+
+ // Dropped rather than rendered as a stub.
+ //
+ // haveMatchSet is redundant here and kept deliberately: ref.matched is
+ // already true for every message when no query was given, so the two
+ // conditions cannot disagree today. It states the invariant this
+ // depends on at the point that depends on it, so a later change to how
+ // ref.matched is computed cannot silently empty the pane.
+ if (matchedOnly && haveMatchSet && !ref.matched)
+ continue;
+
result.append(ref);
}