Libksieve

sieveconditionvirustest.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 "sieveconditionvirustest.h"
20 #include "autocreatescripts/autocreatescriptutil_p.h"
21 #include "editor/sieveeditorutil.h"
22 #include "widgets/selectrelationalmatchtype.h"
23 #include "widgets/selectcomparatorcombobox.h"
24 
25 #include <KLocalizedString>
26 
27 #include <QSpinBox>
28 #include "libksieve_debug.h"
29 #include <QXmlStreamReader>
30 #include <QVBoxLayout>
31 
32 using namespace KSieveUi;
33 
34 SieveConditionVirusTest::SieveConditionVirusTest(SieveEditorGraphicalModeWidget *sieveGraphicalModeWidget, QObject *parent)
35  : SieveCondition(sieveGraphicalModeWidget, QStringLiteral("virustest"), i18n("Virus Test"), parent)
36 {
37 }
38 
39 QWidget *SieveConditionVirusTest::createParamWidget(QWidget *parent) const
40 {
41  QWidget *w = new QWidget(parent);
42  QVBoxLayout *lay = new QVBoxLayout;
43  lay->setContentsMargins(0, 0, 0, 0);
44  w->setLayout(lay);
45 
46  SelectRelationalMatchType *relation = new SelectRelationalMatchType;
47  connect(relation, &SelectRelationalMatchType::valueChanged, this, &SieveConditionVirusTest::valueChanged);
48  relation->setObjectName(QStringLiteral("relation"));
49  lay->addWidget(relation);
50 
51  SelectComparatorComboBox *comparator = new SelectComparatorComboBox(mSieveGraphicalModeWidget);
52  connect(comparator, &SelectComparatorComboBox::valueChanged, this, &SieveConditionVirusTest::valueChanged);
53  comparator->setObjectName(QStringLiteral("comparator"));
54  lay->addWidget(comparator);
55 
56  QSpinBox *spinbox = new QSpinBox;
57  spinbox->setMaximum(5);
58  spinbox->setMinimum(0);
59  spinbox->setObjectName(QStringLiteral("value"));
60  connect(spinbox, QOverload<int>::of(&QSpinBox::valueChanged), this, &SieveConditionVirusTest::valueChanged);
61  lay->addWidget(spinbox);
62  return w;
63 }
64 
65 QString SieveConditionVirusTest::code(QWidget *w) const
66 {
67  const SelectRelationalMatchType *relation = w->findChild<SelectRelationalMatchType *>(QStringLiteral("relation"));
68  const QString relationStr = relation->code();
69 
70  const SelectComparatorComboBox *comparator = w->findChild<SelectComparatorComboBox *>(QStringLiteral("comparator"));
71  const QString comparatorStr = comparator->code();
72 
73  const QSpinBox *spinbox = w->findChild<QSpinBox *>(QStringLiteral("value"));
74  const QString value = QString::number(spinbox->value());
75 
76  return QStringLiteral("virustest %1 %2 \"%3\"").arg(relationStr, comparatorStr, value) + AutoCreateScriptUtil::generateConditionComment(comment());
77 }
78 
79 bool SieveConditionVirusTest::needCheckIfServerHasCapability() const
80 {
81  return true;
82 }
83 
84 QString SieveConditionVirusTest::serverNeedsCapability() const
85 {
86  return QStringLiteral("virustest");
87 }
88 
89 QStringList SieveConditionVirusTest::needRequires(QWidget *w) const
90 {
91  const SelectComparatorComboBox *comparator = w->findChild<SelectComparatorComboBox *>(QStringLiteral("comparator"));
92  const QString comparatorRequires = comparator->require();
93  QStringList lst;
94  lst << QStringLiteral("spamtest") << QStringLiteral("relational");
95  if (!comparatorRequires.isEmpty()) {
96  lst << comparatorRequires;
97  }
98  return lst;
99 }
100 
101 QString SieveConditionVirusTest::help() const
102 {
103  return i18n("Sieve implementations that implement the \"virustest\" test have an identifier of \"virustest\" for use with the capability mechanism.");
104 }
105 
106 void SieveConditionVirusTest::setParamWidgetValue(QXmlStreamReader &element, QWidget *w, bool /*notCondition*/, QString &error)
107 {
108  QString commentStr;
109  while (element.readNextStartElement()) {
110  const QStringRef tagName = element.name();
111 
112  if (tagName == QLatin1String("tag")) {
113  const QString tagValue = element.readElementText();
114  if (tagValue == QLatin1String("count") || tagValue == QLatin1String("value")) {
115  if (element.readNextStartElement()) {
116  if (element.name() == QLatin1String("str")) {
117  SelectRelationalMatchType *relation = w->findChild<SelectRelationalMatchType *>(QStringLiteral("relation"));
118  relation->setCode(AutoCreateScriptUtil::tagValue(tagValue), element.readElementText(), name(), error);
119  }
120  }
121  } else if (tagValue == QLatin1String("comparator")) {
122  if (element.readNextStartElement()) {
123  if (element.name() == QLatin1String("str")) {
124  SelectComparatorComboBox *comparator = w->findChild<SelectComparatorComboBox *>(QStringLiteral("comparator"));
125  comparator->setCode(element.readElementText(), name(), error);
126  }
127  }
128  } else {
129  unknownTagValue(tagValue, error);
130  qCDebug(LIBKSIEVE_LOG) << " SieveConditionVirusTest::setParamWidgetValue unknown tagValue " << tagValue;
131  }
132  } else if (tagName == QLatin1String("str")) {
133  QSpinBox *spinbox = w->findChild<QSpinBox *>(QStringLiteral("value"));
134  spinbox->setValue(element.readElementText().toInt());
135  } else if (tagName == QLatin1String("crlf")) {
136  element.skipCurrentElement();
137  //nothing
138  } else if (tagName == QLatin1String("comment")) {
139  commentStr = AutoCreateScriptUtil::loadConditionComment(commentStr, element.readElementText());
140  } else {
141  unknownTag(tagName, error);
142  qCDebug(LIBKSIEVE_LOG) << " SieveConditionVirusTest::setParamWidgetValue unknown tagName " << tagName;
143  }
144  }
145  if (!commentStr.isEmpty()) {
146  setComment(commentStr);
147  }
148 }
149 
150 QUrl SieveConditionVirusTest::href() const
151 {
152  return SieveEditorUtil::helpUrl(SieveEditorUtil::strToVariableName(name()));
153 }
void setMinimum(int min)
bool readNextStartElement()
void setContentsMargins(int left, int top, int right, int bottom)
QString name(const QVariant &location)
void valueChanged(int i)
void skipCurrentElement()
void addWidget(QWidget *widget, int stretch, Qt::Alignment alignment)
QString number(int n, int base)
void setLayout(QLayout *layout)
QString readElementText(QXmlStreamReader::ReadElementTextBehaviour behaviour)
int toInt(bool *ok, int base) const const
void setObjectName(const QString &name)
bool isEmpty() const const
void setMaximum(int max)
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)
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-2020 The KDE developers.
Generated on Fri Jun 5 2020 23:09:20 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.