File indexing completed on 2024-11-10 04:50:04

0001 /*
0002  * SPDX-FileCopyrightText: 1996-1998 Stefan Taferner <taferner@kde.org>
0003  *
0004  * SPDX-License-Identifier: GPL-2.0-or-later
0005  *
0006  */
0007 
0008 #include "filteractionsetstatus.h"
0009 #include "mailcommon_debug.h"
0010 #include <Akonadi/MessageStatus>
0011 #include <KLocalizedString>
0012 using namespace MailCommon;
0013 
0014 FilterAction *FilterActionSetStatus::newAction()
0015 {
0016     return new FilterActionSetStatus;
0017 }
0018 
0019 FilterActionSetStatus::FilterActionSetStatus(QObject *parent)
0020     : FilterActionStatus(QStringLiteral("set status"), i18n("Mark As"), parent)
0021 {
0022 }
0023 
0024 bool FilterActionSetStatus::isEmpty() const
0025 {
0026     const int index = mParameterList.indexOf(mParameter);
0027     return index < 1;
0028 }
0029 
0030 QString FilterActionSetStatus::informationAboutNotValidAction() const
0031 {
0032     return i18n("Status not specified.");
0033 }
0034 
0035 FilterAction::ReturnCode FilterActionSetStatus::process(ItemContext &context, bool) const
0036 {
0037     const int index = mParameterList.indexOf(mParameter);
0038     if (index < 1) {
0039         return ErrorButGoOn;
0040     }
0041 
0042     Akonadi::MessageStatus status;
0043     status.setStatusFromFlags(context.item().flags());
0044 
0045     Akonadi::MessageStatus oldStatus = status;
0046     const Akonadi::MessageStatus newStatus = FilterActionStatus::statuses[index - 1];
0047     if (newStatus == Akonadi::MessageStatus::statusUnread()) {
0048         status.setRead(false);
0049     } else {
0050         status.set(newStatus);
0051     }
0052     if (oldStatus != status) {
0053         context.item().setFlags(status.statusFlags());
0054         context.setNeedsFlagStore();
0055     }
0056 
0057     return GoOn;
0058 }
0059 
0060 SearchRule::RequiredPart FilterActionSetStatus::requiredPart() const
0061 {
0062     return SearchRule::Envelope;
0063 }
0064 
0065 QString FilterActionSetStatus::sieveCode() const
0066 {
0067     QString flagCode;
0068     const QString parameter = argsAsString();
0069     if (parameter == QLatin1Char('R')) {
0070         flagCode = QStringLiteral("\\\\Seen");
0071     } else if (parameter == QLatin1Char('D')) {
0072         flagCode = QStringLiteral("\\\\Deleted");
0073     } else if (parameter == QLatin1Char('A')) {
0074         flagCode = QStringLiteral("\\\\Answered");
0075     } else {
0076         qCDebug(MAILCOMMON_LOG) << " FilterActionSetStatus::sieveCode() unknown flags" << parameter;
0077         flagCode = parameter;
0078     }
0079 
0080     const QString result = QStringLiteral("setflag \"%1\";").arg(flagCode);
0081     return result;
0082 }
0083 
0084 QStringList FilterActionSetStatus::sieveRequires() const
0085 {
0086     return QStringList() << QStringLiteral("imap4flags");
0087 }
0088 
0089 #include "moc_filteractionsetstatus.cpp"