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 "sieveactionreject.h"
0007 #include "autocreatescripts/autocreatescriptutil_p.h"
0008 #include "editor/sieveeditorutil.h"
0009 #include "widgets/multilineedit.h"
0010 
0011 #include <KLocalizedString>
0012 
0013 #include "libksieveui_debug.h"
0014 #include <QHBoxLayout>
0015 #include <QLabel>
0016 #include <QXmlStreamReader>
0017 
0018 using namespace KSieveUi;
0019 SieveActionReject::SieveActionReject(SieveEditorGraphicalModeWidget *sieveGraphicalModeWidget, QObject *parent)
0020     : SieveAction(sieveGraphicalModeWidget, QStringLiteral("reject"), i18n("Reject"), parent)
0021 {
0022 }
0023 
0024 QWidget *SieveActionReject::createParamWidget(QWidget *parent) const
0025 {
0026     auto w = new QWidget(parent);
0027     auto lay = new QHBoxLayout;
0028     lay->setContentsMargins({});
0029     w->setLayout(lay);
0030     auto lab = new QLabel(i18n("text:"));
0031     lay->addWidget(lab);
0032 
0033     auto edit = new MultiLineEdit;
0034     connect(edit, &MultiLineEdit::textChanged, this, &SieveActionReject::valueChanged);
0035     edit->setObjectName(QLatin1StringView("rejectmessage"));
0036     lay->addWidget(edit);
0037     return w;
0038 }
0039 
0040 void SieveActionReject::setParamWidgetValue(QXmlStreamReader &element, QWidget *w, QString &error)
0041 {
0042     while (element.readNextStartElement()) {
0043         const QStringView tagName = element.name();
0044         if (tagName == QLatin1StringView("str")) {
0045             const QString tagValue = element.readElementText();
0046             auto edit = w->findChild<MultiLineEdit *>(QStringLiteral("rejectmessage"));
0047             edit->setPlainText(tagValue);
0048         } else if (tagName == QLatin1StringView("crlf")) {
0049             element.skipCurrentElement();
0050             // nothing
0051         } else if (tagName == QLatin1StringView("comment")) {
0052             element.skipCurrentElement();
0053             // implement in the future ?
0054         } else {
0055             unknownTag(tagName, error);
0056             qCDebug(LIBKSIEVEUI_LOG) << " SieveActionReject::setParamWidgetValue unknown tagName " << tagName;
0057         }
0058     }
0059 }
0060 
0061 QString SieveActionReject::code(QWidget *w) const
0062 {
0063     const MultiLineEdit *edit = w->findChild<MultiLineEdit *>(QStringLiteral("rejectmessage"));
0064     const QString text = edit->toPlainText();
0065 
0066     return QStringLiteral("reject text:%1").arg(AutoCreateScriptUtil::createMultiLine(text));
0067 }
0068 
0069 QStringList SieveActionReject::needRequires(QWidget *) const
0070 {
0071     return QStringList() << QStringLiteral("reject");
0072 }
0073 
0074 QString SieveActionReject::serverNeedsCapability() const
0075 {
0076     return QStringLiteral("reject");
0077 }
0078 
0079 bool SieveActionReject::needCheckIfServerHasCapability() const
0080 {
0081     return true;
0082 }
0083 
0084 QString SieveActionReject::help() const
0085 {
0086     return i18n(" The \"reject\" action cancels the implicit keep and refuses delivery of a message.");
0087 }
0088 
0089 QUrl SieveActionReject::href() const
0090 {
0091     return SieveEditorUtil::helpUrl(SieveEditorUtil::strToVariableName(name()));
0092 }
0093 
0094 #include "moc_sieveactionreject.cpp"