summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorDanilo M. <danix@danix.xyz>2026-08-13 11:00:07 +0200
committerDanilo M. <danix@danix.xyz>2026-08-13 11:00:07 +0200
commita3651eac1528a2b90cfb8e26934facf856ae3531 (patch)
treef356ae6eba375590b74b040d6ce904180cb9cf01 /src
parentdf6461fa9136f2110c8a24567a3e4a32432a9476 (diff)
downloadqtmaildir-a3651eac1528a2b90cfb8e26934facf856ae3531.tar.gz
qtmaildir-a3651eac1528a2b90cfb8e26934facf856ae3531.zip
feat(rulequery): compile a single term
Diffstat (limited to 'src')
-rw-r--r--src/CMakeLists.txt1
-rw-r--r--src/rulequery.cpp65
-rw-r--r--src/rulequery.h65
3 files changed, 131 insertions, 0 deletions
diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt
index 926f6a9..6ae157b 100644
--- a/src/CMakeLists.txt
+++ b/src/CMakeLists.txt
@@ -23,6 +23,7 @@ add_library(qtmaildir_lib STATIC
messageview.cpp
mainwindow.cpp
querycompleter.cpp
+ rulequery.cpp
)
target_include_directories(qtmaildir_lib
diff --git a/src/rulequery.cpp b/src/rulequery.cpp
new file mode 100644
index 0000000..f80e6c4
--- /dev/null
+++ b/src/rulequery.cpp
@@ -0,0 +1,65 @@
+/*
+ * 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 "rulequery.h"
+
+namespace {
+
+/// The notmuch prefix each field compiles to. Wire format, never translated.
+QString prefixFor(RuleTerm::Field field)
+{
+ switch (field) {
+ case RuleTerm::From: return QStringLiteral("from");
+ case RuleTerm::To: return QStringLiteral("to");
+ case RuleTerm::Cc: return QStringLiteral("cc");
+ case RuleTerm::Subject: return QStringLiteral("subject");
+ case RuleTerm::Tag: return QStringLiteral("tag");
+ case RuleTerm::Folder: return QStringLiteral("path");
+ case RuleTerm::Attachment: return QStringLiteral("attachment");
+ case RuleTerm::Date: return QStringLiteral("date");
+ }
+ return QString();
+}
+
+} // namespace
+
+bool operator==(const RuleTerm &a, const RuleTerm &b)
+{
+ return a.field == b.field && a.op == b.op && a.value == b.value;
+}
+
+bool operator==(const RuleQuery &a, const RuleQuery &b)
+{
+ return a.parsed == b.parsed && a.join == b.join
+ && a.terms == b.terms && a.exclusions == b.exclusions;
+}
+
+QString RuleQuery::compile() const
+{
+ if (terms.isEmpty())
+ return QString();
+
+ return prefixFor(terms.first().field) + QLatin1Char(':')
+ + terms.first().value;
+}
+
+RuleQuery RuleQuery::parse(const QString &query)
+{
+ Q_UNUSED(query);
+ return RuleQuery();
+}
diff --git a/src/rulequery.h b/src/rulequery.h
new file mode 100644
index 0000000..9d487d2
--- /dev/null
+++ b/src/rulequery.h
@@ -0,0 +1,65 @@
+/*
+ * 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 <QList>
+#include <QString>
+
+/// One row in the builder: a field, an operator, and a value.
+struct RuleTerm
+{
+ enum Field { From, To, Cc, Subject, Tag, Folder, Attachment, Date };
+
+ enum Op {
+ Contains, ContainsNot, ///< Unquoted term, optionally negated.
+ Is, IsNot, ///< Quoted phrase, optionally negated.
+ Has, HasNot, ///< attachment: only.
+ Before, After ///< date: only. "not before" is "after", so
+ ///< these carry no negated twin.
+ };
+
+ Field field = From;
+ Op op = Contains;
+ QString value;
+};
+
+bool operator==(const RuleTerm &a, const RuleTerm &b);
+
+/// A tagging rule's query, as rows.
+///
+/// The STRING is the stored format, shared with mailctl and executed by the
+/// notmuch post-new hook. This type is a view over it, never the store: a
+/// query it cannot represent must still open, save and run unchanged.
+struct RuleQuery
+{
+ enum Join { All, Any }; ///< and / or, over the positive terms only.
+
+ Join join = All;
+ QList<RuleTerm> terms; ///< Positive section.
+ QList<RuleTerm> exclusions; ///< The "but not" block, joined `and not`.
+
+ /// False when the query cannot be shown as rows. NOT an error and NOT a
+ /// claim that the query is invalid: the rule opens in text mode.
+ bool parsed = false;
+
+ static RuleQuery parse(const QString &query);
+ QString compile() const;
+};
+
+bool operator==(const RuleQuery &a, const RuleQuery &b);