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 "sieveactionkeep.h"
0007 #include "autocreatescripts/autocreatescriptutil_p.h"
0008 #include "autocreatescripts/sieveeditorgraphicalmodewidget.h"
0009 #include "editor/sieveeditorutil.h"
0010 #include "widgets/selectflagswidget.h"
0011 
0012 #include "libksieveui_debug.h"
0013 #include <KLocalizedString>
0014 #include <QHBoxLayout>
0015 #include <QLabel>
0016 #include <QXmlStreamReader>
0017 
0018 using namespace KSieveUi;
0019 SieveActionKeep::SieveActionKeep(SieveEditorGraphicalModeWidget *sieveGraphicalModeWidget, QObject *parent)
0020     : SieveAction(sieveGraphicalModeWidget, QStringLiteral("keep"), i18n("Keep"), parent)
0021 {
0022     mHasImapFlag4Support = sieveCapabilities().contains(QLatin1StringView("imap4flags"));
0023     mHasFlagSupport = sieveCapabilities().contains(QLatin1StringView("imapflags")) || mHasImapFlag4Support;
0024 }
0025 
0026 QString SieveActionKeep::code(QWidget *w) const
0027 {
0028     if (mHasFlagSupport) {
0029         const SelectFlagsWidget *flagsWidget = w->findChild<SelectFlagsWidget *>(QStringLiteral("flagswidget"));
0030         const QString flagCode = flagsWidget->code();
0031         if (flagCode.isEmpty()) {
0032             return QStringLiteral("keep;");
0033         } else {
0034             return QStringLiteral("keep :flags") + QLatin1Char(' ') + flagCode;
0035         }
0036     } else {
0037         return QStringLiteral("keep;");
0038     }
0039 }
0040 
0041 QString SieveActionKeep::help() const
0042 {
0043     return i18n(
0044         "The \"keep\" action is whatever action is taken in lieu of all other actions, if no filtering happens at all; generally, this simply means to file "
0045         "the message into the user's main mailbox.");
0046 }
0047 
0048 QWidget *SieveActionKeep::createParamWidget(QWidget *parent) const
0049 {
0050     if (mHasFlagSupport) {
0051         auto w = new QWidget(parent);
0052         auto lay = new QHBoxLayout;
0053         lay->setContentsMargins({});
0054         w->setLayout(lay);
0055         auto addFlags = new QLabel(i18n("Add flags:"));
0056         lay->addWidget(addFlags);
0057 
0058         auto flagsWidget = new SelectFlagsWidget;
0059         connect(flagsWidget, &SelectFlagsWidget::valueChanged, this, &SieveActionKeep::valueChanged);
0060         flagsWidget->setObjectName(QLatin1StringView("flagswidget"));
0061         lay->addWidget(flagsWidget);
0062         return w;
0063     } else {
0064         return nullptr;
0065     }
0066 }
0067 
0068 void SieveActionKeep::setParamWidgetValue(QXmlStreamReader &element, QWidget *w, QString &error)
0069 {
0070     if (mHasFlagSupport) {
0071         while (element.readNextStartElement()) {
0072             const QStringView tagName = element.name();
0073             if (tagName == QLatin1StringView("list")) {
0074                 auto flagsWidget = w->findChild<SelectFlagsWidget *>(QStringLiteral("flagswidget"));
0075                 flagsWidget->setFlags(AutoCreateScriptUtil::listValue(element));
0076             } else if (tagName == QLatin1StringView("str")) {
0077                 auto flagsWidget = w->findChild<SelectFlagsWidget *>(QStringLiteral("flagswidget"));
0078                 flagsWidget->setFlags(QStringList() << element.readElementText());
0079             } else if (tagName == QLatin1StringView("tag") && element.readElementText() == QLatin1StringView("flags")) {
0080                 // nothing :)
0081                 // Don't skip here.
0082             } else if (tagName == QLatin1StringView("crlf")) {
0083                 element.skipCurrentElement();
0084                 // nothing
0085             } else if (tagName == QLatin1StringView("comment")) {
0086                 element.skipCurrentElement();
0087                 // implement in the future ?
0088             } else {
0089                 unknownTag(tagName, error);
0090                 qCDebug(LIBKSIEVEUI_LOG) << " SieveActionAbstractFlags::setParamWidgetValue unknown tag :" << tagName;
0091             }
0092         }
0093     } else {
0094         qCDebug(LIBKSIEVEUI_LOG) << " Server doesn't support imapflags";
0095     }
0096 }
0097 
0098 QStringList SieveActionKeep::needRequires(QWidget *) const
0099 {
0100     QStringList requiresLst;
0101     if (mHasImapFlag4Support) {
0102         requiresLst << QStringLiteral("imap4flags");
0103     } else if (mHasFlagSupport) {
0104         requiresLst << QStringLiteral("imapflags");
0105     }
0106     return requiresLst;
0107 }
0108 
0109 QUrl SieveActionKeep::href() const
0110 {
0111     return SieveEditorUtil::helpUrl(SieveEditorUtil::strToVariableName(name()));
0112 }
0113 
0114 #include "moc_sieveactionkeep.cpp"