Libksieve

sieveconditionsize.cpp
1/*
2 SPDX-FileCopyrightText: 2013-2024 Laurent Montel <montel@kde.org>
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 "libksieveui_debug.h"
13#include <QComboBox>
14#include <QHBoxLayout>
15#include <QXmlStreamReader>
16
17using namespace KSieveUi;
18
19SieveConditionSize::SieveConditionSize(SieveEditorGraphicalModeWidget *sieveGraphicalModeWidget, QObject *parent)
20 : SieveCondition(sieveGraphicalModeWidget, QStringLiteral("size"), i18n("Size"), parent)
21{
22}
23
24QWidget *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(QLatin1StringView("combosize"));
33 combo->addItem(i18n("under"), QStringLiteral(":under"));
34 combo->addItem(i18n("over"), QStringLiteral(":over"));
35 lay->addWidget(combo);
36 connect(combo, &QComboBox::activated, this, &SieveConditionSize::valueChanged);
37
38 auto sizeWidget = new SelectSizeWidget;
39 connect(sizeWidget, &SelectSizeWidget::valueChanged, this, &SieveConditionSize::valueChanged);
40 sizeWidget->setObjectName(QLatin1StringView("sizewidget"));
41 lay->addWidget(sizeWidget);
42
43 return w;
44}
45
46QString SieveConditionSize::code(QWidget *w) const
47{
48 const QComboBox *combo = w->findChild<QComboBox *>(QStringLiteral("combosize"));
49 const QString comparison = combo->itemData(combo->currentIndex()).toString();
50 const SelectSizeWidget *sizeWidget = w->findChild<SelectSizeWidget *>(QStringLiteral("sizewidget"));
51 return QStringLiteral("size %1 %2").arg(comparison, sizeWidget->code()) + AutoCreateScriptUtil::generateConditionComment(comment());
52}
53
54QString 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
61void SieveConditionSize::setParamWidgetValue(QXmlStreamReader &element, QWidget *w, bool /*notCondition*/, QString &error)
62{
63 QString commentStr;
64 while (element.readNextStartElement()) {
65 const QStringView tagName = element.name();
66 if (tagName == QLatin1StringView("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 == QLatin1StringView("num")) {
74 QString numIdentifier;
75 if (element.attributes().hasAttribute(QLatin1StringView("quantifier"))) {
76 numIdentifier = element.attributes().value(QLatin1StringView("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 == QLatin1StringView("crlf")) {
82 element.skipCurrentElement();
83 // nothing
84 } else if (tagName == QLatin1StringView("comment")) {
85 commentStr = AutoCreateScriptUtil::loadConditionComment(commentStr, element.readElementText());
86 } else {
87 unknownTag(tagName, error);
88 qCDebug(LIBKSIEVEUI_LOG) << " SieveConditionSize::setParamWidgetValue unknown tagName " << tagName;
89 }
90 }
91 if (!commentStr.isEmpty()) {
92 setComment(commentStr);
93 }
94}
95
96QUrl SieveConditionSize::href() const
97{
98 return SieveEditorUtil::helpUrl(SieveEditorUtil::strToVariableName(name()));
99}
100
101#include "moc_sieveconditionsize.cpp"
QString i18n(const char *text, const TYPE &arg...)
QByteArray tagValue(const Elem &elem, const char *keyName)
void activated(int index)
int findData(const QVariant &data, int role, Qt::MatchFlags flags) const const
QVariant itemData(int index, int role) const const
void setContentsMargins(const QMargins &margins)
QMetaObject::Connection connect(const QObject *sender, PointerToMemberFunction signal, Functor functor)
T findChild(const QString &name, Qt::FindChildOptions options) const const
QObject * parent() const const
void setObjectName(QAnyStringView name)
bool isEmpty() const const
qlonglong toLongLong(bool *ok, int base) const const
QString toString() const const
QString toString() const const
bool hasAttribute(QAnyStringView namespaceUri, QAnyStringView name) const const
QStringView value(QAnyStringView namespaceUri, QAnyStringView name) const const
QXmlStreamAttributes attributes() const const
QStringView name() const const
QString readElementText(ReadElementTextBehaviour behaviour)
bool readNextStartElement()
void skipCurrentElement()
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Sat Dec 21 2024 16:57:10 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.