Libksieve

sieveactionconvert.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 "sieveactionconvert.h"
7#include "autocreatescripts/autocreatescriptutil_p.h"
8#include "autocreatescripts/commonwidgets/selectconvertparameterwidget.h"
9#include "autocreatescripts/commonwidgets/selectmimetypecombobox.h"
10
11#include "libksieveui_debug.h"
12#include <KLocalizedString>
13#include <QGridLayout>
14#include <QLabel>
15#include <QUrl>
16#include <QXmlStreamReader>
17
18using namespace KSieveUi;
19SieveActionConvert::SieveActionConvert(SieveEditorGraphicalModeWidget *sieveGraphicalModeWidget, QObject *parent)
20 : SieveAction(sieveGraphicalModeWidget, QStringLiteral("convert"), i18n("Convert"), parent)
21{
22}
23
24QWidget *SieveActionConvert::createParamWidget(QWidget *parent) const
25{
26 auto w = new QWidget(parent);
27 auto lay = new QGridLayout;
28 lay->setContentsMargins({});
29 w->setLayout(lay);
30
31 auto lab = new QLabel(i18nc("@label:textbox", "From:"));
32 lay->addWidget(lab, 0, 0);
33
34 auto fromMimeType = new SelectMimeTypeComboBox;
35 connect(fromMimeType, &SelectMimeTypeComboBox::valueChanged, this, &SieveActionConvert::valueChanged);
36 fromMimeType->setObjectName(QLatin1StringView("from"));
37 lay->addWidget(fromMimeType, 0, 1);
38
39 lab = new QLabel(i18nc("@label:textbox", "To:"));
40 lay->addWidget(lab, 0, 2);
41
42 auto toMimeType = new SelectMimeTypeComboBox;
43 connect(toMimeType, &SelectMimeTypeComboBox::valueChanged, this, &SieveActionConvert::valueChanged);
44 toMimeType->setObjectName(QLatin1StringView("to"));
45 lay->addWidget(toMimeType, 0, 3);
46
47 lab = new QLabel(i18nc("@label:textbox", "Parameters:"));
48 lay->addWidget(lab, 1, 0);
49
50 auto params = new SelectConvertParameterWidget;
51 connect(params, &SelectConvertParameterWidget::valueChanged, this, &SieveActionConvert::valueChanged);
52 params->setObjectName(QLatin1StringView("params"));
53 lay->addWidget(params, 1, 1, 2, 3);
54
55 return w;
56}
57
58void SieveActionConvert::setParamWidgetValue(QXmlStreamReader &element, QWidget *w, QString &error)
59{
60 int index = 0;
61 while (element.readNextStartElement()) {
62 const QStringView tagName = element.name();
63 if (tagName == QLatin1StringView("str")) {
64 if (index == 0) {
65 auto fromMimeType = w->findChild<SelectMimeTypeComboBox *>(QStringLiteral("from"));
66 fromMimeType->setCode(element.readElementText(), name(), error);
67 } else if (index == 1) {
68 auto toMimeType = w->findChild<SelectMimeTypeComboBox *>(QStringLiteral("to"));
69 toMimeType->setCode(element.readElementText(), name(), error);
70 } else {
71 tooManyArguments(tagName, index, 2, error);
72 qCDebug(LIBKSIEVEUI_LOG) << " SieveActionConvert::setParamWidgetValue too many argument :" << index;
73 }
74 ++index;
75 } else if (tagName == QLatin1StringView("list")) {
76 auto params = w->findChild<SelectConvertParameterWidget *>(QStringLiteral("params"));
77 params->setCode(AutoCreateScriptUtil::listValue(element), error);
78 } else if (tagName == QLatin1StringView("crlf")) {
79 element.skipCurrentElement();
80 // nothing
81 } else if (tagName == QLatin1StringView("comment")) {
82 element.skipCurrentElement();
83 // implement in the future ?
84 } else {
85 unknownTag(tagName, error);
86 qCDebug(LIBKSIEVEUI_LOG) << "SieveActionConvert::setParamWidgetValue unknown tag " << tagName;
87 }
88 }
89}
90
91QString SieveActionConvert::code(QWidget *w) const
92{
93 QString result = QStringLiteral("convert ");
94 const SelectMimeTypeComboBox *fromMimeType = w->findChild<SelectMimeTypeComboBox *>(QStringLiteral("from"));
95 const QString fromMimeTypeStr = fromMimeType->code();
96 result += QStringLiteral("%1 ").arg(fromMimeTypeStr);
97
98 const SelectMimeTypeComboBox *toMimeType = w->findChild<SelectMimeTypeComboBox *>(QStringLiteral("to"));
99 const QString toMimeTypeStr = toMimeType->code();
100 result += QStringLiteral("%1 ").arg(toMimeTypeStr);
101
102 const SelectConvertParameterWidget *params = w->findChild<SelectConvertParameterWidget *>(QStringLiteral("params"));
103 const QString paramsStr = params->code();
104 if (!paramsStr.isEmpty()) {
105 result += paramsStr;
106 }
107 result += QLatin1Char(';');
108 return result;
109}
110
111QStringList SieveActionConvert::needRequires(QWidget *) const
112{
113 return QStringList() << QStringLiteral("convert");
114}
115
116bool SieveActionConvert::needCheckIfServerHasCapability() const
117{
118 return true;
119}
120
121QString SieveActionConvert::serverNeedsCapability() const
122{
123 return QStringLiteral("convert");
124}
125
126QString SieveActionConvert::help() const
127{
128 return i18n(
129 "The \"convert\" action specifies that all body parts with a media type equal to \"media-type\" be converted to the media type in \"media-type\" using "
130 "conversion parameters.");
131}
132
133QUrl SieveActionConvert::href() const
134{
135 return QUrl(QStringLiteral("https://tools.ietf.org/html/draft-ietf-sieve-convert-06"));
136}
137
138#include "moc_sieveactionconvert.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
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 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.