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"
13#include <KLocalizedString>
15#include "libksieveui_debug.h"
19#include <QXmlStreamReader>
21using namespace KSieveUi;
23SieveConditionSpamTest::SieveConditionSpamTest(SieveEditorGraphicalModeWidget *sieveGraphicalModeWidget,
QObject *parent)
24 : SieveCondition(sieveGraphicalModeWidget, QStringLiteral(
"spamtest"),
i18n(
"Spam Test"), parent)
26 mHasSpamTestPlusSupport = sieveCapabilities().contains(
QLatin1StringView(
"spamtestplus"));
29QWidget *SieveConditionSpamTest::createParamWidget(
QWidget *parent)
const
31 auto w =
new QWidget(
parent);
32 auto lay =
new QVBoxLayout;
33 lay->setContentsMargins({});
36 if (mHasSpamTestPlusSupport) {
37 auto percent =
new QCheckBox(
i18nc(
"@option:check",
"Percent"));
38 percent->setObjectName(QLatin1StringView(
"percent"));
40 lay->addWidget(percent);
43 auto relation =
new SelectRelationalMatchType;
44 connect(relation, &SelectRelationalMatchType::valueChanged,
this, &SieveConditionSpamTest::valueChanged);
45 relation->setObjectName(QLatin1StringView(
"relation"));
46 lay->addWidget(relation);
48 auto comparator =
new SelectComparatorComboBox(mSieveGraphicalModeWidget);
49 connect(comparator, &SelectComparatorComboBox::valueChanged,
this, &SieveConditionSpamTest::valueChanged);
50 comparator->setObjectName(QLatin1StringView(
"comparator"));
51 lay->addWidget(comparator);
53 auto spinbox =
new QSpinBox;
54 spinbox->setMaximum(10);
55 spinbox->setMinimum(0);
56 spinbox->setObjectName(QLatin1StringView(
"value"));
58 lay->addWidget(spinbox);
62QString SieveConditionSpamTest::code(QWidget *w)
const
65 if (mHasSpamTestPlusSupport) {
66 const QCheckBox *checkbox = w->
findChild<QCheckBox *>(QStringLiteral(
"percent"));
67 percentStr = checkbox->
isChecked() ? QStringLiteral(
":percent") : QString();
70 const SelectRelationalMatchType *relation = w->
findChild<SelectRelationalMatchType *>(QStringLiteral(
"relation"));
71 const QString relationStr = relation->code();
73 const SelectComparatorComboBox *comparator = w->
findChild<SelectComparatorComboBox *>(QStringLiteral(
"comparator"));
74 const QString comparatorStr = comparator->code();
76 const QSpinBox *spinbox = w->
findChild<QSpinBox *>(QStringLiteral(
"value"));
79 return QStringLiteral(
"spamtest %1 %2 %3 \"%4\"").arg(percentStr, relationStr, comparatorStr, value)
80 + AutoCreateScriptUtil::generateConditionComment(comment());
83bool SieveConditionSpamTest::needCheckIfServerHasCapability()
const
88QString SieveConditionSpamTest::serverNeedsCapability()
const
90 return QStringLiteral(
"spamtest");
93QStringList SieveConditionSpamTest::needRequires(QWidget *w)
const
95 const SelectComparatorComboBox *comparator = w->
findChild<SelectComparatorComboBox *>(QStringLiteral(
"comparator"));
97 lst << QStringLiteral(
"spamtest") << QStringLiteral(
"relational");
98 const QString comparatorRequires = comparator->require();
99 if (!comparatorRequires.
isEmpty()) {
100 lst << comparatorRequires;
102 if (mHasSpamTestPlusSupport) {
103 lst << QStringLiteral(
"spamtestplus");
108QString SieveConditionSpamTest::help()
const
111 "Sieve implementations that implement the \"spamtest\" test use an identifier of either \"spamtest\" or \"spamtestplus\" for use with the capability "
115void SieveConditionSpamTest::setParamWidgetValue(QXmlStreamReader &element, QWidget *w,
bool , QString &error)
119 const QStringView tagName = element.
name();
120 if (tagName == QLatin1StringView(
"tag")) {
122 if (tagValue == QLatin1StringView(
"count") || tagValue == QLatin1StringView(
"value")) {
124 if (element.
name() == QLatin1StringView(
"str")) {
125 auto relation = w->
findChild<SelectRelationalMatchType *>(QStringLiteral(
"relation"));
126 relation->setCode(AutoCreateScriptUtil::tagValue(tagValue), element.
readElementText(), name(), error);
129 }
else if (tagValue == QLatin1StringView(
"comparator")) {
131 if (element.
name() == QLatin1StringView(
"str")) {
132 auto comparator = w->
findChild<SelectComparatorComboBox *>(QStringLiteral(
"comparator"));
136 }
else if (tagValue == QLatin1StringView(
"percent")) {
137 if (mHasSpamTestPlusSupport) {
138 auto checkbox = w->
findChild<QCheckBox *>(QStringLiteral(
"percent"));
141 serverDoesNotSupportFeatures(QStringLiteral(
"percent"), error);
142 qCDebug(LIBKSIEVEUI_LOG) <<
" SieveConditionSpamTest::setParamWidgetValue server has not percent support";
145 unknownTagValue(tagValue, error);
146 qCDebug(LIBKSIEVEUI_LOG) <<
" SieveConditionSpamTest::setParamWidgetValue unknown tagvalue " <<
tagValue;
148 }
else if (tagName == QLatin1StringView(
"str")) {
149 auto spinbox = w->
findChild<QSpinBox *>(QStringLiteral(
"value"));
151 }
else if (tagName == QLatin1StringView(
"crlf")) {
154 }
else if (tagName == QLatin1StringView(
"comment")) {
155 commentStr = AutoCreateScriptUtil::loadConditionComment(commentStr, element.
readElementText());
157 unknownTag(tagName, error);
158 qCDebug(LIBKSIEVEUI_LOG) <<
" SieveSpamTest::setParamWidgetValue unknown tagName " << tagName;
162 setComment(commentStr);
166QUrl SieveConditionSpamTest::href()
const
168 return SieveEditorUtil::helpUrl(SieveEditorUtil::strToVariableName(name()));
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)
QMetaObject::Connection connect(const QObject *sender, PointerToMemberFunction signal, Functor functor)
T findChild(const QString &name, Qt::FindChildOptions options) const const
QObject * parent() const const
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()