Libksieve

sieveactionenclose.cpp
1 /*
2  Copyright (C) 2013-2020 Laurent Montel <[email protected]>
3 
4  This library is free software; you can redistribute it and/or
5  modify it under the terms of the GNU Library General Public
6  License as published by the Free Software Foundation; either
7  version 2 of the License, or (at your option) any later version.
8 
9  This library is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  Library General Public License for more details.
13 
14  You should have received a copy of the GNU Library General Public License
15  along with this library; see the file COPYING.LIB. If not, write to
16  the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17  Boston, MA 02110-1301, USA.
18 */
19 #include "sieveactionenclose.h"
20 #include "widgets/multilineedit.h"
21 #include "autocreatescripts/autocreatescriptutil_p.h"
22 #include "editor/sieveeditorutil.h"
23 
24 #include <KLocalizedString>
25 #include <QLineEdit>
26 
27 #include <QLabel>
28 #include <QXmlStreamReader>
29 #include "libksieve_debug.h"
30 #include <QGridLayout>
31 
32 using namespace KSieveUi;
33 SieveActionEnclose::SieveActionEnclose(SieveEditorGraphicalModeWidget *sieveGraphicalModeWidget, QObject *parent)
34  : SieveAction(sieveGraphicalModeWidget, QStringLiteral("enclose"), i18n("Enclose"), parent)
35 {
36 }
37 
38 QWidget *SieveActionEnclose::createParamWidget(QWidget *parent) const
39 {
40  QWidget *w = new QWidget(parent);
41  QGridLayout *grid = new QGridLayout;
42  grid->setContentsMargins(0, 0, 0, 0);
43  w->setLayout(grid);
44 
45  QLabel *lab = new QLabel(i18n("Subject:"));
46  grid->addWidget(lab, 0, 0);
47 
49  subject->setObjectName(QStringLiteral("subject"));
50  connect(subject, &QLineEdit::textChanged, this, &SieveActionEnclose::valueChanged);
51  grid->addWidget(subject, 0, 1);
52 
53  lab = new QLabel(i18n("headers:"));
54  grid->addWidget(lab, 1, 0);
55 
56  QLineEdit *headers = new QLineEdit;
57  headers->setObjectName(QStringLiteral("headers"));
58  connect(headers, &QLineEdit::textChanged, this, &SieveActionEnclose::valueChanged);
59  grid->addWidget(headers, 1, 1);
60 
61  lab = new QLabel(i18n("text:"));
62  grid->addWidget(lab, 2, 0);
63 
64  MultiLineEdit *text = new MultiLineEdit;
65  text->setObjectName(QStringLiteral("text"));
66  connect(text, &MultiLineEdit::valueChanged, this, &SieveActionEnclose::valueChanged);
67  grid->addWidget(text, 2, 1);
68 
69  return w;
70 }
71 
72 void SieveActionEnclose::setParamWidgetValue(QXmlStreamReader &element, QWidget *w, QString &error)
73 {
74  while (element.readNextStartElement()) {
75  const QStringRef tagName = element.name();
76  if (tagName == QLatin1String("tag")) {
77  const QString tagValue = element.readElementText();
78  if (tagValue == QLatin1String("headers")) {
79  const QString strValue = AutoCreateScriptUtil::strValue(element);
80  if (!strValue.isEmpty()) {
81  QLineEdit *subject = w->findChild<QLineEdit *>(QStringLiteral("headers"));
82  subject->setText(strValue);
83  }
84  } else if (tagValue == QLatin1String("subject")) {
85  const QString strValue = AutoCreateScriptUtil::strValue(element);
86  if (!strValue.isEmpty()) {
87  QLineEdit *headers = w->findChild<QLineEdit *>(QStringLiteral("subject"));
88  headers->setText(strValue);
89  }
90  } else {
91  unknownTagValue(tagValue, error);
92  qCDebug(LIBKSIEVE_LOG) << " SieveActionEnclose::setParamWidgetValue unknown tag value:" << tagValue;
93  }
94  } else if (tagName == QLatin1String("str")) {
95  MultiLineEdit *edit = w->findChild<MultiLineEdit *>(QStringLiteral("text"));
96  edit->setPlainText(element.readElementText());
97  } else if (tagName == QLatin1String("crlf")) {
98  element.skipCurrentElement();
99  //nothing
100  } else if (tagName == QLatin1String("comment")) {
101  element.skipCurrentElement();
102  //implement in the future ?
103  } else {
104  unknownTag(tagName, error);
105  qCDebug(LIBKSIEVE_LOG) << " SieveActionEnclose::setParamWidgetValue unknown tagName " << tagName;
106  }
107  }
108 }
109 
110 QString SieveActionEnclose::code(QWidget *w) const
111 {
112  QString result = QStringLiteral("enclose ");
113  const QLineEdit *subject = w->findChild<QLineEdit *>(QStringLiteral("subject"));
114  const QString subjectStr = subject->text();
115  if (!subjectStr.isEmpty()) {
116  result += QStringLiteral(":subject \"%1\" ").arg(subjectStr);
117  }
118 
119  const QLineEdit *headers = w->findChild<QLineEdit *>(QStringLiteral("headers"));
120  const QString headersStr = headers->text();
121  if (!headersStr.isEmpty()) {
122  result += QStringLiteral(":headers \"%1\" ").arg(headersStr);
123  }
124 
125  const MultiLineEdit *edit = w->findChild<MultiLineEdit *>(QStringLiteral("text"));
126  const QString text = edit->toPlainText();
127  if (!text.isEmpty()) {
128  result += QStringLiteral("text:%1").arg(AutoCreateScriptUtil::createMultiLine(text));
129  } else {
130  result += QLatin1Char(';');
131  }
132 
133  return result;
134 }
135 
136 QStringList SieveActionEnclose::needRequires(QWidget * /*parent*/) const
137 {
138  return QStringList() << QStringLiteral("enclose");
139 }
140 
141 bool SieveActionEnclose::needCheckIfServerHasCapability() const
142 {
143  return true;
144 }
145 
146 QString SieveActionEnclose::serverNeedsCapability() const
147 {
148  return QStringLiteral("enclose");
149 }
150 
151 QString SieveActionEnclose::help() const
152 {
153  return i18n("Enclose action command is defined to allow an entire message to be enclosed as an attachment to a new message.");
154 }
155 
156 QUrl SieveActionEnclose::href() const
157 {
158  return SieveEditorUtil::helpUrl(SieveEditorUtil::strToVariableName(name()));
159 }
bool readNextStartElement()
void setContentsMargins(int left, int top, int right, int bottom)
QString name(const QVariant &location)
void addWidget(QWidget *widget, int row, int column, Qt::Alignment alignment)
void setText(const QString &)
void textChanged(const QString &text)
void skipCurrentElement()
void setLayout(QLayout *layout)
QString readElementText(QXmlStreamReader::ReadElementTextBehaviour behaviour)
void setObjectName(const QString &name)
bool isEmpty() const const
QString subject() 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-2020 The KDE developers.
Generated on Fri Jun 5 2020 23:09:20 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.