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"
9#include <QVariant>
11
12using namespace MailCommon;
13
14struct _statusNames {
15 const char *name;
17};
18
19static struct _statusNames statusNames[] = {{"Important", Akonadi::MessageStatus::statusImportant()},
29 {"Action Item", Akonadi::MessageStatus::statusToAct()},
33
34QString englishNameForStatus(Akonadi::MessageStatus status)
35{
36 for (const _statusNames &statusName : statusNames) {
37 if (statusName.status == status) {
38 return QString::fromLatin1(statusName.name);
39 }
40 }
41 return {};
42}
43
44SearchRuleStatus::SearchRuleStatus(const QByteArray &field, Function func, const QString &aContents)
45 : SearchRule(field, func, aContents)
46{
47 // the values are always in english, both from the conf file as well as
48 // the patternedit gui
49 mStatus = statusFromEnglishName(aContents);
50}
51
52SearchRuleStatus::SearchRuleStatus(Akonadi::MessageStatus status, Function func)
53 : SearchRule("<status>", func, englishNameForStatus(status))
54{
55 mStatus = status;
56}
57
58Akonadi::MessageStatus SearchRuleStatus::statusFromEnglishName(const QString &aStatusString)
59{
60 for (const _statusNames &statusName : statusNames) {
61 if (!aStatusString.compare(QString::fromLatin1(statusName.name))) {
62 return statusName.status;
63 }
64 }
66 return unknown;
67}
68
69QString SearchRuleStatus::informationAboutNotValidRules() const
70{
71 // TODO
72 return {};
73}
74
76{
77 return field().trimmed().isEmpty() || contents().isEmpty();
78}
79
81{
84 bool rc = false;
85 switch (function()) {
86 case FuncEquals: // fallthrough. So that "<status> 'is' 'read'" works
87 case FuncContains:
88 if (status & mStatus) {
89 rc = true;
90 }
91 break;
92 case FuncNotEqual: // fallthrough. So that "<status> 'is not' 'read'" works
93 case FuncContainsNot:
94 if (!(status & mStatus)) {
95 rc = true;
96 }
97 break;
98 // FIXME what about the remaining funcs, how can they make sense for
99 // statuses?
100 default:
101 break;
102 }
103 if (FilterLog::instance()->isLogging()) {
104 QString msg = (rc ? QStringLiteral("<font color=#00FF00>1 = </font>") : QStringLiteral("<font color=#FF0000>0 = </font>"));
105 msg += FilterLog::recode(asString());
107 }
108 return rc;
109}
110
115
116void SearchRuleStatus::addQueryTerms(Akonadi::SearchTerm &groupTerm, bool &emptyIsNotAnError) const
117{
118 using namespace Akonadi;
119 emptyIsNotAnError = true;
120 // TODO double check that isRead also works
121 if (!mStatus.statusFlags().isEmpty()) {
122 EmailSearchTerm term(EmailSearchTerm::MessageStatus, mStatus.statusFlags().values().first(), akonadiComparator());
123 term.setIsNegated(isNegated());
124 groupTerm.addSubTerm(term);
125 } else {
126 // Special case Unread
128 status.setRead(true);
129 EmailSearchTerm term(EmailSearchTerm::MessageStatus, status.statusFlags().values().first(), akonadiComparator());
130 term.setIsNegated(!isNegated());
131 groupTerm.addSubTerm(term);
132 }
133}
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 Mon Nov 18 2024 12:09:01 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.