Libksieve

sieveactionfileinto.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 "sieveactionfileinto.h"
7#include "autocreatescripts/autocreatescriptutil_p.h"
8#include "autocreatescripts/sieveeditorgraphicalmodewidget.h"
9#include "editor/sieveeditorutil.h"
10#include "widgets/moveimapfolderwidget.h"
11#include <KLocalizedString>
12
13#include "libksieveui_debug.h"
14#include <QCheckBox>
15#include <QHBoxLayout>
16#include <QXmlStreamReader>
17// Add support for adding flags
18using namespace KSieveUi;
19SieveActionFileInto::SieveActionFileInto(SieveEditorGraphicalModeWidget *sieveGraphicalModeWidget, QObject *parent)
20 : SieveAction(sieveGraphicalModeWidget, QStringLiteral("fileinto"), i18n("File Into"), parent)
21{
22 mHasCopySupport = sieveCapabilities().contains(QLatin1StringView("copy"));
23 mHasMailBoxSupport = sieveCapabilities().contains(QLatin1StringView("mailbox"));
24}
25
26QString SieveActionFileInto::code(QWidget *w) const
27{
28 QString result = QStringLiteral("fileinto ");
29 const KSieveUi::AbstractMoveImapFolderWidget *edit = w->findChild<KSieveUi::AbstractMoveImapFolderWidget *>(QStringLiteral("fileintolineedit"));
30 const QString text = edit->text();
31 if (mHasCopySupport) {
32 const QCheckBox *copy = w->findChild<QCheckBox *>(QStringLiteral("copy"));
33 if (copy->isChecked()) {
34 result += QLatin1StringView(":copy ");
35 }
36 }
37 if (mHasMailBoxSupport) {
38 const QCheckBox *create = w->findChild<QCheckBox *>(QStringLiteral("create"));
39 if (create->isChecked()) {
40 result += QLatin1StringView(":create ");
41 }
42 }
43 return result + QStringLiteral("\"%1\";").arg(text);
44}
45
46void SieveActionFileInto::setParamWidgetValue(QXmlStreamReader &element, QWidget *w, QString &error)
47{
48 while (element.readNextStartElement()) {
49 const QStringView tagName = element.name();
50 if (tagName == QLatin1StringView("tag")) {
51 const QString tagValue = element.readElementText();
52 if (tagValue == QLatin1StringView("copy")) {
53 if (mHasCopySupport) {
54 auto copy = w->findChild<QCheckBox *>(QStringLiteral("copy"));
55 copy->setChecked(true);
56 } else {
57 error += i18n("Action \"fileinto\" has \"copy\" argument but current server does not support it") + QLatin1Char('\n');
58 qCDebug(LIBKSIEVEUI_LOG) << "SieveActionFileInto::setParamWidgetValue has not copy support ";
59 }
60 } else if (tagValue == QLatin1StringView("create")) {
61 if (mHasMailBoxSupport) {
62 auto create = w->findChild<QCheckBox *>(QStringLiteral("create"));
63 create->setChecked(true);
64 } else {
65 serverDoesNotSupportFeatures(QStringLiteral("create"), error);
66 qCDebug(LIBKSIEVEUI_LOG) << "SieveActionFileInto::setParamWidgetValue server has not create support ";
67 }
68 } else {
69 serverDoesNotSupportFeatures(tagValue, error);
70 qCDebug(LIBKSIEVEUI_LOG) << "SieveActionFileInto::setParamWidgetValue server has not flags support ";
71 }
72 } else if (tagName == QLatin1StringView("str")) {
73 const QString tagValue = element.readElementText();
74 auto edit = w->findChild<KSieveUi::AbstractMoveImapFolderWidget *>(QStringLiteral("fileintolineedit"));
75 edit->setText(AutoCreateScriptUtil::protectSlash(tagValue));
76 } else if (tagName == QLatin1StringView("crlf")) {
77 element.skipCurrentElement();
78 // nothing
79 } else if (tagName == QLatin1StringView("comment")) {
80 element.skipCurrentElement();
81 // implement in the future ?
82 } else {
83 unknownTag(tagName, error);
84 qCDebug(LIBKSIEVEUI_LOG) << " SieveActionFileInto::setParamWidgetValue unknown tagName " << tagName;
85 }
86 }
87}
88
89QWidget *SieveActionFileInto::createParamWidget(QWidget *parent) const
90{
91 auto w = new QWidget(parent);
92 auto lay = new QHBoxLayout(w);
93 lay->setContentsMargins({});
94
95 if (mHasCopySupport) {
96 auto copy = new QCheckBox(i18nc("@option:check", "Keep a copy"));
98 lay->addWidget(copy);
99 connect(copy, &QCheckBox::clicked, this, &SieveActionFileInto::valueChanged);
100 }
101 if (mHasMailBoxSupport) {
102 auto create = new QCheckBox(i18nc("@option:check", "Create folder"));
104 connect(create, &QCheckBox::clicked, this, &SieveActionFileInto::valueChanged);
105 lay->addWidget(create);
106 }
107
108 KSieveUi::AbstractMoveImapFolderWidget *edit = AutoCreateScriptUtil::createImapFolderWidget();
109 edit->setSieveImapAccountSettings(sieveImapAccountSettings());
110 connect(edit, &KSieveUi::AbstractMoveImapFolderWidget::textChanged, this, &SieveActionFileInto::valueChanged);
111 lay->addWidget(edit);
112 edit->setObjectName(QLatin1StringView("fileintolineedit"));
113 return w;
114}
115
116QStringList SieveActionFileInto::needRequires(QWidget *parent) const
117{
118 QStringList lst;
119 lst << QStringLiteral("fileinto");
120 if (mHasCopySupport) {
121 const QCheckBox *copy = parent->findChild<QCheckBox *>(QStringLiteral("copy"));
122 if (copy->isChecked()) {
123 lst << QStringLiteral("copy");
124 }
125 }
126 if (mHasMailBoxSupport) {
127 const QCheckBox *create = parent->findChild<QCheckBox *>(QStringLiteral("create"));
128 if (create->isChecked()) {
129 lst << QStringLiteral("mailbox");
130 }
131 }
132 return lst;
133}
134
135bool SieveActionFileInto::needCheckIfServerHasCapability() const
136{
137 return true;
138}
139
140QString SieveActionFileInto::serverNeedsCapability() const
141{
142 return QStringLiteral("fileinto");
143}
144
145QString SieveActionFileInto::help() const
146{
147 QString helpStr = i18n("The \"fileinto\" action delivers the message into the specified mailbox.");
148 if (mHasMailBoxSupport) {
149 helpStr += QLatin1Char('\n')
150 + i18n("If the optional \":create\" argument is specified, it instructs the Sieve interpreter to create the specified mailbox, if needed, before "
151 "attempting to deliver the message into the specified mailbox.");
152 }
153 if (mHasCopySupport) {
154 helpStr += QLatin1Char('\n')
155 + i18n("If the optional \":copy\" keyword is specified, the tagged command does not cancel the implicit \"keep\". Instead, it merely files or "
156 "redirects a copy in addition to whatever else is happening to the message.");
157 }
158 return helpStr;
159}
160
161QUrl KSieveUi::SieveActionFileInto::href() const
162{
163 return SieveEditorUtil::helpUrl(SieveEditorUtil::strToVariableName(name()));
164}
165
166#include "moc_sieveactionfileinto.cpp"
The AbstractMoveImapFolderWidget class.
QString i18nc(const char *context, const char *text, const TYPE &arg...)
QString i18n(const char *text, const TYPE &arg...)
QAction * create(GameStandardAction id, const QObject *recvr, const char *slot, QObject *parent)
QString name(GameStandardAction id)
KIOCORE_EXPORT CopyJob * copy(const QList< QUrl > &src, const QUrl &dest, JobFlags flags=DefaultFlags)
void error(QWidget *parent, const QString &text, const QString &title, const KGuiItem &buttonOk, Options options=Notify)
QByteArray tagValue(const Elem &elem, const char *keyName)
void clicked(bool checked)
bool isChecked() const const
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)
QString arg(Args &&... args) const const
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 Fri Nov 29 2024 11:56:12 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.