Mailcommon

searchrule.h
1/*
2 SPDX-FileCopyrightText: 2015-2024 Laurent Montel <montel@kde.org>
3
4 SPDX-License-Identifier: GPL-2.0-or-later
5*/
6#pragma once
7
8#include "mailcommon_export.h"
9#include <Akonadi/SearchQuery>
10
11#include <Akonadi/Item>
12
13class KConfigGroup;
14namespace MailCommon
15{
16/**
17 * @short This class represents one search pattern rule.
18 * Incoming mail is sent through the list of mail filter
19 * rules before it is placed in the associated mail folder (usually "inbox").
20 * This class represents one mail filter rule. It is also used to represent
21 * a search rule as used by the search dialog and folders.
22 */
23class MAILCOMMON_EXPORT SearchRule
24{
25public:
26 /**
27 * Defines a pointer to a search rule.
28 */
29 using Ptr = std::shared_ptr<SearchRule>;
30
31 /**
32 * Describes operators for comparison of field and contents.
33 *
34 * If you change the order or contents of the enum: do not forget
35 * to change funcConfigNames[], sFilterFuncList and matches()
36 * in SearchRule, too.
37 * Also, it is assumed that these functions come in pairs of logical
38 * opposites (ie. "=" <-> "!=", ">" <-> "<=", etc.).
39 */
40 enum Function {
41 FuncNone = -1,
42 FuncContains = 0,
43 FuncContainsNot,
44 FuncEquals,
45 FuncNotEqual,
46 FuncRegExp,
47 FuncNotRegExp,
48 FuncIsGreater,
49 FuncIsLessOrEqual,
50 FuncIsLess,
51 FuncIsGreaterOrEqual,
52 FuncIsInAddressbook,
53 FuncIsNotInAddressbook,
54 FuncIsInCategory,
55 FuncIsNotInCategory,
56 FuncHasAttachment,
57 FuncHasNoAttachment,
58 FuncStartWith,
59 FuncNotStartWith,
60 FuncEndWith,
61 FuncNotEndWith,
62 FuncHasInvitation,
63 FuncHasNoInvitation,
64 };
65
66 /**
67 * @enum RequiredPart
68 * @brief Possible required parts.
69 */
71 Envelope = 0, ///< Envelope
72 Header, ///< Header
73 CompleteMessage ///< Whole message
74 };
75
76 /**
77 * Creates new new search rule.
78 *
79 * @param field The field to search in.
80 * @param function The function to use for searching.
81 * @param contents The contents to search for.
82 */
83 explicit SearchRule(const QByteArray &field = QByteArray(), Function function = FuncContains, const QString &contents = QString());
84
85 /**
86 * Creates a new search rule from an @p other rule.
87 */
88 SearchRule(const SearchRule &other);
89
90 /**
91 * Initializes this rule with an @p other rule.
92 */
93 const SearchRule &operator=(const SearchRule &other);
94
95 /**
96 * Creates a new search rule of a certain type by instantiating the
97 * appropriate subclass depending on the @p field.
98 *
99 * @param field The field to search in.
100 * @param function The function to use for searching.
101 * @param contents The contents to search for.
102 */
103 static SearchRule::Ptr createInstance(const QByteArray &field = QByteArray(), Function function = FuncContains, const QString &contents = QString());
104
105 /**
106 * Creates a new search rule of a certain type by instantiating the
107 * appropriate subclass depending on the @p field.
108 *
109 * @param field The field to search in.
110 * @param function The name of the function to use for searching.
111 * @param contents The contents to search for.
112 */
113 static SearchRule::Ptr createInstance(const QByteArray &field, const char *function, const QString &contents);
114
115 /**
116 * Creates a new search rule by cloning an @p other rule.
117 */
118 static SearchRule::Ptr createInstance(const SearchRule &other);
119
120 /**
121 * Creates a new search rule by deseralizing its structure from a data @p stream.
122 */
123 static SearchRule::Ptr createInstance(QDataStream &stream);
124
125 /**
126 * Creates a new search rule from a given config @p group.
127 *
128 * @param group The config group to read the structure from.
129 * @param index The identifier that is used to distinguish
130 * rules within a single config group.
131 *
132 * @note This function does no validation of the data obtained
133 * from the config file. You should call isEmpty yourself
134 * if you need valid rules.
135 */
136 static SearchRule::Ptr createInstanceFromConfig(const KConfigGroup &group, int index);
137
138 /**
139 * Destroys the search rule.
140 */
141 virtual ~SearchRule();
142
143 /**
144 * Tries to match the rule against the KMime::Message in the
145 * given @p item.
146 *
147 * @return true if the rule matched, false otherwise.
148 *
149 * @note Must be implemented by subclasses.
150 */
151 virtual bool matches(const Akonadi::Item &item) const = 0;
152
153 /**
154 * Determines whether the rule is worth considering.
155 * It isn't if either the field is not set or the contents is empty.
156 * The calling code should make sure that it's rule list contains
157 * only non-empty rules, as matches doesn't check this.
158 */
159 virtual bool isEmpty() const = 0;
160
161 /**
162 * Returns the required part from the item that is needed for the search to
163 * operate. See @ref RequiredPart */
165
166 /**
167 * Saves the object into a given config @p group.
168 *
169 * @param group The config group.
170 * @param index The identifier that is used to distinguish
171 * rules within a single config group.
172 *
173 * @note This function will happily write itself even when it's
174 * not valid, assuming higher layers to Do The Right Thing(TM).
175 */
176 void writeConfig(KConfigGroup &group, int index) const;
177
178 void generateSieveScript(QStringList &requireModules, QString &code);
179
180 /**
181 * Sets the filter @p function of the rule.
182 */
183 void setFunction(Function function);
184
185 /**
186 * Returns the filter function of the rule.
187 */
188 Function function() const;
189
190 /**
191 * Sets the message header field @p name.
192 *
193 * @note Make sure the name contains no trailing ':'.
194 */
195 void setField(const QByteArray &name);
196
197 /**
198 * Returns the message header field name (without the trailing ':').
199 *
200 * There are also six pseudo-headers:
201 * @li <message>: Try to match against the whole message.
202 * @li <body>: Try to match against the body of the message.
203 * @li <any header>: Try to match against any header field.
204 * @li <recipients>: Try to match against both To: and Cc: header fields.
205 * @li <size>: Try to match against size of message (numerical).
206 * @li <age in days>: Try to match against age of message (numerical).
207 * @li <status>: Try to match against status of message (status).
208 * @li <tag>: Try to match against message tags.
209 */
210 [[nodiscard]] QByteArray field() const;
211
212 /**
213 * Set the @p contents of the rule.
214 *
215 * This can be either a substring to search for in
216 * or a regexp pattern to match against the header.
217 */
218 void setContents(const QString &contents);
219
220 /**
221 * Returns the contents of the rule.
222 */
223 [[nodiscard]] QString contents() const;
224
225 /**
226 * Returns the rule as string for debugging purpose
227 */
228 [[nodiscard]] const QString asString() const;
229
230 /**
231 * Adds query terms to the given term group.
232 */
233 virtual void addQueryTerms(Akonadi::SearchTerm &groupTerm, bool &emptyIsNotAnError) const
234 {
235 Q_UNUSED(groupTerm)
236 Q_UNUSED(emptyIsNotAnError)
237 }
238
239 QDataStream &operator>>(QDataStream &) const;
240 virtual QString informationAboutNotValidRules() const
241 {
242 return {};
243 }
244
245protected:
246 /**
247 * Helper that returns whether the rule has a negated function.
248 */
249 [[nodiscard]] bool isNegated() const;
250
251 /**
252 * Converts the rule function into the corresponding Akonadi query operator.
253 */
254 [[nodiscard]] Akonadi::SearchTerm::Condition akonadiComparator() const;
255
256private:
257 MAILCOMMON_NO_EXPORT static Function configValueToFunc(const char *);
258 MAILCOMMON_NO_EXPORT static QString functionToString(Function);
259 MAILCOMMON_NO_EXPORT QString conditionToString(Function function);
260
261 QByteArray mField;
262 Function mFunction;
263 QString mContents;
264};
265}
This class represents one search pattern rule.
Definition searchrule.h:24
virtual SearchRule::RequiredPart requiredPart() const =0
Returns the required part from the item that is needed for the search to operate.
virtual void addQueryTerms(Akonadi::SearchTerm &groupTerm, bool &emptyIsNotAnError) const
Adds query terms to the given term group.
Definition searchrule.h:233
SearchRule(const SearchRule &other)
Creates a new search rule from an other rule.
std::shared_ptr< SearchRule > Ptr
Defines a pointer to a search rule.
Definition searchrule.h:29
virtual bool isEmpty() const =0
Determines whether the rule is worth considering.
virtual ~SearchRule()
Destroys the search rule.
Function
Describes operators for comparison of field and contents.
Definition searchrule.h:40
RequiredPart
Possible required parts.
Definition searchrule.h:70
virtual bool matches(const Akonadi::Item &item) const =0
Tries to match the rule against the KMime::Message in the given item.
The filter dialog.
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Sat Dec 21 2024 17:00:25 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.