Mailcommon

searchrulestatus.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 "searchrulestatus.h"
8 #include "filter/filterlog.h"
10 
11 using namespace MailCommon;
12 
13 struct _statusNames {
14  const char *name;
16 };
17 
18 static struct _statusNames statusNames[] = {{"Important", Akonadi::MessageStatus::statusImportant()},
28  {"Action Item", Akonadi::MessageStatus::statusToAct()},
31  {"Has Attachment", Akonadi::MessageStatus::statusHasAttachment()}};
32 
33 QString 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 
43 SearchRuleStatus::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 
51 SearchRuleStatus::SearchRuleStatus(Akonadi::MessageStatus status, Function func)
52  : SearchRule("<status>", func, englishNameForStatus(status))
53 {
54  mStatus = status;
55 }
56 
57 Akonadi::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  }
64  Akonadi::MessageStatus unknown;
65  return unknown;
66 }
67 
68 QString SearchRuleStatus::informationAboutNotValidRules() const
69 {
70  // TODO
71  return {};
72 }
73 
75 {
76  return field().trimmed().isEmpty() || contents().isEmpty();
77 }
78 
80 {
82  status.setStatusFromFlags(item.flags());
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 
111 {
112  return SearchRule::Envelope;
113 }
114 
115 void 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
126  Akonadi::MessageStatus status;
127  status.setRead(true);
128  EmailSearchTerm term(EmailSearchTerm::MessageStatus, status.statusFlags().values().first(), akonadiComparator());
129  term.setIsNegated(!isNegated());
130  groupTerm.addSubTerm(term);
131  }
132 }
static const MessageStatus statusImportant()
QString contents() const
Returns the contents of the rule.
Definition: searchrule.cpp:508
bool isNegated() const
Helper that returns whether the rule has a negated function.
Definition: searchrule.cpp:563
Flags flags() const
static const MessageStatus statusToAct()
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
static const MessageStatus statusWatched()
@ RuleResult
Log all rule matching results.
Definition: filterlog.h:53
static const MessageStatus statusSpam()
void setIsNegated(bool negated)
QByteArray trimmed() const const
static const MessageStatus statusUnread()
bool matches(const Akonadi::Item &item) const override
Tries to match the rule against the KMime::Message in the given item.
static const MessageStatus statusQueued()
bool isEmpty() const override
Determines whether the rule is worth considering.
KMail Filter Log Collector.
Definition: filterlog.h:32
Akonadi::SearchTerm::Condition akonadiComparator() const
Converts the rule function into the corresponding Akonadi query operator.
Definition: searchrule.cpp:522
static const MessageStatus statusForwarded()
void addSubTerm(const SearchTerm &term)
RequiredPart
Possible required parts.
Definition: searchrule.h:68
bool isEmpty() const const
NETWORKMANAGERQT_EXPORT NetworkManager::Status status()
static FilterLog * instance()
Returns the single global instance of the filter log.
Definition: filterlog.cpp:72
void addQueryTerms(Akonadi::SearchTerm &groupTerm, bool &emptyIsNotAnError) const override
Adds query terms to the given term group.
static const MessageStatus statusIgnored()
This class represents one search pattern rule.
Definition: searchrule.h:23
RequiredPart requiredPart() const override
Returns the required part from the item that is needed for the search to operate.
bool isEmpty() const const
QString fromLatin1(const char *str, int size)
QString name(StandardShortcut id)
static const MessageStatus statusReplied()
void add(const QString &entry, ContentType type)
Adds the given log entry under the given content type to the log.
Definition: filterlog.cpp:129
static const MessageStatus statusRead()
static const MessageStatus statusHam()
int compare(const QString &other, Qt::CaseSensitivity cs) const const
static const MessageStatus statusHasAttachment()
static const MessageStatus statusDeleted()
const QString asString() const
Returns the rule as string for debugging purpose.
Definition: searchrule.cpp:513
static const MessageStatus statusSent()
QByteArray field() const
Returns the message header field name (without the trailing ':').
Definition: searchrule.cpp:498
The filter dialog.
This file is part of the KDE documentation.
Documentation copyright © 1996-2022 The KDE developers.
Generated on Sat Oct 1 2022 04:00:53 by doxygen 1.8.17 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.