Libksieve

sieveconditionvirustest.cpp
1 /*
2  SPDX-FileCopyrightText: 2013-2021 Laurent Montel <[email protected]>
3 
4  SPDX-License-Identifier: LGPL-2.0-or-later
5 */
6 #include "sieveconditionvirustest.h"
7 #include "autocreatescripts/autocreatescriptutil_p.h"
8 #include "editor/sieveeditorutil.h"
9 #include "widgets/selectcomparatorcombobox.h"
10 #include "widgets/selectrelationalmatchtype.h"
11 
12 #include <KLocalizedString>
13 
14 #include "libksieve_debug.h"
15 #include <QSpinBox>
16 #include <QVBoxLayout>
17 #include <QXmlStreamReader>
18 
19 using namespace KSieveUi;
20 
21 SieveConditionVirusTest::SieveConditionVirusTest(SieveEditorGraphicalModeWidget *sieveGraphicalModeWidget, QObject *parent)
22  : SieveCondition(sieveGraphicalModeWidget, QStringLiteral("virustest"), i18n("Virus Test"), parent)
23 {
24 }
25 
26 QWidget *SieveConditionVirusTest::createParamWidget(QWidget *parent) const
27 {
28  auto w = new QWidget(parent);
29  auto lay = new QVBoxLayout;
30  lay->setContentsMargins({});
31  w->setLayout(lay);
32 
33  auto relation = new SelectRelationalMatchType;
34  connect(relation, &SelectRelationalMatchType::valueChanged, this, &SieveConditionVirusTest::valueChanged);
35  relation->setObjectName(QStringLiteral("relation"));
36  lay->addWidget(relation);
37 
38  auto comparator = new SelectComparatorComboBox(mSieveGraphicalModeWidget);
39  connect(comparator, &SelectComparatorComboBox::valueChanged, this, &SieveConditionVirusTest::valueChanged);
40  comparator->setObjectName(QStringLiteral("comparator"));
41  lay->addWidget(comparator);
42 
43  auto spinbox = new QSpinBox;
44  spinbox->setMaximum(5);
45  spinbox->setMinimum(0);
46  spinbox->setObjectName(QStringLiteral("value"));
47  connect(spinbox, QOverload<int>::of(&QSpinBox::valueChanged), this, &SieveConditionVirusTest::valueChanged);
48  lay->addWidget(spinbox);
49  return w;
50 }
51 
52 QString SieveConditionVirusTest::code(QWidget *w) const
53 {
54  const SelectRelationalMatchType *relation = w->findChild<SelectRelationalMatchType *>(QStringLiteral("relation"));
55  const QString relationStr = relation->code();
56 
57  const SelectComparatorComboBox *comparator = w->findChild<SelectComparatorComboBox *>(QStringLiteral("comparator"));
58  const QString comparatorStr = comparator->code();
59 
60  const QSpinBox *spinbox = w->findChild<QSpinBox *>(QStringLiteral("value"));
61  const QString value = QString::number(spinbox->value());
62 
63  return QStringLiteral("virustest %1 %2 \"%3\"").arg(relationStr, comparatorStr, value) + AutoCreateScriptUtil::generateConditionComment(comment());
64 }
65 
66 bool SieveConditionVirusTest::needCheckIfServerHasCapability() const
67 {
68  return true;
69 }
70 
71 QString SieveConditionVirusTest::serverNeedsCapability() const
72 {
73  return QStringLiteral("virustest");
74 }
75 
76 QStringList SieveConditionVirusTest::needRequires(QWidget *w) const
77 {
78  const SelectComparatorComboBox *comparator = w->findChild<SelectComparatorComboBox *>(QStringLiteral("comparator"));
79  const QString comparatorRequires = comparator->require();
80  QStringList lst;
81  lst << QStringLiteral("spamtest") << QStringLiteral("relational");
82  if (!comparatorRequires.isEmpty()) {
83  lst << comparatorRequires;
84  }
85  return lst;
86 }
87 
88 QString SieveConditionVirusTest::help() const
89 {
90  return i18n("Sieve implementations that implement the \"virustest\" test have an identifier of \"virustest\" for use with the capability mechanism.");
91 }
92 
93 void SieveConditionVirusTest::setParamWidgetValue(QXmlStreamReader &element, QWidget *w, bool /*notCondition*/, QString &error)
94 {
95  QString commentStr;
96  while (element.readNextStartElement()) {
97  const QStringRef tagName = element.name();
98 
99  if (tagName == QLatin1String("tag")) {
100  const QString tagValue = element.readElementText();
101  if (tagValue == QLatin1String("count") || tagValue == QLatin1String("value")) {
102  if (element.readNextStartElement()) {
103  if (element.name() == QLatin1String("str")) {
104  auto relation = w->findChild<SelectRelationalMatchType *>(QStringLiteral("relation"));
105  relation->setCode(AutoCreateScriptUtil::tagValue(tagValue), element.readElementText(), name(), error);
106  }
107  }
108  } else if (tagValue == QLatin1String("comparator")) {
109  if (element.readNextStartElement()) {
110  if (element.name() == QLatin1String("str")) {
111  auto comparator = w->findChild<SelectComparatorComboBox *>(QStringLiteral("comparator"));
112  comparator->setCode(element.readElementText(), name(), error);
113  }
114  }
115  } else {
116  unknownTagValue(tagValue, error);
117  qCDebug(LIBKSIEVE_LOG) << " SieveConditionVirusTest::setParamWidgetValue unknown tagValue " << tagValue;
118  }
119  } else if (tagName == QLatin1String("str")) {
120  auto spinbox = w->findChild<QSpinBox *>(QStringLiteral("value"));
121  spinbox->setValue(element.readElementText().toInt());
122  } else if (tagName == QLatin1String("crlf")) {
123  element.skipCurrentElement();
124  // nothing
125  } else if (tagName == QLatin1String("comment")) {
126  commentStr = AutoCreateScriptUtil::loadConditionComment(commentStr, element.readElementText());
127  } else {
128  unknownTag(tagName, error);
129  qCDebug(LIBKSIEVE_LOG) << " SieveConditionVirusTest::setParamWidgetValue unknown tagName " << tagName;
130  }
131  }
132  if (!commentStr.isEmpty()) {
133  setComment(commentStr);
134  }
135 }
136 
137 QUrl SieveConditionVirusTest::href() const
138 {
139  return SieveEditorUtil::helpUrl(SieveEditorUtil::strToVariableName(name()));
140 }
bool readNextStartElement()
void setContentsMargins(int left, int top, int right, int bottom)
QString name(const QVariant &location)
void valueChanged(int i)
void skipCurrentElement()
QString number(int n, int base)
QString readElementText(QXmlStreamReader::ReadElementTextBehaviour behaviour)
QByteArray tagValue(const Elem &elem, TagKey key)
int toInt(bool *ok, int base) const const
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-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.