Libksieve

sieveactionredirect.cpp
1/*
2 SPDX-FileCopyrightText: 2013-2024 Laurent Montel <montel@kde.org>
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 "libksieveui_debug.h"
15#include <QCheckBox>
16#include <QHBoxLayout>
17#include <QXmlStreamReader>
18
19using namespace KSieveUi;
20
21SieveActionRedirect::SieveActionRedirect(SieveEditorGraphicalModeWidget *sieveGraphicalModeWidget, QObject *parent)
22 : SieveAction(sieveGraphicalModeWidget, QStringLiteral("redirect"), i18n("Redirect"), parent)
23{
24 mHasCopySupport = sieveCapabilities().contains(QLatin1StringView("copy"));
25 mHasListSupport = sieveCapabilities().contains(QLatin1StringView("extlists"));
26}
27
28QWidget *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(i18nc("@option:check", "Keep a copy"));
36 copy->setObjectName(QLatin1StringView("copy"));
37 connect(copy, &QCheckBox::clicked, this, &SieveActionRedirect::valueChanged);
38 lay->addWidget(copy);
39 }
40 if (mHasListSupport) {
41 auto list = new QCheckBox(i18nc("@option:check", "Use list"));
42 list->setObjectName(QLatin1StringView("list"));
43 connect(list, &QCheckBox::clicked, this, &SieveActionRedirect::valueChanged);
44 lay->addWidget(list);
45 }
46
47 KSieveUi::AbstractSelectEmailLineEdit *edit = AutoCreateScriptUtil::createSelectEmailsWidget();
48 edit->setObjectName(QLatin1StringView("RedirectEdit"));
49 connect(edit, &AddressLineEdit::valueChanged, this, &SieveActionRedirect::valueChanged);
50 lay->addWidget(edit);
51 return w;
52}
53
54void SieveActionRedirect::setParamWidgetValue(QXmlStreamReader &element, QWidget *w, QString &error)
55{
56 while (element.readNextStartElement()) {
57 const QStringView tagName = element.name();
58 if (tagName == QLatin1StringView("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 == QLatin1StringView("tag")) {
63 const QString tagValue = element.readElementText();
64 if (tagValue == QLatin1StringView("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 == QLatin1StringView("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(LIBKSIEVEUI_LOG) << " SieveActionRedirect::setParamWidgetValue tagValue unknown" << tagValue;
81 }
82 } else if (tagName == QLatin1StringView("crlf")) {
83 element.skipCurrentElement();
84 // nothing
85 } else if (tagName == QLatin1StringView("comment")) {
86 setComment(element.readElementText());
87 // implement in the future ?
88 } else {
89 unknownTag(tagName, error);
90 qCDebug(LIBKSIEVEUI_LOG) << " SieveActionRedirect::setParamWidgetValue unknown tagName " << tagName;
91 }
92 }
93}
94
95QString 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 += QLatin1StringView(":copy ");
105 }
106 }
107
108 if (mHasListSupport) {
109 const QCheckBox *list = w->findChild<QCheckBox *>(QStringLiteral("list"));
110 if (list->isChecked()) {
111 result += QLatin1StringView(":list ");
112 }
113 }
114
115 return result + QStringLiteral("\"%1\";").arg(text);
116}
117
118QStringList 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
136QString 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
150QUrl SieveActionRedirect::href() const
151{
152 return SieveEditorUtil::helpUrl(SieveEditorUtil::strToVariableName(name()));
153}
154
155#include "moc_sieveactionredirect.cpp"
The AbstractSelectEmailLineEdit class.
QString i18nc(const char *context, const char *text, const TYPE &arg...)
QString i18n(const char *text, const TYPE &arg...)
KIOCORE_EXPORT QStringList list(const QString &fileClass)
const QList< QKeySequence > & copy()
QByteArray tagValue(const Elem &elem, const char *keyName)
void clicked(bool checked)
void setContentsMargins(const QMargins &margins)
QMetaObject::Connection connect(const QObject *sender, PointerToMemberFunction signal, Functor functor)
T findChild(const QString &name, Qt::FindChildOptions options) const const
QObject * parent() const const
void setObjectName(QAnyStringView name)
QString arg(Args &&... args) const const
QStringView name() const const
QString readElementText(ReadElementTextBehaviour behaviour)
bool readNextStartElement()
void skipCurrentElement()
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Sat Dec 21 2024 16:57:10 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.