Libksieve

sieveactionsetvariable.cpp
1 /*
2  SPDX-FileCopyrightText: 2013-2021 Laurent Montel <[email protected]>
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 <KLocalizedString>
13 #include <Libkdepim/LineEditCatchReturnKey>
14 #include <QLineEdit>
15 
16 #include "libksieve_debug.h"
17 #include <QCheckBox>
18 #include <QGridLayout>
19 #include <QLabel>
20 #include <QXmlStreamReader>
21 
22 using namespace KSieveUi;
23 SieveActionSetVariable::SieveActionSetVariable(SieveEditorGraphicalModeWidget *sieveGraphicalModeWidget, QObject *parent)
24  : SieveAction(sieveGraphicalModeWidget, QStringLiteral("set"), i18n("Variable"), parent)
25 {
26  mHasRegexCapability = sieveCapabilities().contains(QLatin1String("regex"));
27 }
28 
29 QWidget *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(QStringLiteral("modifier"));
38  connect(modifier, &SelectVariableModifierComboBox::valueChanged, this, &SieveActionSetVariable::valueChanged);
39  grid->addWidget(modifier, 0, 0);
40 
41  if (mHasRegexCapability) {
42  auto protectAgainstUseRegexp = new QCheckBox(i18n("Protect special character"));
43  connect(protectAgainstUseRegexp, &QCheckBox::clicked, this, &SieveActionSetVariable::valueChanged);
44  protectAgainstUseRegexp->setObjectName(QStringLiteral("regexprotect"));
45  grid->addWidget(protectAgainstUseRegexp, 0, 1);
46  }
47 
48  auto lab = new QLabel(i18n("Value:"));
49  grid->addWidget(lab, 1, 0);
50 
51  auto value = new QLineEdit;
52  new KPIM::LineEditCatchReturnKey(value, w);
53  value->setObjectName(QStringLiteral("value"));
54  connect(value, &QLineEdit::textChanged, this, &SieveActionSetVariable::valueChanged);
55  grid->addWidget(value, 1, 1);
56 
57  lab = new QLabel(i18n("In variable:"));
58  grid->addWidget(lab, 2, 0);
59 
60  auto variable = new QLineEdit;
61  new KPIM::LineEditCatchReturnKey(variable, w);
62  variable->setObjectName(QStringLiteral("variable"));
63  connect(variable, &QLineEdit::textChanged, this, &SieveActionSetVariable::valueChanged);
64  grid->addWidget(variable, 2, 1);
65 
66  return w;
67 }
68 
69 QUrl SieveActionSetVariable::href() const
70 {
71  return SieveEditorUtil::helpUrl(SieveEditorUtil::strToVariableName(name()));
72 }
73 
74 void 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 
82 void SieveActionSetVariable::setParamWidgetValue(QXmlStreamReader &element, QWidget *w, QString &error)
83 {
84  while (element.readNextStartElement()) {
85  const QStringRef tagName = element.name();
86  if (tagName == QLatin1String("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 QStringRef variableTagName = element.name();
92  if (variableTagName == QLatin1String("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 == QLatin1String("tag")) {
100  const QString tagValue = element.readElementText();
101  if (tagValue == QLatin1String("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 == QLatin1String("crlf")) {
113  element.skipCurrentElement();
114  // nothing
115  } else if (tagName == QLatin1String("comment")) {
116  element.skipCurrentElement();
117  // implement in the future ?
118  } else {
119  unknownTag(tagName, error);
120  qCDebug(LIBKSIEVE_LOG) << " SieveActionSetVariable::setParamWidgetValue unknown tagName " << tagName;
121  }
122  }
123 }
124 
125 QString 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 += QLatin1String(":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 
152 QStringList SieveActionSetVariable::needRequires(QWidget *) const
153 {
154  return QStringList() << QStringLiteral("variables");
155 }
156 
157 bool SieveActionSetVariable::needCheckIfServerHasCapability() const
158 {
159  return true;
160 }
161 
162 QString SieveActionSetVariable::serverNeedsCapability() const
163 {
164  return QStringLiteral("variables");
165 }
166 
167 QString 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 }
bool readNextStartElement()
void setContentsMargins(int left, int top, int right, int bottom)
QString name(const QVariant &location)
void setText(const QString &)
void textChanged(const QString &text)
void skipCurrentElement()
QString readElementText(QXmlStreamReader::ReadElementTextBehaviour behaviour)
QByteArray tagValue(const Elem &elem, TagKey key)
void clicked(bool checked)
void setChecked(bool)
QString i18n(const char *text, const TYPE &arg...)
QString arg(qlonglong a, int fieldWidth, int base, QChar fillChar) const const
QMetaObject::Connection connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
QStringRef name() const const
T findChild(const QString &name, Qt::FindChildOptions options) const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2021 The KDE developers.
Generated on Fri Apr 16 2021 23:09:34 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.