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 "filteractionremoveheader.h"
0009 
0010 #include <KComboBox>
0011 #include <KLocalizedString>
0012 #include <QLineEdit>
0013 
0014 using namespace MailCommon;
0015 
0016 FilterAction *FilterActionRemoveHeader::newAction()
0017 {
0018     return new FilterActionRemoveHeader;
0019 }
0020 
0021 FilterActionRemoveHeader::FilterActionRemoveHeader(QObject *parent)
0022     : FilterActionWithStringList(QStringLiteral("remove header"), i18n("Remove Header"), parent)
0023 {
0024     mParameterList << QString() << QStringLiteral("Reply-To") << QStringLiteral("Delivered-To") << QStringLiteral("X-KDE-PR-Message")
0025                    << QStringLiteral("X-KDE-PR-Package") << QStringLiteral("X-KDE-PR-Keywords");
0026 
0027     mParameter = mParameterList.at(0);
0028 }
0029 
0030 QWidget *FilterActionRemoveHeader::createParamWidget(QWidget *parent) const
0031 {
0032     auto comboBox = new KComboBox(parent);
0033     comboBox->setEditable(true);
0034     comboBox->setMinimumWidth(50);
0035     comboBox->setInsertPolicy(QComboBox::InsertAtBottom);
0036     setParamWidgetValue(comboBox);
0037     connect(comboBox, &KComboBox::currentIndexChanged, this, &FilterActionRemoveHeader::filterActionModified);
0038     connect(comboBox->lineEdit(), &QLineEdit::textChanged, this, &FilterAction::filterActionModified);
0039 
0040     return comboBox;
0041 }
0042 
0043 FilterAction::ReturnCode FilterActionRemoveHeader::process(ItemContext &context, bool) const
0044 {
0045     if (isEmpty()) {
0046         return ErrorButGoOn;
0047     }
0048 
0049     auto msg = context.item().payload<KMime::Message::Ptr>();
0050     const QByteArray param(mParameter.toLatin1());
0051     bool headerRemove = false;
0052     while (msg->removeHeader(param.constData())) {
0053         headerRemove = true;
0054     }
0055     if (headerRemove) {
0056         msg->assemble();
0057         context.setNeedsPayloadStore();
0058     }
0059 
0060     return GoOn;
0061 }
0062 
0063 SearchRule::RequiredPart FilterActionRemoveHeader::requiredPart() const
0064 {
0065     return SearchRule::CompleteMessage;
0066 }
0067 
0068 void FilterActionRemoveHeader::setParamWidgetValue(QWidget *paramWidget) const
0069 {
0070     const auto comboBox = qobject_cast<KComboBox *>(paramWidget);
0071     Q_ASSERT(comboBox);
0072 
0073     const int index = mParameterList.indexOf(mParameter);
0074     comboBox->clear();
0075     comboBox->addItems(mParameterList);
0076     if (index < 0) {
0077         comboBox->addItem(mParameter);
0078         comboBox->setCurrentIndex(comboBox->count() - 1);
0079     } else {
0080         comboBox->setCurrentIndex(index);
0081     }
0082 }
0083 
0084 QStringList FilterActionRemoveHeader::sieveRequires() const
0085 {
0086     return QStringList() << QStringLiteral("editheader");
0087 }
0088 
0089 QString FilterActionRemoveHeader::sieveCode() const
0090 {
0091     return QStringLiteral("deleteheader \"%1\";").arg(mParameter);
0092 }
0093 
0094 QString FilterActionRemoveHeader::informationAboutNotValidAction() const
0095 {
0096     return i18n("Header name undefined.");
0097 }
0098 
0099 #include "moc_filteractionremoveheader.cpp"