Libksieve

sieveactionnotify.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 "sieveactionnotify.h"
7#include "autocreatescripts/autocreatescriptutil_p.h"
8#include "editor/sieveeditorutil.h"
9#include "widgets/selectimportancecombobox.h"
10
11#include <KLineEditEventHandler>
12#include <KLocalizedString>
13#include <QLineEdit>
14
15#include "libksieveui_debug.h"
16#include <QHBoxLayout>
17#include <QLabel>
18#include <QXmlStreamReader>
19
20using namespace KSieveUi;
21
22SieveActionNotify::SieveActionNotify(SieveEditorGraphicalModeWidget *sieveGraphicalModeWidget, QObject *parent)
23 : SieveAction(sieveGraphicalModeWidget, QStringLiteral("notify"), i18n("Notify"), parent)
24{
25}
26
27QWidget *SieveActionNotify::createParamWidget(QWidget *parent) const
28{
29 auto w = new QWidget(parent);
30 auto lay = new QHBoxLayout;
31 lay->setContentsMargins({});
32 w->setLayout(lay);
33
34 auto importanceCombobox = new SelectImportanceCombobox;
35 importanceCombobox->setObjectName(QLatin1StringView("importancecombo"));
36 connect(importanceCombobox, &SelectImportanceCombobox::valueChanged, this, &SieveActionNotify::valueChanged);
37 lay->addWidget(importanceCombobox);
38
39 auto lab = new QLabel(i18nc("@label:textbox", "message:"));
40 lay->addWidget(lab);
41
42 auto message = new QLineEdit;
44 message->setObjectName(QLatin1StringView("message"));
45 connect(message, &QLineEdit::textChanged, this, &SieveActionNotify::valueChanged);
46 lay->addWidget(message);
47
48 lab = new QLabel(i18nc("@label:textbox", "method:"));
49 lay->addWidget(lab);
50
51 auto method = new QLineEdit;
53 method->setObjectName(QLatin1StringView("method"));
54 lay->addWidget(method);
55 connect(method, &QLineEdit::textChanged, this, &SieveActionNotify::valueChanged);
56
57 return w;
58}
59
60void SieveActionNotify::setParamWidgetValue(QXmlStreamReader &element, QWidget *w, QString &error)
61{
62 while (element.readNextStartElement()) {
63 const QStringView tagName = element.name();
64 if (tagName == QLatin1StringView("tag")) {
65 const QString tagValue = element.readElementText();
66 if (tagValue == QLatin1StringView("message")) {
67 const QString strValue = AutoCreateScriptUtil::strValue(element);
68 if (!strValue.isEmpty()) {
69 auto message = w->findChild<QLineEdit *>(QStringLiteral("message"));
70 message->setText(AutoCreateScriptUtil::quoteStr(strValue));
71 }
72 } else if (tagValue == QLatin1StringView("importance")) {
73 const QString strValue = AutoCreateScriptUtil::strValue(element);
74 if (!strValue.isEmpty()) {
75 auto importance = w->findChild<SelectImportanceCombobox *>(QStringLiteral("importancecombo"));
76 importance->setCode(strValue, name(), error);
77 }
78 } else {
79 unknownTagValue(tagValue, error);
80 qCDebug(LIBKSIEVEUI_LOG) << " SieveActionNotify::setParamWidgetValue unknown tagValue" << tagValue;
81 }
82 } else if (tagName == QLatin1StringView("crlf")) {
83 element.skipCurrentElement();
84 // nothing
85 } else if (tagName == QLatin1StringView("comment")) {
86 element.skipCurrentElement();
87 // implement in the future ?
88 } else if (tagName == QLatin1StringView("str")) {
89 auto method = w->findChild<QLineEdit *>(QStringLiteral("method"));
90 method->setText(AutoCreateScriptUtil::quoteStr(element.readElementText()));
91 } else {
92 unknownTag(tagName, error);
93 qCDebug(LIBKSIEVEUI_LOG) << " SieveActionNotify::setParamWidgetValue unknown tagName " << tagName;
94 }
95 }
96}
97
98QString SieveActionNotify::code(QWidget *w) const
99{
100 QString result = QStringLiteral("notify");
101 const SelectImportanceCombobox *importance = w->findChild<SelectImportanceCombobox *>(QStringLiteral("importancecombo"));
102 const QString importanceStr = importance->code();
103 if (!importanceStr.isEmpty()) {
104 result += QStringLiteral(" :importance \"%1\"").arg(importanceStr);
105 }
106
107 const QLineEdit *message = w->findChild<QLineEdit *>(QStringLiteral("message"));
108 const QString messageStr = message->text();
109 if (!messageStr.isEmpty()) {
110 result += QStringLiteral(" :message \"%1\"").arg(messageStr);
111 }
112
113 const QLineEdit *method = w->findChild<QLineEdit *>(QStringLiteral("method"));
114 const QString methodStr = method->text();
115 result += QStringLiteral(" \"%1\";").arg(methodStr);
116
117 return result;
118}
119
120QString SieveActionNotify::serverNeedsCapability() const
121{
122 return QStringLiteral("enotify");
123}
124
125bool SieveActionNotify::needCheckIfServerHasCapability() const
126{
127 return true;
128}
129
130QString SieveActionNotify::help() const
131{
132 return i18n("The \"notify\" action specifies that a notification should be sent to a user.");
133}
134
135QStringList SieveActionNotify::needRequires(QWidget *) const
136{
137 QStringList lst;
138 lst << QStringLiteral("enotify");
139 return lst;
140}
141
142QUrl SieveActionNotify::href() const
143{
144 return SieveEditorUtil::helpUrl(SieveEditorUtil::strToVariableName(name()));
145}
146
147#include "moc_sieveactionnotify.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 setText(const QString &)
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
QString arg(Args &&... args) 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.