Libksieve

sieveconditionconvert.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 "sieveconditionconvert.h"
7#include "autocreatescripts/autocreatescriptutil_p.h"
8#include "autocreatescripts/commonwidgets/selectconvertparameterwidget.h"
9#include "autocreatescripts/commonwidgets/selectmimetypecombobox.h"
10
11#include <KLocalizedString>
12
13#include "libksieveui_debug.h"
14#include <QGridLayout>
15#include <QLabel>
16#include <QXmlStreamReader>
17
18using namespace KSieveUi;
19SieveConditionConvert::SieveConditionConvert(SieveEditorGraphicalModeWidget *sieveGraphicalModeWidget, QObject *parent)
20 : SieveCondition(sieveGraphicalModeWidget, QStringLiteral("convert"), i18n("Convert"), parent)
21{
22}
23
24QWidget *SieveConditionConvert::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 fromMimeType->setObjectName(QLatin1StringView("from"));
36 connect(fromMimeType, &SelectMimeTypeComboBox::valueChanged, this, &SieveConditionConvert::valueChanged);
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, &SieveConditionConvert::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, &SieveConditionConvert::valueChanged);
52 params->setObjectName(QLatin1StringView("params"));
53 lay->addWidget(params, 1, 1, 2, 3);
54 return w;
55}
56
57QString SieveConditionConvert::code(QWidget *w) const
58{
59 QString result = QStringLiteral("convert ");
60 const SelectMimeTypeComboBox *fromMimeType = w->findChild<SelectMimeTypeComboBox *>(QStringLiteral("from"));
61 const QString fromMimeTypeStr = fromMimeType->code();
62 result += QStringLiteral("%1 ").arg(fromMimeTypeStr);
63
64 const SelectMimeTypeComboBox *toMimeType = w->findChild<SelectMimeTypeComboBox *>(QStringLiteral("to"));
65 const QString toMimeTypeStr = toMimeType->code();
66 result += QStringLiteral("%1 ").arg(toMimeTypeStr);
67
68 const SelectConvertParameterWidget *params = w->findChild<SelectConvertParameterWidget *>(QStringLiteral("params"));
69 const QString paramsStr = params->code();
70 if (!paramsStr.isEmpty()) {
71 result += paramsStr;
72 }
73 return result + AutoCreateScriptUtil::generateConditionComment(comment());
74}
75
76QStringList SieveConditionConvert::needRequires(QWidget *) const
77{
78 return QStringList() << QStringLiteral("convert");
79}
80
81bool SieveConditionConvert::needCheckIfServerHasCapability() const
82{
83 return true;
84}
85
86QString SieveConditionConvert::serverNeedsCapability() const
87{
88 return QStringLiteral("convert");
89}
90
91QString SieveConditionConvert::help() const
92{
93 return i18n(
94 "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 "
95 "conversion parameters.");
96}
97
98void SieveConditionConvert::setParamWidgetValue(QXmlStreamReader &element, QWidget *w, bool /*notCondition*/, QString &error)
99{
100 int index = 0;
101 QString commentStr;
102 while (element.readNextStartElement()) {
103 const QStringView tagName = element.name();
104 if (tagName == QLatin1StringView("str")) {
105 if (index == 0) {
106 auto fromMimeType = w->findChild<SelectMimeTypeComboBox *>(QStringLiteral("from"));
107 fromMimeType->setCode(element.readElementText(), name(), error);
108 } else if (index == 1) {
109 auto toMimeType = w->findChild<SelectMimeTypeComboBox *>(QStringLiteral("to"));
110 toMimeType->setCode(element.readElementText(), name(), error);
111 } else {
112 tooManyArguments(tagName, index, 2, error);
113 qCDebug(LIBKSIEVEUI_LOG) << " SieveActionConvert::setParamWidgetValue too many argument :" << index;
114 }
115 ++index;
116 } else if (tagName == QLatin1StringView("list")) {
117 auto params = w->findChild<SelectConvertParameterWidget *>(QStringLiteral("params"));
118 params->setCode(AutoCreateScriptUtil::listValue(element), error);
119 } else if (tagName == QLatin1StringView("crlf")) {
120 element.skipCurrentElement();
121 // nothing
122 } else if (tagName == QLatin1StringView("comment")) {
123 commentStr = AutoCreateScriptUtil::loadConditionComment(commentStr, element.readElementText());
124 } else {
125 unknownTag(tagName, error);
126 qCDebug(LIBKSIEVEUI_LOG) << "SieveActionConvert::setParamWidgetValue unknown tag " << tagName;
127 }
128 }
129 if (!commentStr.isEmpty()) {
130 setComment(commentStr);
131 }
132}
133
134#include "moc_sieveconditionconvert.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
bool isEmpty() 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 Sat Dec 21 2024 16:57:10 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.