8#include "filteractionrewriteheader.h"
12#include <KLocalizedString>
21 return new FilterActionRewriteHeader;
24FilterActionRewriteHeader::FilterActionRewriteHeader(QObject *parent)
27 mParameterList <<
QString() << QStringLiteral(
"Subject") << QStringLiteral(
"Reply-To") << QStringLiteral(
"Delivered-To")
28 << QStringLiteral(
"X-KDE-PR-Message") << QStringLiteral(
"X-KDE-PR-Package") << QStringLiteral(
"X-KDE-PR-Keywords");
30 mParameter = mParameterList.at(0);
33bool FilterActionRewriteHeader::isEmpty()
const
35 return mParameter.isEmpty() || mRegex.pattern().isEmpty();
38QString FilterActionRewriteHeader::informationAboutNotValidAction()
const
41 if (mParameter.isEmpty()) {
42 info =
i18n(
"Header not defined");
44 if (mRegex.pattern().isEmpty()) {
46 info += QLatin1Char(
'\n');
48 info +=
i18n(
"Search string is empty.");
61 const QByteArray param(mParameter.toLatin1());
62 KMime::Headers::Base *header = msg->headerByType(param.constData());
68 const QString oldValue = value;
69 const QString newValue = value.
replace(mRegex, mReplacementString);
70 if (newValue != oldValue) {
71 msg->removeHeader(param.constData());
73 KMime::Headers::Base *newheader = KMime::Headers::createHeader(param);
75 newheader =
new KMime::Headers::Generic(param.constData());
78 msg->setHeader(newheader);
91QWidget *FilterActionRewriteHeader::createParamWidget(QWidget *parent)
const
93 auto widget =
new QWidget(
parent);
94 auto layout =
new QHBoxLayout(widget);
95 layout->setSpacing(4);
96 layout->setContentsMargins({});
98 auto comboBox =
new KComboBox(widget);
99 comboBox->setMinimumWidth(50);
100 comboBox->setEditable(
true);
101 comboBox->setObjectName(QLatin1StringView(
"combo"));
103 layout->addWidget(comboBox, 0 );
105 KCompletion *comp = comboBox->completionObject();
110 auto label =
new QLabel(
i18nc(
"@label:textbox",
"Replace:"), widget);
111 label->setObjectName(QLatin1StringView(
"label_replace"));
112 label->setFixedWidth(
label->sizeHint().width());
113 layout->addWidget(
label, 0);
115 auto regExpLineEdit =
new KLineEdit(widget);
116 regExpLineEdit->setClearButtonEnabled(
true);
117 regExpLineEdit->setTrapReturnKey(
true);
118 regExpLineEdit->setObjectName(QLatin1StringView(
"search"));
119 layout->addWidget(regExpLineEdit, 1);
121 label =
new QLabel(
i18nc(
"@label:textbox",
"With:"), widget);
122 label->setFixedWidth(
label->sizeHint().width());
123 label->setObjectName(QLatin1StringView(
"label_with"));
124 layout->addWidget(
label, 0);
126 auto lineEdit =
new KLineEdit(widget);
127 lineEdit->setObjectName(QLatin1StringView(
"replace"));
128 lineEdit->setClearButtonEnabled(
true);
129 lineEdit->setTrapReturnKey(
true);
130 layout->addWidget(lineEdit, 1);
132 setParamWidgetValue(widget);
141void FilterActionRewriteHeader::setParamWidgetValue(QWidget *paramWidget)
const
143 const int index = mParameterList.indexOf(mParameter);
144 const auto comboBox = paramWidget->
findChild<KComboBox *>(QStringLiteral(
"combo"));
148 comboBox->addItems(mParameterList);
150 comboBox->addItem(mParameter);
151 comboBox->setCurrentIndex(comboBox->count() - 1);
153 comboBox->setCurrentIndex(index);
156 auto regExpLineEdit = paramWidget->
findChild<KLineEdit *>(QStringLiteral(
"search"));
157 Q_ASSERT(regExpLineEdit);
158 regExpLineEdit->setText(mRegex.pattern());
160 auto lineEdit = paramWidget->
findChild<KLineEdit *>(QStringLiteral(
"replace"));
162 lineEdit->setText(mReplacementString);
165void FilterActionRewriteHeader::applyParamWidgetValue(QWidget *paramWidget)
167 const auto comboBox = paramWidget->
findChild<KComboBox *>(QStringLiteral(
"combo"));
169 mParameter = comboBox->currentText();
171 const KLineEdit *regExpLineEdit = paramWidget->
findChild<KLineEdit *>(QStringLiteral(
"search"));
172 Q_ASSERT(regExpLineEdit);
173 mRegex.setPattern(regExpLineEdit->
text());
175 const KLineEdit *lineEdit = paramWidget->
findChild<KLineEdit *>(QStringLiteral(
"replace"));
177 mReplacementString = lineEdit->
text();
180void FilterActionRewriteHeader::clearParamWidget(QWidget *paramWidget)
const
182 const auto comboBox = paramWidget->
findChild<KComboBox *>(QStringLiteral(
"combo"));
184 comboBox->setCurrentIndex(0);
186 auto regExpLineEdit = paramWidget->
findChild<KLineEdit *>(QStringLiteral(
"search"));
187 Q_ASSERT(regExpLineEdit);
188 regExpLineEdit->
clear();
190 auto lineEdit = paramWidget->
findChild<KLineEdit *>(QStringLiteral(
"replace"));
195QString FilterActionRewriteHeader::argsAsString()
const
197 QString result = mParameter;
198 result += QLatin1Char(
'\t');
199 result += mRegex.pattern();
200 result += QLatin1Char(
'\t');
201 result += mReplacementString;
206QString FilterActionRewriteHeader::displayString()
const
208 return label() + QStringLiteral(
" \"") + argsAsString().
toHtmlEscaped() + QStringLiteral(
"\"");
211void FilterActionRewriteHeader::argsFromString(
const QString &argsStr)
213 const QStringList
list = argsStr.
split(QLatin1Char(
'\t'));
220 mRegex.setPattern(list[1]);
221 mReplacementString =
list[2];
223 int index = mParameterList.indexOf(result);
225 mParameterList.append(result);
226 index = mParameterList.count() - 1;
229 mParameter = mParameterList.at(index);
232#include "moc_filteractionrewriteheader.cpp"
void insertItems(const QStringList &items)
virtual void setCompletionMode(CompletionMode mode)
virtual void setIgnoreCase(bool ignoreCase)
QSharedPointer< Message > Ptr
Abstract base class for filter actions with a fixed set of string parameters.
Abstract base class for mail filter actions.
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.
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.
@ CompleteMessage
Whole message.
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)
void currentIndexChanged(int index)
void textChanged(const QString &text)
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
bool isEmpty() const const
QString & replace(QChar before, QChar after, Qt::CaseSensitivity cs)
QStringList split(QChar sep, Qt::SplitBehavior behavior, Qt::CaseSensitivity cs) const const
QString toHtmlEscaped() const const