7#include "mailcommon_debug.h"
8#include "searchrule/searchruleattachment.h"
9#include "searchrule/searchruledate.h"
10#include "searchrule/searchruleencryption.h"
11#include "searchrule/searchruleinvitation.h"
12#include "searchrule/searchrulenumerical.h"
13#include "searchrule/searchrulestatus.h"
14#include "searchrule/searchrulestring.h"
16#include <KConfigGroup>
24static const char *
const funcConfigNames[] = {
"contains",
35 "is-not-in-addressbook",
47static const int numFuncConfigNames =
sizeof funcConfigNames /
sizeof *funcConfigNames;
72 mField = other.mField;
73 mFunction = other.mFunction;
74 mContents = other.mContents;
82 if (
field ==
"<status>") {
84 }
else if (
field ==
"<age in days>" ||
field ==
"<size>") {
86 }
else if (
field ==
"<date>") {
88 }
else if (
field ==
"<encryption>") {
90 }
else if (
field ==
"<attachment>") {
92 }
else if (
field ==
"<invitation>") {
113 const char cIdx = char(
int(
'A') + aIdx);
116 static const QString func = QStringLiteral(
"func");
123 if (field2 ==
"<To or Cc>") {
150 for (
int i = 0; i < numFuncConfigNames; ++i) {
151 if (qstricmp(funcConfigNames[i], str) == 0) {
152 return static_cast<Function
>(i);
159QString SearchRule::functionToString(Function function)
162 return funcConfigNames[int(
function)];
164 return QStringLiteral(
"invalid");
170 const char cIdx = char(
'A' + aIdx);
172 static const QString func = QStringLiteral(
"func");
176 config.
writeEntry(func + cIdx, functionToString(mFunction));
180QString SearchRule::conditionToString(Function function)
188 str =
i18n(
"not equal");
191 str =
i18n(
"is greater");
193 case FuncIsLessOrEqual:
194 str =
i18n(
"is less or equal");
197 str =
i18n(
"is less");
199 case FuncIsGreaterOrEqual:
200 str =
i18n(
"is greater or equal");
202 case FuncIsInAddressbook:
203 str =
i18n(
"is in addressbook");
205 case FuncIsNotInAddressbook:
206 str =
i18n(
"is not in addressbook");
208 case FuncIsInCategory:
209 str =
i18n(
"is in category");
211 case FuncIsNotInCategory:
212 str =
i18n(
"is in category");
214 case FuncHasAttachment:
215 str =
i18n(
"has an attachment");
217 case FuncHasNoAttachment:
218 str =
i18n(
"has not an attachment");
221 str =
i18n(
"start with");
223 case FuncNotStartWith:
224 str =
i18n(
"not start with");
227 str =
i18n(
"end with");
230 str =
i18n(
"not end with");
236 str =
i18n(
"contains");
238 case FuncContainsNot:
239 str =
i18n(
"not contains");
242 str =
i18n(
"has regexp");
245 str =
i18n(
"not regexp");
253 QString contentStr = mContents;
254 if (mField ==
"<size>") {
265 comparison = QStringLiteral(
":over");
267 case FuncIsLessOrEqual:
268 comparison = QStringLiteral(
":under");
272 comparison = QStringLiteral(
":under");
274 case FuncIsGreaterOrEqual:
275 comparison = QStringLiteral(
":over");
278 case FuncIsInAddressbook:
279 case FuncIsNotInAddressbook:
280 case FuncIsInCategory:
281 case FuncIsNotInCategory:
282 case FuncHasAttachment:
283 case FuncHasNoAttachment:
285 case FuncNotStartWith:
290 case FuncContainsNot:
298 }
else if (mField ==
"<status>") {
301 }
else if (mField ==
"<any header>") {
304 }
else if (mField ==
"contents") {
307 }
else if (mField ==
"<age in days>") {
310 }
else if (mField ==
"<date>") {
313 }
else if (mField ==
"<recipients>") {
316 }
else if (mField ==
"<tag>") {
318 }
else if (mField ==
"<message>") {
320 code +=
i18n(
"<message> not implemented/supported");
321 }
else if (mField ==
"<body>") {
323 requireModules << QStringLiteral(
"body");
326 bool negative =
false;
331 comparison = QStringLiteral(
":contains");
333 case FuncContainsNot:
335 comparison = QStringLiteral(
":contains");
338 comparison = QStringLiteral(
":is");
341 comparison = QStringLiteral(
":is");
345 comparison = QStringLiteral(
":regex");
347 requireModules << QStringLiteral(
"regex");
352 requireModules << QStringLiteral(
"regex");
354 comparison = QStringLiteral(
":regex");
358 comparison = QStringLiteral(
":regex");
360 requireModules << QStringLiteral(
"regex");
364 case FuncNotStartWith:
365 comparison = QStringLiteral(
":regex");
367 requireModules << QStringLiteral(
"regex");
369 comparison = QStringLiteral(
":regex");
374 comparison = QStringLiteral(
":regex");
376 requireModules << QStringLiteral(
"regex");
378 comparison = QStringLiteral(
":regex");
382 comparison = QStringLiteral(
":regex");
384 requireModules << QStringLiteral(
"regex");
386 comparison = QStringLiteral(
":regex");
391 case FuncIsLessOrEqual:
393 case FuncIsGreaterOrEqual:
394 case FuncIsInAddressbook:
395 case FuncIsNotInAddressbook:
396 case FuncIsInCategory:
397 case FuncIsNotInCategory:
398 case FuncHasAttachment:
399 case FuncHasNoAttachment:
404 code += (negative ? QStringLiteral(
"not ") :
QString()) + QStringLiteral(
"body :text %1 \"%2\"").arg(comparison, contentStr);
407 bool negative =
false;
412 comparison = QStringLiteral(
":contains");
414 case FuncContainsNot:
416 comparison = QStringLiteral(
":contains");
419 comparison = QStringLiteral(
":is");
422 comparison = QStringLiteral(
":is");
426 comparison = QStringLiteral(
":regex");
428 requireModules << QStringLiteral(
"regex");
433 requireModules << QStringLiteral(
"regex");
435 comparison = QStringLiteral(
":regex");
439 comparison = QStringLiteral(
":regex");
441 requireModules << QStringLiteral(
"regex");
445 case FuncNotStartWith:
446 comparison = QStringLiteral(
":regex");
448 requireModules << QStringLiteral(
"regex");
450 comparison = QStringLiteral(
":regex");
455 comparison = QStringLiteral(
":regex");
457 requireModules << QStringLiteral(
"regex");
459 comparison = QStringLiteral(
":regex");
463 comparison = QStringLiteral(
":regex");
465 requireModules << QStringLiteral(
"regex");
467 comparison = QStringLiteral(
":regex");
473 case FuncIsLessOrEqual:
475 case FuncIsGreaterOrEqual:
476 case FuncIsInAddressbook:
477 case FuncIsNotInAddressbook:
478 case FuncIsInCategory:
479 case FuncIsNotInCategory:
480 case FuncHasAttachment:
481 case FuncHasNoAttachment:
486 code += (negative ? QStringLiteral(
"not ") :
QString())
487 + QStringLiteral(
"header %1 \"%2\" \"%3\"").arg(comparison).arg(
QLatin1StringView(mField)).arg(contentStr);
524 result += functionToString(mFunction);
533 case SearchRule::FuncContains:
534 case SearchRule::FuncContainsNot:
535 return Akonadi::SearchTerm::CondContains;
537 case SearchRule::FuncEquals:
538 case SearchRule::FuncNotEqual:
539 return Akonadi::SearchTerm::CondEqual;
541 case SearchRule::FuncIsGreater:
542 return Akonadi::SearchTerm::CondGreaterThan;
544 case SearchRule::FuncIsGreaterOrEqual:
545 return Akonadi::SearchTerm::CondGreaterOrEqual;
547 case SearchRule::FuncIsLess:
548 return Akonadi::SearchTerm::CondLessThan;
550 case SearchRule::FuncIsLessOrEqual:
551 return Akonadi::SearchTerm::CondLessOrEqual;
553 case SearchRule::FuncRegExp:
554 case SearchRule::FuncNotRegExp:
556 return Akonadi::SearchTerm::CondContains;
558 case SearchRule::FuncStartWith:
559 case SearchRule::FuncNotStartWith:
560 case SearchRule::FuncEndWith:
561 case SearchRule::FuncNotEndWith:
563 return Akonadi::SearchTerm::CondContains;
565 qCDebug(MAILCOMMON_LOG) <<
"Unhandled function type: " <<
function();
568 return Akonadi::SearchTerm::CondEqual;
575 case SearchRule::FuncContainsNot:
576 case SearchRule::FuncNotEqual:
577 case SearchRule::FuncNotRegExp:
578 case SearchRule::FuncHasNoAttachment:
579 case SearchRule::FuncIsNotInCategory:
580 case SearchRule::FuncIsNotInAddressbook:
581 case SearchRule::FuncNotStartWith:
582 case SearchRule::FuncNotEndWith:
592 s << mField << functionToString(mFunction) << mContents;
void writeEntry(const char *key, const char *value, WriteConfigFlags pFlags=Normal)
QString readEntry(const char *key, const char *aDefault=nullptr) const
This class represents a search pattern rule operating on numerical values.
This class represents a search to be performed against the status of a message.
This class represents one search pattern rule.
QByteArray field() const
Returns the message header field name (without the trailing ':').
static SearchRule::Ptr createInstanceFromConfig(const KConfigGroup &group, int index)
Creates a new search rule from a given config group.
Function function() const
Returns the filter function of the rule.
QString contents() const
Returns the contents of the rule.
void setField(const QByteArray &name)
Sets the message header field name.
std::shared_ptr< SearchRule > Ptr
Defines a pointer to a search rule.
Akonadi::SearchTerm::Condition akonadiComparator() const
Converts the rule function into the corresponding Akonadi query operator.
void setFunction(Function function)
Sets the filter function of the rule.
virtual ~SearchRule()
Destroys the search rule.
Function
Describes operators for comparison of field and contents.
SearchRule(const QByteArray &field=QByteArray(), Function function=FuncContains, const QString &contents=QString())
Creates new new search rule.
bool isNegated() const
Helper that returns whether the rule has a negated function.
const SearchRule & operator=(const SearchRule &other)
Initializes this rule with an other rule.
const QString asString() const
Returns the rule as string for debugging purpose.
void setContents(const QString &contents)
Set the contents of the rule.
void writeConfig(KConfigGroup &group, int index) const
Saves the object into a given config group.
static SearchRule::Ptr createInstance(const QByteArray &field=QByteArray(), Function function=FuncContains, const QString &contents=QString())
Creates a new search rule of a certain type by instantiating the appropriate subclass depending on th...
QString i18n(const char *text, const TYPE &arg...)
QString arg(Args &&... args) const const
QString fromLatin1(QByteArrayView str)
QString number(double n, char format, int precision)
int toInt(bool *ok, int base) const const
QByteArray toLatin1() const const
bool contains(QLatin1StringView str, Qt::CaseSensitivity cs) const const