Libksieve

sieveconditionspamtest.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 "sieveconditionspamtest.h"
7#include "autocreatescripts/autocreatescriptutil_p.h"
8#include "autocreatescripts/sieveeditorgraphicalmodewidget.h"
9#include "editor/sieveeditorutil.h"
10#include "widgets/selectcomparatorcombobox.h"
11#include "widgets/selectrelationalmatchtype.h"
12
13#include <KLocalizedString>
14
15#include "libksieveui_debug.h"
16#include <QCheckBox>
17#include <QSpinBox>
18#include <QVBoxLayout>
19#include <QXmlStreamReader>
20
21using namespace KSieveUi;
22
23SieveConditionSpamTest::SieveConditionSpamTest(SieveEditorGraphicalModeWidget *sieveGraphicalModeWidget, QObject *parent)
24 : SieveCondition(sieveGraphicalModeWidget, QStringLiteral("spamtest"), i18n("Spam Test"), parent)
25{
26 mHasSpamTestPlusSupport = sieveCapabilities().contains(QLatin1StringView("spamtestplus"));
27}
28
29QWidget *SieveConditionSpamTest::createParamWidget(QWidget *parent) const
30{
31 auto w = new QWidget(parent);
32 auto lay = new QVBoxLayout;
33 lay->setContentsMargins({});
34 w->setLayout(lay);
35
36 if (mHasSpamTestPlusSupport) {
37 auto percent = new QCheckBox(i18nc("@option:check", "Percent"));
38 percent->setObjectName(QLatin1StringView("percent"));
39 connect(percent, &QCheckBox::toggled, this, &SieveConditionSpamTest::valueChanged);
40 lay->addWidget(percent);
41 }
42
43 auto relation = new SelectRelationalMatchType;
44 connect(relation, &SelectRelationalMatchType::valueChanged, this, &SieveConditionSpamTest::valueChanged);
45 relation->setObjectName(QLatin1StringView("relation"));
46 lay->addWidget(relation);
47
48 auto comparator = new SelectComparatorComboBox(mSieveGraphicalModeWidget);
49 connect(comparator, &SelectComparatorComboBox::valueChanged, this, &SieveConditionSpamTest::valueChanged);
50 comparator->setObjectName(QLatin1StringView("comparator"));
51 lay->addWidget(comparator);
52
53 auto spinbox = new QSpinBox;
54 spinbox->setMaximum(10);
55 spinbox->setMinimum(0);
56 spinbox->setObjectName(QLatin1StringView("value"));
57 connect(spinbox, &QSpinBox::valueChanged, this, &SieveConditionSpamTest::valueChanged);
58 lay->addWidget(spinbox);
59 return w;
60}
61
62QString SieveConditionSpamTest::code(QWidget *w) const
63{
64 QString percentStr;
65 if (mHasSpamTestPlusSupport) {
66 const QCheckBox *checkbox = w->findChild<QCheckBox *>(QStringLiteral("percent"));
67 percentStr = checkbox->isChecked() ? QStringLiteral(":percent") : QString();
68 }
69
70 const SelectRelationalMatchType *relation = w->findChild<SelectRelationalMatchType *>(QStringLiteral("relation"));
71 const QString relationStr = relation->code();
72
73 const SelectComparatorComboBox *comparator = w->findChild<SelectComparatorComboBox *>(QStringLiteral("comparator"));
74 const QString comparatorStr = comparator->code();
75
76 const QSpinBox *spinbox = w->findChild<QSpinBox *>(QStringLiteral("value"));
77 const QString value = QString::number(spinbox->value());
78
79 return QStringLiteral("spamtest %1 %2 %3 \"%4\"").arg(percentStr, relationStr, comparatorStr, value)
80 + AutoCreateScriptUtil::generateConditionComment(comment());
81}
82
83bool SieveConditionSpamTest::needCheckIfServerHasCapability() const
84{
85 return true;
86}
87
88QString SieveConditionSpamTest::serverNeedsCapability() const
89{
90 return QStringLiteral("spamtest");
91}
92
93QStringList SieveConditionSpamTest::needRequires(QWidget *w) const
94{
95 const SelectComparatorComboBox *comparator = w->findChild<SelectComparatorComboBox *>(QStringLiteral("comparator"));
96 QStringList lst;
97 lst << QStringLiteral("spamtest") << QStringLiteral("relational");
98 const QString comparatorRequires = comparator->require();
99 if (!comparatorRequires.isEmpty()) {
100 lst << comparatorRequires;
101 }
102 if (mHasSpamTestPlusSupport) {
103 lst << QStringLiteral("spamtestplus");
104 }
105 return lst;
106}
107
108QString SieveConditionSpamTest::help() const
109{
110 return i18n(
111 "Sieve implementations that implement the \"spamtest\" test use an identifier of either \"spamtest\" or \"spamtestplus\" for use with the capability "
112 "mechanism.");
113}
114
115void SieveConditionSpamTest::setParamWidgetValue(QXmlStreamReader &element, QWidget *w, bool /*notCondition*/, QString &error)
116{
117 QString commentStr;
118 while (element.readNextStartElement()) {
119 const QStringView tagName = element.name();
120 if (tagName == QLatin1StringView("tag")) {
121 const QString tagValue = element.readElementText();
122 if (tagValue == QLatin1StringView("count") || tagValue == QLatin1StringView("value")) {
123 if (element.readNextStartElement()) {
124 if (element.name() == QLatin1StringView("str")) {
125 auto relation = w->findChild<SelectRelationalMatchType *>(QStringLiteral("relation"));
126 relation->setCode(AutoCreateScriptUtil::tagValue(tagValue), element.readElementText(), name(), error);
127 }
128 }
129 } else if (tagValue == QLatin1StringView("comparator")) {
130 if (element.readNextStartElement()) {
131 if (element.name() == QLatin1StringView("str")) {
132 auto comparator = w->findChild<SelectComparatorComboBox *>(QStringLiteral("comparator"));
133 comparator->setCode(element.readElementText(), name(), error);
134 }
135 }
136 } else if (tagValue == QLatin1StringView("percent")) {
137 if (mHasSpamTestPlusSupport) {
138 auto checkbox = w->findChild<QCheckBox *>(QStringLiteral("percent"));
139 checkbox->setChecked(true);
140 } else {
141 serverDoesNotSupportFeatures(QStringLiteral("percent"), error);
142 qCDebug(LIBKSIEVEUI_LOG) << " SieveConditionSpamTest::setParamWidgetValue server has not percent support";
143 }
144 } else {
145 unknownTagValue(tagValue, error);
146 qCDebug(LIBKSIEVEUI_LOG) << " SieveConditionSpamTest::setParamWidgetValue unknown tagvalue " << tagValue;
147 }
148 } else if (tagName == QLatin1StringView("str")) {
149 auto spinbox = w->findChild<QSpinBox *>(QStringLiteral("value"));
150 spinbox->setValue(element.readElementText().toInt());
151 } else if (tagName == QLatin1StringView("crlf")) {
152 element.skipCurrentElement();
153 // nothing
154 } else if (tagName == QLatin1StringView("comment")) {
155 commentStr = AutoCreateScriptUtil::loadConditionComment(commentStr, element.readElementText());
156 } else {
157 unknownTag(tagName, error);
158 qCDebug(LIBKSIEVEUI_LOG) << " SieveSpamTest::setParamWidgetValue unknown tagName " << tagName;
159 }
160 }
161 if (!commentStr.isEmpty()) {
162 setComment(commentStr);
163 }
164}
165
166QUrl SieveConditionSpamTest::href() const
167{
168 return SieveEditorUtil::helpUrl(SieveEditorUtil::strToVariableName(name()));
169}
170
171#include "moc_sieveconditionspamtest.cpp"
QString i18nc(const char *context, const char *text, const TYPE &arg...)
QString i18n(const char *text, const TYPE &arg...)
QByteArray tagValue(const Elem &elem, const char *keyName)
bool isChecked() const const
void toggled(bool checked)
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 Mon Nov 18 2024 12:14:30 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.