Libksieve

sieveconditionihave.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 "sieveconditionihave.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 <QHBoxLayout>
16#include <QWidget>
17#include <QXmlStreamReader>
18
19using namespace KSieveUi;
20SieveConditionIhave::SieveConditionIhave(SieveEditorGraphicalModeWidget *sieveGraphicalModeWidget, QObject *parent)
21 : SieveCondition(sieveGraphicalModeWidget, QStringLiteral("ihave"), i18n("IHave"), parent)
22{
23}
24
25QWidget *SieveConditionIhave::createParamWidget(QWidget *parent) const
26{
27 auto w = new QWidget(parent);
28 auto lay = new QHBoxLayout;
29 lay->setContentsMargins({});
30 w->setLayout(lay);
31
32 auto edit = new QLineEdit;
34 connect(edit, &QLineEdit::textChanged, this, &SieveConditionIhave::valueChanged);
35 edit->setPlaceholderText(i18nc("@info:placeholder", "Use \",\" to separate capabilities"));
36 edit->setClearButtonEnabled(true);
37 lay->addWidget(edit);
38 edit->setObjectName(QLatin1StringView("edit"));
39
40 return w;
41}
42
43QString SieveConditionIhave::code(QWidget *w) const
44{
45 const QLineEdit *edit = w->findChild<QLineEdit *>(QStringLiteral("edit"));
46 const QString editValue = edit->text();
47 return QStringLiteral("ihave %1").arg(AutoCreateScriptUtil::createList(editValue, QLatin1Char(',')))
48 + AutoCreateScriptUtil::generateConditionComment(comment());
49}
50
51QStringList SieveConditionIhave::needRequires(QWidget *) const
52{
53 return QStringList() << QStringLiteral("ihave");
54}
55
56bool SieveConditionIhave::needCheckIfServerHasCapability() const
57{
58 return true;
59}
60
61QString SieveConditionIhave::serverNeedsCapability() const
62{
63 return QStringLiteral("ihave");
64}
65
66QString SieveConditionIhave::help() const
67{
68 return i18n("The \"ihave\" test provides a means for Sieve scripts to test for the existence of a given extension prior to actually using it.");
69}
70
71void SieveConditionIhave::setParamWidgetValue(QXmlStreamReader &element, QWidget *w, bool, QString &error)
72{
73 QString commentStr;
74 while (element.readNextStartElement()) {
75 const QStringView tagName = element.name();
76 if (tagName == QLatin1StringView("str")) {
77 const QString tagValue = element.readElementText();
78 auto edit = w->findChild<QLineEdit *>(QStringLiteral("edit"));
79 edit->setText(tagValue);
80 } else if (tagName == QLatin1StringView("crlf")) {
81 element.skipCurrentElement();
82 // nothing
83 } else if (tagName == QLatin1StringView("comment")) {
84 commentStr = AutoCreateScriptUtil::loadConditionComment(commentStr, element.readElementText());
85 } else {
86 unknownTag(tagName, error);
87 qCDebug(LIBKSIEVEUI_LOG) << " SieveConditionIhave::setParamWidgetValue unknown tagName " << tagName;
88 }
89 }
90 if (!commentStr.isEmpty()) {
91 setComment(commentStr);
92 }
93}
94
95QUrl SieveConditionIhave::href() const
96{
97 return SieveEditorUtil::helpUrl(SieveEditorUtil::strToVariableName(name()));
98}
99
100#include "moc_sieveconditionihave.cpp"
QString i18nc(const char *context, const char *text, const TYPE &arg...)
QString i18n(const char *text, const TYPE &arg...)
void catchReturnKey(QObject *lineEdit)
QByteArray tagValue(const Elem &elem, const char *keyName)
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
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.