File indexing completed on 2024-12-29 04:54:41

0001 /*
0002    SPDX-FileCopyrightText: 2013-2024 Laurent Montel <montel@kde.org>
0003 
0004    SPDX-License-Identifier: LGPL-2.0-or-later
0005 */
0006 #include "sieveconditionconvert.h"
0007 #include "autocreatescripts/autocreatescriptutil_p.h"
0008 #include "autocreatescripts/commonwidgets/selectconvertparameterwidget.h"
0009 #include "autocreatescripts/commonwidgets/selectmimetypecombobox.h"
0010 
0011 #include <KLocalizedString>
0012 
0013 #include "libksieveui_debug.h"
0014 #include <QGridLayout>
0015 #include <QLabel>
0016 #include <QXmlStreamReader>
0017 
0018 using namespace KSieveUi;
0019 SieveConditionConvert::SieveConditionConvert(SieveEditorGraphicalModeWidget *sieveGraphicalModeWidget, QObject *parent)
0020     : SieveCondition(sieveGraphicalModeWidget, QStringLiteral("convert"), i18n("Convert"), parent)
0021 {
0022 }
0023 
0024 QWidget *SieveConditionConvert::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     fromMimeType->setObjectName(QLatin1StringView("from"));
0036     connect(fromMimeType, &SelectMimeTypeComboBox::valueChanged, this, &SieveConditionConvert::valueChanged);
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, &SieveConditionConvert::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, &SieveConditionConvert::valueChanged);
0052     params->setObjectName(QLatin1StringView("params"));
0053     lay->addWidget(params, 1, 1, 2, 3);
0054     return w;
0055 }
0056 
0057 QString SieveConditionConvert::code(QWidget *w) const
0058 {
0059     QString result = QStringLiteral("convert ");
0060     const SelectMimeTypeComboBox *fromMimeType = w->findChild<SelectMimeTypeComboBox *>(QStringLiteral("from"));
0061     const QString fromMimeTypeStr = fromMimeType->code();
0062     result += QStringLiteral("%1 ").arg(fromMimeTypeStr);
0063 
0064     const SelectMimeTypeComboBox *toMimeType = w->findChild<SelectMimeTypeComboBox *>(QStringLiteral("to"));
0065     const QString toMimeTypeStr = toMimeType->code();
0066     result += QStringLiteral("%1 ").arg(toMimeTypeStr);
0067 
0068     const SelectConvertParameterWidget *params = w->findChild<SelectConvertParameterWidget *>(QStringLiteral("params"));
0069     const QString paramsStr = params->code();
0070     if (!paramsStr.isEmpty()) {
0071         result += paramsStr;
0072     }
0073     return result + AutoCreateScriptUtil::generateConditionComment(comment());
0074 }
0075 
0076 QStringList SieveConditionConvert::needRequires(QWidget *) const
0077 {
0078     return QStringList() << QStringLiteral("convert");
0079 }
0080 
0081 bool SieveConditionConvert::needCheckIfServerHasCapability() const
0082 {
0083     return true;
0084 }
0085 
0086 QString SieveConditionConvert::serverNeedsCapability() const
0087 {
0088     return QStringLiteral("convert");
0089 }
0090 
0091 QString SieveConditionConvert::help() const
0092 {
0093     return i18n(
0094         "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 "
0095         "conversion parameters.");
0096 }
0097 
0098 void SieveConditionConvert::setParamWidgetValue(QXmlStreamReader &element, QWidget *w, bool /*notCondition*/, QString &error)
0099 {
0100     int index = 0;
0101     QString commentStr;
0102     while (element.readNextStartElement()) {
0103         const QStringView tagName = element.name();
0104         if (tagName == QLatin1StringView("str")) {
0105             if (index == 0) {
0106                 auto fromMimeType = w->findChild<SelectMimeTypeComboBox *>(QStringLiteral("from"));
0107                 fromMimeType->setCode(element.readElementText(), name(), error);
0108             } else if (index == 1) {
0109                 auto toMimeType = w->findChild<SelectMimeTypeComboBox *>(QStringLiteral("to"));
0110                 toMimeType->setCode(element.readElementText(), name(), error);
0111             } else {
0112                 tooManyArguments(tagName, index, 2, error);
0113                 qCDebug(LIBKSIEVEUI_LOG) << " SieveActionConvert::setParamWidgetValue too many argument :" << index;
0114             }
0115             ++index;
0116         } else if (tagName == QLatin1StringView("list")) {
0117             auto params = w->findChild<SelectConvertParameterWidget *>(QStringLiteral("params"));
0118             params->setCode(AutoCreateScriptUtil::listValue(element), error);
0119         } else if (tagName == QLatin1StringView("crlf")) {
0120             element.skipCurrentElement();
0121             // nothing
0122         } else if (tagName == QLatin1StringView("comment")) {
0123             commentStr = AutoCreateScriptUtil::loadConditionComment(commentStr, element.readElementText());
0124         } else {
0125             unknownTag(tagName, error);
0126             qCDebug(LIBKSIEVEUI_LOG) << "SieveActionConvert::setParamWidgetValue unknown tag " << tagName;
0127         }
0128     }
0129     if (!commentStr.isEmpty()) {
0130         setComment(commentStr);
0131     }
0132 }
0133 
0134 #include "moc_sieveconditionconvert.cpp"