Libksieve

sieveconditionenvironment.cpp
1 /*
2  SPDX-FileCopyrightText: 2013-2021 Laurent Montel <[email protected]>
3 
4  SPDX-License-Identifier: LGPL-2.0-or-later
5 */
6 #include "sieveconditionenvironment.h"
7 #include "autocreatescripts/autocreatescriptutil_p.h"
8 #include "editor/sieveeditorutil.h"
9 
10 #include <KLocalizedString>
11 #include <Libkdepim/LineEditCatchReturnKey>
12 #include <QLineEdit>
13 
14 #include "libksieve_debug.h"
15 #include <QCompleter>
16 #include <QGridLayout>
17 #include <QLabel>
18 #include <QWidget>
19 #include <QXmlStreamReader>
20 
21 using namespace KSieveUi;
22 SieveConditionEnvironment::SieveConditionEnvironment(SieveEditorGraphicalModeWidget *sieveGraphicalModeWidget, QObject *parent)
23  : SieveCondition(sieveGraphicalModeWidget, QStringLiteral("environment"), i18n("Environment"), parent)
24 {
25 }
26 
27 QWidget *SieveConditionEnvironment::createParamWidget(QWidget *parent) const
28 {
29  auto w = new QWidget(parent);
30  auto grid = new QGridLayout;
31  grid->setContentsMargins({});
32  w->setLayout(grid);
33  auto lab = new QLabel(i18n("Item:"));
34  grid->addWidget(lab, 0, 0);
35 
36  auto item = new QLineEdit;
37  new KPIM::LineEditCatchReturnKey(item, w);
38  QStringList itemList;
39  itemList << QStringLiteral("domain") << QStringLiteral("host") << QStringLiteral("location") << QStringLiteral("name") << QStringLiteral("phase")
40  << QStringLiteral("remote-host") << QStringLiteral("remote-ip") << QStringLiteral("version");
41  auto completer = new QCompleter(itemList, w);
42  completer->setCaseSensitivity(Qt::CaseInsensitive);
43  item->setCompleter(completer);
44  connect(item, &QLineEdit::textChanged, this, &SieveConditionEnvironment::valueChanged);
45 
46  item->setObjectName(QStringLiteral("item"));
47  grid->addWidget(item, 0, 1);
48 
49  lab = new QLabel(i18n("Value:"));
50  grid->addWidget(lab, 1, 0);
51 
52  auto value = new QLineEdit;
53  new KPIM::LineEditCatchReturnKey(value, w);
54  connect(value, &QLineEdit::textChanged, this, &SieveConditionEnvironment::valueChanged);
55  value->setObjectName(QStringLiteral("value"));
56  grid->addWidget(value, 1, 1);
57 
58  return w;
59 }
60 
61 QString SieveConditionEnvironment::code(QWidget *w) const
62 {
63  const QLineEdit *item = w->findChild<QLineEdit *>(QStringLiteral("item"));
64  const QString itemStr = item->text();
65 
66  const QLineEdit *value = w->findChild<QLineEdit *>(QStringLiteral("value"));
67  const QString valueStr = value->text();
68 
69  return QStringLiteral("environment \"%1\" \"%2\"").arg(itemStr, valueStr) + AutoCreateScriptUtil::generateConditionComment(comment());
70 }
71 
72 QStringList SieveConditionEnvironment::needRequires(QWidget *) const
73 {
74  return QStringList() << QStringLiteral("environment");
75 }
76 
77 bool SieveConditionEnvironment::needCheckIfServerHasCapability() const
78 {
79  return true;
80 }
81 
82 QString SieveConditionEnvironment::serverNeedsCapability() const
83 {
84  return QStringLiteral("environment");
85 }
86 
87 QString SieveConditionEnvironment::help() const
88 {
89  return i18n(
90  "The environment test retrieves the item of environment information specified by the name string and matches it to the values specified in the "
91  "key-list argument.");
92 }
93 
94 void SieveConditionEnvironment::setParamWidgetValue(QXmlStreamReader &element, QWidget *w, bool /*notCondition*/, QString &error)
95 {
96  int index = 0;
97  QString commentStr;
98  while (element.readNextStartElement()) {
99  const QStringRef tagName = element.name();
100  if (tagName == QLatin1String("str")) {
101  if (index == 0) {
102  auto item = w->findChild<QLineEdit *>(QStringLiteral("item"));
103  item->setText(AutoCreateScriptUtil::quoteStr(element.readElementText()));
104  } else if (index == 1) {
105  auto value = w->findChild<QLineEdit *>(QStringLiteral("value"));
106  value->setText(AutoCreateScriptUtil::quoteStr(element.readElementText()));
107  } else {
108  tooManyArguments(tagName, index, 2, error);
109  qCDebug(LIBKSIEVE_LOG) << " SieveConditionEnvironment::setParamWidgetValue to many argument " << index;
110  }
111  ++index;
112  } else if (tagName == QLatin1String("crlf")) {
113  element.skipCurrentElement();
114  // nothing
115  } else if (tagName == QLatin1String("comment")) {
116  commentStr = AutoCreateScriptUtil::loadConditionComment(commentStr, element.readElementText());
117  } else {
118  unknownTag(tagName, error);
119  qCDebug(LIBKSIEVE_LOG) << " SieveActionSetVariable::setParamWidgetValue unknown tagName " << tagName;
120  }
121  }
122  if (!commentStr.isEmpty()) {
123  setComment(commentStr);
124  }
125 }
126 
127 QUrl KSieveUi::SieveConditionEnvironment::href() const
128 {
129  return SieveEditorUtil::helpUrl(SieveEditorUtil::strToVariableName(name()));
130 }
bool readNextStartElement()
void setContentsMargins(int left, int top, int right, int bottom)
QString name(const QVariant &location)
void textChanged(const QString &text)
void skipCurrentElement()
QString readElementText(QXmlStreamReader::ReadElementTextBehaviour behaviour)
CaseInsensitive
bool isEmpty() const const
QString i18n(const char *text, const TYPE &arg...)
QMetaObject::Connection connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
QObject * parent() const const
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 Sun Apr 11 2021 23:09:36 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.