Mailcommon

searchrulenumerical.cpp
1/*
2 SPDX-FileCopyrightText: 2015-2024 Laurent Montel <montel@kde.org>
3
4 SPDX-License-Identifier: GPL-2.0-or-later
5*/
6
7#include "searchrulenumerical.h"
8
9#include "filter/filterlog.h"
11
12#include <KMime/KMimeMessage>
13#include <QDateTime>
14
15#include <QRegularExpression>
16
17#include <algorithm>
18
19using namespace MailCommon;
20
22 : SearchRule(field, func, contents)
23{
24}
25
27{
28 bool ok = false;
29 contents().toLongLong(&ok);
30
31 return !ok;
32}
33
35{
36 if (!item.hasPayload<KMime::Message::Ptr>()) {
37 return false;
38 }
39
40 const auto msg = item.payload<KMime::Message::Ptr>();
41
42 QString msgContents;
43 qint64 numericalMsgContents = 0;
44 qint64 numericalValue = 0;
45
46 if (qstricmp(field().constData(), "<size>") == 0) {
47 numericalMsgContents = item.size();
48 numericalValue = contents().toLongLong();
49 msgContents.setNum(numericalMsgContents);
50 } else if (qstricmp(field().constData(), "<age in days>") == 0) {
51 QDateTime msgDateTime = msg->date()->dateTime();
52 numericalMsgContents = msgDateTime.daysTo(QDateTime::currentDateTime());
53 numericalValue = contents().toInt();
54 msgContents.setNum(numericalMsgContents);
55 } else {
56 return false;
57 }
58 bool rc = matchesInternal(numericalValue, numericalMsgContents, msgContents);
59 if (FilterLog::instance()->isLogging()) {
60 QString msg = (rc ? QStringLiteral("<font color=#00FF00>1 = </font>") : QStringLiteral("<font color=#FF0000>0 = </font>"));
62 msg += QLatin1StringView(" ( <i>") + QString::number(numericalMsgContents) + QLatin1StringView("</i> )");
64 }
65 return rc;
66}
67
72
73bool SearchRuleNumerical::matchesInternal(long numericalValue, long numericalMsgContents, const QString &msgContents) const
74{
75 switch (function()) {
76 case SearchRule::FuncEquals:
77 return numericalValue == numericalMsgContents;
78
79 case SearchRule::FuncNotEqual:
80 return numericalValue != numericalMsgContents;
81
82 case SearchRule::FuncContains:
83 return msgContents.contains(contents(), Qt::CaseInsensitive);
84
85 case SearchRule::FuncContainsNot:
86 return !msgContents.contains(contents(), Qt::CaseInsensitive);
87
88 case SearchRule::FuncRegExp:
90
91 case SearchRule::FuncNotRegExp:
93
94 case FuncIsGreater:
95 return numericalMsgContents > numericalValue;
96
97 case FuncIsLessOrEqual:
98 return numericalMsgContents <= numericalValue;
99
100 case FuncIsLess:
101 return numericalMsgContents < numericalValue;
102
103 case FuncIsGreaterOrEqual:
104 return numericalMsgContents >= numericalValue;
105
106 case FuncIsInAddressbook: // since email-addresses are not numerical, I settle for false here
107 return false;
108
109 case FuncIsNotInAddressbook:
110 return false;
111
112 default:;
113 }
114
115 return false;
116}
117
118void SearchRuleNumerical::addQueryTerms(Akonadi::SearchTerm &groupTerm, bool &emptyIsNotAnError) const
119{
120 using namespace Akonadi;
121 emptyIsNotAnError = false;
122 if (qstricmp(field().constData(), "<size>") == 0) {
123 EmailSearchTerm term(EmailSearchTerm::ByteSize, contents().toInt(), akonadiComparator());
124 term.setIsNegated(isNegated());
125 groupTerm.addSubTerm(term);
126 } else if (qstricmp(field().constData(), "<age in days>") == 0) {
128 date = date.addDays(contents().toInt());
129 EmailSearchTerm term(EmailSearchTerm::HeaderOnlyDate, date, akonadiComparator());
130 term.setIsNegated(isNegated());
131 groupTerm.addSubTerm(term);
132 }
133}
134
135QString SearchRuleNumerical::informationAboutNotValidRules() const
136{
137 return i18n("Content is not a number.");
138}
qint64 size() const
bool hasPayload() const
T payload() const
void addSubTerm(const SearchTerm &term)
void setIsNegated(bool negated)
KMail Filter Log Collector.
Definition filterlog.h:33
void add(const QString &entry, ContentType type)
Adds the given log entry under the given content type to the log.
@ RuleResult
Log all rule matching results.
Definition filterlog.h:53
static QString recode(const QString &plain)
Returns an escaped version of the log which can be used in a HTML document.
static FilterLog * instance()
Returns the single global instance of the filter log.
Definition filterlog.cpp:71
bool matches(const Akonadi::Item &item) const override
Tries to match the rule against the KMime::Message in the given item.
void addQueryTerms(Akonadi::SearchTerm &groupTerm, bool &emptyIsNotAnError) const override
Adds query terms to the given term group.
RequiredPart requiredPart() const override
Returns the required part from the item that is needed for the search to operate.
bool isEmpty() const override
Determines whether the rule is worth considering.
SearchRuleNumerical(const QByteArray &field=QByteArray(), Function function=FuncContains, const QString &contents=QString())
Creates new numerical search rule.
bool matchesInternal(long numericalValue, long numericalContents, const QString &contents) const
A helper method for the main matches() method.
This class represents one search pattern rule.
Definition searchrule.h:24
QByteArray field() const
Returns the message header field name (without the trailing ':').
Function function() const
Returns the filter function of the rule.
QString contents() const
Returns the contents of the rule.
Akonadi::SearchTerm::Condition akonadiComparator() const
Converts the rule function into the corresponding Akonadi query operator.
Function
Describes operators for comparison of field and contents.
Definition searchrule.h:40
RequiredPart
Possible required parts.
Definition searchrule.h:68
bool isNegated() const
Helper that returns whether the rule has a negated function.
const QString asString() const
Returns the rule as string for debugging purpose.
QString i18n(const char *text, const TYPE &arg...)
The filter dialog.
QDate addDays(qint64 ndays) const const
QDate currentDate()
QDateTime currentDateTime()
QDate date() const const
qint64 daysTo(const QDateTime &other) const const
bool contains(QChar ch, Qt::CaseSensitivity cs) const const
QString number(double n, char format, int precision)
int toInt(bool *ok, int base) const const
qlonglong toLongLong(bool *ok, int base) const const
CaseInsensitive
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Tue Mar 26 2024 11:14:01 by doxygen 1.10.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.