Mailcommon

searchrulestatus.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 "searchrulestatus.h"
8#include "filter/filterlog.h"
10
11using namespace MailCommon;
12
13struct _statusNames {
14 const char *name;
16};
17
18static struct _statusNames statusNames[] = {{"Important", Akonadi::MessageStatus::statusImportant()},
28 {"Action Item", Akonadi::MessageStatus::statusToAct()},
32
33QString englishNameForStatus(Akonadi::MessageStatus status)
34{
35 for (const _statusNames &statusName : statusNames) {
36 if (statusName.status == status) {
37 return QString::fromLatin1(statusName.name);
38 }
39 }
40 return {};
41}
42
43SearchRuleStatus::SearchRuleStatus(const QByteArray &field, Function func, const QString &aContents)
44 : SearchRule(field, func, aContents)
45{
46 // the values are always in english, both from the conf file as well as
47 // the patternedit gui
48 mStatus = statusFromEnglishName(aContents);
49}
50
51SearchRuleStatus::SearchRuleStatus(Akonadi::MessageStatus status, Function func)
52 : SearchRule("<status>", func, englishNameForStatus(status))
53{
54 mStatus = status;
55}
56
57Akonadi::MessageStatus SearchRuleStatus::statusFromEnglishName(const QString &aStatusString)
58{
59 for (const _statusNames &statusName : statusNames) {
60 if (!aStatusString.compare(QString::fromLatin1(statusName.name))) {
61 return statusName.status;
62 }
63 }
65 return unknown;
66}
67
68QString SearchRuleStatus::informationAboutNotValidRules() const
69{
70 // TODO
71 return {};
72}
73
75{
76 return field().trimmed().isEmpty() || contents().isEmpty();
77}
78
80{
83 bool rc = false;
84 switch (function()) {
85 case FuncEquals: // fallthrough. So that "<status> 'is' 'read'" works
86 case FuncContains:
87 if (status & mStatus) {
88 rc = true;
89 }
90 break;
91 case FuncNotEqual: // fallthrough. So that "<status> 'is not' 'read'" works
92 case FuncContainsNot:
93 if (!(status & mStatus)) {
94 rc = true;
95 }
96 break;
97 // FIXME what about the remaining funcs, how can they make sense for
98 // statuses?
99 default:
100 break;
101 }
102 if (FilterLog::instance()->isLogging()) {
103 QString msg = (rc ? QStringLiteral("<font color=#00FF00>1 = </font>") : QStringLiteral("<font color=#FF0000>0 = </font>"));
104 msg += FilterLog::recode(asString());
106 }
107 return rc;
108}
109
114
115void SearchRuleStatus::addQueryTerms(Akonadi::SearchTerm &groupTerm, bool &emptyIsNotAnError) const
116{
117 using namespace Akonadi;
118 emptyIsNotAnError = true;
119 // TODO double check that isRead also works
120 if (!mStatus.statusFlags().isEmpty()) {
121 EmailSearchTerm term(EmailSearchTerm::MessageStatus, mStatus.statusFlags().values().first(), akonadiComparator());
122 term.setIsNegated(isNegated());
123 groupTerm.addSubTerm(term);
124 } else {
125 // Special case Unread
127 status.setRead(true);
128 EmailSearchTerm term(EmailSearchTerm::MessageStatus, status.statusFlags().values().first(), akonadiComparator());
129 term.setIsNegated(!isNegated());
130 groupTerm.addSubTerm(term);
131 }
132}
Flags flags() const
static const MessageStatus statusRead()
static const MessageStatus statusSent()
static const MessageStatus statusUnread()
static const MessageStatus statusHasAttachment()
static const MessageStatus statusSpam()
void setRead(bool read=true)
static const MessageStatus statusDeleted()
static const MessageStatus statusReplied()
void setStatusFromFlags(const QSet< QByteArray > &flags)
static const MessageStatus statusImportant()
static const MessageStatus statusWatched()
static const MessageStatus statusForwarded()
static const MessageStatus statusToAct()
static const MessageStatus statusQueued()
static const MessageStatus statusIgnored()
static const MessageStatus statusHam()
QSet< QByteArray > statusFlags() 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.
RequiredPart requiredPart() const override
Returns the required part from the item that is needed for the search to operate.
void addQueryTerms(Akonadi::SearchTerm &groupTerm, bool &emptyIsNotAnError) const override
Adds query terms to the given term group.
bool isEmpty() const override
Determines whether the rule is worth considering.
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.
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.
Q_SCRIPTABLE CaptureState status()
The filter dialog.
bool isEmpty() const const
QByteArray trimmed() const const
int compare(QLatin1StringView s1, const QString &s2, Qt::CaseSensitivity cs)
QString fromLatin1(QByteArrayView str)
bool isEmpty() const const
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.