File indexing completed on 2025-02-16 04:55:57

0001 /*
0002    SPDX-FileCopyrightText: 2013-2024 Laurent Montel <montel@kde.org>
0003 
0004    SPDX-License-Identifier: LGPL-2.0-or-later
0005 */
0006 #include "sieveactionredirect.h"
0007 #include "autocreatescripts/autocreatescriptutil_p.h"
0008 #include "autocreatescripts/sieveeditorgraphicalmodewidget.h"
0009 #include "editor/sieveeditorutil.h"
0010 #include "widgets/addresslineedit.h"
0011 
0012 #include <KLocalizedString>
0013 
0014 #include "libksieveui_debug.h"
0015 #include <QCheckBox>
0016 #include <QHBoxLayout>
0017 #include <QXmlStreamReader>
0018 
0019 using namespace KSieveUi;
0020 
0021 SieveActionRedirect::SieveActionRedirect(SieveEditorGraphicalModeWidget *sieveGraphicalModeWidget, QObject *parent)
0022     : SieveAction(sieveGraphicalModeWidget, QStringLiteral("redirect"), i18n("Redirect"), parent)
0023 {
0024     mHasCopySupport = sieveCapabilities().contains(QLatin1StringView("copy"));
0025     mHasListSupport = sieveCapabilities().contains(QLatin1StringView("extlists"));
0026 }
0027 
0028 QWidget *SieveActionRedirect::createParamWidget(QWidget *parent) const
0029 {
0030     auto w = new QWidget(parent);
0031     auto lay = new QHBoxLayout;
0032     lay->setContentsMargins({});
0033     w->setLayout(lay);
0034     if (mHasCopySupport) {
0035         auto copy = new QCheckBox(i18n("Keep a copy"));
0036         copy->setObjectName(QLatin1StringView("copy"));
0037         connect(copy, &QCheckBox::clicked, this, &SieveActionRedirect::valueChanged);
0038         lay->addWidget(copy);
0039     }
0040     if (mHasListSupport) {
0041         auto list = new QCheckBox(i18n("Use list"));
0042         list->setObjectName(QLatin1StringView("list"));
0043         connect(list, &QCheckBox::clicked, this, &SieveActionRedirect::valueChanged);
0044         lay->addWidget(list);
0045     }
0046 
0047     KSieveUi::AbstractSelectEmailLineEdit *edit = AutoCreateScriptUtil::createSelectEmailsWidget();
0048     edit->setObjectName(QLatin1StringView("RedirectEdit"));
0049     connect(edit, &AddressLineEdit::valueChanged, this, &SieveActionRedirect::valueChanged);
0050     lay->addWidget(edit);
0051     return w;
0052 }
0053 
0054 void SieveActionRedirect::setParamWidgetValue(QXmlStreamReader &element, QWidget *w, QString &error)
0055 {
0056     while (element.readNextStartElement()) {
0057         const QStringView tagName = element.name();
0058         if (tagName == QLatin1StringView("str")) {
0059             auto edit = w->findChild<AbstractSelectEmailLineEdit *>(QStringLiteral("RedirectEdit"));
0060             const QString tagValue = element.readElementText();
0061             edit->setText(AutoCreateScriptUtil::quoteStr(tagValue));
0062         } else if (tagName == QLatin1StringView("tag")) {
0063             const QString tagValue = element.readElementText();
0064             if (tagValue == QLatin1StringView("copy")) {
0065                 if (mHasCopySupport) {
0066                     auto copy = w->findChild<QCheckBox *>(QStringLiteral("copy"));
0067                     copy->setChecked(true);
0068                 } else {
0069                     serverDoesNotSupportFeatures(QStringLiteral("copy"), error);
0070                 }
0071             } else if (tagValue == QLatin1StringView("list")) {
0072                 if (mHasListSupport) {
0073                     auto list = w->findChild<QCheckBox *>(QStringLiteral("list"));
0074                     list->setChecked(true);
0075                 } else {
0076                     serverDoesNotSupportFeatures(QStringLiteral("list"), error);
0077                 }
0078             } else {
0079                 unknownTagValue(tagValue, error);
0080                 qCDebug(LIBKSIEVEUI_LOG) << " SieveActionRedirect::setParamWidgetValue tagValue unknown" << tagValue;
0081             }
0082         } else if (tagName == QLatin1StringView("crlf")) {
0083             element.skipCurrentElement();
0084             // nothing
0085         } else if (tagName == QLatin1StringView("comment")) {
0086             setComment(element.readElementText());
0087             // implement in the future ?
0088         } else {
0089             unknownTag(tagName, error);
0090             qCDebug(LIBKSIEVEUI_LOG) << " SieveActionRedirect::setParamWidgetValue unknown tagName " << tagName;
0091         }
0092     }
0093 }
0094 
0095 QString SieveActionRedirect::code(QWidget *w) const
0096 {
0097     QString result = QStringLiteral("redirect ");
0098     const AbstractSelectEmailLineEdit *edit = w->findChild<AbstractSelectEmailLineEdit *>(QStringLiteral("RedirectEdit"));
0099     const QString text = edit->text();
0100 
0101     if (mHasCopySupport) {
0102         const QCheckBox *copy = w->findChild<QCheckBox *>(QStringLiteral("copy"));
0103         if (copy->isChecked()) {
0104             result += QLatin1StringView(":copy ");
0105         }
0106     }
0107 
0108     if (mHasListSupport) {
0109         const QCheckBox *list = w->findChild<QCheckBox *>(QStringLiteral("list"));
0110         if (list->isChecked()) {
0111             result += QLatin1StringView(":list ");
0112         }
0113     }
0114 
0115     return result + QStringLiteral("\"%1\";").arg(text);
0116 }
0117 
0118 QStringList SieveActionRedirect::needRequires(QWidget *parent) const
0119 {
0120     QStringList lst;
0121     if (mHasCopySupport) {
0122         const QCheckBox *copy = parent->findChild<QCheckBox *>(QStringLiteral("copy"));
0123         if (copy->isChecked()) {
0124             lst << QStringLiteral("copy");
0125         }
0126     }
0127     if (mHasListSupport) {
0128         const QCheckBox *list = parent->findChild<QCheckBox *>(QStringLiteral("list"));
0129         if (list->isChecked()) {
0130             lst << QStringLiteral("extlists");
0131         }
0132     }
0133     return lst;
0134 }
0135 
0136 QString SieveActionRedirect::help() const
0137 {
0138     QString helpStr = i18n(
0139         "The \"redirect\" action is used to send the message to another user at a supplied address, as a mail forwarding feature does.  The \"redirect\" "
0140         "action makes no changes to the message body or existing headers, but it may add new headers.");
0141     if (mHasCopySupport) {
0142         helpStr += QLatin1Char('\n')
0143             + i18n("If the optional \":copy\" keyword is specified, the tagged command does not cancel the implicit \"keep\". Instead, it redirects a copy in "
0144                    "addition to whatever else is happening to the message.");
0145     }
0146     // TODO add list info
0147     return helpStr;
0148 }
0149 
0150 QUrl SieveActionRedirect::href() const
0151 {
0152     return SieveEditorUtil::helpUrl(SieveEditorUtil::strToVariableName(name()));
0153 }
0154 
0155 #include "moc_sieveactionredirect.cpp"