Libksieve

sieveconditionsize.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 "sieveconditionsize.h"
20 #include "widgets/selectsizewidget.h"
21 #include "autocreatescripts/autocreatescriptutil_p.h"
22 #include "editor/sieveeditorutil.h"
23 #include <KLocalizedString>
24 
25 #include <QHBoxLayout>
26 #include <QComboBox>
27 #include <QXmlStreamReader>
28 #include "libksieve_debug.h"
29 
30 using namespace KSieveUi;
31 
32 SieveConditionSize::SieveConditionSize(SieveEditorGraphicalModeWidget *sieveGraphicalModeWidget, QObject *parent)
33  : SieveCondition(sieveGraphicalModeWidget, QStringLiteral("size"), i18n("Size"), parent)
34 {
35 }
36 
37 QWidget *SieveConditionSize::createParamWidget(QWidget *parent) const
38 {
39  QWidget *w = new QWidget(parent);
40  QHBoxLayout *lay = new QHBoxLayout;
41  lay->setContentsMargins(0, 0, 0, 0);
42  w->setLayout(lay);
43 
44  QComboBox *combo = new QComboBox;
45  combo->setObjectName(QStringLiteral("combosize"));
46  combo->addItem(i18n("under"), QStringLiteral(":under"));
47  combo->addItem(i18n("over"), QStringLiteral(":over"));
48  lay->addWidget(combo);
49  connect(combo, QOverload<int>::of(&QComboBox::activated), this, &SieveConditionSize::valueChanged);
50 
51  SelectSizeWidget *sizeWidget = new SelectSizeWidget;
52  connect(sizeWidget, &SelectSizeWidget::valueChanged, this, &SieveConditionSize::valueChanged);
53  sizeWidget->setObjectName(QStringLiteral("sizewidget"));
54  lay->addWidget(sizeWidget);
55 
56  return w;
57 }
58 
59 QString SieveConditionSize::code(QWidget *w) const
60 {
61  const QComboBox *combo = w->findChild<QComboBox *>(QStringLiteral("combosize"));
62  const QString comparaison = combo->itemData(combo->currentIndex()).toString();
63  const SelectSizeWidget *sizeWidget = w->findChild<SelectSizeWidget *>(QStringLiteral("sizewidget"));
64  return QStringLiteral("size %1 %2").arg(comparaison, sizeWidget->code()) + AutoCreateScriptUtil::generateConditionComment(comment());
65 }
66 
67 QString SieveConditionSize::help() const
68 {
69  return i18n("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 the size of the message.");
70 }
71 
72 void SieveConditionSize::setParamWidgetValue(QXmlStreamReader &element, QWidget *w, bool /*notCondition*/, QString &error)
73 {
74  QString commentStr;
75  while (element.readNextStartElement()) {
76  const QStringRef tagName = element.name();
77  if (tagName == QLatin1String("tag")) {
78  const QString tagValue = element.readElementText();
79  QComboBox *combo = w->findChild<QComboBox *>(QStringLiteral("combosize"));
80  const int index = combo->findData(AutoCreateScriptUtil::tagValue(tagValue));
81  if (index != -1) {
82  combo->setCurrentIndex(index);
83  }
84  } else if (tagName == QLatin1String("num")) {
85  QString numIdentifier;
86  if (element.attributes().hasAttribute(QLatin1String("quantifier"))) {
87  numIdentifier = element.attributes().value(QLatin1String("quantifier")).toString();
88  }
89  const qlonglong tagValue = element.readElementText().toLongLong();
90  SelectSizeWidget *sizeWidget = w->findChild<SelectSizeWidget *>(QStringLiteral("sizewidget"));
91  sizeWidget->setCode(tagValue, numIdentifier, name(), error);
92  } else if (tagName == QLatin1String("crlf")) {
93  element.skipCurrentElement();
94  //nothing
95  } else if (tagName == QLatin1String("comment")) {
96  commentStr = AutoCreateScriptUtil::loadConditionComment(commentStr, element.readElementText());
97  } else {
98  unknownTag(tagName, error);
99  qCDebug(LIBKSIEVE_LOG) << " SieveConditionSize::setParamWidgetValue unknown tagName " << tagName;
100  }
101  }
102  if (!commentStr.isEmpty()) {
103  setComment(commentStr);
104  }
105 }
106 
107 QUrl SieveConditionSize::href() const
108 {
109  return SieveEditorUtil::helpUrl(SieveEditorUtil::strToVariableName(name()));
110 }
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 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
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-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.