7#include "mailcommon_debug.h"
8#include "searchrule/searchruleattachment.h"
9#include "searchrule/searchruledate.h"
10#include "searchrule/searchruleencryption.h"
11#include "searchrule/searchrulenumerical.h"
12#include "searchrule/searchrulestatus.h"
13#include "searchrule/searchrulestring.h"
15#include <KConfigGroup>
23static const char *
const funcConfigNames[] = {
"contains",
34 "is-not-in-addressbook",
44static const int numFuncConfigNames =
sizeof funcConfigNames /
sizeof *funcConfigNames;
69 mField = other.mField;
70 mFunction = other.mFunction;
71 mContents = other.mContents;
79 if (
field ==
"<status>") {
81 }
else if (
field ==
"<age in days>" ||
field ==
"<size>") {
83 }
else if (
field ==
"<date>") {
85 }
else if (
field ==
"<encryption>") {
87 }
else if (
field ==
"<attachment>") {
108 const char cIdx = char(
int(
'A') + aIdx);
111 static const QString func = QStringLiteral(
"func");
118 if (field2 ==
"<To or Cc>") {
145 for (
int i = 0; i < numFuncConfigNames; ++i) {
146 if (qstricmp(funcConfigNames[i], str) == 0) {
147 return static_cast<Function
>(i);
154QString SearchRule::functionToString(Function function)
157 return funcConfigNames[int(
function)];
159 return QStringLiteral(
"invalid");
165 const char cIdx = char(
'A' + aIdx);
167 static const QString func = QStringLiteral(
"func");
171 config.
writeEntry(func + cIdx, functionToString(mFunction));
175QString SearchRule::conditionToString(Function function)
183 str =
i18n(
"not equal");
186 str =
i18n(
"is greater");
188 case FuncIsLessOrEqual:
189 str =
i18n(
"is less or equal");
192 str =
i18n(
"is less");
194 case FuncIsGreaterOrEqual:
195 str =
i18n(
"is greater or equal");
197 case FuncIsInAddressbook:
198 str =
i18n(
"is in addressbook");
200 case FuncIsNotInAddressbook:
201 str =
i18n(
"is not in addressbook");
203 case FuncIsInCategory:
204 str =
i18n(
"is in category");
206 case FuncIsNotInCategory:
207 str =
i18n(
"is in category");
209 case FuncHasAttachment:
210 str =
i18n(
"has an attachment");
212 case FuncHasNoAttachment:
213 str =
i18n(
"has not an attachment");
216 str =
i18n(
"start with");
218 case FuncNotStartWith:
219 str =
i18n(
"not start with");
222 str =
i18n(
"end with");
225 str =
i18n(
"not end with");
231 str =
i18n(
"contains");
233 case FuncContainsNot:
234 str =
i18n(
"not contains");
237 str =
i18n(
"has regexp");
240 str =
i18n(
"not regexp");
248 QString contentStr = mContents;
249 if (mField ==
"<size>") {
260 comparison = QStringLiteral(
":over");
262 case FuncIsLessOrEqual:
263 comparison = QStringLiteral(
":under");
267 comparison = QStringLiteral(
":under");
269 case FuncIsGreaterOrEqual:
270 comparison = QStringLiteral(
":over");
273 case FuncIsInAddressbook:
274 case FuncIsNotInAddressbook:
275 case FuncIsInCategory:
276 case FuncIsNotInCategory:
277 case FuncHasAttachment:
278 case FuncHasNoAttachment:
280 case FuncNotStartWith:
285 case FuncContainsNot:
293 }
else if (mField ==
"<status>") {
296 }
else if (mField ==
"<any header>") {
299 }
else if (mField ==
"contents") {
302 }
else if (mField ==
"<age in days>") {
305 }
else if (mField ==
"<date>") {
308 }
else if (mField ==
"<recipients>") {
311 }
else if (mField ==
"<tag>") {
313 }
else if (mField ==
"<message>") {
315 code +=
i18n(
"<message> not implemented/supported");
316 }
else if (mField ==
"<body>") {
318 requireModules << QStringLiteral(
"body");
321 bool negative =
false;
326 comparison = QStringLiteral(
":contains");
328 case FuncContainsNot:
330 comparison = QStringLiteral(
":contains");
333 comparison = QStringLiteral(
":is");
336 comparison = QStringLiteral(
":is");
340 comparison = QStringLiteral(
":regex");
342 requireModules << QStringLiteral(
"regex");
347 requireModules << QStringLiteral(
"regex");
349 comparison = QStringLiteral(
":regex");
353 comparison = QStringLiteral(
":regex");
355 requireModules << QStringLiteral(
"regex");
359 case FuncNotStartWith:
360 comparison = QStringLiteral(
":regex");
362 requireModules << QStringLiteral(
"regex");
364 comparison = QStringLiteral(
":regex");
369 comparison = QStringLiteral(
":regex");
371 requireModules << QStringLiteral(
"regex");
373 comparison = QStringLiteral(
":regex");
377 comparison = QStringLiteral(
":regex");
379 requireModules << QStringLiteral(
"regex");
381 comparison = QStringLiteral(
":regex");
386 case FuncIsLessOrEqual:
388 case FuncIsGreaterOrEqual:
389 case FuncIsInAddressbook:
390 case FuncIsNotInAddressbook:
391 case FuncIsInCategory:
392 case FuncIsNotInCategory:
393 case FuncHasAttachment:
394 case FuncHasNoAttachment:
399 code += (negative ? QStringLiteral(
"not ") :
QString()) + QStringLiteral(
"body :text %1 \"%2\"").arg(comparison, contentStr);
402 bool negative =
false;
407 comparison = QStringLiteral(
":contains");
409 case FuncContainsNot:
411 comparison = QStringLiteral(
":contains");
414 comparison = QStringLiteral(
":is");
417 comparison = QStringLiteral(
":is");
421 comparison = QStringLiteral(
":regex");
423 requireModules << QStringLiteral(
"regex");
428 requireModules << QStringLiteral(
"regex");
430 comparison = QStringLiteral(
":regex");
434 comparison = QStringLiteral(
":regex");
436 requireModules << QStringLiteral(
"regex");
440 case FuncNotStartWith:
441 comparison = QStringLiteral(
":regex");
443 requireModules << QStringLiteral(
"regex");
445 comparison = QStringLiteral(
":regex");
450 comparison = QStringLiteral(
":regex");
452 requireModules << QStringLiteral(
"regex");
454 comparison = QStringLiteral(
":regex");
458 comparison = QStringLiteral(
":regex");
460 requireModules << QStringLiteral(
"regex");
462 comparison = QStringLiteral(
":regex");
468 case FuncIsLessOrEqual:
470 case FuncIsGreaterOrEqual:
471 case FuncIsInAddressbook:
472 case FuncIsNotInAddressbook:
473 case FuncIsInCategory:
474 case FuncIsNotInCategory:
475 case FuncHasAttachment:
476 case FuncHasNoAttachment:
481 code += (negative ? QStringLiteral(
"not ") :
QString())
482 + QStringLiteral(
"header %1 \"%2\" \"%3\"").arg(comparison).arg(
QLatin1StringView(mField)).arg(contentStr);
519 result += functionToString(mFunction);
528 case SearchRule::FuncContains:
529 case SearchRule::FuncContainsNot:
530 return Akonadi::SearchTerm::CondContains;
532 case SearchRule::FuncEquals:
533 case SearchRule::FuncNotEqual:
534 return Akonadi::SearchTerm::CondEqual;
536 case SearchRule::FuncIsGreater:
537 return Akonadi::SearchTerm::CondGreaterThan;
539 case SearchRule::FuncIsGreaterOrEqual:
540 return Akonadi::SearchTerm::CondGreaterOrEqual;
542 case SearchRule::FuncIsLess:
543 return Akonadi::SearchTerm::CondLessThan;
545 case SearchRule::FuncIsLessOrEqual:
546 return Akonadi::SearchTerm::CondLessOrEqual;
548 case SearchRule::FuncRegExp:
549 case SearchRule::FuncNotRegExp:
551 return Akonadi::SearchTerm::CondContains;
553 case SearchRule::FuncStartWith:
554 case SearchRule::FuncNotStartWith:
555 case SearchRule::FuncEndWith:
556 case SearchRule::FuncNotEndWith:
558 return Akonadi::SearchTerm::CondContains;
560 qCDebug(MAILCOMMON_LOG) <<
"Unhandled function type: " <<
function();
563 return Akonadi::SearchTerm::CondEqual;
570 case SearchRule::FuncContainsNot:
571 case SearchRule::FuncNotEqual:
572 case SearchRule::FuncNotRegExp:
573 case SearchRule::FuncHasNoAttachment:
574 case SearchRule::FuncIsNotInCategory:
575 case SearchRule::FuncIsNotInAddressbook:
576 case SearchRule::FuncNotStartWith:
577 case SearchRule::FuncNotEndWith:
587 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