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

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 "filteractionaddheader.h"
0009 
0010 #include <KComboBox>
0011 #include <KLineEdit>
0012 #include <KLocalizedString>
0013 
0014 #include <QComboBox>
0015 #include <QHBoxLayout>
0016 #include <QLabel>
0017 
0018 using namespace MailCommon;
0019 
0020 FilterActionAddHeader::FilterActionAddHeader(QObject *parent)
0021     : FilterActionWithStringList(QStringLiteral("add header"), i18n("Add Header"), parent)
0022 {
0023     mParameterList << QString() << QStringLiteral("Reply-To") << QStringLiteral("Delivered-To") << QStringLiteral("X-KDE-PR-Message")
0024                    << QStringLiteral("X-KDE-PR-Package") << QStringLiteral("X-KDE-PR-Keywords");
0025 
0026     mParameter = mParameterList.at(0);
0027 }
0028 
0029 bool FilterActionAddHeader::isEmpty() const
0030 {
0031     return mParameter.isEmpty() || mValue.isEmpty();
0032 }
0033 
0034 FilterAction::ReturnCode FilterActionAddHeader::process(ItemContext &context, bool) const
0035 {
0036     if (isEmpty()) {
0037         return ErrorButGoOn;
0038     }
0039 
0040     auto msg = context.item().payload<KMime::Message::Ptr>();
0041 
0042     KMime::Headers::Base *header = KMime::Headers::createHeader(mParameter.toLatin1());
0043     if (!header) {
0044         header = new KMime::Headers::Generic(mParameter.toLatin1().constData());
0045     }
0046     header->fromUnicodeString(mValue, "utf-8");
0047 
0048     msg->setHeader(header);
0049     msg->assemble();
0050 
0051     context.setNeedsPayloadStore();
0052 
0053     return GoOn;
0054 }
0055 
0056 QWidget *FilterActionAddHeader::createParamWidget(QWidget *parent) const
0057 {
0058     auto widget = new QWidget(parent);
0059     auto layout = new QHBoxLayout(widget);
0060     layout->setSpacing(4);
0061     layout->setContentsMargins({});
0062 
0063     auto comboBox = new KComboBox(widget);
0064     comboBox->setMinimumWidth(50);
0065     comboBox->setObjectName(QLatin1StringView("combo"));
0066     comboBox->setEditable(true);
0067     comboBox->setInsertPolicy(QComboBox::InsertAtBottom);
0068 
0069     KCompletion *comp = comboBox->completionObject();
0070     comp->setIgnoreCase(true);
0071     comp->insertItems(mParameterList);
0072     comp->setCompletionMode(KCompletion::CompletionPopupAuto);
0073 
0074     layout->addWidget(comboBox, 0 /* stretch */);
0075 
0076     auto label = new QLabel(i18n("With value:"), widget);
0077     label->setObjectName(QLatin1StringView("label_value"));
0078     label->setFixedWidth(label->sizeHint().width());
0079     layout->addWidget(label, 0);
0080 
0081     auto lineEdit = new KLineEdit(widget);
0082     lineEdit->setObjectName(QLatin1StringView("ledit"));
0083     lineEdit->setTrapReturnKey(true);
0084     lineEdit->setClearButtonEnabled(true);
0085     layout->addWidget(lineEdit, 1);
0086 
0087     setParamWidgetValue(widget);
0088     connect(comboBox, &KComboBox::currentIndexChanged, this, &FilterActionAddHeader::filterActionModified);
0089     connect(comboBox->lineEdit(), &QLineEdit::textChanged, this, &FilterAction::filterActionModified);
0090     connect(lineEdit, &QLineEdit::textChanged, this, &FilterActionAddHeader::filterActionModified);
0091 
0092     return widget;
0093 }
0094 
0095 void FilterActionAddHeader::setParamWidgetValue(QWidget *paramWidget) const
0096 {
0097     const int index = mParameterList.indexOf(mParameter);
0098 
0099     const auto comboBox = paramWidget->findChild<KComboBox *>(QStringLiteral("combo"));
0100     Q_ASSERT(comboBox);
0101     comboBox->clear();
0102     comboBox->addItems(mParameterList);
0103 
0104     if (index < 0) {
0105         comboBox->addItem(mParameter);
0106         comboBox->setCurrentIndex(comboBox->count() - 1);
0107     } else {
0108         comboBox->setCurrentIndex(index);
0109     }
0110 
0111     auto lineEdit = paramWidget->findChild<QLineEdit *>(QStringLiteral("ledit"));
0112     Q_ASSERT(lineEdit);
0113 
0114     lineEdit->setText(mValue);
0115 }
0116 
0117 void FilterActionAddHeader::applyParamWidgetValue(QWidget *paramWidget)
0118 {
0119     const auto comboBox = paramWidget->findChild<KComboBox *>(QStringLiteral("combo"));
0120     Q_ASSERT(comboBox);
0121     mParameter = comboBox->currentText();
0122 
0123     const QLineEdit *lineEdit = paramWidget->findChild<QLineEdit *>(QStringLiteral("ledit"));
0124     Q_ASSERT(lineEdit);
0125     mValue = lineEdit->text();
0126 }
0127 
0128 void FilterActionAddHeader::clearParamWidget(QWidget *paramWidget) const
0129 {
0130     const auto comboBox = paramWidget->findChild<KComboBox *>(QStringLiteral("combo"));
0131     Q_ASSERT(comboBox);
0132     comboBox->setCurrentIndex(0);
0133 
0134     auto lineEdit = paramWidget->findChild<QLineEdit *>(QStringLiteral("ledit"));
0135     Q_ASSERT(lineEdit);
0136     lineEdit->clear();
0137 }
0138 
0139 SearchRule::RequiredPart FilterActionAddHeader::requiredPart() const
0140 {
0141     return SearchRule::CompleteMessage;
0142 }
0143 
0144 QString FilterActionAddHeader::argsAsString() const
0145 {
0146     QString result = mParameter;
0147     result += QLatin1Char('\t');
0148     result += mValue;
0149 
0150     return result;
0151 }
0152 
0153 QString FilterActionAddHeader::displayString() const
0154 {
0155     return label() + QStringLiteral(" \"") + argsAsString().toHtmlEscaped() + QStringLiteral("\"");
0156 }
0157 
0158 void FilterActionAddHeader::argsFromString(const QString &argsStr)
0159 {
0160     const QStringList list = argsStr.split(QLatin1Char('\t'));
0161     QString result;
0162     if (list.count() < 2) {
0163         result = list[0];
0164         mValue.clear();
0165     } else {
0166         result = list[0];
0167         mValue = list[1];
0168     }
0169 
0170     int index = mParameterList.indexOf(result);
0171     if (index < 0) {
0172         mParameterList.append(result);
0173         index = mParameterList.count() - 1;
0174     }
0175 
0176     mParameter = mParameterList.at(index);
0177 }
0178 
0179 FilterAction *FilterActionAddHeader::newAction()
0180 {
0181     return new FilterActionAddHeader;
0182 }
0183 
0184 QStringList FilterActionAddHeader::sieveRequires() const
0185 {
0186     return QStringList() << QStringLiteral("editheader");
0187 }
0188 
0189 QString FilterActionAddHeader::sieveCode() const
0190 {
0191     if (isEmpty()) {
0192         return QStringLiteral("# invalid filter. Need to fix it by hand");
0193     } else {
0194         return QStringLiteral("addheader \"%1\" \"%2\";").arg(mParameter, mValue);
0195     }
0196 }
0197 
0198 QString FilterActionAddHeader::informationAboutNotValidAction() const
0199 {
0200     QString result;
0201     if (mParameter.isEmpty()) {
0202         result = i18n("The header name was missing.");
0203     }
0204     if (mValue.isEmpty()) {
0205         if (result.isEmpty()) {
0206             result += QLatin1Char('\n');
0207         }
0208         result += i18n("The header value was missing.");
0209     }
0210     if (!result.isEmpty()) {
0211         result = name() + QLatin1Char('\n') + result;
0212     }
0213     return result;
0214 }
0215 
0216 #include "moc_filteractionaddheader.cpp"