Libksieve

sieveactionkeep.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 "sieveactionkeep.h"
7#include "autocreatescripts/autocreatescriptutil_p.h"
8#include "autocreatescripts/sieveeditorgraphicalmodewidget.h"
9#include "editor/sieveeditorutil.h"
10#include "widgets/selectflagswidget.h"
11
12#include "libksieveui_debug.h"
13#include <KLocalizedString>
14#include <QHBoxLayout>
15#include <QLabel>
16#include <QXmlStreamReader>
17
18using namespace KSieveUi;
19SieveActionKeep::SieveActionKeep(SieveEditorGraphicalModeWidget *sieveGraphicalModeWidget, QObject *parent)
20 : SieveAction(sieveGraphicalModeWidget, QStringLiteral("keep"), i18n("Keep"), parent)
21{
22 mHasImapFlag4Support = sieveCapabilities().contains(QLatin1StringView("imap4flags"));
23 mHasFlagSupport = sieveCapabilities().contains(QLatin1StringView("imapflags")) || mHasImapFlag4Support;
24}
25
26QString SieveActionKeep::code(QWidget *w) const
27{
28 if (mHasFlagSupport) {
29 const SelectFlagsWidget *flagsWidget = w->findChild<SelectFlagsWidget *>(QStringLiteral("flagswidget"));
30 const QString flagCode = flagsWidget->code();
31 if (flagCode.isEmpty()) {
32 return QStringLiteral("keep;");
33 } else {
34 return QStringLiteral("keep :flags") + QLatin1Char(' ') + flagCode;
35 }
36 } else {
37 return QStringLiteral("keep;");
38 }
39}
40
41QString SieveActionKeep::help() const
42{
43 return i18n(
44 "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 "
45 "the message into the user's main mailbox.");
46}
47
48QWidget *SieveActionKeep::createParamWidget(QWidget *parent) const
49{
50 if (mHasFlagSupport) {
51 auto w = new QWidget(parent);
52 auto lay = new QHBoxLayout;
53 lay->setContentsMargins({});
54 w->setLayout(lay);
55 auto addFlags = new QLabel(i18nc("@label:textbox", "Add flags:"));
56 lay->addWidget(addFlags);
57
58 auto flagsWidget = new SelectFlagsWidget;
59 connect(flagsWidget, &SelectFlagsWidget::valueChanged, this, &SieveActionKeep::valueChanged);
60 flagsWidget->setObjectName(QLatin1StringView("flagswidget"));
61 lay->addWidget(flagsWidget);
62 return w;
63 } else {
64 return nullptr;
65 }
66}
67
68void SieveActionKeep::setParamWidgetValue(QXmlStreamReader &element, QWidget *w, QString &error)
69{
70 if (mHasFlagSupport) {
71 while (element.readNextStartElement()) {
72 const QStringView tagName = element.name();
73 if (tagName == QLatin1StringView("list")) {
74 auto flagsWidget = w->findChild<SelectFlagsWidget *>(QStringLiteral("flagswidget"));
75 flagsWidget->setFlags(AutoCreateScriptUtil::listValue(element));
76 } else if (tagName == QLatin1StringView("str")) {
77 auto flagsWidget = w->findChild<SelectFlagsWidget *>(QStringLiteral("flagswidget"));
78 flagsWidget->setFlags(QStringList() << element.readElementText());
79 } else if (tagName == QLatin1StringView("tag") && element.readElementText() == QLatin1StringView("flags")) {
80 // nothing :)
81 // Don't skip here.
82 } else if (tagName == QLatin1StringView("crlf")) {
83 element.skipCurrentElement();
84 // nothing
85 } else if (tagName == QLatin1StringView("comment")) {
86 element.skipCurrentElement();
87 // implement in the future ?
88 } else {
89 unknownTag(tagName, error);
90 qCDebug(LIBKSIEVEUI_LOG) << " SieveActionAbstractFlags::setParamWidgetValue unknown tag :" << tagName;
91 }
92 }
93 } else {
94 qCDebug(LIBKSIEVEUI_LOG) << " Server doesn't support imapflags";
95 }
96}
97
98QStringList SieveActionKeep::needRequires(QWidget *) const
99{
100 QStringList requiresLst;
101 if (mHasImapFlag4Support) {
102 requiresLst << QStringLiteral("imap4flags");
103 } else if (mHasFlagSupport) {
104 requiresLst << QStringLiteral("imapflags");
105 }
106 return requiresLst;
107}
108
109QUrl SieveActionKeep::href() const
110{
111 return SieveEditorUtil::helpUrl(SieveEditorUtil::strToVariableName(name()));
112}
113
114#include "moc_sieveactionkeep.cpp"
QString i18nc(const char *context, const char *text, const TYPE &arg...)
QString i18n(const char *text, const TYPE &arg...)
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)
void setLayout(QLayout *layout)
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 Mon Nov 18 2024 12:14:30 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.