Libksieve

sieveconditionenvironment.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 "sieveconditionenvironment.h"
7#include "autocreatescripts/autocreatescriptutil_p.h"
8#include "editor/sieveeditorutil.h"
9
10#include <KLineEditEventHandler>
11#include <KLocalizedString>
12#include <QLineEdit>
13
14#include "libksieveui_debug.h"
15#include <QCompleter>
16#include <QGridLayout>
17#include <QLabel>
18#include <QWidget>
19#include <QXmlStreamReader>
20
21using namespace KSieveUi;
22SieveConditionEnvironment::SieveConditionEnvironment(SieveEditorGraphicalModeWidget *sieveGraphicalModeWidget, QObject *parent)
23 : SieveCondition(sieveGraphicalModeWidget, QStringLiteral("environment"), i18n("Environment"), parent)
24{
25}
26
27QWidget *SieveConditionEnvironment::createParamWidget(QWidget *parent) const
28{
29 auto w = new QWidget(parent);
30 auto grid = new QGridLayout;
31 grid->setContentsMargins({});
32 w->setLayout(grid);
33 auto lab = new QLabel(i18nc("@label:textbox", "Item:"));
34 grid->addWidget(lab, 0, 0);
35
36 auto item = new QLineEdit;
38 QStringList itemList;
39 itemList << QStringLiteral("domain") << QStringLiteral("host") << QStringLiteral("location") << QStringLiteral("name") << QStringLiteral("phase")
40 << QStringLiteral("remote-host") << QStringLiteral("remote-ip") << QStringLiteral("version");
41 auto completer = new QCompleter(itemList, w);
42 completer->setCaseSensitivity(Qt::CaseInsensitive);
43 item->setCompleter(completer);
44 connect(item, &QLineEdit::textChanged, this, &SieveConditionEnvironment::valueChanged);
45
46 item->setObjectName(QLatin1StringView("item"));
47 grid->addWidget(item, 0, 1);
48
49 lab = new QLabel(i18nc("@label:textbox", "Value:"));
50 grid->addWidget(lab, 1, 0);
51
52 auto value = new QLineEdit;
54 connect(value, &QLineEdit::textChanged, this, &SieveConditionEnvironment::valueChanged);
55 value->setObjectName(QLatin1StringView("value"));
56 grid->addWidget(value, 1, 1);
57
58 return w;
59}
60
61QString SieveConditionEnvironment::code(QWidget *w) const
62{
63 const QLineEdit *item = w->findChild<QLineEdit *>(QStringLiteral("item"));
64 const QString itemStr = item->text();
65
66 const QLineEdit *value = w->findChild<QLineEdit *>(QStringLiteral("value"));
67 const QString valueStr = value->text();
68
69 return QStringLiteral("environment \"%1\" \"%2\"").arg(itemStr, valueStr) + AutoCreateScriptUtil::generateConditionComment(comment());
70}
71
72QStringList SieveConditionEnvironment::needRequires(QWidget *) const
73{
74 return QStringList() << QStringLiteral("environment");
75}
76
77bool SieveConditionEnvironment::needCheckIfServerHasCapability() const
78{
79 return true;
80}
81
82QString SieveConditionEnvironment::serverNeedsCapability() const
83{
84 return QStringLiteral("environment");
85}
86
87QString SieveConditionEnvironment::help() const
88{
89 return i18n(
90 "The environment test retrieves the item of environment information specified by the name string and matches it to the values specified in the "
91 "key-list argument.");
92}
93
94void SieveConditionEnvironment::setParamWidgetValue(QXmlStreamReader &element, QWidget *w, bool /*notCondition*/, QString &error)
95{
96 int index = 0;
97 QString commentStr;
98 while (element.readNextStartElement()) {
99 const QStringView tagName = element.name();
100 if (tagName == QLatin1StringView("str")) {
101 if (index == 0) {
102 auto item = w->findChild<QLineEdit *>(QStringLiteral("item"));
103 item->setText(AutoCreateScriptUtil::quoteStr(element.readElementText()));
104 } else if (index == 1) {
105 auto value = w->findChild<QLineEdit *>(QStringLiteral("value"));
106 value->setText(AutoCreateScriptUtil::quoteStr(element.readElementText()));
107 } else {
108 tooManyArguments(tagName, index, 2, error);
109 qCDebug(LIBKSIEVEUI_LOG) << " SieveConditionEnvironment::setParamWidgetValue to many argument " << index;
110 }
111 ++index;
112 } else if (tagName == QLatin1StringView("crlf")) {
113 element.skipCurrentElement();
114 // nothing
115 } else if (tagName == QLatin1StringView("comment")) {
116 commentStr = AutoCreateScriptUtil::loadConditionComment(commentStr, element.readElementText());
117 } else {
118 unknownTag(tagName, error);
119 qCDebug(LIBKSIEVEUI_LOG) << " SieveActionSetVariable::setParamWidgetValue unknown tagName " << tagName;
120 }
121 }
122 if (!commentStr.isEmpty()) {
123 setComment(commentStr);
124 }
125}
126
127QUrl KSieveUi::SieveConditionEnvironment::href() const
128{
129 return SieveEditorUtil::helpUrl(SieveEditorUtil::strToVariableName(name()));
130}
131
132#include "moc_sieveconditionenvironment.cpp"
QString i18nc(const char *context, const char *text, const TYPE &arg...)
QString i18n(const char *text, const TYPE &arg...)
void catchReturnKey(QObject *lineEdit)
QString name(StandardAction id)
void setContentsMargins(const QMargins &margins)
void textChanged(const QString &text)
QMetaObject::Connection connect(const QObject *sender, PointerToMemberFunction signal, Functor functor)
T findChild(const QString &name, Qt::FindChildOptions options) const const
QObject * parent() const const
bool isEmpty() const const
CaseInsensitive
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 Mon Nov 18 2024 12:14:30 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.