Libksieve

sieveconditionsize.cpp
1 /*
2  SPDX-FileCopyrightText: 2013-2021 Laurent Montel <[email protected]>
3 
4  SPDX-License-Identifier: LGPL-2.0-or-later
5 */
6 #include "sieveconditionsize.h"
7 #include "autocreatescripts/autocreatescriptutil_p.h"
8 #include "editor/sieveeditorutil.h"
9 #include "widgets/selectsizewidget.h"
10 #include <KLocalizedString>
11 
12 #include "libksieve_debug.h"
13 #include <QComboBox>
14 #include <QHBoxLayout>
15 #include <QXmlStreamReader>
16 
17 using namespace KSieveUi;
18 
19 SieveConditionSize::SieveConditionSize(SieveEditorGraphicalModeWidget *sieveGraphicalModeWidget, QObject *parent)
20  : SieveCondition(sieveGraphicalModeWidget, QStringLiteral("size"), i18n("Size"), parent)
21 {
22 }
23 
24 QWidget *SieveConditionSize::createParamWidget(QWidget *parent) const
25 {
26  auto w = new QWidget(parent);
27  auto lay = new QHBoxLayout;
28  lay->setContentsMargins({});
29  w->setLayout(lay);
30 
31  auto combo = new QComboBox;
32  combo->setObjectName(QStringLiteral("combosize"));
33  combo->addItem(i18n("under"), QStringLiteral(":under"));
34  combo->addItem(i18n("over"), QStringLiteral(":over"));
35  lay->addWidget(combo);
36  connect(combo, QOverload<int>::of(&QComboBox::activated), this, &SieveConditionSize::valueChanged);
37 
38  auto sizeWidget = new SelectSizeWidget;
39  connect(sizeWidget, &SelectSizeWidget::valueChanged, this, &SieveConditionSize::valueChanged);
40  sizeWidget->setObjectName(QStringLiteral("sizewidget"));
41  lay->addWidget(sizeWidget);
42 
43  return w;
44 }
45 
46 QString SieveConditionSize::code(QWidget *w) const
47 {
48  const QComboBox *combo = w->findChild<QComboBox *>(QStringLiteral("combosize"));
49  const QString comparaison = combo->itemData(combo->currentIndex()).toString();
50  const SelectSizeWidget *sizeWidget = w->findChild<SelectSizeWidget *>(QStringLiteral("sizewidget"));
51  return QStringLiteral("size %1 %2").arg(comparaison, sizeWidget->code()) + AutoCreateScriptUtil::generateConditionComment(comment());
52 }
53 
54 QString SieveConditionSize::help() const
55 {
56  return i18n(
57  "The \"size\" test deals with the size of a message. It takes either a tagged argument of \":over\" or \":under\", followed by a number representing "
58  "the size of the message.");
59 }
60 
61 void SieveConditionSize::setParamWidgetValue(QXmlStreamReader &element, QWidget *w, bool /*notCondition*/, QString &error)
62 {
63  QString commentStr;
64  while (element.readNextStartElement()) {
65  const QStringRef tagName = element.name();
66  if (tagName == QLatin1String("tag")) {
67  const QString tagValue = element.readElementText();
68  auto combo = w->findChild<QComboBox *>(QStringLiteral("combosize"));
69  const int index = combo->findData(AutoCreateScriptUtil::tagValue(tagValue));
70  if (index != -1) {
71  combo->setCurrentIndex(index);
72  }
73  } else if (tagName == QLatin1String("num")) {
74  QString numIdentifier;
75  if (element.attributes().hasAttribute(QLatin1String("quantifier"))) {
76  numIdentifier = element.attributes().value(QLatin1String("quantifier")).toString();
77  }
78  const qlonglong tagValue = element.readElementText().toLongLong();
79  auto sizeWidget = w->findChild<SelectSizeWidget *>(QStringLiteral("sizewidget"));
80  sizeWidget->setCode(tagValue, numIdentifier, name(), error);
81  } else if (tagName == QLatin1String("crlf")) {
82  element.skipCurrentElement();
83  // nothing
84  } else if (tagName == QLatin1String("comment")) {
85  commentStr = AutoCreateScriptUtil::loadConditionComment(commentStr, element.readElementText());
86  } else {
87  unknownTag(tagName, error);
88  qCDebug(LIBKSIEVE_LOG) << " SieveConditionSize::setParamWidgetValue unknown tagName " << tagName;
89  }
90  }
91  if (!commentStr.isEmpty()) {
92  setComment(commentStr);
93  }
94 }
95 
96 QUrl SieveConditionSize::href() const
97 {
98  return SieveEditorUtil::helpUrl(SieveEditorUtil::strToVariableName(name()));
99 }
bool readNextStartElement()
void setContentsMargins(int left, int top, int right, int bottom)
QString name(const QVariant &location)
QString toString() const const
QStringRef value(const QString &namespaceUri, const QString &name) const const
void skipCurrentElement()
QString readElementText(QXmlStreamReader::ReadElementTextBehaviour behaviour)
QByteArray tagValue(const Elem &elem, TagKey key)
void setObjectName(const QString &name)
bool isEmpty() const const
void activated(int index)
QVariant itemData(int index, int role) const const
bool hasAttribute(const QString &qualifiedName) const const
QString i18n(const char *text, const TYPE &arg...)
int findData(const QVariant &data, int role, Qt::MatchFlags flags) const const
char * toString(const T &value)
QXmlStreamAttributes attributes() const const
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
qlonglong toLongLong(bool *ok, int base) 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.