Libksieve

sieveactionenclose.cpp
1 /*
2  SPDX-FileCopyrightText: 2013-2021 Laurent Montel <[email protected]>
3 
4  SPDX-License-Identifier: LGPL-2.0-or-later
5 */
6 #include "sieveactionenclose.h"
7 #include "autocreatescripts/autocreatescriptutil_p.h"
8 #include "editor/sieveeditorutil.h"
9 #include "widgets/multilineedit.h"
10 
11 #include <KLocalizedString>
12 #include <Libkdepim/LineEditCatchReturnKey>
13 #include <QLineEdit>
14 
15 #include "libksieve_debug.h"
16 #include <QGridLayout>
17 #include <QLabel>
18 #include <QXmlStreamReader>
19 
20 using namespace KSieveUi;
21 SieveActionEnclose::SieveActionEnclose(SieveEditorGraphicalModeWidget *sieveGraphicalModeWidget, QObject *parent)
22  : SieveAction(sieveGraphicalModeWidget, QStringLiteral("enclose"), i18n("Enclose"), parent)
23 {
24 }
25 
26 QWidget *SieveActionEnclose::createParamWidget(QWidget *parent) const
27 {
28  auto w = new QWidget(parent);
29  auto grid = new QGridLayout;
30  grid->setContentsMargins({});
31  w->setLayout(grid);
32 
33  auto lab = new QLabel(i18n("Subject:"));
34  grid->addWidget(lab, 0, 0);
35 
36  auto subject = new QLineEdit;
37  new KPIM::LineEditCatchReturnKey(subject, w);
38  subject->setObjectName(QStringLiteral("subject"));
39  connect(subject, &QLineEdit::textChanged, this, &SieveActionEnclose::valueChanged);
40  grid->addWidget(subject, 0, 1);
41 
42  lab = new QLabel(i18n("headers:"));
43  grid->addWidget(lab, 1, 0);
44 
45  auto headers = new QLineEdit;
46  new KPIM::LineEditCatchReturnKey(headers, w);
47  headers->setObjectName(QStringLiteral("headers"));
48  connect(headers, &QLineEdit::textChanged, this, &SieveActionEnclose::valueChanged);
49  grid->addWidget(headers, 1, 1);
50 
51  lab = new QLabel(i18n("text:"));
52  grid->addWidget(lab, 2, 0);
53 
54  auto text = new MultiLineEdit;
55  text->setObjectName(QStringLiteral("text"));
56  connect(text, &MultiLineEdit::valueChanged, this, &SieveActionEnclose::valueChanged);
57  grid->addWidget(text, 2, 1);
58 
59  return w;
60 }
61 
62 void SieveActionEnclose::setParamWidgetValue(QXmlStreamReader &element, QWidget *w, QString &error)
63 {
64  while (element.readNextStartElement()) {
65  const QStringRef tagName = element.name();
66  if (tagName == QLatin1String("tag")) {
67  const QString tagValue = element.readElementText();
68  if (tagValue == QLatin1String("headers")) {
69  const QString strValue = AutoCreateScriptUtil::strValue(element);
70  if (!strValue.isEmpty()) {
71  auto subject = w->findChild<QLineEdit *>(QStringLiteral("headers"));
72  subject->setText(strValue);
73  }
74  } else if (tagValue == QLatin1String("subject")) {
75  const QString strValue = AutoCreateScriptUtil::strValue(element);
76  if (!strValue.isEmpty()) {
77  auto headers = w->findChild<QLineEdit *>(QStringLiteral("subject"));
78  headers->setText(strValue);
79  }
80  } else {
81  unknownTagValue(tagValue, error);
82  qCDebug(LIBKSIEVE_LOG) << " SieveActionEnclose::setParamWidgetValue unknown tag value:" << tagValue;
83  }
84  } else if (tagName == QLatin1String("str")) {
85  auto edit = w->findChild<MultiLineEdit *>(QStringLiteral("text"));
86  edit->setPlainText(element.readElementText());
87  } else if (tagName == QLatin1String("crlf")) {
88  element.skipCurrentElement();
89  // nothing
90  } else if (tagName == QLatin1String("comment")) {
91  element.skipCurrentElement();
92  // implement in the future ?
93  } else {
94  unknownTag(tagName, error);
95  qCDebug(LIBKSIEVE_LOG) << " SieveActionEnclose::setParamWidgetValue unknown tagName " << tagName;
96  }
97  }
98 }
99 
100 QString SieveActionEnclose::code(QWidget *w) const
101 {
102  QString result = QStringLiteral("enclose ");
103  const QLineEdit *subject = w->findChild<QLineEdit *>(QStringLiteral("subject"));
104  const QString subjectStr = subject->text();
105  if (!subjectStr.isEmpty()) {
106  result += QStringLiteral(":subject \"%1\" ").arg(subjectStr);
107  }
108 
109  const QLineEdit *headers = w->findChild<QLineEdit *>(QStringLiteral("headers"));
110  const QString headersStr = headers->text();
111  if (!headersStr.isEmpty()) {
112  result += QStringLiteral(":headers \"%1\" ").arg(headersStr);
113  }
114 
115  const MultiLineEdit *edit = w->findChild<MultiLineEdit *>(QStringLiteral("text"));
116  const QString text = edit->toPlainText();
117  if (!text.isEmpty()) {
118  result += QStringLiteral("text:%1").arg(AutoCreateScriptUtil::createMultiLine(text));
119  } else {
120  result += QLatin1Char(';');
121  }
122 
123  return result;
124 }
125 
126 QStringList SieveActionEnclose::needRequires(QWidget * /*parent*/) const
127 {
128  return QStringList() << QStringLiteral("enclose");
129 }
130 
131 bool SieveActionEnclose::needCheckIfServerHasCapability() const
132 {
133  return true;
134 }
135 
136 QString SieveActionEnclose::serverNeedsCapability() const
137 {
138  return QStringLiteral("enclose");
139 }
140 
141 QString SieveActionEnclose::help() const
142 {
143  return i18n("Enclose action command is defined to allow an entire message to be enclosed as an attachment to a new message.");
144 }
145 
146 QUrl SieveActionEnclose::href() const
147 {
148  return SieveEditorUtil::helpUrl(SieveEditorUtil::strToVariableName(name()));
149 }
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 Sun Apr 11 2021 23:09:36 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.