Libksieve

sieveactionnotify.cpp
1 /*
2  SPDX-FileCopyrightText: 2013-2021 Laurent Montel <[email protected]>
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 <KLocalizedString>
12 #include <Libkdepim/LineEditCatchReturnKey>
13 #include <QLineEdit>
14 
15 #include "libksieve_debug.h"
16 #include <QHBoxLayout>
17 #include <QLabel>
18 #include <QXmlStreamReader>
19 
20 using namespace KSieveUi;
21 
22 SieveActionNotify::SieveActionNotify(SieveEditorGraphicalModeWidget *sieveGraphicalModeWidget, QObject *parent)
23  : SieveAction(sieveGraphicalModeWidget, QStringLiteral("notify"), i18n("Notify"), parent)
24 {
25 }
26 
27 QWidget *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(QStringLiteral("importancecombo"));
36  connect(importanceCombobox, &SelectImportanceCombobox::valueChanged, this, &SieveActionNotify::valueChanged);
37  lay->addWidget(importanceCombobox);
38 
39  auto lab = new QLabel(i18n("message:"));
40  lay->addWidget(lab);
41 
42  auto message = new QLineEdit;
43  new KPIM::LineEditCatchReturnKey(message, w);
44  message->setObjectName(QStringLiteral("message"));
45  connect(message, &QLineEdit::textChanged, this, &SieveActionNotify::valueChanged);
46  lay->addWidget(message);
47 
48  lab = new QLabel(i18n("method:"));
49  lay->addWidget(lab);
50 
51  auto method = new QLineEdit;
52  new KPIM::LineEditCatchReturnKey(method, w);
53  method->setObjectName(QStringLiteral("method"));
54  lay->addWidget(method);
55  connect(method, &QLineEdit::textChanged, this, &SieveActionNotify::valueChanged);
56 
57  return w;
58 }
59 
60 void SieveActionNotify::setParamWidgetValue(QXmlStreamReader &element, QWidget *w, QString &error)
61 {
62  while (element.readNextStartElement()) {
63  const QStringRef tagName = element.name();
64  if (tagName == QLatin1String("tag")) {
65  const QString tagValue = element.readElementText();
66  if (tagValue == QLatin1String("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 == QLatin1String("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(LIBKSIEVE_LOG) << " SieveActionNotify::setParamWidgetValue unknown tagValue" << tagValue;
81  }
82  } else if (tagName == QLatin1String("crlf")) {
83  element.skipCurrentElement();
84  // nothing
85  } else if (tagName == QLatin1String("comment")) {
86  element.skipCurrentElement();
87  // implement in the future ?
88  } else if (tagName == QLatin1String("str")) {
89  auto method = w->findChild<QLineEdit *>(QStringLiteral("method"));
90  method->setText(AutoCreateScriptUtil::quoteStr(element.readElementText()));
91  } else {
92  unknownTag(tagName, error);
93  qCDebug(LIBKSIEVE_LOG) << " SieveActionNotify::setParamWidgetValue unknown tagName " << tagName;
94  }
95  }
96 }
97 
98 QString 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 
120 QString SieveActionNotify::serverNeedsCapability() const
121 {
122  return QStringLiteral("enotify");
123 }
124 
125 bool SieveActionNotify::needCheckIfServerHasCapability() const
126 {
127  return true;
128 }
129 
130 QString SieveActionNotify::help() const
131 {
132  return i18n("The \"notify\" action specifies that a notification should be sent to a user.");
133 }
134 
135 QStringList SieveActionNotify::needRequires(QWidget *) const
136 {
137  QStringList lst;
138  lst << QStringLiteral("enotify");
139  return lst;
140 }
141 
142 QUrl SieveActionNotify::href() const
143 {
144  return SieveEditorUtil::helpUrl(SieveEditorUtil::strToVariableName(name()));
145 }
KCOREADDONS_EXPORT void message(KMessage::MessageType messageType, const QString &text, const QString &caption=QString())
bool readNextStartElement()
void setContentsMargins(int left, int top, int right, int bottom)
QString name(const QVariant &location)
void setText(const QString &)
void textChanged(const QString &text)
void skipCurrentElement()
QString readElementText(QXmlStreamReader::ReadElementTextBehaviour behaviour)
QByteArray tagValue(const Elem &elem, TagKey key)
bool isEmpty() const const
QString i18n(const char *text, const TYPE &arg...)
QString arg(qlonglong a, int fieldWidth, int base, QChar fillChar) const const
QMetaObject::Connection connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
QObject * parent() const const
QStringRef name() const const
T findChild(const QString &name, Qt::FindChildOptions options) const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2021 The KDE developers.
Generated on Fri Apr 16 2021 23:09:34 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.