File indexing completed on 2024-12-29 04:54:43

0001 /*
0002    SPDX-FileCopyrightText: 2013-2024 Laurent Montel <montel@kde.org>
0003 
0004    SPDX-License-Identifier: LGPL-2.0-or-later
0005 */
0006 #include "sieveconditionvirustest.h"
0007 #include "autocreatescripts/autocreatescriptutil_p.h"
0008 #include "editor/sieveeditorutil.h"
0009 #include "widgets/selectcomparatorcombobox.h"
0010 #include "widgets/selectrelationalmatchtype.h"
0011 
0012 #include <KLocalizedString>
0013 
0014 #include "libksieveui_debug.h"
0015 #include <QSpinBox>
0016 #include <QVBoxLayout>
0017 #include <QXmlStreamReader>
0018 
0019 using namespace KSieveUi;
0020 
0021 SieveConditionVirusTest::SieveConditionVirusTest(SieveEditorGraphicalModeWidget *sieveGraphicalModeWidget, QObject *parent)
0022     : SieveCondition(sieveGraphicalModeWidget, QStringLiteral("virustest"), i18n("Virus Test"), parent)
0023 {
0024 }
0025 
0026 QWidget *SieveConditionVirusTest::createParamWidget(QWidget *parent) const
0027 {
0028     auto w = new QWidget(parent);
0029     auto lay = new QVBoxLayout;
0030     lay->setContentsMargins({});
0031     w->setLayout(lay);
0032 
0033     auto relation = new SelectRelationalMatchType;
0034     connect(relation, &SelectRelationalMatchType::valueChanged, this, &SieveConditionVirusTest::valueChanged);
0035     relation->setObjectName(QLatin1StringView("relation"));
0036     lay->addWidget(relation);
0037 
0038     auto comparator = new SelectComparatorComboBox(mSieveGraphicalModeWidget);
0039     connect(comparator, &SelectComparatorComboBox::valueChanged, this, &SieveConditionVirusTest::valueChanged);
0040     comparator->setObjectName(QLatin1StringView("comparator"));
0041     lay->addWidget(comparator);
0042 
0043     auto spinbox = new QSpinBox;
0044     spinbox->setMaximum(5);
0045     spinbox->setMinimum(0);
0046     spinbox->setObjectName(QLatin1StringView("value"));
0047     connect(spinbox, &QSpinBox::valueChanged, this, &SieveConditionVirusTest::valueChanged);
0048     lay->addWidget(spinbox);
0049     return w;
0050 }
0051 
0052 QString SieveConditionVirusTest::code(QWidget *w) const
0053 {
0054     const SelectRelationalMatchType *relation = w->findChild<SelectRelationalMatchType *>(QStringLiteral("relation"));
0055     const QString relationStr = relation->code();
0056 
0057     const SelectComparatorComboBox *comparator = w->findChild<SelectComparatorComboBox *>(QStringLiteral("comparator"));
0058     const QString comparatorStr = comparator->code();
0059 
0060     const QSpinBox *spinbox = w->findChild<QSpinBox *>(QStringLiteral("value"));
0061     const QString value = QString::number(spinbox->value());
0062 
0063     return QStringLiteral("virustest %1 %2 \"%3\"").arg(relationStr, comparatorStr, value) + AutoCreateScriptUtil::generateConditionComment(comment());
0064 }
0065 
0066 bool SieveConditionVirusTest::needCheckIfServerHasCapability() const
0067 {
0068     return true;
0069 }
0070 
0071 QString SieveConditionVirusTest::serverNeedsCapability() const
0072 {
0073     return QStringLiteral("virustest");
0074 }
0075 
0076 QStringList SieveConditionVirusTest::needRequires(QWidget *w) const
0077 {
0078     const SelectComparatorComboBox *comparator = w->findChild<SelectComparatorComboBox *>(QStringLiteral("comparator"));
0079     const QString comparatorRequires = comparator->require();
0080     QStringList lst;
0081     lst << QStringLiteral("spamtest") << QStringLiteral("relational");
0082     if (!comparatorRequires.isEmpty()) {
0083         lst << comparatorRequires;
0084     }
0085     return lst;
0086 }
0087 
0088 QString SieveConditionVirusTest::help() const
0089 {
0090     return i18n("Sieve implementations that implement the \"virustest\" test have an identifier of \"virustest\" for use with the capability mechanism.");
0091 }
0092 
0093 void SieveConditionVirusTest::setParamWidgetValue(QXmlStreamReader &element, QWidget *w, bool /*notCondition*/, QString &error)
0094 {
0095     QString commentStr;
0096     while (element.readNextStartElement()) {
0097         const QStringView tagName = element.name();
0098 
0099         if (tagName == QLatin1StringView("tag")) {
0100             const QString tagValue = element.readElementText();
0101             if (tagValue == QLatin1StringView("count") || tagValue == QLatin1StringView("value")) {
0102                 if (element.readNextStartElement()) {
0103                     if (element.name() == QLatin1StringView("str")) {
0104                         auto relation = w->findChild<SelectRelationalMatchType *>(QStringLiteral("relation"));
0105                         relation->setCode(AutoCreateScriptUtil::tagValue(tagValue), element.readElementText(), name(), error);
0106                     }
0107                 }
0108             } else if (tagValue == QLatin1StringView("comparator")) {
0109                 if (element.readNextStartElement()) {
0110                     if (element.name() == QLatin1StringView("str")) {
0111                         auto comparator = w->findChild<SelectComparatorComboBox *>(QStringLiteral("comparator"));
0112                         comparator->setCode(element.readElementText(), name(), error);
0113                     }
0114                 }
0115             } else {
0116                 unknownTagValue(tagValue, error);
0117                 qCDebug(LIBKSIEVEUI_LOG) << " SieveConditionVirusTest::setParamWidgetValue unknown tagValue " << tagValue;
0118             }
0119         } else if (tagName == QLatin1StringView("str")) {
0120             auto spinbox = w->findChild<QSpinBox *>(QStringLiteral("value"));
0121             spinbox->setValue(element.readElementText().toInt());
0122         } else if (tagName == QLatin1StringView("crlf")) {
0123             element.skipCurrentElement();
0124             // nothing
0125         } else if (tagName == QLatin1StringView("comment")) {
0126             commentStr = AutoCreateScriptUtil::loadConditionComment(commentStr, element.readElementText());
0127         } else {
0128             unknownTag(tagName, error);
0129             qCDebug(LIBKSIEVEUI_LOG) << " SieveConditionVirusTest::setParamWidgetValue unknown tagName " << tagName;
0130         }
0131     }
0132     if (!commentStr.isEmpty()) {
0133         setComment(commentStr);
0134     }
0135 }
0136 
0137 QUrl SieveConditionVirusTest::href() const
0138 {
0139     return SieveEditorUtil::helpUrl(SieveEditorUtil::strToVariableName(name()));
0140 }
0141 
0142 #include "moc_sieveconditionvirustest.cpp"