Mailcommon

searchrulenumerical.cpp
1 /*
2  SPDX-FileCopyrightText: 2015-2022 Laurent Montel <[email protected]>
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 
19 using 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>"));
61  msg += FilterLog::recode(asString());
62  msg += QLatin1String(" ( <i>") + QString::number(numericalMsgContents) + QLatin1String("</i> )");
64  }
65  return rc;
66 }
67 
69 {
70  return SearchRule::Envelope;
71 }
72 
73 bool 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 
118 void 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) {
127  QDate date(QDate::currentDate());
128  date = date.addDays(contents().toInt());
129  EmailSearchTerm term(EmailSearchTerm::HeaderOnlyDate, date, akonadiComparator());
130  term.setIsNegated(isNegated());
131  groupTerm.addSubTerm(term);
132  }
133 }
134 
135 QString SearchRuleNumerical::informationAboutNotValidRules() const
136 {
137  return i18n("Content is not a number.");
138 }
QString number(int n, int base)
QString contents() const
Returns the contents of the rule.
Definition: searchrule.cpp:510
CaseInsensitive
bool isNegated() const
Helper that returns whether the rule has a negated function.
Definition: searchrule.cpp:565
QDateTime currentDateTime()
static QString recode(const QString &plain)
Returns an escaped version of the log which can be used in a HTML document.
Definition: filterlog.cpp:189
qlonglong toLongLong(bool *ok, int base) const const
@ RuleResult
Log all rule matching results.
Definition: filterlog.h:53
void setIsNegated(bool negated)
KMail Filter Log Collector.
Definition: filterlog.h:32
bool hasPayload() const
qint64 size() const
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 matchesInternal(long numericalValue, long numericalContents, const QString &contents) const
A helper method for the main matches() method.
Akonadi::SearchTerm::Condition akonadiComparator() const
Converts the rule function into the corresponding Akonadi query operator.
Definition: searchrule.cpp:524
void addSubTerm(const SearchTerm &term)
QString i18n(const char *text, const TYPE &arg...)
bool matches(const Akonadi::Item &item) const override
Tries to match the rule against the KMime::Message in the given item.
RequiredPart
Possible required parts.
Definition: searchrule.h:68
static FilterLog * instance()
Returns the single global instance of the filter log.
Definition: filterlog.cpp:72
int toInt(bool *ok, int base) const const
This class represents one search pattern rule.
Definition: searchrule.h:23
QDate currentDate()
QDate addDays(qint64 ndays) const const
Function
Describes operators for comparison of field and contents.
Definition: searchrule.h:40
bool isEmpty() const override
Determines whether the rule is worth considering.
qint64 daysTo(const QDateTime &other) const const
QDate date() const const
void add(const QString &entry, ContentType type)
Adds the given log entry under the given content type to the log.
Definition: filterlog.cpp:129
SearchRuleNumerical(const QByteArray &field=QByteArray(), Function function=FuncContains, const QString &contents=QString())
Creates new numerical search rule.
bool contains(QChar ch, Qt::CaseSensitivity cs) const const
const QString asString() const
Returns the rule as string for debugging purpose.
Definition: searchrule.cpp:515
QByteArray field() const
Returns the message header field name (without the trailing ':').
Definition: searchrule.cpp:500
The filter dialog.
T payload() const
This file is part of the KDE documentation.
Documentation copyright © 1996-2022 The KDE developers.
Generated on Sat Sep 24 2022 03:58:15 by doxygen 1.8.17 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.