Libksieve

sieveconditionvirustest.cpp
1/*
2 SPDX-FileCopyrightText: 2013-2024 Laurent Montel <montel@kde.org>
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 "libksieveui_debug.h"
15#include <QSpinBox>
16#include <QVBoxLayout>
17#include <QXmlStreamReader>
18
19using namespace KSieveUi;
20
21SieveConditionVirusTest::SieveConditionVirusTest(SieveEditorGraphicalModeWidget *sieveGraphicalModeWidget, QObject *parent)
22 : SieveCondition(sieveGraphicalModeWidget, QStringLiteral("virustest"), i18n("Virus Test"), parent)
23{
24}
25
26QWidget *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(QLatin1StringView("relation"));
36 lay->addWidget(relation);
37
38 auto comparator = new SelectComparatorComboBox(mSieveGraphicalModeWidget);
39 connect(comparator, &SelectComparatorComboBox::valueChanged, this, &SieveConditionVirusTest::valueChanged);
40 comparator->setObjectName(QLatin1StringView("comparator"));
41 lay->addWidget(comparator);
42
43 auto spinbox = new QSpinBox;
44 spinbox->setMaximum(5);
45 spinbox->setMinimum(0);
46 spinbox->setObjectName(QLatin1StringView("value"));
47 connect(spinbox, &QSpinBox::valueChanged, this, &SieveConditionVirusTest::valueChanged);
48 lay->addWidget(spinbox);
49 return w;
50}
51
52QString 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
66bool SieveConditionVirusTest::needCheckIfServerHasCapability() const
67{
68 return true;
69}
70
71QString SieveConditionVirusTest::serverNeedsCapability() const
72{
73 return QStringLiteral("virustest");
74}
75
76QStringList 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
88QString 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
93void SieveConditionVirusTest::setParamWidgetValue(QXmlStreamReader &element, QWidget *w, bool /*notCondition*/, QString &error)
94{
95 QString commentStr;
96 while (element.readNextStartElement()) {
97 const QStringView tagName = element.name();
98
99 if (tagName == QLatin1StringView("tag")) {
100 const QString tagValue = element.readElementText();
101 if (tagValue == QLatin1StringView("count") || tagValue == QLatin1StringView("value")) {
102 if (element.readNextStartElement()) {
103 if (element.name() == QLatin1StringView("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 == QLatin1StringView("comparator")) {
109 if (element.readNextStartElement()) {
110 if (element.name() == QLatin1StringView("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(LIBKSIEVEUI_LOG) << " SieveConditionVirusTest::setParamWidgetValue unknown tagValue " << tagValue;
118 }
119 } else if (tagName == QLatin1StringView("str")) {
120 auto spinbox = w->findChild<QSpinBox *>(QStringLiteral("value"));
121 spinbox->setValue(element.readElementText().toInt());
122 } else if (tagName == QLatin1StringView("crlf")) {
123 element.skipCurrentElement();
124 // nothing
125 } else if (tagName == QLatin1StringView("comment")) {
126 commentStr = AutoCreateScriptUtil::loadConditionComment(commentStr, element.readElementText());
127 } else {
128 unknownTag(tagName, error);
129 qCDebug(LIBKSIEVEUI_LOG) << " SieveConditionVirusTest::setParamWidgetValue unknown tagName " << tagName;
130 }
131 }
132 if (!commentStr.isEmpty()) {
133 setComment(commentStr);
134 }
135}
136
137QUrl SieveConditionVirusTest::href() const
138{
139 return SieveEditorUtil::helpUrl(SieveEditorUtil::strToVariableName(name()));
140}
141
142#include "moc_sieveconditionvirustest.cpp"
QString i18n(const char *text, const TYPE &arg...)
QByteArray tagValue(const Elem &elem, const char *keyName)
void setContentsMargins(const QMargins &margins)
QMetaObject::Connection connect(const QObject *sender, PointerToMemberFunction signal, Functor functor)
T findChild(const QString &name, Qt::FindChildOptions options) const const
QObject * parent() const const
void setMaximum(int max)
void valueChanged(int i)
bool isEmpty() const const
QString number(double n, char format, int precision)
int toInt(bool *ok, int base) const const
QStringView name() const const
QString readElementText(ReadElementTextBehaviour behaviour)
bool readNextStartElement()
void skipCurrentElement()
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Sat Dec 21 2024 16:57:10 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.