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

0001 /*
0002    SPDX-FileCopyrightText: 2013-2024 Laurent Montel <montel@kde.org>
0003 
0004    SPDX-License-Identifier: LGPL-2.0-or-later
0005 */
0006 #include "sieveconditionexists.h"
0007 #include "autocreatescripts/autocreatescriptutil_p.h"
0008 #include "editor/sieveeditorutil.h"
0009 #include "widgets/selectheadertypecombobox.h"
0010 
0011 #include <KLocalizedString>
0012 
0013 #include "libksieveui_debug.h"
0014 #include <QComboBox>
0015 #include <QHBoxLayout>
0016 #include <QLabel>
0017 #include <QXmlStreamReader>
0018 
0019 using namespace KSieveUi;
0020 
0021 SieveConditionExists::SieveConditionExists(SieveEditorGraphicalModeWidget *sieveGraphicalModeWidget, QObject *parent)
0022     : SieveCondition(sieveGraphicalModeWidget, QStringLiteral("exists"), i18n("Exists"), parent)
0023 {
0024 }
0025 
0026 QWidget *SieveConditionExists::createParamWidget(QWidget *parent) const
0027 {
0028     auto w = new QWidget(parent);
0029     auto lay = new QHBoxLayout;
0030     lay->setContentsMargins({});
0031     w->setLayout(lay);
0032 
0033     auto combo = new QComboBox;
0034     combo->setObjectName(QLatin1StringView("existscheck"));
0035     combo->addItem(i18n("exists"), QStringLiteral("exists"));
0036     combo->addItem(i18n("not exists"), QStringLiteral("not exists"));
0037     lay->addWidget(combo);
0038     connect(combo, &QComboBox::activated, this, &SieveConditionExists::valueChanged);
0039 
0040     auto lab = new QLabel(i18n("headers:"));
0041     lay->addWidget(lab);
0042 
0043     auto value = new SelectHeaderTypeComboBox;
0044     connect(value, &SelectHeaderTypeComboBox::valueChanged, this, &SieveConditionExists::valueChanged);
0045     value->setObjectName(QLatin1StringView("headervalue"));
0046 
0047     lay->addWidget(value);
0048     return w;
0049 }
0050 
0051 QString SieveConditionExists::code(QWidget *w) const
0052 {
0053     const QComboBox *combo = w->findChild<QComboBox *>(QStringLiteral("existscheck"));
0054     const QString comparison = combo->itemData(combo->currentIndex()).toString();
0055 
0056     const SelectHeaderTypeComboBox *value = w->findChild<SelectHeaderTypeComboBox *>(QStringLiteral("headervalue"));
0057     return QStringLiteral("%1 %2").arg(comparison, value->code()) + AutoCreateScriptUtil::generateConditionComment(comment());
0058 }
0059 
0060 QString SieveConditionExists::help() const
0061 {
0062     return i18n(
0063         "The \"exists\" test is true if the headers listed in the header-names argument exist within the message.  All of the headers must exist or the test "
0064         "is false.");
0065 }
0066 
0067 void SieveConditionExists::setParamWidgetValue(QXmlStreamReader &element, QWidget *w, bool notCondition, QString &error)
0068 {
0069     QString commentStr;
0070     while (element.readNextStartElement()) {
0071         const QStringView tagName = element.name();
0072         if (notCondition) {
0073             auto combo = w->findChild<QComboBox *>(QStringLiteral("existscheck"));
0074             combo->setCurrentIndex(1);
0075         }
0076         if (tagName == QLatin1StringView("str")) {
0077             auto value = w->findChild<SelectHeaderTypeComboBox *>(QStringLiteral("headervalue"));
0078             value->setCode(element.readElementText());
0079         } else if (tagName == QLatin1StringView("list")) {
0080             auto selectHeaderType = w->findChild<SelectHeaderTypeComboBox *>(QStringLiteral("headervalue"));
0081             selectHeaderType->setCode(AutoCreateScriptUtil::listValueToStr(element));
0082         } else if (tagName == QLatin1StringView("crlf")) {
0083             element.skipCurrentElement();
0084             // nothing
0085         } else if (tagName == QLatin1StringView("comment")) {
0086             commentStr = AutoCreateScriptUtil::loadConditionComment(commentStr, element.readElementText());
0087         } else {
0088             unknownTag(tagName, error);
0089             qCDebug(LIBKSIEVEUI_LOG) << " SieveConditionExists::setParamWidgetValue unknown tagName " << tagName;
0090         }
0091     }
0092     if (!commentStr.isEmpty()) {
0093         setComment(commentStr);
0094     }
0095 }
0096 
0097 QUrl SieveConditionExists::href() const
0098 {
0099     return SieveEditorUtil::helpUrl(SieveEditorUtil::strToVariableName(name()));
0100 }
0101 
0102 #include "moc_sieveconditionexists.cpp"