Mailcommon

filteractionunsetstatus.cpp
1 /*
2 
3  SPDX-FileCopyrightText: 2012-2022 Laurent Montel <[email protected]>
4 
5  SPDX-License-Identifier: GPL-2.0-or-later
6 */
7 
8 #include "filteractionunsetstatus.h"
9 #include "mailcommon_debug.h"
10 #include <KLocalizedString>
11 using namespace MailCommon;
12 
13 FilterAction *FilterActionUnsetStatus::newAction()
14 {
15  return new FilterActionUnsetStatus;
16 }
17 
18 FilterActionUnsetStatus::FilterActionUnsetStatus(QObject *parent)
19  : FilterActionStatus(QStringLiteral("unset status"), i18nc("action: to unset the status", "Unset Status"), parent)
20 {
21 }
22 
23 bool FilterActionUnsetStatus::checkIsValid(int &index) const
24 {
25  index = mParameterList.indexOf(mParameter);
26  return index < 1;
27 }
28 
29 bool FilterActionUnsetStatus::isEmpty() const
30 {
31  int index = 0;
32  return checkIsValid(index);
33 }
34 
35 QString FilterActionUnsetStatus::informationAboutNotValidAction() const
36 {
37  return i18n("Status not defined.");
38 }
39 
40 FilterAction::ReturnCode FilterActionUnsetStatus::process(ItemContext &context, bool) const
41 {
42  int index = 0;
43  if (checkIsValid(index)) {
44  return ErrorButGoOn;
45  }
46 
48  status.setStatusFromFlags(context.item().flags());
49 
50  Akonadi::MessageStatus newStatus = FilterActionStatus::statuses[index - 1];
51  // Laurent unread status doesn't exist. And crash if we use "const Akonadi::Item::Flag flag = *(flags.begin());" etc. => if we unset "unread" we set read
52  if (newStatus == Akonadi::MessageStatus::statusUnread()) {
53  Akonadi::MessageStatus oldStatus = status;
54  newStatus.setRead(true);
55  if (oldStatus != newStatus) {
56  context.item().setFlags(newStatus.statusFlags());
57  context.setNeedsFlagStore();
58  }
59  return GoOn;
60  }
61 
62  QSet<QByteArray> flags = newStatus.statusFlags();
63  const Akonadi::Item::Flag flag = *(flags.begin());
64  if (context.item().hasFlag(flag)) {
65  context.item().clearFlag(flag);
66  context.setNeedsFlagStore();
67  }
68  return GoOn;
69 }
70 
71 SearchRule::RequiredPart FilterActionUnsetStatus::requiredPart() const
72 {
73  return SearchRule::Envelope;
74 }
75 
76 QString FilterActionUnsetStatus::sieveCode() const
77 {
78  QString flagCode;
79  const QString parameter = argsAsString();
80  if (parameter == QLatin1Char('R')) {
81  flagCode = QStringLiteral("\\\\Seen");
82  } else if (parameter == QLatin1Char('D')) {
83  flagCode = QStringLiteral("\\\\Deleted");
84  } else if (parameter == QLatin1Char('A')) {
85  flagCode = QStringLiteral("\\\\Answered");
86  } else {
87  qCDebug(MAILCOMMON_LOG) << " FilterActionSetStatus::sieveCode() unknown flags" << parameter;
88  flagCode = parameter;
89  }
90 
91  const QString result = QStringLiteral("removeflag \"%1\";").arg(flagCode);
92  return result;
93 }
94 
95 QStringList FilterActionUnsetStatus::sieveRequires() const
96 {
97  return QStringList() << QStringLiteral("imap4flags");
98 }
QSet< QByteArray > statusFlags() const
bool hasFlag(const QByteArray &name) const
Flags flags() const
void setRead(bool read=true)
static const MessageStatus statusUnread()
Abstract base class for mail filter actions.
Definition: filteraction.h:38
QString i18n(const char *text, const TYPE &arg...)
void setNeedsFlagStore()
Marks that the item's flags has been changed and needs to be written back.
Definition: itemcontext.cpp:43
RequiredPart
Possible required parts.
Definition: searchrule.h:68
NETWORKMANAGERQT_EXPORT NetworkManager::Status status()
QSet::iterator begin()
A helper class for the filtering process.
Definition: itemcontext.h:26
ReturnCode
Describes the possible return codes of filter processing:
Definition: filteraction.h:45
QString arg(qlonglong a, int fieldWidth, int base, QChar fillChar) const const
QString i18nc(const char *context, const char *text, const TYPE &arg...)
void setFlags(const Flags &flags)
void clearFlag(const QByteArray &name)
Akonadi::Item & item()
Returns the item of the context.
Definition: itemcontext.cpp:18
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:52 by doxygen 1.8.17 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.