Libksieve

sieveactionredirect.cpp
1 /*
2  SPDX-FileCopyrightText: 2013-2021 Laurent Montel <[email protected]>
3 
4  SPDX-License-Identifier: LGPL-2.0-or-later
5 */
6 #include "sieveactionredirect.h"
7 #include "autocreatescripts/autocreatescriptutil_p.h"
8 #include "autocreatescripts/sieveeditorgraphicalmodewidget.h"
9 #include "editor/sieveeditorutil.h"
10 #include "widgets/addresslineedit.h"
11 
12 #include <KLocalizedString>
13 
14 #include "libksieve_debug.h"
15 #include <QCheckBox>
16 #include <QHBoxLayout>
17 #include <QXmlStreamReader>
18 
19 using namespace KSieveUi;
20 
21 SieveActionRedirect::SieveActionRedirect(SieveEditorGraphicalModeWidget *sieveGraphicalModeWidget, QObject *parent)
22  : SieveAction(sieveGraphicalModeWidget, QStringLiteral("redirect"), i18n("Redirect"), parent)
23 {
24  mHasCopySupport = sieveCapabilities().contains(QLatin1String("copy"));
25  mHasListSupport = sieveCapabilities().contains(QLatin1String("extlists"));
26 }
27 
28 QWidget *SieveActionRedirect::createParamWidget(QWidget *parent) const
29 {
30  auto w = new QWidget(parent);
31  auto lay = new QHBoxLayout;
32  lay->setContentsMargins({});
33  w->setLayout(lay);
34  if (mHasCopySupport) {
35  auto copy = new QCheckBox(i18n("Keep a copy"));
36  copy->setObjectName(QStringLiteral("copy"));
37  connect(copy, &QCheckBox::clicked, this, &SieveActionRedirect::valueChanged);
38  lay->addWidget(copy);
39  }
40  if (mHasListSupport) {
41  auto list = new QCheckBox(i18n("Use list"));
42  list->setObjectName(QStringLiteral("list"));
43  connect(list, &QCheckBox::clicked, this, &SieveActionRedirect::valueChanged);
44  lay->addWidget(list);
45  }
46 
47  KSieveUi::AbstractSelectEmailLineEdit *edit = AutoCreateScriptUtil::createSelectEmailsWidget();
48  edit->setObjectName(QStringLiteral("RedirectEdit"));
49  connect(edit, &AddressLineEdit::valueChanged, this, &SieveActionRedirect::valueChanged);
50  lay->addWidget(edit);
51  return w;
52 }
53 
54 void SieveActionRedirect::setParamWidgetValue(QXmlStreamReader &element, QWidget *w, QString &error)
55 {
56  while (element.readNextStartElement()) {
57  const QStringRef tagName = element.name();
58  if (tagName == QLatin1String("str")) {
59  auto edit = w->findChild<AbstractSelectEmailLineEdit *>(QStringLiteral("RedirectEdit"));
60  const QString tagValue = element.readElementText();
61  edit->setText(AutoCreateScriptUtil::quoteStr(tagValue));
62  } else if (tagName == QLatin1String("tag")) {
63  const QString tagValue = element.readElementText();
64  if (tagValue == QLatin1String("copy")) {
65  if (mHasCopySupport) {
66  auto copy = w->findChild<QCheckBox *>(QStringLiteral("copy"));
67  copy->setChecked(true);
68  } else {
69  serverDoesNotSupportFeatures(QStringLiteral("copy"), error);
70  }
71  } else if (tagValue == QLatin1String("list")) {
72  if (mHasListSupport) {
73  auto list = w->findChild<QCheckBox *>(QStringLiteral("list"));
74  list->setChecked(true);
75  } else {
76  serverDoesNotSupportFeatures(QStringLiteral("list"), error);
77  }
78  } else {
79  unknownTagValue(tagValue, error);
80  qCDebug(LIBKSIEVE_LOG) << " SieveActionRedirect::setParamWidgetValue tagValue unknown" << tagValue;
81  }
82  } else if (tagName == QLatin1String("crlf")) {
83  element.skipCurrentElement();
84  // nothing
85  } else if (tagName == QLatin1String("comment")) {
86  setComment(element.readElementText());
87  // implement in the future ?
88  } else {
89  unknownTag(tagName, error);
90  qCDebug(LIBKSIEVE_LOG) << " SieveActionRedirect::setParamWidgetValue unknown tagName " << tagName;
91  }
92  }
93 }
94 
95 QString SieveActionRedirect::code(QWidget *w) const
96 {
97  QString result = QStringLiteral("redirect ");
98  const AbstractSelectEmailLineEdit *edit = w->findChild<AbstractSelectEmailLineEdit *>(QStringLiteral("RedirectEdit"));
99  const QString text = edit->text();
100 
101  if (mHasCopySupport) {
102  const QCheckBox *copy = w->findChild<QCheckBox *>(QStringLiteral("copy"));
103  if (copy->isChecked()) {
104  result += QLatin1String(":copy ");
105  }
106  }
107 
108  if (mHasListSupport) {
109  const QCheckBox *list = w->findChild<QCheckBox *>(QStringLiteral("list"));
110  if (list->isChecked()) {
111  result += QLatin1String(":list ");
112  }
113  }
114 
115  return result + QStringLiteral("\"%1\";").arg(text);
116 }
117 
118 QStringList SieveActionRedirect::needRequires(QWidget *parent) const
119 {
120  QStringList lst;
121  if (mHasCopySupport) {
122  const QCheckBox *copy = parent->findChild<QCheckBox *>(QStringLiteral("copy"));
123  if (copy->isChecked()) {
124  lst << QStringLiteral("copy");
125  }
126  }
127  if (mHasListSupport) {
128  const QCheckBox *list = parent->findChild<QCheckBox *>(QStringLiteral("list"));
129  if (list->isChecked()) {
130  lst << QStringLiteral("extlists");
131  }
132  }
133  return lst;
134 }
135 
136 QString SieveActionRedirect::help() const
137 {
138  QString helpStr = i18n(
139  "The \"redirect\" action is used to send the message to another user at a supplied address, as a mail forwarding feature does. The \"redirect\" "
140  "action makes no changes to the message body or existing headers, but it may add new headers.");
141  if (mHasCopySupport) {
142  helpStr += QLatin1Char('\n')
143  + i18n("If the optional \":copy\" keyword is specified, the tagged command does not cancel the implicit \"keep\". Instead, it redirects a copy in "
144  "addition to whatever else is happening to the message.");
145  }
146  // TODO add list info
147  return helpStr;
148 }
149 
150 QUrl SieveActionRedirect::href() const
151 {
152  return SieveEditorUtil::helpUrl(SieveEditorUtil::strToVariableName(name()));
153 }
bool readNextStartElement()
void setContentsMargins(int left, int top, int right, int bottom)
QString name(const QVariant &location)
The AbstractSelectEmailLineEdit class.
void skipCurrentElement()
KIOCORE_EXPORT CopyJob * copy(const QUrl &src, const QUrl &dest, JobFlags flags=DefaultFlags)
QString readElementText(QXmlStreamReader::ReadElementTextBehaviour behaviour)
QByteArray tagValue(const Elem &elem, TagKey key)
void setObjectName(const QString &name)
void clicked(bool checked)
bool isChecked() 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)
QStringRef name() const const
KIOFILEWIDGETS_EXPORT QStringList list(const QString &fileClass)
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 Sat Apr 17 2021 23:09:33 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.