File indexing completed on 2025-02-16 04:55:56

0001 /*
0002    SPDX-FileCopyrightText: 2013-2024 Laurent Montel <montel@kde.org>
0003 
0004    SPDX-License-Identifier: LGPL-2.0-or-later
0005 */
0006 #include "sieveactionconvert.h"
0007 #include "autocreatescripts/autocreatescriptutil_p.h"
0008 #include "autocreatescripts/commonwidgets/selectconvertparameterwidget.h"
0009 #include "autocreatescripts/commonwidgets/selectmimetypecombobox.h"
0010 
0011 #include "libksieveui_debug.h"
0012 #include <KLocalizedString>
0013 #include <QGridLayout>
0014 #include <QLabel>
0015 #include <QUrl>
0016 #include <QXmlStreamReader>
0017 
0018 using namespace KSieveUi;
0019 SieveActionConvert::SieveActionConvert(SieveEditorGraphicalModeWidget *sieveGraphicalModeWidget, QObject *parent)
0020     : SieveAction(sieveGraphicalModeWidget, QStringLiteral("convert"), i18n("Convert"), parent)
0021 {
0022 }
0023 
0024 QWidget *SieveActionConvert::createParamWidget(QWidget *parent) const
0025 {
0026     auto w = new QWidget(parent);
0027     auto lay = new QGridLayout;
0028     lay->setContentsMargins({});
0029     w->setLayout(lay);
0030 
0031     auto lab = new QLabel(i18n("From:"));
0032     lay->addWidget(lab, 0, 0);
0033 
0034     auto fromMimeType = new SelectMimeTypeComboBox;
0035     connect(fromMimeType, &SelectMimeTypeComboBox::valueChanged, this, &SieveActionConvert::valueChanged);
0036     fromMimeType->setObjectName(QLatin1StringView("from"));
0037     lay->addWidget(fromMimeType, 0, 1);
0038 
0039     lab = new QLabel(i18n("To:"));
0040     lay->addWidget(lab, 0, 2);
0041 
0042     auto toMimeType = new SelectMimeTypeComboBox;
0043     connect(toMimeType, &SelectMimeTypeComboBox::valueChanged, this, &SieveActionConvert::valueChanged);
0044     toMimeType->setObjectName(QLatin1StringView("to"));
0045     lay->addWidget(toMimeType, 0, 3);
0046 
0047     lab = new QLabel(i18n("Parameters:"));
0048     lay->addWidget(lab, 1, 0);
0049 
0050     auto params = new SelectConvertParameterWidget;
0051     connect(params, &SelectConvertParameterWidget::valueChanged, this, &SieveActionConvert::valueChanged);
0052     params->setObjectName(QLatin1StringView("params"));
0053     lay->addWidget(params, 1, 1, 2, 3);
0054 
0055     return w;
0056 }
0057 
0058 void SieveActionConvert::setParamWidgetValue(QXmlStreamReader &element, QWidget *w, QString &error)
0059 {
0060     int index = 0;
0061     while (element.readNextStartElement()) {
0062         const QStringView tagName = element.name();
0063         if (tagName == QLatin1StringView("str")) {
0064             if (index == 0) {
0065                 auto fromMimeType = w->findChild<SelectMimeTypeComboBox *>(QStringLiteral("from"));
0066                 fromMimeType->setCode(element.readElementText(), name(), error);
0067             } else if (index == 1) {
0068                 auto toMimeType = w->findChild<SelectMimeTypeComboBox *>(QStringLiteral("to"));
0069                 toMimeType->setCode(element.readElementText(), name(), error);
0070             } else {
0071                 tooManyArguments(tagName, index, 2, error);
0072                 qCDebug(LIBKSIEVEUI_LOG) << " SieveActionConvert::setParamWidgetValue too many argument :" << index;
0073             }
0074             ++index;
0075         } else if (tagName == QLatin1StringView("list")) {
0076             auto params = w->findChild<SelectConvertParameterWidget *>(QStringLiteral("params"));
0077             params->setCode(AutoCreateScriptUtil::listValue(element), error);
0078         } else if (tagName == QLatin1StringView("crlf")) {
0079             element.skipCurrentElement();
0080             // nothing
0081         } else if (tagName == QLatin1StringView("comment")) {
0082             element.skipCurrentElement();
0083             // implement in the future ?
0084         } else {
0085             unknownTag(tagName, error);
0086             qCDebug(LIBKSIEVEUI_LOG) << "SieveActionConvert::setParamWidgetValue unknown tag " << tagName;
0087         }
0088     }
0089 }
0090 
0091 QString SieveActionConvert::code(QWidget *w) const
0092 {
0093     QString result = QStringLiteral("convert ");
0094     const SelectMimeTypeComboBox *fromMimeType = w->findChild<SelectMimeTypeComboBox *>(QStringLiteral("from"));
0095     const QString fromMimeTypeStr = fromMimeType->code();
0096     result += QStringLiteral("%1 ").arg(fromMimeTypeStr);
0097 
0098     const SelectMimeTypeComboBox *toMimeType = w->findChild<SelectMimeTypeComboBox *>(QStringLiteral("to"));
0099     const QString toMimeTypeStr = toMimeType->code();
0100     result += QStringLiteral("%1 ").arg(toMimeTypeStr);
0101 
0102     const SelectConvertParameterWidget *params = w->findChild<SelectConvertParameterWidget *>(QStringLiteral("params"));
0103     const QString paramsStr = params->code();
0104     if (!paramsStr.isEmpty()) {
0105         result += paramsStr;
0106     }
0107     result += QLatin1Char(';');
0108     return result;
0109 }
0110 
0111 QStringList SieveActionConvert::needRequires(QWidget *) const
0112 {
0113     return QStringList() << QStringLiteral("convert");
0114 }
0115 
0116 bool SieveActionConvert::needCheckIfServerHasCapability() const
0117 {
0118     return true;
0119 }
0120 
0121 QString SieveActionConvert::serverNeedsCapability() const
0122 {
0123     return QStringLiteral("convert");
0124 }
0125 
0126 QString SieveActionConvert::help() const
0127 {
0128     return i18n(
0129         "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 "
0130         "conversion parameters.");
0131 }
0132 
0133 QUrl SieveActionConvert::href() const
0134 {
0135     return QUrl(QStringLiteral("https://tools.ietf.org/html/draft-ietf-sieve-convert-06"));
0136 }
0137 
0138 #include "moc_sieveactionconvert.cpp"