Libksieve

sieveconditionexists.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 "sieveconditionexists.h"
20 #include "autocreatescripts/autocreatescriptutil_p.h"
21 #include "widgets/selectheadertypecombobox.h"
22 #include "editor/sieveeditorutil.h"
23 
24 #include <KLocalizedString>
25 
26 #include <QHBoxLayout>
27 #include <QComboBox>
28 #include <QLabel>
29 #include <QXmlStreamReader>
30 #include "libksieve_debug.h"
31 
32 using namespace KSieveUi;
33 
34 SieveConditionExists::SieveConditionExists(SieveEditorGraphicalModeWidget *sieveGraphicalModeWidget, QObject *parent)
35  : SieveCondition(sieveGraphicalModeWidget, QStringLiteral("exists"), i18n("Exists"), parent)
36 {
37 }
38 
39 QWidget *SieveConditionExists::createParamWidget(QWidget *parent) const
40 {
41  QWidget *w = new QWidget(parent);
42  QHBoxLayout *lay = new QHBoxLayout;
43  lay->setContentsMargins(0, 0, 0, 0);
44  w->setLayout(lay);
45 
46  QComboBox *combo = new QComboBox;
47  combo->setObjectName(QStringLiteral("existscheck"));
48  combo->addItem(i18n("exists"), QStringLiteral("exists"));
49  combo->addItem(i18n("not exists"), QStringLiteral("not exists"));
50  lay->addWidget(combo);
51  connect(combo, QOverload<int>::of(&QComboBox::activated), this, &SieveConditionExists::valueChanged);
52 
53  QLabel *lab = new QLabel(i18n("headers:"));
54  lay->addWidget(lab);
55 
56  SelectHeaderTypeComboBox *value = new SelectHeaderTypeComboBox;
57  connect(value, &SelectHeaderTypeComboBox::valueChanged, this, &SieveConditionExists::valueChanged);
58  value->setObjectName(QStringLiteral("headervalue"));
59 
60  lay->addWidget(value);
61  return w;
62 }
63 
64 QString SieveConditionExists::code(QWidget *w) const
65 {
66  const QComboBox *combo = w->findChild<QComboBox *>(QStringLiteral("existscheck"));
67  const QString comparaison = combo->itemData(combo->currentIndex()).toString();
68 
69  const SelectHeaderTypeComboBox *value = w->findChild<SelectHeaderTypeComboBox *>(QStringLiteral("headervalue"));
70  return QStringLiteral("%1 %2").arg(comparaison, value->code()) + AutoCreateScriptUtil::generateConditionComment(comment());
71 }
72 
73 QString SieveConditionExists::help() const
74 {
75  return i18n("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 is false.");
76 }
77 
78 void SieveConditionExists::setParamWidgetValue(QXmlStreamReader &element, QWidget *w, bool notCondition, QString &error)
79 {
80  QString commentStr;
81  while (element.readNextStartElement()) {
82  const QStringRef tagName = element.name();
83  if (notCondition) {
84  QComboBox *combo = w->findChild<QComboBox *>(QStringLiteral("existscheck"));
85  combo->setCurrentIndex(1);
86  }
87  if (tagName == QLatin1String("str")) {
88  SelectHeaderTypeComboBox *value = w->findChild<SelectHeaderTypeComboBox *>(QStringLiteral("headervalue"));
89  value->setCode(element.readElementText());
90  } else if (tagName == QLatin1String("list")) {
91  SelectHeaderTypeComboBox *selectHeaderType = w->findChild<SelectHeaderTypeComboBox *>(QStringLiteral("headervalue"));
92  selectHeaderType->setCode(AutoCreateScriptUtil::listValueToStr(element));
93  } else if (tagName == QLatin1String("crlf")) {
94  element.skipCurrentElement();
95  //nothing
96  } else if (tagName == QLatin1String("comment")) {
97  commentStr = AutoCreateScriptUtil::loadConditionComment(commentStr, element.readElementText());
98  } else {
99  unknownTag(tagName, error);
100  qCDebug(LIBKSIEVE_LOG) << " SieveConditionExists::setParamWidgetValue unknown tagName " << tagName;
101  }
102  }
103  if (!commentStr.isEmpty()) {
104  setComment(commentStr);
105  }
106 }
107 
108 QUrl SieveConditionExists::href() const
109 {
110  return SieveEditorUtil::helpUrl(SieveEditorUtil::strToVariableName(name()));
111 }
bool readNextStartElement()
void setContentsMargins(int left, int top, int right, int bottom)
QString name(const QVariant &location)
void addItem(const QString &text, const QVariant &userData)
void skipCurrentElement()
void addWidget(QWidget *widget, int stretch, Qt::Alignment alignment)
void setLayout(QLayout *layout)
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-2020 The KDE developers.
Generated on Thu Jul 9 2020 23:07:44 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.