Libksieve

sieveactionenclose.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 "sieveactionenclose.h"
7#include "autocreatescripts/autocreatescriptutil_p.h"
8#include "editor/sieveeditorutil.h"
9#include "widgets/multilineedit.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 <QXmlStreamReader>
19
20using namespace KSieveUi;
21SieveActionEnclose::SieveActionEnclose(SieveEditorGraphicalModeWidget *sieveGraphicalModeWidget, QObject *parent)
22 : SieveAction(sieveGraphicalModeWidget, QStringLiteral("enclose"), i18n("Enclose"), parent)
23{
24}
25
26QWidget *SieveActionEnclose::createParamWidget(QWidget *parent) const
27{
28 auto w = new QWidget(parent);
29 auto grid = new QGridLayout;
30 grid->setContentsMargins({});
31 w->setLayout(grid);
32
33 auto lab = new QLabel(i18nc("@label:textbox", "Subject:"));
34 grid->addWidget(lab, 0, 0);
35
36 auto subject = new QLineEdit;
38 subject->setObjectName(QLatin1StringView("subject"));
39 connect(subject, &QLineEdit::textChanged, this, &SieveActionEnclose::valueChanged);
40 grid->addWidget(subject, 0, 1);
41
42 lab = new QLabel(i18nc("@label:textbox", "headers:"));
43 grid->addWidget(lab, 1, 0);
44
45 auto headers = new QLineEdit;
47 headers->setObjectName(QLatin1StringView("headers"));
48 connect(headers, &QLineEdit::textChanged, this, &SieveActionEnclose::valueChanged);
49 grid->addWidget(headers, 1, 1);
50
51 lab = new QLabel(i18nc("@label:textbox", "text:"));
52 grid->addWidget(lab, 2, 0);
53
54 auto text = new MultiLineEdit;
55 text->setObjectName(QLatin1StringView("text"));
56 connect(text, &MultiLineEdit::valueChanged, this, &SieveActionEnclose::valueChanged);
57 grid->addWidget(text, 2, 1);
58
59 return w;
60}
61
62void SieveActionEnclose::setParamWidgetValue(QXmlStreamReader &element, QWidget *w, QString &error)
63{
64 while (element.readNextStartElement()) {
65 const QStringView tagName = element.name();
66 if (tagName == QLatin1StringView("tag")) {
67 const QString tagValue = element.readElementText();
68 if (tagValue == QLatin1StringView("headers")) {
69 const QString strValue = AutoCreateScriptUtil::strValue(element);
70 if (!strValue.isEmpty()) {
71 auto subject = w->findChild<QLineEdit *>(QStringLiteral("headers"));
72 subject->setText(strValue);
73 }
74 } else if (tagValue == QLatin1StringView("subject")) {
75 const QString strValue = AutoCreateScriptUtil::strValue(element);
76 if (!strValue.isEmpty()) {
77 auto headers = w->findChild<QLineEdit *>(QStringLiteral("subject"));
78 headers->setText(strValue);
79 }
80 } else {
81 unknownTagValue(tagValue, error);
82 qCDebug(LIBKSIEVEUI_LOG) << " SieveActionEnclose::setParamWidgetValue unknown tag value:" << tagValue;
83 }
84 } else if (tagName == QLatin1StringView("str")) {
85 auto edit = w->findChild<MultiLineEdit *>(QStringLiteral("text"));
86 edit->setPlainText(element.readElementText());
87 } else if (tagName == QLatin1StringView("crlf")) {
88 element.skipCurrentElement();
89 // nothing
90 } else if (tagName == QLatin1StringView("comment")) {
91 element.skipCurrentElement();
92 // implement in the future ?
93 } else {
94 unknownTag(tagName, error);
95 qCDebug(LIBKSIEVEUI_LOG) << " SieveActionEnclose::setParamWidgetValue unknown tagName " << tagName;
96 }
97 }
98}
99
100QString SieveActionEnclose::code(QWidget *w) const
101{
102 QString result = QStringLiteral("enclose ");
103 const QLineEdit *subject = w->findChild<QLineEdit *>(QStringLiteral("subject"));
104 const QString subjectStr = subject->text();
105 if (!subjectStr.isEmpty()) {
106 result += QStringLiteral(":subject \"%1\" ").arg(subjectStr);
107 }
108
109 const QLineEdit *headers = w->findChild<QLineEdit *>(QStringLiteral("headers"));
110 const QString headersStr = headers->text();
111 if (!headersStr.isEmpty()) {
112 result += QStringLiteral(":headers \"%1\" ").arg(headersStr);
113 }
114
115 const MultiLineEdit *edit = w->findChild<MultiLineEdit *>(QStringLiteral("text"));
116 const QString text = edit->toPlainText();
117 if (!text.isEmpty()) {
118 result += QStringLiteral("text:%1").arg(AutoCreateScriptUtil::createMultiLine(text));
119 } else {
120 result += QLatin1Char(';');
121 }
122
123 return result;
124}
125
126QStringList SieveActionEnclose::needRequires(QWidget * /*parent*/) const
127{
128 return QStringList() << QStringLiteral("enclose");
129}
130
131bool SieveActionEnclose::needCheckIfServerHasCapability() const
132{
133 return true;
134}
135
136QString SieveActionEnclose::serverNeedsCapability() const
137{
138 return QStringLiteral("enclose");
139}
140
141QString SieveActionEnclose::help() const
142{
143 return i18n("Enclose action command is defined to allow an entire message to be enclosed as an attachment to a new message.");
144}
145
146QUrl SieveActionEnclose::href() const
147{
148 return SieveEditorUtil::helpUrl(SieveEditorUtil::strToVariableName(name()));
149}
150
151#include "moc_sieveactionenclose.cpp"
QString i18nc(const char *context, const char *text, const TYPE &arg...)
QString i18n(const char *text, const TYPE &arg...)
void catchReturnKey(QObject *lineEdit)
QByteArray tagValue(const Elem &elem, const char *keyName)
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 toPlainText() const const
QString arg(Args &&... args) const const
bool isEmpty() 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 Mon Nov 18 2024 12:14:30 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.