Libksieve

sieveconditionexists.cpp
1 /*
2  SPDX-FileCopyrightText: 2013-2021 Laurent Montel <[email protected]>
3 
4  SPDX-License-Identifier: LGPL-2.0-or-later
5 */
6 #include "sieveconditionexists.h"
7 #include "autocreatescripts/autocreatescriptutil_p.h"
8 #include "editor/sieveeditorutil.h"
9 #include "widgets/selectheadertypecombobox.h"
10 
11 #include <KLocalizedString>
12 
13 #include "libksieve_debug.h"
14 #include <QComboBox>
15 #include <QHBoxLayout>
16 #include <QLabel>
17 #include <QXmlStreamReader>
18 
19 using namespace KSieveUi;
20 
21 SieveConditionExists::SieveConditionExists(SieveEditorGraphicalModeWidget *sieveGraphicalModeWidget, QObject *parent)
22  : SieveCondition(sieveGraphicalModeWidget, QStringLiteral("exists"), i18n("Exists"), parent)
23 {
24 }
25 
26 QWidget *SieveConditionExists::createParamWidget(QWidget *parent) const
27 {
28  auto w = new QWidget(parent);
29  auto lay = new QHBoxLayout;
30  lay->setContentsMargins({});
31  w->setLayout(lay);
32 
33  auto combo = new QComboBox;
34  combo->setObjectName(QStringLiteral("existscheck"));
35  combo->addItem(i18n("exists"), QStringLiteral("exists"));
36  combo->addItem(i18n("not exists"), QStringLiteral("not exists"));
37  lay->addWidget(combo);
38  connect(combo, QOverload<int>::of(&QComboBox::activated), this, &SieveConditionExists::valueChanged);
39 
40  auto lab = new QLabel(i18n("headers:"));
41  lay->addWidget(lab);
42 
43  auto value = new SelectHeaderTypeComboBox;
44  connect(value, &SelectHeaderTypeComboBox::valueChanged, this, &SieveConditionExists::valueChanged);
45  value->setObjectName(QStringLiteral("headervalue"));
46 
47  lay->addWidget(value);
48  return w;
49 }
50 
51 QString SieveConditionExists::code(QWidget *w) const
52 {
53  const QComboBox *combo = w->findChild<QComboBox *>(QStringLiteral("existscheck"));
54  const QString comparaison = combo->itemData(combo->currentIndex()).toString();
55 
56  const SelectHeaderTypeComboBox *value = w->findChild<SelectHeaderTypeComboBox *>(QStringLiteral("headervalue"));
57  return QStringLiteral("%1 %2").arg(comparaison, value->code()) + AutoCreateScriptUtil::generateConditionComment(comment());
58 }
59 
60 QString SieveConditionExists::help() const
61 {
62  return i18n(
63  "The \"exists\" test is true if the headers listed in the header-names argument exist within the message. All of the headers must exist or the test "
64  "is false.");
65 }
66 
67 void SieveConditionExists::setParamWidgetValue(QXmlStreamReader &element, QWidget *w, bool notCondition, QString &error)
68 {
69  QString commentStr;
70  while (element.readNextStartElement()) {
71  const QStringRef tagName = element.name();
72  if (notCondition) {
73  auto combo = w->findChild<QComboBox *>(QStringLiteral("existscheck"));
74  combo->setCurrentIndex(1);
75  }
76  if (tagName == QLatin1String("str")) {
77  auto value = w->findChild<SelectHeaderTypeComboBox *>(QStringLiteral("headervalue"));
78  value->setCode(element.readElementText());
79  } else if (tagName == QLatin1String("list")) {
80  auto selectHeaderType = w->findChild<SelectHeaderTypeComboBox *>(QStringLiteral("headervalue"));
81  selectHeaderType->setCode(AutoCreateScriptUtil::listValueToStr(element));
82  } else if (tagName == QLatin1String("crlf")) {
83  element.skipCurrentElement();
84  // nothing
85  } else if (tagName == QLatin1String("comment")) {
86  commentStr = AutoCreateScriptUtil::loadConditionComment(commentStr, element.readElementText());
87  } else {
88  unknownTag(tagName, error);
89  qCDebug(LIBKSIEVE_LOG) << " SieveConditionExists::setParamWidgetValue unknown tagName " << tagName;
90  }
91  }
92  if (!commentStr.isEmpty()) {
93  setComment(commentStr);
94  }
95 }
96 
97 QUrl SieveConditionExists::href() const
98 {
99  return SieveEditorUtil::helpUrl(SieveEditorUtil::strToVariableName(name()));
100 }
bool readNextStartElement()
void setContentsMargins(int left, int top, int right, int bottom)
QString name(const QVariant &location)
void skipCurrentElement()
QString readElementText(QXmlStreamReader::ReadElementTextBehaviour behaviour)
void setObjectName(const QString &name)
bool isEmpty() const const
void activated(int index)
QVariant itemData(int index, int role) const const
QString i18n(const char *text, const TYPE &arg...)
char * toString(const T &value)
QMetaObject::Connection connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
QObject * parent() const const
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 Fri Apr 16 2021 23:09:34 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.