Mailcommon

searchruledate.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 "searchruledate.h"
8
9#include "filter/filterlog.h"
11
12#include <KLocalizedString>
13#include <KMime/KMimeMessage>
14using namespace MailCommon;
15
16SearchRuleDate::SearchRuleDate(const QByteArray &field, Function func, const QString &contents)
17 : SearchRule(field, func, contents)
18{
19}
20
21QString SearchRuleDate::informationAboutNotValidRules() const
22{
23 return i18n("Date is not valid.");
24}
25
26bool SearchRuleDate::isEmpty() const
27{
29}
30
31bool 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>"));
44 msg += QLatin1StringView(" ( <i>") + contents() + QLatin1StringView("</i> )"); // TODO change with locale?
46 }
47 return rc;
48}
49
50bool 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
76SearchRule::RequiredPart SearchRuleDate::requiredPart() const
77{
79}
80
81void SearchRuleDate::addQueryTerms(Akonadi::SearchTerm &groupTerm, bool &emptyIsNotAnError) const
82{
83 using namespace Akonadi;
84 emptyIsNotAnError = false;
85
87 EmailSearchTerm term(EmailSearchTerm::HeaderOnlyDate, date, akonadiComparator());
88 term.setIsNegated(isNegated());
89 groupTerm.addSubTerm(term);
90}
bool hasPayload() const
T payload() const
void addSubTerm(const SearchTerm &term)
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
This class represents one search pattern rule.
Definition searchrule.h:24
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.
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 fromString(QStringView string, QStringView format, QCalendar cal)
bool isValid(int year, int month, int day)
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.