Libksieve

sieveactionaddheader.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 "sieveactionaddheader.h"
7#include "autocreatescripts/autocreatescriptutil_p.h"
8#include "widgets/selectaddheaderpositioncombobox.h"
9#include <KLineEditEventHandler>
10
11#include <KLocalizedString>
12#include <QLineEdit>
13
14#include "libksieveui_debug.h"
15#include <QGridLayout>
16#include <QLabel>
17#include <QUrl>
18#include <QWidget>
19#include <QXmlStreamReader>
20
21using namespace KSieveUi;
22
23SieveActionAddHeader::SieveActionAddHeader(SieveEditorGraphicalModeWidget *sieveGraphicalModeWidget, QObject *parent)
24 : SieveActionAbstractEditHeader(sieveGraphicalModeWidget, QStringLiteral("addheader"), i18n("Add header"), parent)
25{
26}
27
28QWidget *SieveActionAddHeader::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 combo = new SelectAddHeaderPositionCombobox;
36 combo->setObjectName(QLatin1StringView("selectposition"));
37 connect(combo, &SelectAddHeaderPositionCombobox::valueChanged, this, &SieveActionAddHeader::valueChanged);
38 grid->addWidget(combo, 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 connect(headerEdit, &QLineEdit::textChanged, this, &SieveActionAddHeader::valueChanged);
46 headerEdit->setObjectName(QLatin1StringView("headeredit"));
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 connect(valueEdit, &QLineEdit::textChanged, this, &SieveActionAddHeader::valueChanged);
55 valueEdit->setObjectName(QLatin1StringView("valueedit"));
56 grid->addWidget(valueEdit, 1, 2);
57
58 return w;
59}
60
61void SieveActionAddHeader::setParamWidgetValue(QXmlStreamReader &element, QWidget *w, QString &error)
62{
63 int index = 0;
64 while (element.readNextStartElement()) {
65 const QStringView tagName = element.name();
66 if (tagName == QLatin1StringView("tag")) {
67 auto combo = w->findChild<SelectAddHeaderPositionCombobox *>(QStringLiteral("selectposition"));
68 combo->setCode(AutoCreateScriptUtil::tagValue(element.readElementText()), name(), error);
69 } else if (tagName == QLatin1StringView("str")) {
70 if (index == 0) {
71 auto edit = w->findChild<QLineEdit *>(QStringLiteral("headeredit"));
72 edit->setText(element.readElementText());
73 } else if (index == 1) {
74 auto value = w->findChild<QLineEdit *>(QStringLiteral("valueedit"));
75 value->setText(AutoCreateScriptUtil::quoteStr(element.readElementText()));
76 } else {
77 tooManyArguments(tagName, index, 2, error);
78 qCDebug(LIBKSIEVEUI_LOG) << " SieveActionAddHeader::setParamWidgetValue too many argument :" << index;
79 }
80 ++index;
81 } else if (tagName == QLatin1StringView("crlf")) {
82 element.skipCurrentElement();
83 // nothing
84 } else if (tagName == QLatin1StringView("comment")) {
85 element.skipCurrentElement();
86 // implement in the future ?
87 } else {
88 unknownTag(tagName, error);
89 qCDebug(LIBKSIEVEUI_LOG) << "SieveActionAddHeader::setParamWidgetValue unknown tag " << tagName;
90 }
91 }
92}
93
94QString SieveActionAddHeader::code(QWidget *w) const
95{
96 const SelectAddHeaderPositionCombobox *combo = w->findChild<SelectAddHeaderPositionCombobox *>(QStringLiteral("selectposition"));
97 const QString position = combo->code();
98
99 const QLineEdit *edit = w->findChild<QLineEdit *>(QStringLiteral("headeredit"));
100 const QString headerStr = edit->text();
101
102 const QLineEdit *value = w->findChild<QLineEdit *>(QStringLiteral("valueedit"));
103 const QString valueStr = value->text();
104
105 if (position.isEmpty()) {
106 return QStringLiteral("addheader \"%1\" \"%2\";").arg(headerStr, valueStr);
107 } else {
108 return QStringLiteral("addheader %1 \"%2\" \"%3\";").arg(position, headerStr, valueStr);
109 }
110}
111
112QString SieveActionAddHeader::help() const
113{
114 return i18n("The addheader action adds a header field to the existing message header.");
115}
116
117QUrl SieveActionAddHeader::href() const
118{
119 return QUrl(QStringLiteral("https://tools.ietf.org/html/rfc5293"));
120}
121
122#include "moc_sieveactionaddheader.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
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 Dec 6 2024 12:07:25 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.