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 "filteractionsettransport.h"
0009 
0010 #include "filter/dialog/filteractionmissingtransportdialog.h"
0011 
0012 #include <KLocalizedString>
0013 #include <MailTransport/Transport>
0014 #include <MailTransport/TransportComboBox>
0015 #include <MailTransport/TransportManager>
0016 
0017 #include <QPointer>
0018 
0019 using namespace MailCommon;
0020 
0021 FilterAction *FilterActionSetTransport::newAction()
0022 {
0023     return new FilterActionSetTransport;
0024 }
0025 
0026 QWidget *FilterActionSetTransport::createParamWidget(QWidget *parent) const
0027 {
0028     auto transportCombobox = new MailTransport::TransportComboBox(parent);
0029     transportCombobox->setObjectName(QLatin1StringView("transportcombobox"));
0030     setParamWidgetValue(transportCombobox);
0031     connect(transportCombobox, &MailTransport::TransportComboBox::currentIndexChanged, this, &FilterActionSetTransport::filterActionModified);
0032     return transportCombobox;
0033 }
0034 
0035 FilterActionSetTransport::FilterActionSetTransport(QObject *parent)
0036     : FilterAction(QStringLiteral("set transport"), i18n("Set Transport To"), parent)
0037     , mParameter(-1)
0038 {
0039 }
0040 
0041 bool FilterActionSetTransport::argsFromStringInteractive(const QString &argsStr, const QString &filterName)
0042 {
0043     bool needUpdate = false;
0044     argsFromString(argsStr);
0045     if (!MailTransport::TransportManager::self()->transportById(mParameter, false)) {
0046         QPointer<MailCommon::FilterActionMissingTransportDialog> dlg = new MailCommon::FilterActionMissingTransportDialog(filterName);
0047         if (dlg->exec()) {
0048             mParameter = dlg->selectedTransport();
0049             needUpdate = true;
0050         } else {
0051             mParameter = -1;
0052         }
0053         delete dlg;
0054     }
0055     return needUpdate;
0056 }
0057 
0058 FilterAction::ReturnCode FilterActionSetTransport::process(ItemContext &context, bool) const
0059 {
0060     if (isEmpty()) {
0061         return ErrorButGoOn;
0062     }
0063 
0064     const MailTransport::Transport *transport = MailTransport::TransportManager::self()->transportById(mParameter);
0065     // Error if we don't have transport here.
0066     if (!transport) {
0067         return ErrorButGoOn;
0068     }
0069 
0070     const auto msg = context.item().payload<KMime::Message::Ptr>();
0071     auto header = new KMime::Headers::Generic("X-KMail-Transport");
0072     header->fromUnicodeString(argsAsString(), "utf-8");
0073     msg->setHeader(header);
0074     msg->assemble();
0075 
0076     context.setNeedsPayloadStore();
0077 
0078     return GoOn;
0079 }
0080 
0081 SearchRule::RequiredPart FilterActionSetTransport::requiredPart() const
0082 {
0083     return SearchRule::CompleteMessage;
0084 }
0085 
0086 void FilterActionSetTransport::applyParamWidgetValue(QWidget *paramWidget)
0087 {
0088     const MailTransport::TransportComboBox *comboBox = qobject_cast<MailTransport::TransportComboBox *>(paramWidget);
0089     Q_ASSERT(comboBox);
0090 
0091     mParameter = comboBox->currentTransportId();
0092     mTransportName.clear();
0093 }
0094 
0095 void FilterActionSetTransport::clearParamWidget(QWidget *paramWidget) const
0096 {
0097     auto comboBox = qobject_cast<MailTransport::TransportComboBox *>(paramWidget);
0098     Q_ASSERT(comboBox);
0099 
0100     comboBox->setCurrentIndex(0);
0101 }
0102 
0103 void FilterActionSetTransport::setParamWidgetValue(QWidget *paramWidget) const
0104 {
0105     auto comboBox = qobject_cast<MailTransport::TransportComboBox *>(paramWidget);
0106     Q_ASSERT(comboBox);
0107 
0108     comboBox->setCurrentTransport(mParameter);
0109 }
0110 
0111 bool FilterActionSetTransport::isEmpty() const
0112 {
0113     return mParameter == -1;
0114 }
0115 
0116 void FilterActionSetTransport::argsFromString(const QString &argsStr)
0117 {
0118     bool ok;
0119     const int val = argsStr.trimmed().toInt(&ok);
0120     mParameter = ok ? val : -1;
0121 }
0122 
0123 QString FilterActionSetTransport::argsAsString() const
0124 {
0125     return QString::number(mParameter);
0126 }
0127 
0128 QString FilterActionSetTransport::displayString() const
0129 {
0130     if (mTransportName.isEmpty()) {
0131         const QStringList listNames = MailTransport::TransportManager::self()->transportNames();
0132         const int index = MailTransport::TransportManager::self()->transportIds().indexOf(mParameter);
0133         if (index != -1) {
0134             mTransportName = listNames.at(index);
0135         }
0136     }
0137     return label() + QLatin1StringView(" \"") + (mTransportName.isEmpty() ? argsAsString().toHtmlEscaped() : mTransportName) + QLatin1StringView("\"");
0138 }
0139 
0140 QString FilterActionSetTransport::informationAboutNotValidAction() const
0141 {
0142     return i18n("Mail transport not defined.");
0143 }
0144 
0145 #include "moc_filteractionsettransport.cpp"