Libksieve

sieveconditionspamtest.cpp
1 /*
2  SPDX-FileCopyrightText: 2013-2021 Laurent Montel <[email protected]>
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 "libksieve_debug.h"
16 #include <QCheckBox>
17 #include <QSpinBox>
18 #include <QVBoxLayout>
19 #include <QXmlStreamReader>
20 
21 using namespace KSieveUi;
22 
23 SieveConditionSpamTest::SieveConditionSpamTest(SieveEditorGraphicalModeWidget *sieveGraphicalModeWidget, QObject *parent)
24  : SieveCondition(sieveGraphicalModeWidget, QStringLiteral("spamtest"), i18n("Spam Test"), parent)
25 {
26  mHasSpamTestPlusSupport = sieveCapabilities().contains(QLatin1String("spamtestplus"));
27 }
28 
29 QWidget *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(i18n("Percent"));
38  percent->setObjectName(QStringLiteral("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(QStringLiteral("relation"));
46  lay->addWidget(relation);
47 
48  auto comparator = new SelectComparatorComboBox(mSieveGraphicalModeWidget);
49  connect(comparator, &SelectComparatorComboBox::valueChanged, this, &SieveConditionSpamTest::valueChanged);
50  comparator->setObjectName(QStringLiteral("comparator"));
51  lay->addWidget(comparator);
52 
53  auto spinbox = new QSpinBox;
54  spinbox->setMaximum(10);
55  spinbox->setMinimum(0);
56  spinbox->setObjectName(QStringLiteral("value"));
57  connect(spinbox, QOverload<int>::of(&QSpinBox::valueChanged), this, &SieveConditionSpamTest::valueChanged);
58  lay->addWidget(spinbox);
59  return w;
60 }
61 
62 QString 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 
83 bool SieveConditionSpamTest::needCheckIfServerHasCapability() const
84 {
85  return true;
86 }
87 
88 QString SieveConditionSpamTest::serverNeedsCapability() const
89 {
90  return QStringLiteral("spamtest");
91 }
92 
93 QStringList 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 
108 QString 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 
115 void SieveConditionSpamTest::setParamWidgetValue(QXmlStreamReader &element, QWidget *w, bool /*notCondition*/, QString &error)
116 {
117  QString commentStr;
118  while (element.readNextStartElement()) {
119  const QStringRef tagName = element.name();
120  if (tagName == QLatin1String("tag")) {
121  const QString tagValue = element.readElementText();
122  if (tagValue == QLatin1String("count") || tagValue == QLatin1String("value")) {
123  if (element.readNextStartElement()) {
124  if (element.name() == QLatin1String("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 == QLatin1String("comparator")) {
130  if (element.readNextStartElement()) {
131  if (element.name() == QLatin1String("str")) {
132  auto comparator = w->findChild<SelectComparatorComboBox *>(QStringLiteral("comparator"));
133  comparator->setCode(element.readElementText(), name(), error);
134  }
135  }
136  } else if (tagValue == QLatin1String("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(LIBKSIEVE_LOG) << " SieveConditionSpamTest::setParamWidgetValue server has not percent support";
143  }
144  } else {
145  unknownTagValue(tagValue, error);
146  qCDebug(LIBKSIEVE_LOG) << " SieveConditionSpamTest::setParamWidgetValue unknown tagvalue " << tagValue;
147  }
148  } else if (tagName == QLatin1String("str")) {
149  auto spinbox = w->findChild<QSpinBox *>(QStringLiteral("value"));
150  spinbox->setValue(element.readElementText().toInt());
151  } else if (tagName == QLatin1String("crlf")) {
152  element.skipCurrentElement();
153  // nothing
154  } else if (tagName == QLatin1String("comment")) {
155  commentStr = AutoCreateScriptUtil::loadConditionComment(commentStr, element.readElementText());
156  } else {
157  unknownTag(tagName, error);
158  qCDebug(LIBKSIEVE_LOG) << " SieveSpamTest::setParamWidgetValue unknown tagName " << tagName;
159  }
160  }
161  if (!commentStr.isEmpty()) {
162  setComment(commentStr);
163  }
164 }
165 
166 QUrl SieveConditionSpamTest::href() const
167 {
168  return SieveEditorUtil::helpUrl(SieveEditorUtil::strToVariableName(name()));
169 }
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)
bool isChecked() const const
QString i18n(const char *text, const TYPE &arg...)
QString arg(qlonglong a, int fieldWidth, int base, QChar fillChar) const const
void toggled(bool checked)
QMetaObject::Connection connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
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 Sun Apr 11 2021 23:09:36 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.