Libksieve

sieveactionreplace.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 "sieveactionreplace.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
20#include <KSieveUi/AbstractSelectEmailLineEdit>
21
22using namespace KSieveUi;
23SieveActionReplace::SieveActionReplace(SieveEditorGraphicalModeWidget *sieveGraphicalModeWidget, QObject *parent)
24 : SieveAction(sieveGraphicalModeWidget, QStringLiteral("replace"), i18n("Replace"), parent)
25{
26}
27
28QWidget *SieveActionReplace::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 lab = new QLabel(i18nc("@label:textbox", "Subject:"));
36 grid->addWidget(lab, 0, 0);
37
38 auto subject = new QLineEdit;
40 subject->setObjectName(QLatin1StringView("subject"));
41 connect(subject, &QLineEdit::textChanged, this, &SieveActionReplace::valueChanged);
42 grid->addWidget(subject, 0, 1);
43
44 lab = new QLabel(i18nc("@label:textbox", "from:"));
45 grid->addWidget(lab, 1, 0);
46
47 KSieveUi::AbstractSelectEmailLineEdit *headers = AutoCreateScriptUtil::createSelectEmailsWidget();
48 headers->setObjectName(QLatin1StringView("from"));
49 connect(headers, &AbstractSelectEmailLineEdit::valueChanged, this, &SieveActionReplace::valueChanged);
50 grid->addWidget(headers, 1, 1);
51
52 lab = new QLabel(i18nc("@label:textbox", "text:"));
53 grid->addWidget(lab, 2, 0);
54
55 auto text = new MultiLineEdit;
56 text->setObjectName(QLatin1StringView("text"));
57 connect(text, &MultiLineEdit::textChanged, this, &SieveActionReplace::valueChanged);
58 grid->addWidget(text, 2, 1);
59
60 return w;
61}
62
63void SieveActionReplace::setParamWidgetValue(QXmlStreamReader &element, QWidget *w, QString &error)
64{
65 while (element.readNextStartElement()) {
66 const QStringView tagName = element.name();
67 if (tagName == QLatin1StringView("str")) {
68 auto edit = w->findChild<MultiLineEdit *>(QStringLiteral("text"));
69 edit->setPlainText(element.readElementText());
70 } else if (tagName == QLatin1StringView("tag")) {
71 const QString tagValue = element.readElementText();
72 if (tagValue == QLatin1StringView("subject")) {
73 const QString strValue = AutoCreateScriptUtil::strValue(element);
74 if (!strValue.isEmpty()) {
75 auto subject = w->findChild<QLineEdit *>(QStringLiteral("subject"));
76 subject->setText(strValue);
77 }
78 } else if (tagValue == QLatin1StringView("from")) {
79 const QString strValue = AutoCreateScriptUtil::strValue(element);
80 if (!strValue.isEmpty()) {
81 auto headers = w->findChild<KSieveUi::AbstractSelectEmailLineEdit *>(QStringLiteral("from"));
82 headers->setText(strValue);
83 }
84 } else {
85 unknownTagValue(tagValue, error);
86 qCDebug(LIBKSIEVEUI_LOG) << " SieveActionReplace::setParamWidgetValue unknown tagValue " << tagValue;
87 }
88 } else if (tagName == QLatin1StringView("crlf")) {
89 element.skipCurrentElement();
90 // nothing
91 } else if (tagName == QLatin1StringView("comment")) {
92 element.skipCurrentElement();
93 // implement in the future ?
94 } else {
95 unknownTag(tagName, error);
96 qCDebug(LIBKSIEVEUI_LOG) << " SieveActionReplace::setParamWidgetValue unknown tagName " << tagName;
97 }
98 }
99}
100
101QString SieveActionReplace::code(QWidget *w) const
102{
103 QString result = QStringLiteral("replace ");
104 const QLineEdit *subject = w->findChild<QLineEdit *>(QStringLiteral("subject"));
105 const QString subjectStr = subject->text();
106 if (!subjectStr.isEmpty()) {
107 result += QStringLiteral(":subject \"%1\" ").arg(subjectStr);
108 }
109
111 const QString headersStr = headers->text();
112 if (!headersStr.isEmpty()) {
113 result += QStringLiteral(":from \"%1\" ").arg(headersStr);
114 }
115
116 const MultiLineEdit *edit = w->findChild<MultiLineEdit *>(QStringLiteral("text"));
117 const QString text = edit->toPlainText();
118 if (!text.isEmpty()) {
119 result += QStringLiteral("text:%1").arg(AutoCreateScriptUtil::createMultiLine(text));
120 }
121
122 return result;
123}
124
125QStringList SieveActionReplace::needRequires(QWidget *) const
126{
127 return QStringList() << QStringLiteral("replace");
128}
129
130bool SieveActionReplace::needCheckIfServerHasCapability() const
131{
132 return true;
133}
134
135QString SieveActionReplace::serverNeedsCapability() const
136{
137 return QStringLiteral("replace");
138}
139
140QString SieveActionReplace::help() const
141{
142 return i18n("The \"replace\" command is defined to allow a MIME part to be replaced with the text supplied in the command.");
143}
144
145QUrl SieveActionReplace::href() const
146{
147 return SieveEditorUtil::helpUrl(SieveEditorUtil::strToVariableName(name()));
148}
149
150#include "moc_sieveactionreplace.cpp"
The AbstractSelectEmailLineEdit class.
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
void setObjectName(QAnyStringView name)
void textChanged()
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 Sat Dec 21 2024 16:57:10 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.