Mailcommon

filteractionremoveheader.cpp
1 /*
2  * SPDX-FileCopyrightText: 1996-1998 Stefan Taferner <[email protected]>
3  *
4  * SPDX-License-Identifier: GPL-2.0-or-later
5  *
6  */
7 
8 #include "filteractionremoveheader.h"
9 
10 #include <KComboBox>
11 #include <KLocalizedString>
12 #include <QLineEdit>
13 
14 using namespace MailCommon;
15 
16 FilterAction *FilterActionRemoveHeader::newAction()
17 {
18  return new FilterActionRemoveHeader;
19 }
20 
21 FilterActionRemoveHeader::FilterActionRemoveHeader(QObject *parent)
22  : FilterActionWithStringList(QStringLiteral("remove header"), i18n("Remove Header"), parent)
23 {
24  mParameterList << QString() << QStringLiteral("Reply-To") << QStringLiteral("Delivered-To") << QStringLiteral("X-KDE-PR-Message")
25  << QStringLiteral("X-KDE-PR-Package") << QStringLiteral("X-KDE-PR-Keywords");
26 
27  mParameter = mParameterList.at(0);
28 }
29 
30 QWidget *FilterActionRemoveHeader::createParamWidget(QWidget *parent) const
31 {
32  auto comboBox = new KComboBox(parent);
33  comboBox->setEditable(true);
34  comboBox->setMinimumWidth(50);
35  comboBox->setInsertPolicy(QComboBox::InsertAtBottom);
36  setParamWidgetValue(comboBox);
37  connect(comboBox, &KComboBox::currentIndexChanged, this, &FilterActionRemoveHeader::filterActionModified);
38  connect(comboBox->lineEdit(), &QLineEdit::textChanged, this, &FilterAction::filterActionModified);
39 
40  return comboBox;
41 }
42 
43 FilterAction::ReturnCode FilterActionRemoveHeader::process(ItemContext &context, bool) const
44 {
45  if (isEmpty()) {
46  return ErrorButGoOn;
47  }
48 
49  auto msg = context.item().payload<KMime::Message::Ptr>();
50  const QByteArray param(mParameter.toLatin1());
51  bool headerRemove = false;
52  while (msg->removeHeader(param.constData())) {
53  headerRemove = true;
54  }
55  if (headerRemove) {
56  msg->assemble();
57  context.setNeedsPayloadStore();
58  }
59 
60  return GoOn;
61 }
62 
63 SearchRule::RequiredPart FilterActionRemoveHeader::requiredPart() const
64 {
66 }
67 
68 void FilterActionRemoveHeader::setParamWidgetValue(QWidget *paramWidget) const
69 {
70  const auto comboBox = qobject_cast<KComboBox *>(paramWidget);
71  Q_ASSERT(comboBox);
72 
73  const int index = mParameterList.indexOf(mParameter);
74  comboBox->clear();
75  comboBox->addItems(mParameterList);
76  if (index < 0) {
77  comboBox->addItem(mParameter);
78  comboBox->setCurrentIndex(comboBox->count() - 1);
79  } else {
80  comboBox->setCurrentIndex(index);
81  }
82 }
83 
84 QStringList FilterActionRemoveHeader::sieveRequires() const
85 {
86  return QStringList() << QStringLiteral("editheader");
87 }
88 
89 QString FilterActionRemoveHeader::sieveCode() const
90 {
91  return QStringLiteral("deleteheader \"%1\";").arg(mParameter);
92 }
93 
94 QString FilterActionRemoveHeader::informationAboutNotValidAction() const
95 {
96  return i18n("Header name undefined.");
97 }
98 
99 #include "moc_filteractionremoveheader.cpp"
Abstract base class for filter actions with a fixed set of string parameters.
Abstract base class for mail filter actions.
Definition: filteraction.h:38
QString i18n(const char *text, const TYPE &arg...)
RequiredPart
Possible required parts.
Definition: searchrule.h:68
void textChanged(const QString &text)
void filterActionModified()
Called to notify that the current FilterAction has had some value modification.
void setNeedsPayloadStore()
Marks that the item's payload has been changed and needs to be written back.
Definition: itemcontext.cpp:33
A helper class for the filtering process.
Definition: itemcontext.h:26
ReturnCode
Describes the possible return codes of filter processing:
Definition: filteraction.h:45
QString arg(qlonglong a, int fieldWidth, int base, QChar fillChar) const const
@ CompleteMessage
Whole message.
Definition: searchrule.h:71
void currentIndexChanged(int index)
T payload() const
Akonadi::Item & item()
Returns the item of the context.
Definition: itemcontext.cpp:18
The filter dialog.
This file is part of the KDE documentation.
Documentation copyright © 1996-2023 The KDE developers.
Generated on Wed Dec 6 2023 04:03:01 by doxygen 1.8.17 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.