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

0001 /*
0002    SPDX-FileCopyrightText: 2013-2024 Laurent Montel <montel@kde.org>
0003 
0004    SPDX-License-Identifier: LGPL-2.0-or-later
0005 */
0006 #include "sieveactionbreak.h"
0007 #include "autocreatescripts/autocreatescriptutil_p.h"
0008 #include "editor/sieveeditorutil.h"
0009 
0010 #include <KLineEditEventHandler>
0011 #include <KLocalizedString>
0012 #include <QLineEdit>
0013 
0014 #include "libksieveui_debug.h"
0015 #include <QHBoxLayout>
0016 #include <QLabel>
0017 #include <QXmlStreamReader>
0018 
0019 using namespace KSieveUi;
0020 SieveActionBreak::SieveActionBreak(SieveEditorGraphicalModeWidget *sieveGraphicalModeWidget, QObject *parent)
0021     : SieveAction(sieveGraphicalModeWidget, QStringLiteral("break"), i18n("Break"), parent)
0022 {
0023 }
0024 
0025 QWidget *SieveActionBreak::createParamWidget(QWidget *parent) const
0026 {
0027     auto w = new QWidget(parent);
0028     auto lay = new QHBoxLayout;
0029     lay->setContentsMargins({});
0030     w->setLayout(lay);
0031 
0032     auto lab = new QLabel(i18n("Name (optional):"));
0033     lay->addWidget(lab);
0034 
0035     auto subject = new QLineEdit;
0036     KLineEditEventHandler::catchReturnKey(subject);
0037     subject->setObjectName(QLatin1StringView("name"));
0038     connect(subject, &QLineEdit::textChanged, this, &SieveActionBreak::valueChanged);
0039     lay->addWidget(subject);
0040     return w;
0041 }
0042 
0043 void SieveActionBreak::setParamWidgetValue(QXmlStreamReader &element, QWidget *w, QString &error)
0044 {
0045     while (element.readNextStartElement()) {
0046         const QStringView tagName = element.name();
0047         if (tagName == QLatin1StringView("tag")) {
0048             const QString tagValue = element.readElementText();
0049             if (tagValue == QLatin1StringView("name")) {
0050                 auto name = w->findChild<QLineEdit *>(QStringLiteral("name"));
0051                 name->setText(AutoCreateScriptUtil::strValue(element));
0052             } else {
0053                 unknownTagValue(tagValue, error);
0054                 qCDebug(LIBKSIEVEUI_LOG) << " SieveActionBreak::setParamWidgetValue unknown tagValue " << tagValue;
0055             }
0056         } else if (tagName == QLatin1StringView("str")) {
0057             element.skipCurrentElement();
0058             // Nothing
0059         } else if (tagName == QLatin1StringView("crlf")) {
0060             element.skipCurrentElement();
0061             // nothing
0062         } else if (tagName == QLatin1StringView("comment")) {
0063             element.skipCurrentElement();
0064             // implement in the future ?
0065         } else {
0066             unknownTag(tagName, error);
0067             qCDebug(LIBKSIEVEUI_LOG) << "SieveActionBreak::setParamWidgetValue unknown tag " << tagName;
0068         }
0069     }
0070 }
0071 
0072 QUrl SieveActionBreak::href() const
0073 {
0074     return SieveEditorUtil::helpUrl(SieveEditorUtil::strToVariableName(name()));
0075 }
0076 
0077 QString SieveActionBreak::code(QWidget *w) const
0078 {
0079     const QLineEdit *name = w->findChild<QLineEdit *>(QStringLiteral("name"));
0080     const QString nameStr = name->text();
0081     if (!nameStr.isEmpty()) {
0082         return QStringLiteral("break :name \"%1\";").arg(nameStr);
0083     }
0084     return QStringLiteral("break;");
0085 }
0086 
0087 QString SieveActionBreak::help() const
0088 {
0089     return i18n("The break command terminates the closest enclosing loop.");
0090 }
0091 
0092 QStringList SieveActionBreak::needRequires(QWidget * /*parent*/) const
0093 {
0094     return QStringList() << QStringLiteral("foreverypart");
0095 }
0096 
0097 bool SieveActionBreak::needCheckIfServerHasCapability() const
0098 {
0099     return true;
0100 }
0101 
0102 QString SieveActionBreak::serverNeedsCapability() const
0103 {
0104     return QStringLiteral("foreverypart");
0105 }
0106 
0107 #include "moc_sieveactionbreak.cpp"