Libksieve

sieveactionreplace.cpp
1 /*
2  SPDX-FileCopyrightText: 2013-2021 Laurent Montel <[email protected]>
3 
4  SPDX-License-Identifier: LGPL-2.0-or-later
5 */
6 #include "sieveactionreplace.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 #include <KSieveUi/AbstractSelectEmailLineEdit>
21 
22 using namespace KSieveUi;
23 SieveActionReplace::SieveActionReplace(SieveEditorGraphicalModeWidget *sieveGraphicalModeWidget, QObject *parent)
24  : SieveAction(sieveGraphicalModeWidget, QStringLiteral("replace"), i18n("Replace"), parent)
25 {
26 }
27 
28 QWidget *SieveActionReplace::createParamWidget(QWidget *parent) const
29 {
30  auto w = new QWidget(parent);
31  auto grid = new QGridLayout;
32  grid->setContentsMargins({});
33  w->setLayout(grid);
34 
35  auto lab = new QLabel(i18n("Subject:"));
36  grid->addWidget(lab, 0, 0);
37 
38  auto subject = new QLineEdit;
39  new KPIM::LineEditCatchReturnKey(subject, w);
40  subject->setObjectName(QStringLiteral("subject"));
41  connect(subject, &QLineEdit::textChanged, this, &SieveActionReplace::valueChanged);
42  grid->addWidget(subject, 0, 1);
43 
44  lab = new QLabel(i18n("from:"));
45  grid->addWidget(lab, 1, 0);
46 
47  KSieveUi::AbstractSelectEmailLineEdit *headers = AutoCreateScriptUtil::createSelectEmailsWidget();
48  headers->setObjectName(QStringLiteral("from"));
49  connect(headers, &AbstractSelectEmailLineEdit::valueChanged, this, &SieveActionReplace::valueChanged);
50  grid->addWidget(headers, 1, 1);
51 
52  lab = new QLabel(i18n("text:"));
53  grid->addWidget(lab, 2, 0);
54 
55  auto text = new MultiLineEdit;
56  text->setObjectName(QStringLiteral("text"));
57  connect(text, &MultiLineEdit::textChanged, this, &SieveActionReplace::valueChanged);
58  grid->addWidget(text, 2, 1);
59 
60  return w;
61 }
62 
63 void SieveActionReplace::setParamWidgetValue(QXmlStreamReader &element, QWidget *w, QString &error)
64 {
65  while (element.readNextStartElement()) {
66  const QStringRef tagName = element.name();
67  if (tagName == QLatin1String("str")) {
68  auto edit = w->findChild<MultiLineEdit *>(QStringLiteral("text"));
69  edit->setPlainText(element.readElementText());
70  } else if (tagName == QLatin1String("tag")) {
71  const QString tagValue = element.readElementText();
72  if (tagValue == QLatin1String("subject")) {
73  const QString strValue = AutoCreateScriptUtil::strValue(element);
74  if (!strValue.isEmpty()) {
75  auto subject = w->findChild<QLineEdit *>(QStringLiteral("subject"));
76  subject->setText(strValue);
77  }
78  } else if (tagValue == QLatin1String("from")) {
79  const QString strValue = AutoCreateScriptUtil::strValue(element);
80  if (!strValue.isEmpty()) {
81  auto headers = w->findChild<KSieveUi::AbstractSelectEmailLineEdit *>(QStringLiteral("from"));
82  headers->setText(strValue);
83  }
84  } else {
85  unknownTagValue(tagValue, error);
86  qCDebug(LIBKSIEVE_LOG) << " SieveActionReplace::setParamWidgetValue unknown tagValue " << tagValue;
87  }
88  } else if (tagName == QLatin1String("crlf")) {
89  element.skipCurrentElement();
90  // nothing
91  } else if (tagName == QLatin1String("comment")) {
92  element.skipCurrentElement();
93  // implement in the future ?
94  } else {
95  unknownTag(tagName, error);
96  qCDebug(LIBKSIEVE_LOG) << " SieveActionReplace::setParamWidgetValue unknown tagName " << tagName;
97  }
98  }
99 }
100 
101 QString SieveActionReplace::code(QWidget *w) const
102 {
103  QString result = QStringLiteral("replace ");
104  const QLineEdit *subject = w->findChild<QLineEdit *>(QStringLiteral("subject"));
105  const QString subjectStr = subject->text();
106  if (!subjectStr.isEmpty()) {
107  result += QStringLiteral(":subject \"%1\" ").arg(subjectStr);
108  }
109 
110  const KSieveUi::AbstractSelectEmailLineEdit *headers = w->findChild<KSieveUi::AbstractSelectEmailLineEdit *>(QStringLiteral("from"));
111  const QString headersStr = headers->text();
112  if (!headersStr.isEmpty()) {
113  result += QStringLiteral(":from \"%1\" ").arg(headersStr);
114  }
115 
116  const MultiLineEdit *edit = w->findChild<MultiLineEdit *>(QStringLiteral("text"));
117  const QString text = edit->toPlainText();
118  if (!text.isEmpty()) {
119  result += QStringLiteral("text:%1").arg(AutoCreateScriptUtil::createMultiLine(text));
120  }
121 
122  return result;
123 }
124 
125 QStringList SieveActionReplace::needRequires(QWidget *) const
126 {
127  return QStringList() << QStringLiteral("replace");
128 }
129 
130 bool SieveActionReplace::needCheckIfServerHasCapability() const
131 {
132  return true;
133 }
134 
135 QString SieveActionReplace::serverNeedsCapability() const
136 {
137  return QStringLiteral("replace");
138 }
139 
140 QString SieveActionReplace::help() const
141 {
142  return i18n("The \"replace\" command is defined to allow a MIME part to be replaced with the text supplied in the command.");
143 }
144 
145 QUrl SieveActionReplace::href() const
146 {
147  return SieveEditorUtil::helpUrl(SieveEditorUtil::strToVariableName(name()));
148 }
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)
The AbstractSelectEmailLineEdit class.
void skipCurrentElement()
QString readElementText(QXmlStreamReader::ReadElementTextBehaviour behaviour)
QByteArray tagValue(const Elem &elem, TagKey key)
void setObjectName(const QString &name)
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.