Mailcommon

searchruledate.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 "searchruledate.h"
8 
9 #include "filter/filterlog.h"
11 
12 #include <KLocalizedString>
13 #include <KMime/KMimeMessage>
14 using namespace MailCommon;
15 
16 SearchRuleDate::SearchRuleDate(const QByteArray &field, Function func, const QString &contents)
17  : SearchRule(field, func, contents)
18 {
19 }
20 
21 QString SearchRuleDate::informationAboutNotValidRules() const
22 {
23  return i18n("Date is not valid.");
24 }
25 
26 bool SearchRuleDate::isEmpty() const
27 {
28  return !QDate::fromString(contents(), Qt::ISODate).isValid();
29 }
30 
31 bool SearchRuleDate::matches(const Akonadi::Item &item) const
32 {
33  if (!item.hasPayload<KMime::Message::Ptr>()) {
34  return false;
35  }
36  const auto msg = item.payload<KMime::Message::Ptr>();
37 
38  const QDate msgDate = msg->date()->dateTime().date();
39  const QDate dateValue = QDate::fromString(contents(), Qt::ISODate);
40  bool rc = matchesInternal(dateValue, msgDate);
41  if (FilterLog::instance()->isLogging()) {
42  QString msg = (rc ? QStringLiteral("<font color=#00FF00>1 = </font>") : QStringLiteral("<font color=#FF0000>0 = </font>"));
43  msg += FilterLog::recode(asString());
44  msg += QLatin1String(" ( <i>") + contents() + QLatin1String("</i> )"); // TODO change with locale?
46  }
47  return rc;
48 }
49 
50 bool SearchRuleDate::matchesInternal(QDate dateValue, QDate msgDate) const
51 {
52  switch (function()) {
53  case SearchRule::FuncEquals:
54  return dateValue == msgDate;
55 
56  case SearchRule::FuncNotEqual:
57  return dateValue != msgDate;
58 
59  case FuncIsGreater:
60  return msgDate > dateValue;
61 
62  case FuncIsLessOrEqual:
63  return msgDate <= dateValue;
64 
65  case FuncIsLess:
66  return msgDate < dateValue;
67 
68  case FuncIsGreaterOrEqual:
69  return msgDate >= dateValue;
70 
71  default:;
72  }
73  return false;
74 }
75 
76 SearchRule::RequiredPart SearchRuleDate::requiredPart() const
77 {
78  return SearchRule::Envelope;
79 }
80 
81 void SearchRuleDate::addQueryTerms(Akonadi::SearchTerm &groupTerm, bool &emptyIsNotAnError) const
82 {
83  using namespace Akonadi;
84  emptyIsNotAnError = false;
85 
86  const QDate date = QDate::fromString(contents(), Qt::ISODate);
87  EmailSearchTerm term(EmailSearchTerm::HeaderOnlyDate, date, akonadiComparator());
88  term.setIsNegated(isNegated());
89  groupTerm.addSubTerm(term);
90 }
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
@ RuleResult
Log all rule matching results.
Definition: filterlog.h:53
KMail Filter Log Collector.
Definition: filterlog.h:32
bool hasPayload() const
void addSubTerm(const SearchTerm &term)
QString i18n(const char *text, const TYPE &arg...)
RequiredPart
Possible required parts.
Definition: searchrule.h:68
static FilterLog * instance()
Returns the single global instance of the filter log.
Definition: filterlog.cpp:72
bool isValid() const const
This class represents one search pattern rule.
Definition: searchrule.h:23
QDate fromString(const QString &string, Qt::DateFormat format)
void add(const QString &entry, ContentType type)
Adds the given log entry under the given content type to the log.
Definition: filterlog.cpp:129
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.