Mailcommon

filteractionaddheader.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 "filteractionaddheader.h"
9 
10 #include <KComboBox>
11 #include <KLineEdit>
12 #include <KLocalizedString>
13 
14 #include <QComboBox>
15 #include <QHBoxLayout>
16 #include <QLabel>
17 
18 using namespace MailCommon;
19 
20 FilterActionAddHeader::FilterActionAddHeader(QObject *parent)
21  : FilterActionWithStringList(QStringLiteral("add header"), i18n("Add Header"), parent)
22 {
23  mParameterList << QString() << QStringLiteral("Reply-To") << QStringLiteral("Delivered-To") << QStringLiteral("X-KDE-PR-Message")
24  << QStringLiteral("X-KDE-PR-Package") << QStringLiteral("X-KDE-PR-Keywords");
25 
26  mParameter = mParameterList.at(0);
27 }
28 
29 bool FilterActionAddHeader::isEmpty() const
30 {
31  return mParameter.isEmpty() || mValue.isEmpty();
32 }
33 
34 FilterAction::ReturnCode FilterActionAddHeader::process(ItemContext &context, bool) const
35 {
36  if (isEmpty()) {
37  return ErrorButGoOn;
38  }
39 
40  auto msg = context.item().payload<KMime::Message::Ptr>();
41 
42  KMime::Headers::Base *header = KMime::Headers::createHeader(mParameter.toLatin1());
43  if (!header) {
44  header = new KMime::Headers::Generic(mParameter.toLatin1().constData());
45  }
46  header->fromUnicodeString(mValue, "utf-8");
47 
48  msg->setHeader(header);
49  msg->assemble();
50 
51  context.setNeedsPayloadStore();
52 
53  return GoOn;
54 }
55 
56 QWidget *FilterActionAddHeader::createParamWidget(QWidget *parent) const
57 {
58  auto widget = new QWidget(parent);
59  auto layout = new QHBoxLayout(widget);
60  layout->setSpacing(4);
61  layout->setContentsMargins({});
62 
63  auto comboBox = new KComboBox(widget);
64  comboBox->setMinimumWidth(50);
65  comboBox->setObjectName(QStringLiteral("combo"));
66  comboBox->setEditable(true);
67  comboBox->setInsertPolicy(QComboBox::InsertAtBottom);
68 
69  KCompletion *comp = comboBox->completionObject();
70  comp->setIgnoreCase(true);
71  comp->insertItems(mParameterList);
73 
74  layout->addWidget(comboBox, 0 /* stretch */);
75 
76  auto label = new QLabel(i18n("With value:"), widget);
77  label->setObjectName(QStringLiteral("label_value"));
78  label->setFixedWidth(label->sizeHint().width());
79  layout->addWidget(label, 0);
80 
81  auto lineEdit = new KLineEdit(widget);
82  lineEdit->setObjectName(QStringLiteral("ledit"));
83  lineEdit->setTrapReturnKey(true);
84  lineEdit->setClearButtonEnabled(true);
85  layout->addWidget(lineEdit, 1);
86 
87  setParamWidgetValue(widget);
88  connect(comboBox, &KComboBox::currentIndexChanged, this, &FilterActionAddHeader::filterActionModified);
89  connect(comboBox->lineEdit(), &QLineEdit::textChanged, this, &FilterAction::filterActionModified);
90  connect(lineEdit, &QLineEdit::textChanged, this, &FilterActionAddHeader::filterActionModified);
91 
92  return widget;
93 }
94 
95 void FilterActionAddHeader::setParamWidgetValue(QWidget *paramWidget) const
96 {
97  const int index = mParameterList.indexOf(mParameter);
98 
99  const auto comboBox = paramWidget->findChild<KComboBox *>(QStringLiteral("combo"));
100  Q_ASSERT(comboBox);
101  comboBox->clear();
102  comboBox->addItems(mParameterList);
103 
104  if (index < 0) {
105  comboBox->addItem(mParameter);
106  comboBox->setCurrentIndex(comboBox->count() - 1);
107  } else {
108  comboBox->setCurrentIndex(index);
109  }
110 
111  auto *lineEdit = paramWidget->findChild<QLineEdit *>(QStringLiteral("ledit"));
112  Q_ASSERT(lineEdit);
113 
114  lineEdit->setText(mValue);
115 }
116 
117 void FilterActionAddHeader::applyParamWidgetValue(QWidget *paramWidget)
118 {
119  const auto comboBox = paramWidget->findChild<KComboBox *>(QStringLiteral("combo"));
120  Q_ASSERT(comboBox);
121  mParameter = comboBox->currentText();
122 
123  const QLineEdit *lineEdit = paramWidget->findChild<QLineEdit *>(QStringLiteral("ledit"));
124  Q_ASSERT(lineEdit);
125  mValue = lineEdit->text();
126 }
127 
128 void FilterActionAddHeader::clearParamWidget(QWidget *paramWidget) const
129 {
130  const auto comboBox = paramWidget->findChild<KComboBox *>(QStringLiteral("combo"));
131  Q_ASSERT(comboBox);
132  comboBox->setCurrentIndex(0);
133 
134  auto *lineEdit = paramWidget->findChild<QLineEdit *>(QStringLiteral("ledit"));
135  Q_ASSERT(lineEdit);
136  lineEdit->clear();
137 }
138 
139 SearchRule::RequiredPart FilterActionAddHeader::requiredPart() const
140 {
142 }
143 
144 QString FilterActionAddHeader::argsAsString() const
145 {
146  QString result = mParameter;
147  result += QLatin1Char('\t');
148  result += mValue;
149 
150  return result;
151 }
152 
153 QString FilterActionAddHeader::displayString() const
154 {
155  return label() + QStringLiteral(" \"") + argsAsString().toHtmlEscaped() + QStringLiteral("\"");
156 }
157 
158 void FilterActionAddHeader::argsFromString(const QString &argsStr)
159 {
160  const QStringList list = argsStr.split(QLatin1Char('\t'));
161  QString result;
162  if (list.count() < 2) {
163  result = list[0];
164  mValue.clear();
165  } else {
166  result = list[0];
167  mValue = list[1];
168  }
169 
170  int index = mParameterList.indexOf(result);
171  if (index < 0) {
172  mParameterList.append(result);
173  index = mParameterList.count() - 1;
174  }
175 
176  mParameter = mParameterList.at(index);
177 }
178 
179 FilterAction *FilterActionAddHeader::newAction()
180 {
181  return new FilterActionAddHeader;
182 }
183 
184 QStringList FilterActionAddHeader::sieveRequires() const
185 {
186  return QStringList() << QStringLiteral("editheader");
187 }
188 
189 QString FilterActionAddHeader::sieveCode() const
190 {
191  if (isEmpty()) {
192  return QStringLiteral("# invalid filter. Need to fix it by hand");
193  } else {
194  return QStringLiteral("addheader \"%1\" \"%2\";").arg(mParameter, mValue);
195  }
196 }
197 
198 QString FilterActionAddHeader::informationAboutNotValidAction() const
199 {
200  QString result;
201  if (mParameter.isEmpty()) {
202  result = i18n("The header name was missing.");
203  }
204  if (mValue.isEmpty()) {
205  if (result.isEmpty()) {
206  result += QLatin1Char('\n');
207  }
208  result += i18n("The header value was missing.");
209  }
210  if (!result.isEmpty()) {
211  result = name() + QLatin1Char('\n') + result;
212  }
213  return result;
214 }
QString toHtmlEscaped() const const
QStringList split(const QString &sep, QString::SplitBehavior behavior, Qt::CaseSensitivity cs) const const
int count(const T &value) const const
Abstract base class for filter actions with a fixed set of string parameters.
KIOFILEWIDGETS_EXPORT QStringList list(const QString &fileClass)
void insertItems(const QStringList &items)
void clear()
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)
bool isEmpty() const const
void filterActionModified()
Called to notify that the current FilterAction has had some value modification.
virtual void setIgnoreCase(bool ignoreCase)
void setNeedsPayloadStore()
Marks that the item's payload has been changed and needs to be written back.
Definition: itemcontext.cpp:33
int indexOf(QStringView str, int from) const const
virtual void setCompletionMode(CompletionMode mode)
T findChild(const QString &name, Qt::FindChildOptions options) const const
A helper class for the filtering process.
Definition: itemcontext.h:26
QString label(StandardShortcut id)
ReturnCode
Describes the possible return codes of filter processing:
Definition: filteraction.h:45
@ CompleteMessage
Whole message.
Definition: searchrule.h:71
QString name(StandardShortcut id)
void currentIndexChanged(int index)
void clear()
Akonadi::Item & item()
Returns the item of the context.
Definition: itemcontext.cpp:18
The filter dialog.
T payload() const
This file is part of the KDE documentation.
Documentation copyright © 1996-2022 The KDE developers.
Generated on Sat Oct 1 2022 04:00:52 by doxygen 1.8.17 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.