Libksieve

sieveconditionspamtest.cpp
1 /*
2  Copyright (C) 2013-2020 Laurent Montel <[email protected]>
3 
4  This library is free software; you can redistribute it and/or
5  modify it under the terms of the GNU Library General Public
6  License as published by the Free Software Foundation; either
7  version 2 of the License, or (at your option) any later version.
8 
9  This library is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  Library General Public License for more details.
13 
14  You should have received a copy of the GNU Library General Public License
15  along with this library; see the file COPYING.LIB. If not, write to
16  the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17  Boston, MA 02110-1301, USA.
18 */
19 #include "sieveconditionspamtest.h"
20 #include "widgets/selectrelationalmatchtype.h"
21 #include "widgets/selectcomparatorcombobox.h"
22 #include "autocreatescripts/autocreatescriptutil_p.h"
23 #include "autocreatescripts/sieveeditorgraphicalmodewidget.h"
24 #include "editor/sieveeditorutil.h"
25 
26 #include <KLocalizedString>
27 
28 #include <QSpinBox>
29 #include "libksieve_debug.h"
30 #include <QXmlStreamReader>
31 #include <QCheckBox>
32 #include <QVBoxLayout>
33 
34 using namespace KSieveUi;
35 
36 SieveConditionSpamTest::SieveConditionSpamTest(SieveEditorGraphicalModeWidget *sieveGraphicalModeWidget, QObject *parent)
37  : SieveCondition(sieveGraphicalModeWidget, QStringLiteral("spamtest"), i18n("Spam Test"), parent)
38 {
39  mHasSpamTestPlusSupport = sieveCapabilities().contains(QLatin1String("spamtestplus"));
40 }
41 
42 QWidget *SieveConditionSpamTest::createParamWidget(QWidget *parent) const
43 {
44  QWidget *w = new QWidget(parent);
45  QVBoxLayout *lay = new QVBoxLayout;
46  lay->setContentsMargins(0, 0, 0, 0);
47  w->setLayout(lay);
48 
49  if (mHasSpamTestPlusSupport) {
50  QCheckBox *percent = new QCheckBox(i18n("Percent"));
51  percent->setObjectName(QStringLiteral("percent"));
52  connect(percent, &QCheckBox::toggled, this, &SieveConditionSpamTest::valueChanged);
53  lay->addWidget(percent);
54  }
55 
56  SelectRelationalMatchType *relation = new SelectRelationalMatchType;
57  connect(relation, &SelectRelationalMatchType::valueChanged, this, &SieveConditionSpamTest::valueChanged);
58  relation->setObjectName(QStringLiteral("relation"));
59  lay->addWidget(relation);
60 
61  SelectComparatorComboBox *comparator = new SelectComparatorComboBox(mSieveGraphicalModeWidget);
62  connect(comparator, &SelectComparatorComboBox::valueChanged, this, &SieveConditionSpamTest::valueChanged);
63  comparator->setObjectName(QStringLiteral("comparator"));
64  lay->addWidget(comparator);
65 
66  QSpinBox *spinbox = new QSpinBox;
67  spinbox->setMaximum(10);
68  spinbox->setMinimum(0);
69  spinbox->setObjectName(QStringLiteral("value"));
70  connect(spinbox, QOverload<int>::of(&QSpinBox::valueChanged), this, &SieveConditionSpamTest::valueChanged);
71  lay->addWidget(spinbox);
72  return w;
73 }
74 
75 QString SieveConditionSpamTest::code(QWidget *w) const
76 {
77  QString percentStr;
78  if (mHasSpamTestPlusSupport) {
79  const QCheckBox *checkbox = w->findChild<QCheckBox *>(QStringLiteral("percent"));
80  percentStr = checkbox->isChecked() ? QStringLiteral(":percent") : QString();
81  }
82 
83  const SelectRelationalMatchType *relation = w->findChild<SelectRelationalMatchType *>(QStringLiteral("relation"));
84  const QString relationStr = relation->code();
85 
86  const SelectComparatorComboBox *comparator = w->findChild<SelectComparatorComboBox *>(QStringLiteral("comparator"));
87  const QString comparatorStr = comparator->code();
88 
89  const QSpinBox *spinbox = w->findChild<QSpinBox *>(QStringLiteral("value"));
90  const QString value = QString::number(spinbox->value());
91 
92  return QStringLiteral("spamtest %1 %2 %3 \"%4\"").arg(percentStr, relationStr, comparatorStr, value) + AutoCreateScriptUtil::generateConditionComment(comment());
93 }
94 
95 bool SieveConditionSpamTest::needCheckIfServerHasCapability() const
96 {
97  return true;
98 }
99 
100 QString SieveConditionSpamTest::serverNeedsCapability() const
101 {
102  return QStringLiteral("spamtest");
103 }
104 
105 QStringList SieveConditionSpamTest::needRequires(QWidget *w) const
106 {
107  const SelectComparatorComboBox *comparator = w->findChild<SelectComparatorComboBox *>(QStringLiteral("comparator"));
108  QStringList lst;
109  lst << QStringLiteral("spamtest") << QStringLiteral("relational");
110  const QString comparatorRequires = comparator->require();
111  if (!comparatorRequires.isEmpty()) {
112  lst << comparatorRequires;
113  }
114  if (mHasSpamTestPlusSupport) {
115  lst << QStringLiteral("spamtestplus");
116  }
117  return lst;
118 }
119 
120 QString SieveConditionSpamTest::help() const
121 {
122  return i18n("Sieve implementations that implement the \"spamtest\" test use an identifier of either \"spamtest\" or \"spamtestplus\" for use with the capability mechanism.");
123 }
124 
125 void SieveConditionSpamTest::setParamWidgetValue(QXmlStreamReader &element, QWidget *w, bool /*notCondition*/, QString &error)
126 {
127  QString commentStr;
128  while (element.readNextStartElement()) {
129  const QStringRef tagName = element.name();
130  if (tagName == QLatin1String("tag")) {
131  const QString tagValue = element.readElementText();
132  if (tagValue == QLatin1String("count") || tagValue == QLatin1String("value")) {
133  if (element.readNextStartElement()) {
134  if (element.name() == QLatin1String("str")) {
135  SelectRelationalMatchType *relation = w->findChild<SelectRelationalMatchType *>(QStringLiteral("relation"));
136  relation->setCode(AutoCreateScriptUtil::tagValue(tagValue), element.readElementText(), name(), error);
137  }
138  }
139  } else if (tagValue == QLatin1String("comparator")) {
140  if (element.readNextStartElement()) {
141  if (element.name() == QLatin1String("str")) {
142  SelectComparatorComboBox *comparator = w->findChild<SelectComparatorComboBox *>(QStringLiteral("comparator"));
143  comparator->setCode(element.readElementText(), name(), error);
144  }
145  }
146  } else if (tagValue == QLatin1String("percent")) {
147  if (mHasSpamTestPlusSupport) {
148  QCheckBox *checkbox = w->findChild<QCheckBox *>(QStringLiteral("percent"));
149  checkbox->setChecked(true);
150  } else {
151  serverDoesNotSupportFeatures(QStringLiteral("percent"), error);
152  qCDebug(LIBKSIEVE_LOG) << " SieveConditionSpamTest::setParamWidgetValue server has not percent support";
153  }
154  } else {
155  unknownTagValue(tagValue, error);
156  qCDebug(LIBKSIEVE_LOG) << " SieveConditionSpamTest::setParamWidgetValue unknown tagvalue " << tagValue;
157  }
158  } else if (tagName == QLatin1String("str")) {
159  QSpinBox *spinbox = w->findChild<QSpinBox *>(QStringLiteral("value"));
160  spinbox->setValue(element.readElementText().toInt());
161  } else if (tagName == QLatin1String("crlf")) {
162  element.skipCurrentElement();
163  //nothing
164  } else if (tagName == QLatin1String("comment")) {
165  commentStr = AutoCreateScriptUtil::loadConditionComment(commentStr, element.readElementText());
166  } else {
167  unknownTag(tagName, error);
168  qCDebug(LIBKSIEVE_LOG) << " SieveSpamTest::setParamWidgetValue unknown tagName " << tagName;
169  }
170  }
171  if (!commentStr.isEmpty()) {
172  setComment(commentStr);
173  }
174 }
175 
176 QUrl SieveConditionSpamTest::href() const
177 {
178  return SieveEditorUtil::helpUrl(SieveEditorUtil::strToVariableName(name()));
179 }
void setMinimum(int min)
bool readNextStartElement()
void setContentsMargins(int left, int top, int right, int bottom)
QString name(const QVariant &location)
void valueChanged(int i)
void skipCurrentElement()
void addWidget(QWidget *widget, int stretch, Qt::Alignment alignment)
QString number(int n, int base)
void setLayout(QLayout *layout)
QString readElementText(QXmlStreamReader::ReadElementTextBehaviour behaviour)
int toInt(bool *ok, int base) const const
void setObjectName(const QString &name)
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-2020 The KDE developers.
Generated on Thu Jun 4 2020 23:09:06 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.