Libksieve

sieveactiondeleteheader.cpp
1/*
2 SPDX-FileCopyrightText: 2013-2024 Laurent Montel <montel@kde.org>
3
4 SPDX-License-Identifier: LGPL-2.0-or-later
5*/
6#include "sieveactiondeleteheader.h"
7#include "autocreatescripts/autocreatescriptutil_p.h"
8#include "autocreatescripts/commonwidgets/selectmatchtypecombobox.h"
9#include "editor/sieveeditorutil.h"
10
11#include <KLineEditEventHandler>
12#include <KLocalizedString>
13#include <QLineEdit>
14
15#include "libksieveui_debug.h"
16#include <QGridLayout>
17#include <QLabel>
18#include <QWidget>
19#include <QXmlStreamReader>
20
21using namespace KSieveUi;
22
23SieveActionDeleteHeader::SieveActionDeleteHeader(SieveEditorGraphicalModeWidget *sieveGraphicalModeWidget, QObject *parent)
24 : SieveActionAbstractEditHeader(sieveGraphicalModeWidget, QStringLiteral("deleteheader"), i18n("Delete header"), parent)
25{
26}
27
28QWidget *SieveActionDeleteHeader::createParamWidget(QWidget *parent) const
29{
30 auto w = new QWidget(parent);
31 auto grid = new QGridLayout;
32 grid->setContentsMargins({});
33 w->setLayout(grid);
34
35 auto matchType = new SelectMatchTypeComboBox(mSieveGraphicalModeWidget);
36 matchType->setObjectName(QLatin1StringView("matchtype"));
37 connect(matchType, &SelectMatchTypeComboBox::valueChanged, this, &SieveActionDeleteHeader::valueChanged);
38 grid->addWidget(matchType, 0, 0);
39
40 auto lab = new QLabel(i18nc("@label:textbox", "header:"));
41 grid->addWidget(lab, 0, 1);
42
43 auto headerEdit = new QLineEdit;
45 headerEdit->setObjectName(QLatin1StringView("headeredit"));
46 connect(headerEdit, &QLineEdit::textChanged, this, &SieveActionDeleteHeader::valueChanged);
47 grid->addWidget(headerEdit, 0, 2);
48
49 lab = new QLabel(i18nc("@label:textbox", "value:"));
50 grid->addWidget(lab, 1, 1);
51
52 auto valueEdit = new QLineEdit;
54 valueEdit->setObjectName(QLatin1StringView("valueedit"));
55 connect(valueEdit, &QLineEdit::textChanged, this, &SieveActionDeleteHeader::valueChanged);
56 grid->addWidget(valueEdit, 1, 2);
57 return w;
58}
59
60void SieveActionDeleteHeader::parseValue(QXmlStreamReader &element, QWidget *w, QString &error, bool isNegative)
61{
62 int index = 0;
63 while (element.readNextStartElement()) {
64 const QStringView tagName = element.name();
65 if (tagName == QLatin1StringView("test")) {
66 QString nameValue;
67 if (element.attributes().hasAttribute(QLatin1StringView("name"))) {
68 nameValue = element.attributes().value(QLatin1StringView("name")).toString();
69 }
70 const bool isNot = (nameValue == QLatin1StringView("not"));
71 parseValue(element, w, error, isNot);
72 element.skipCurrentElement();
73 return;
74 } else if (tagName == QLatin1StringView("tag")) {
75 auto combo = w->findChild<SelectMatchTypeComboBox *>(QStringLiteral("matchtype"));
76 combo->setCode(AutoCreateScriptUtil::tagValueWithCondition(element.readElementText(), isNegative), name(), error);
77 } else if (tagName == QLatin1StringView("str")) {
78 if (index == 0) {
79 auto edit = w->findChild<QLineEdit *>(QStringLiteral("headeredit"));
80 edit->setText(element.readElementText());
81 } else if (index == 1) {
82 auto value = w->findChild<QLineEdit *>(QStringLiteral("valueedit"));
83 value->setText(element.readElementText());
84 } else {
85 tooManyArguments(tagName, index, 2, error);
86 qCDebug(LIBKSIEVEUI_LOG) << " SieveActionAddHeader::setParamWidgetValue too many argument :" << index;
87 }
88 ++index;
89 } else if (tagName == QLatin1StringView("crlf")) {
90 element.skipCurrentElement();
91 // nothing
92 } else if (tagName == QLatin1StringView("comment")) {
93 element.skipCurrentElement();
94 // implement in the future ?
95 } else {
96 unknownTag(tagName, error);
97 qCDebug(LIBKSIEVEUI_LOG) << "SieveActionAddHeader::setParamWidgetValue unknown tag " << tagName;
98 }
99 }
100}
101
102void SieveActionDeleteHeader::setParamWidgetValue(QXmlStreamReader &element, QWidget *w, QString &error)
103{
104 parseValue(element, w, error, false);
105}
106
107QString SieveActionDeleteHeader::code(QWidget *w) const
108{
109 const SelectMatchTypeComboBox *combo = w->findChild<SelectMatchTypeComboBox *>(QStringLiteral("matchtype"));
110 bool isNegative = false;
111 const QString matchTypeStr = combo->code(isNegative);
112
113 const QLineEdit *edit = w->findChild<QLineEdit *>(QStringLiteral("headeredit"));
114 const QString headerStr = edit->text();
115
116 const QLineEdit *value = w->findChild<QLineEdit *>(QStringLiteral("valueedit"));
117 const QString valueStr = value->text();
118
119 return QStringLiteral("deleteheader %1 \"%2\" \"%3\";").arg((isNegative ? QLatin1StringView("not ") + matchTypeStr : matchTypeStr), headerStr, valueStr);
120}
121
122QString SieveActionDeleteHeader::help() const
123{
124 return i18n("By default, the deleteheader action deletes all occurrences of the named header field.");
125}
126
127QUrl SieveActionDeleteHeader::href() const
128{
129 return SieveEditorUtil::helpUrl(SieveEditorUtil::strToVariableName(name()));
130}
131
132QStringList SieveActionDeleteHeader::needRequires(QWidget *w) const
133{
134 const SelectMatchTypeComboBox *combo = w->findChild<SelectMatchTypeComboBox *>(QStringLiteral("matchtype"));
135 return SieveActionAbstractEditHeader::needRequires(w) + combo->needRequires();
136}
137
138#include "moc_sieveactiondeleteheader.cpp"
QString i18nc(const char *context, const char *text, const TYPE &arg...)
QString i18n(const char *text, const TYPE &arg...)
void catchReturnKey(QObject *lineEdit)
void setContentsMargins(const QMargins &margins)
void setText(const QString &)
void textChanged(const QString &text)
QMetaObject::Connection connect(const QObject *sender, PointerToMemberFunction signal, Functor functor)
T findChild(const QString &name, Qt::FindChildOptions options) const const
QObject * parent() const const
QString toString() const const
bool hasAttribute(QAnyStringView namespaceUri, QAnyStringView name) const const
QStringView value(QAnyStringView namespaceUri, QAnyStringView name) const const
QXmlStreamAttributes attributes() const const
QStringView name() const const
QString readElementText(ReadElementTextBehaviour behaviour)
bool readNextStartElement()
void skipCurrentElement()
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Fri Nov 29 2024 11:56:12 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.