Libksieve

sieveconditionenvironment.cpp
1 /*
2  Copyright (C) 2013-2020 Laurent Montel <[email protected]>
3 
4  This library is free software; you can redistribute it and/or
5  modify it under the terms of the GNU Library General Public
6  License as published by the Free Software Foundation; either
7  version 2 of the License, or (at your option) any later version.
8 
9  This library is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  Library General Public License for more details.
13 
14  You should have received a copy of the GNU Library General Public License
15  along with this library; see the file COPYING.LIB. If not, write to
16  the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17  Boston, MA 02110-1301, USA.
18 */
19 #include "sieveconditionenvironment.h"
20 #include "autocreatescripts/autocreatescriptutil_p.h"
21 #include "editor/sieveeditorutil.h"
22 
23 #include <KLocalizedString>
24 #include <QLineEdit>
25 
26 #include <QWidget>
27 #include <QLabel>
28 #include <QCompleter>
29 #include "libksieve_debug.h"
30 #include <QXmlStreamReader>
31 #include <QGridLayout>
32 
33 using namespace KSieveUi;
34 SieveConditionEnvironment::SieveConditionEnvironment(SieveEditorGraphicalModeWidget *sieveGraphicalModeWidget, QObject *parent)
35  : SieveCondition(sieveGraphicalModeWidget, QStringLiteral("environment"), i18n("Environment"), parent)
36 {
37 }
38 
39 QWidget *SieveConditionEnvironment::createParamWidget(QWidget *parent) const
40 {
41  QWidget *w = new QWidget(parent);
42  QGridLayout *grid = new QGridLayout;
43  grid->setContentsMargins(0, 0, 0, 0);
44  w->setLayout(grid);
45  QLabel *lab = new QLabel(i18n("Item:"));
46  grid->addWidget(lab, 0, 0);
47 
48  QLineEdit *item = new QLineEdit;
49  QStringList itemList;
50  itemList << QStringLiteral("domain")
51  << QStringLiteral("host")
52  << QStringLiteral("location")
53  << QStringLiteral("name")
54  << QStringLiteral("phase")
55  << QStringLiteral("remote-host")
56  << QStringLiteral("remote-ip")
57  << QStringLiteral("version");
58  QCompleter *completer = new QCompleter(itemList, w);
60  item->setCompleter(completer);
61  connect(item, &QLineEdit::textChanged, this, &SieveConditionEnvironment::valueChanged);
62 
63  item->setObjectName(QStringLiteral("item"));
64  grid->addWidget(item, 0, 1);
65 
66  lab = new QLabel(i18n("Value:"));
67  grid->addWidget(lab, 1, 0);
68 
69  QLineEdit *value = new QLineEdit;
70  connect(value, &QLineEdit::textChanged, this, &SieveConditionEnvironment::valueChanged);
71  value->setObjectName(QStringLiteral("value"));
72  grid->addWidget(value, 1, 1);
73 
74  return w;
75 }
76 
77 QString SieveConditionEnvironment::code(QWidget *w) const
78 {
79  const QLineEdit *item = w->findChild<QLineEdit *>(QStringLiteral("item"));
80  const QString itemStr = item->text();
81 
82  const QLineEdit *value = w->findChild<QLineEdit *>(QStringLiteral("value"));
83  const QString valueStr = value->text();
84 
85  return QStringLiteral("environment \"%1\" \"%2\"").arg(itemStr, valueStr) + AutoCreateScriptUtil::generateConditionComment(comment());
86 }
87 
88 QStringList SieveConditionEnvironment::needRequires(QWidget *) const
89 {
90  return QStringList() << QStringLiteral("environment");
91 }
92 
93 bool SieveConditionEnvironment::needCheckIfServerHasCapability() const
94 {
95  return true;
96 }
97 
98 QString SieveConditionEnvironment::serverNeedsCapability() const
99 {
100  return QStringLiteral("environment");
101 }
102 
103 QString SieveConditionEnvironment::help() const
104 {
105  return i18n("The environment test retrieves the item of environment information specified by the name string and matches it to the values specified in the key-list argument.");
106 }
107 
108 void SieveConditionEnvironment::setParamWidgetValue(QXmlStreamReader &element, QWidget *w, bool /*notCondition*/, QString &error)
109 {
110  int index = 0;
111  QString commentStr;
112  while (element.readNextStartElement()) {
113  const QStringRef tagName = element.name();
114  if (tagName == QLatin1String("str")) {
115  if (index == 0) {
116  QLineEdit *item = w->findChild<QLineEdit *>(QStringLiteral("item"));
117  item->setText(AutoCreateScriptUtil::quoteStr(element.readElementText()));
118  } else if (index == 1) {
119  QLineEdit *value = w->findChild<QLineEdit *>(QStringLiteral("value"));
120  value->setText(AutoCreateScriptUtil::quoteStr(element.readElementText()));
121  } else {
122  tooManyArguments(tagName, index, 2, error);
123  qCDebug(LIBKSIEVE_LOG) << " SieveConditionEnvironment::setParamWidgetValue to many argument " << index;
124  }
125  ++index;
126  } else if (tagName == QLatin1String("crlf")) {
127  element.skipCurrentElement();
128  //nothing
129  } else if (tagName == QLatin1String("comment")) {
130  commentStr = AutoCreateScriptUtil::loadConditionComment(commentStr, element.readElementText());
131  } else {
132  unknownTag(tagName, error);
133  qCDebug(LIBKSIEVE_LOG) << " SieveActionSetVariable::setParamWidgetValue unknown tagName " << tagName;
134  }
135  }
136  if (!commentStr.isEmpty()) {
137  setComment(commentStr);
138  }
139 }
140 
141 QUrl KSieveUi::SieveConditionEnvironment::href() const
142 {
143  return SieveEditorUtil::helpUrl(SieveEditorUtil::strToVariableName(name()));
144 }
void setCaseSensitivity(Qt::CaseSensitivity caseSensitivity)
bool readNextStartElement()
void setContentsMargins(int left, int top, int right, int bottom)
QString name(const QVariant &location)
void addWidget(QWidget *widget, int row, int column, Qt::Alignment alignment)
void textChanged(const QString &text)
void skipCurrentElement()
void setLayout(QLayout *layout)
QString readElementText(QXmlStreamReader::ReadElementTextBehaviour behaviour)
CaseInsensitive
void setObjectName(const QString &name)
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
void setCompleter(QCompleter *c)
T findChild(const QString &name, Qt::FindChildOptions options) const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Thu Jul 9 2020 23:07:44 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.