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 "filteractionsendfakedisposition.h"
0009 
0010 #include <Akonadi/MDNStateAttribute>
0011 #include <KLocalizedString>
0012 
0013 using namespace MailCommon;
0014 
0015 // if you change this list, also update
0016 // the count in argsFromString
0017 static const KMime::MDN::DispositionType mdns[] = {
0018     KMime::MDN::Displayed,
0019     KMime::MDN::Deleted,
0020     KMime::MDN::Dispatched,
0021     KMime::MDN::Processed,
0022     KMime::MDN::Denied,
0023     KMime::MDN::Failed,
0024 };
0025 static const int numMDNs = sizeof(mdns) / sizeof(*mdns);
0026 
0027 FilterActionSendFakeDisposition::FilterActionSendFakeDisposition(QObject *parent)
0028     : FilterActionWithStringList(QStringLiteral("fake mdn"), i18n("Send Fake MDN"), parent)
0029 {
0030     // if you change this list, also update
0031     // mdns above
0032     mParameterList.append(QString());
0033     mParameterList.append(i18nc("MDN type", "Ignore"));
0034     mParameterList.append(i18nc("MDN type", "Displayed"));
0035     mParameterList.append(i18nc("MDN type", "Deleted"));
0036     mParameterList.append(i18nc("MDN type", "Dispatched"));
0037     mParameterList.append(i18nc("MDN type", "Processed"));
0038     mParameterList.append(i18nc("MDN type", "Denied"));
0039     mParameterList.append(i18nc("MDN type", "Failed"));
0040 
0041     mParameter = mParameterList.at(0);
0042 }
0043 
0044 FilterAction *FilterActionSendFakeDisposition::newAction()
0045 {
0046     return new FilterActionSendFakeDisposition;
0047 }
0048 
0049 bool FilterActionSendFakeDisposition::isEmpty() const
0050 {
0051     const int index = mParameterList.indexOf(mParameter);
0052     return index < 1;
0053 }
0054 
0055 FilterAction::ReturnCode FilterActionSendFakeDisposition::process(ItemContext &context, bool) const
0056 {
0057     const int index = mParameterList.indexOf(mParameter);
0058     if (index < 1) {
0059         return ErrorButGoOn;
0060     }
0061 
0062     if (index == 1) { // ignore
0063         if (context.item().hasAttribute<Akonadi::MDNStateAttribute>()) {
0064             context.item().attribute<Akonadi::MDNStateAttribute>()->setMDNState(Akonadi::MDNStateAttribute::MDNIgnore);
0065             context.setNeedsFlagStore();
0066         }
0067     } else { // send
0068         sendMDN(context.item(), mdns[index - 2]); // skip first two entries: "" and "ignore"
0069     }
0070 
0071     return GoOn;
0072 }
0073 
0074 SearchRule::RequiredPart FilterActionSendFakeDisposition::requiredPart() const
0075 {
0076     return SearchRule::CompleteMessage;
0077 }
0078 
0079 void FilterActionSendFakeDisposition::argsFromString(const QString &argsStr)
0080 {
0081     if (argsStr.length() == 1) {
0082         if (argsStr[0] == QLatin1Char('I')) { // ignore
0083             mParameter = mParameterList.at(1);
0084             return;
0085         }
0086 
0087         for (int i = 0; i < numMDNs; ++i) {
0088             if (char(mdns[i]) == argsStr[0]) { // send
0089                 mParameter = mParameterList.at(i + 2);
0090                 return;
0091             }
0092         }
0093     }
0094 
0095     mParameter = mParameterList.at(0);
0096 }
0097 
0098 QString FilterActionSendFakeDisposition::argsAsString() const
0099 {
0100     const int index = mParameterList.indexOf(mParameter);
0101     if (index < 1) {
0102         return {};
0103     }
0104 
0105     return {QChar(index < 2 ? 'I' : char(mdns[index - 2]))};
0106 }
0107 
0108 QString FilterActionSendFakeDisposition::displayString() const
0109 {
0110     return label() + QLatin1StringView(" \"") + mParameter + QLatin1StringView("\"");
0111 }
0112 
0113 QString FilterActionSendFakeDisposition::informationAboutNotValidAction() const
0114 {
0115     return i18n("Fake type undefined.");
0116 }
0117 
0118 #include "moc_filteractionsendfakedisposition.cpp"