Mailcommon

filteractionaddheader.cpp
1/*
2 * SPDX-FileCopyrightText: 1996-1998 Stefan Taferner <taferner@kde.org>
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
18using namespace MailCommon;
19
20FilterActionAddHeader::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
29bool FilterActionAddHeader::isEmpty() const
30{
31 return mParameter.isEmpty() || mValue.isEmpty();
32}
33
34FilterAction::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);
47
48 msg->setHeader(header);
49 msg->assemble();
50
51 context.setNeedsPayloadStore();
52
53 return GoOn;
54}
55
56QWidget *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(QLatin1StringView("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(i18nc("@label:textbox", "With value:"), widget);
77 label->setObjectName(QLatin1StringView("label_value"));
78 label->setFixedWidth(label->sizeHint().width());
79 layout->addWidget(label, 0);
80
81 auto lineEdit = new KLineEdit(widget);
82 lineEdit->setObjectName(QLatin1StringView("ledit"));
83 lineEdit->setTrapReturnKey(true);
84 lineEdit->setClearButtonEnabled(true);
85 layout->addWidget(lineEdit, 1);
86
87 setParamWidgetValue(widget);
91
92 return widget;
93}
94
95void 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
117void 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
128void 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
139SearchRule::RequiredPart FilterActionAddHeader::requiredPart() const
140{
142}
143
144QString FilterActionAddHeader::argsAsString() const
145{
146 QString result = mParameter;
147 result += QLatin1Char('\t');
148 result += mValue;
149
150 return result;
151}
152
153QString FilterActionAddHeader::displayString() const
154{
155 return label() + QStringLiteral(" \"") + argsAsString().toHtmlEscaped() + QStringLiteral("\"");
156}
157
158void 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
179FilterAction *FilterActionAddHeader::newAction()
180{
181 return new FilterActionAddHeader;
182}
183
184QStringList FilterActionAddHeader::sieveRequires() const
185{
186 return QStringList() << QStringLiteral("editheader");
187}
188
189QString 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
198QString 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}
215
216#include "moc_filteractionaddheader.cpp"
T payload() const
void insertItems(const QStringList &items)
virtual void setCompletionMode(CompletionMode mode)
virtual void setIgnoreCase(bool ignoreCase)
Abstract base class for filter actions with a fixed set of string parameters.
Abstract base class for mail filter actions.
QString name() const
Returns identifier name, ie.
ReturnCode
Describes the possible return codes of filter processing:
@ ErrorButGoOn
A non-critical error occurred.
@ GoOn
Go on with applying filter actions.
void filterActionModified()
Called to notify that the current FilterAction has had some value modification.
QString label() const
Returns i18n'd label, ie.
A helper class for the filtering process.
Definition itemcontext.h:27
void setNeedsPayloadStore()
Marks that the item's payload has been changed and needs to be written back.
Akonadi::Item & item()
Returns the item of the context.
RequiredPart
Possible required parts.
Definition searchrule.h:70
@ CompleteMessage
Whole message.
Definition searchrule.h:73
QString i18nc(const char *context, const char *text, const TYPE &arg...)
QString i18n(const char *text, const TYPE &arg...)
KIOCORE_EXPORT QStringList list(const QString &fileClass)
The filter dialog.
const char * constData() const const
void currentIndexChanged(int index)
void clear()
void textChanged(const QString &text)
void append(QList< T > &&value)
const_reference at(qsizetype i) const const
qsizetype count() const const
QMetaObject::Connection connect(const QObject *sender, PointerToMemberFunction signal, Functor functor)
T findChild(const QString &name, Qt::FindChildOptions options) const const
QObject * parent() const const
void clear()
bool isEmpty() const const
QStringList split(QChar sep, Qt::SplitBehavior behavior, Qt::CaseSensitivity cs) const const
QString toHtmlEscaped() const const
QByteArray toLatin1() const const
qsizetype indexOf(const QRegularExpression &re, qsizetype from) const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Sat Dec 21 2024 17:00:24 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.