Libksieve

sieveconditionexists.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 "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 "libksieveui_debug.h"
14#include <QComboBox>
15#include <QHBoxLayout>
16#include <QLabel>
17#include <QXmlStreamReader>
18
19using namespace KSieveUi;
20
21SieveConditionExists::SieveConditionExists(SieveEditorGraphicalModeWidget *sieveGraphicalModeWidget, QObject *parent)
22 : SieveCondition(sieveGraphicalModeWidget, QStringLiteral("exists"), i18n("Exists"), parent)
23{
24}
25
26QWidget *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(QLatin1StringView("existscheck"));
35 combo->addItem(i18n("exists"), QStringLiteral("exists"));
36 combo->addItem(i18n("not exists"), QStringLiteral("not exists"));
37 lay->addWidget(combo);
38 connect(combo, &QComboBox::activated, this, &SieveConditionExists::valueChanged);
39
40 auto lab = new QLabel(i18nc("@label:textbox", "headers:"));
41 lay->addWidget(lab);
42
43 auto value = new SelectHeaderTypeComboBox;
44 connect(value, &SelectHeaderTypeComboBox::valueChanged, this, &SieveConditionExists::valueChanged);
45 value->setObjectName(QLatin1StringView("headervalue"));
46
47 lay->addWidget(value);
48 return w;
49}
50
51QString SieveConditionExists::code(QWidget *w) const
52{
53 const QComboBox *combo = w->findChild<QComboBox *>(QStringLiteral("existscheck"));
54 const QString comparison = combo->itemData(combo->currentIndex()).toString();
55
56 const SelectHeaderTypeComboBox *value = w->findChild<SelectHeaderTypeComboBox *>(QStringLiteral("headervalue"));
57 return QStringLiteral("%1 %2").arg(comparison, value->code()) + AutoCreateScriptUtil::generateConditionComment(comment());
58}
59
60QString 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
67void SieveConditionExists::setParamWidgetValue(QXmlStreamReader &element, QWidget *w, bool notCondition, QString &error)
68{
69 QString commentStr;
70 while (element.readNextStartElement()) {
71 const QStringView tagName = element.name();
72 if (notCondition) {
73 auto combo = w->findChild<QComboBox *>(QStringLiteral("existscheck"));
74 combo->setCurrentIndex(1);
75 }
76 if (tagName == QLatin1StringView("str")) {
77 auto value = w->findChild<SelectHeaderTypeComboBox *>(QStringLiteral("headervalue"));
78 value->setCode(element.readElementText());
79 } else if (tagName == QLatin1StringView("list")) {
80 auto selectHeaderType = w->findChild<SelectHeaderTypeComboBox *>(QStringLiteral("headervalue"));
81 selectHeaderType->setCode(AutoCreateScriptUtil::listValueToStr(element));
82 } else if (tagName == QLatin1StringView("crlf")) {
83 element.skipCurrentElement();
84 // nothing
85 } else if (tagName == QLatin1StringView("comment")) {
86 commentStr = AutoCreateScriptUtil::loadConditionComment(commentStr, element.readElementText());
87 } else {
88 unknownTag(tagName, error);
89 qCDebug(LIBKSIEVEUI_LOG) << " SieveConditionExists::setParamWidgetValue unknown tagName " << tagName;
90 }
91 }
92 if (!commentStr.isEmpty()) {
93 setComment(commentStr);
94 }
95}
96
97QUrl SieveConditionExists::href() const
98{
99 return SieveEditorUtil::helpUrl(SieveEditorUtil::strToVariableName(name()));
100}
101
102#include "moc_sieveconditionexists.cpp"
QString i18nc(const char *context, const char *text, const TYPE &arg...)
QString i18n(const char *text, const TYPE &arg...)
void activated(int index)
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
QString toString() 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 Fri Oct 11 2024 12:07:48 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.