Libksieve

sieveactionsetvariable.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 "sieveactionsetvariable.h"
7#include "autocreatescripts/autocreatescriptutil_p.h"
8#include "autocreatescripts/sieveeditorgraphicalmodewidget.h"
9#include "editor/sieveeditorutil.h"
10#include "widgets/selectvariablemodifiercombobox.h"
11
12#include <KLineEditEventHandler>
13#include <KLocalizedString>
14#include <QLineEdit>
15
16#include "libksieveui_debug.h"
17#include <QCheckBox>
18#include <QGridLayout>
19#include <QLabel>
20#include <QXmlStreamReader>
21
22using namespace KSieveUi;
23SieveActionSetVariable::SieveActionSetVariable(SieveEditorGraphicalModeWidget *sieveGraphicalModeWidget, QObject *parent)
24 : SieveAction(sieveGraphicalModeWidget, QStringLiteral("set"), i18n("Variable"), parent)
25{
26 mHasRegexCapability = sieveCapabilities().contains(QLatin1StringView("regex"));
27}
28
29QWidget *SieveActionSetVariable::createParamWidget(QWidget *parent) const
30{
31 auto w = new QWidget(parent);
32 auto grid = new QGridLayout;
33 grid->setContentsMargins({});
34 w->setLayout(grid);
35
36 auto modifier = new SelectVariableModifierComboBox;
37 modifier->setObjectName(QLatin1StringView("modifier"));
38 connect(modifier, &SelectVariableModifierComboBox::valueChanged, this, &SieveActionSetVariable::valueChanged);
39 grid->addWidget(modifier, 0, 0);
40
41 if (mHasRegexCapability) {
42 auto protectAgainstUseRegexp = new QCheckBox(i18nc("@option:check", "Protect special character"));
43 connect(protectAgainstUseRegexp, &QCheckBox::clicked, this, &SieveActionSetVariable::valueChanged);
44 protectAgainstUseRegexp->setObjectName(QLatin1StringView("regexprotect"));
45 grid->addWidget(protectAgainstUseRegexp, 0, 1);
46 }
47
48 auto lab = new QLabel(i18nc("@label:textbox", "Value:"));
49 grid->addWidget(lab, 1, 0);
50
51 auto value = new QLineEdit;
53 value->setObjectName(QLatin1StringView("value"));
54 connect(value, &QLineEdit::textChanged, this, &SieveActionSetVariable::valueChanged);
55 grid->addWidget(value, 1, 1);
56
57 lab = new QLabel(i18nc("@label:textbox", "In variable:"));
58 grid->addWidget(lab, 2, 0);
59
60 auto variable = new QLineEdit;
62 variable->setObjectName(QLatin1StringView("variable"));
63 connect(variable, &QLineEdit::textChanged, this, &SieveActionSetVariable::valueChanged);
64 grid->addWidget(variable, 2, 1);
65
66 return w;
67}
68
69QUrl SieveActionSetVariable::href() const
70{
71 return SieveEditorUtil::helpUrl(SieveEditorUtil::strToVariableName(name()));
72}
73
74void SieveActionSetVariable::setLocalVariable(QWidget *w, const SieveGlobalVariableActionWidget::VariableElement &var)
75{
76 auto value = w->findChild<QLineEdit *>(QStringLiteral("value"));
77 value->setText(var.variableValue);
78 auto variable = w->findChild<QLineEdit *>(QStringLiteral("variable"));
79 variable->setText(AutoCreateScriptUtil::protectSlash(var.variableName));
80}
81
82void SieveActionSetVariable::setParamWidgetValue(QXmlStreamReader &element, QWidget *w, QString &error)
83{
84 while (element.readNextStartElement()) {
85 const QStringView tagName = element.name();
86 if (tagName == QLatin1StringView("str")) {
87 const QString tagValue = element.readElementText();
88 auto value = w->findChild<QLineEdit *>(QStringLiteral("value"));
89 value->setText(tagValue);
90 if (element.readNextStartElement()) {
91 const QStringView variableTagName = element.name();
92 if (variableTagName == QLatin1StringView("str")) {
93 auto variable = w->findChild<QLineEdit *>(QStringLiteral("variable"));
94 variable->setText(AutoCreateScriptUtil::protectSlash(element.readElementText()));
95 }
96 } else {
97 return;
98 }
99 } else if (tagName == QLatin1StringView("tag")) {
100 const QString tagValue = element.readElementText();
101 if (tagValue == QLatin1StringView("quoteregex")) {
102 if (mHasRegexCapability) {
103 auto protectAgainstUseRegexp = w->findChild<QCheckBox *>(QStringLiteral("regexprotect"));
104 protectAgainstUseRegexp->setChecked(true);
105 } else {
106 error += i18n("Script needs regex support, but server does not have it.") + QLatin1Char('\n');
107 }
108 } else {
109 auto modifier = w->findChild<SelectVariableModifierComboBox *>(QStringLiteral("modifier"));
110 modifier->setCode(AutoCreateScriptUtil::tagValue(tagValue), name(), error);
111 }
112 } else if (tagName == QLatin1StringView("crlf")) {
113 element.skipCurrentElement();
114 // nothing
115 } else if (tagName == QLatin1StringView("comment")) {
116 element.skipCurrentElement();
117 // implement in the future ?
118 } else {
119 unknownTag(tagName, error);
120 qCDebug(LIBKSIEVEUI_LOG) << " SieveActionSetVariable::setParamWidgetValue unknown tagName " << tagName;
121 }
122 }
123}
124
125QString SieveActionSetVariable::code(QWidget *w) const
126{
127 QString result = QStringLiteral("set ");
128 const SelectVariableModifierComboBox *modifier = w->findChild<SelectVariableModifierComboBox *>(QStringLiteral("modifier"));
129 const QString modifierStr = modifier->code();
130 if (!modifierStr.isEmpty()) {
131 result += modifierStr + QLatin1Char(' ');
132 }
133
134 if (mHasRegexCapability) {
135 const QCheckBox *protectAgainstUseRegexp = w->findChild<QCheckBox *>(QStringLiteral("regexprotect"));
136 if (protectAgainstUseRegexp->isChecked()) {
137 result += QLatin1StringView(":quoteregex ");
138 }
139 }
140
141 const QLineEdit *value = w->findChild<QLineEdit *>(QStringLiteral("value"));
142 const QString valueStr = value->text();
143 result += QStringLiteral("\"%1\" ").arg(valueStr);
144
145 const QLineEdit *variable = w->findChild<QLineEdit *>(QStringLiteral("variable"));
146 const QString variableStr = variable->text();
147 result += QStringLiteral("\"%1\";").arg(variableStr);
148
149 return result;
150}
151
152QStringList SieveActionSetVariable::needRequires(QWidget *) const
153{
154 return QStringList() << QStringLiteral("variables");
155}
156
157bool SieveActionSetVariable::needCheckIfServerHasCapability() const
158{
159 return true;
160}
161
162QString SieveActionSetVariable::serverNeedsCapability() const
163{
164 return QStringLiteral("variables");
165}
166
167QString SieveActionSetVariable::help() const
168{
169 QString helpStr = i18n("The \"set\" action stores the specified value in the variable identified by name.");
170 if (mHasRegexCapability) {
171 helpStr += QLatin1Char('\n')
172 + i18n("This modifier adds the necessary quoting to ensure that the expanded text will only match a literal occurrence if used as a parameter "
173 "to :regex. Every character with special meaning (. , *, ? , etc.) is prefixed with \\ in the expansion");
174 }
175 return helpStr;
176}
177
178#include "moc_sieveactionsetvariable.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 error(QWidget *parent, const QString &text, const QString &title, const KGuiItem &buttonOk, Options options=Notify)
QByteArray tagValue(const Elem &elem, const char *keyName)
void setChecked(bool)
void clicked(bool checked)
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 arg(Args &&... args) 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.