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

0001 /*
0002 
0003   SPDX-FileCopyrightText: 2012-2024 Laurent Montel <montel@kde.org>
0004 
0005   SPDX-License-Identifier: GPL-2.0-or-later
0006 */
0007 
0008 #include "filteractionunsetstatus.h"
0009 #include "mailcommon_debug.h"
0010 #include <KLocalizedString>
0011 using namespace MailCommon;
0012 
0013 FilterAction *FilterActionUnsetStatus::newAction()
0014 {
0015     return new FilterActionUnsetStatus;
0016 }
0017 
0018 FilterActionUnsetStatus::FilterActionUnsetStatus(QObject *parent)
0019     : FilterActionStatus(QStringLiteral("unset status"), i18nc("action: to unset the status", "Unset Status"), parent)
0020 {
0021 }
0022 
0023 bool FilterActionUnsetStatus::checkIsValid(int &index) const
0024 {
0025     index = mParameterList.indexOf(mParameter);
0026     return index < 1;
0027 }
0028 
0029 bool FilterActionUnsetStatus::isEmpty() const
0030 {
0031     int index = 0;
0032     return checkIsValid(index);
0033 }
0034 
0035 QString FilterActionUnsetStatus::informationAboutNotValidAction() const
0036 {
0037     return i18n("Status not defined.");
0038 }
0039 
0040 FilterAction::ReturnCode FilterActionUnsetStatus::process(ItemContext &context, bool) const
0041 {
0042     int index = 0;
0043     if (checkIsValid(index)) {
0044         return ErrorButGoOn;
0045     }
0046 
0047     Akonadi::MessageStatus status;
0048     status.setStatusFromFlags(context.item().flags());
0049 
0050     Akonadi::MessageStatus newStatus = FilterActionStatus::statuses[index - 1];
0051     // 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
0052     if (newStatus == Akonadi::MessageStatus::statusUnread()) {
0053         Akonadi::MessageStatus oldStatus = status;
0054         newStatus.setRead(true);
0055         if (oldStatus != newStatus) {
0056             context.item().setFlags(newStatus.statusFlags());
0057             context.setNeedsFlagStore();
0058         }
0059         return GoOn;
0060     }
0061 
0062     QSet<QByteArray> flags = newStatus.statusFlags();
0063     const Akonadi::Item::Flag flag = *(flags.begin());
0064     if (context.item().hasFlag(flag)) {
0065         context.item().clearFlag(flag);
0066         context.setNeedsFlagStore();
0067     }
0068     return GoOn;
0069 }
0070 
0071 SearchRule::RequiredPart FilterActionUnsetStatus::requiredPart() const
0072 {
0073     return SearchRule::Envelope;
0074 }
0075 
0076 QString FilterActionUnsetStatus::sieveCode() const
0077 {
0078     QString flagCode;
0079     const QString parameter = argsAsString();
0080     if (parameter == QLatin1Char('R')) {
0081         flagCode = QStringLiteral("\\\\Seen");
0082     } else if (parameter == QLatin1Char('D')) {
0083         flagCode = QStringLiteral("\\\\Deleted");
0084     } else if (parameter == QLatin1Char('A')) {
0085         flagCode = QStringLiteral("\\\\Answered");
0086     } else {
0087         qCDebug(MAILCOMMON_LOG) << " FilterActionSetStatus::sieveCode() unknown flags" << parameter;
0088         flagCode = parameter;
0089     }
0090 
0091     const QString result = QStringLiteral("removeflag \"%1\";").arg(flagCode);
0092     return result;
0093 }
0094 
0095 QStringList FilterActionUnsetStatus::sieveRequires() const
0096 {
0097     return QStringList() << QStringLiteral("imap4flags");
0098 }
0099 
0100 #include "moc_filteractionunsetstatus.cpp"