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 "sieveconditionheader.h"
0007 #include "autocreatescripts/autocreatescriptutil_p.h"
0008 #include "autocreatescripts/commonwidgets/selectmatchtypecombobox.h"
0009 #include "widgets/selectheadertypecombobox.h"
0010 
0011 #include <KLocalizedString>
0012 
0013 #include "libksieveui_debug.h"
0014 #include <QHBoxLayout>
0015 #include <QLabel>
0016 #include <QXmlStreamReader>
0017 
0018 using namespace KSieveUi;
0019 
0020 SieveConditionHeader::SieveConditionHeader(SieveEditorGraphicalModeWidget *sieveGraphicalModeWidget, QObject *parent)
0021     : SieveCondition(sieveGraphicalModeWidget, QStringLiteral("header"), i18n("Header"), parent)
0022 {
0023 }
0024 
0025 QWidget *SieveConditionHeader::createParamWidget(QWidget *parent) const
0026 {
0027     auto w = new QWidget(parent);
0028     auto lay = new QHBoxLayout;
0029     lay->setContentsMargins({});
0030     w->setLayout(lay);
0031 
0032     auto matchTypeCombo = new SelectMatchTypeComboBox(mSieveGraphicalModeWidget);
0033     matchTypeCombo->setObjectName(QLatin1StringView("matchtypecombobox"));
0034     connect(matchTypeCombo, &SelectMatchTypeComboBox::valueChanged, this, &SieveConditionHeader::valueChanged);
0035     lay->addWidget(matchTypeCombo);
0036 
0037     auto grid = new QGridLayout;
0038     lay->addLayout(grid);
0039 
0040     auto headerType = new SelectHeaderTypeComboBox;
0041     headerType->setObjectName(QLatin1StringView("headertype"));
0042     connect(headerType, &SelectHeaderTypeComboBox::valueChanged, this, &SieveConditionHeader::valueChanged);
0043     grid->addWidget(headerType, 0, 0, 1, 2);
0044 
0045     auto lab = new QLabel(i18n("With value:"));
0046     grid->addWidget(lab, 1, 0);
0047 
0048     AbstractRegexpEditorLineEdit *value = AutoCreateScriptUtil::createRegexpEditorLineEdit();
0049     connect(value, &AbstractRegexpEditorLineEdit::textChanged, this, &SieveConditionHeader::valueChanged);
0050     connect(matchTypeCombo, &SelectMatchTypeComboBox::switchToRegexp, value, &AbstractRegexpEditorLineEdit::switchToRegexpEditorLineEdit);
0051     value->setObjectName(QLatin1StringView("value"));
0052     grid->addWidget(value, 1, 1);
0053     return w;
0054 }
0055 
0056 QString SieveConditionHeader::code(QWidget *w) const
0057 {
0058     const SelectMatchTypeComboBox *matchTypeCombo = w->findChild<SelectMatchTypeComboBox *>(QStringLiteral("matchtypecombobox"));
0059     bool isNegative = false;
0060     const QString matchString = matchTypeCombo->code(isNegative);
0061 
0062     const SelectHeaderTypeComboBox *headerType = w->findChild<SelectHeaderTypeComboBox *>(QStringLiteral("headertype"));
0063     const QString headerStr = headerType->code();
0064 
0065     const AbstractRegexpEditorLineEdit *edit = w->findChild<AbstractRegexpEditorLineEdit *>(QStringLiteral("value"));
0066     QString valueStr = edit->code();
0067 
0068     valueStr = AutoCreateScriptUtil::fixListValue(valueStr);
0069     return AutoCreateScriptUtil::negativeString(isNegative) + QStringLiteral("header %1 %2 %3").arg(matchString, headerStr, valueStr)
0070         + AutoCreateScriptUtil::generateConditionComment(comment());
0071 }
0072 
0073 QString SieveConditionHeader::help() const
0074 {
0075     return i18n("The \"header\" test evaluates to true if the value of any of the named headers, ignoring leading and trailing whitespace, matches any key.");
0076 }
0077 
0078 void SieveConditionHeader::setParamWidgetValue(QXmlStreamReader &element, QWidget *w, bool notCondition, QString &error)
0079 {
0080     int index = 0;
0081     QString commentStr;
0082     while (element.readNextStartElement()) {
0083         const QStringView tagName = element.name();
0084 
0085         if (tagName == QLatin1StringView("tag")) {
0086             const QString tagValue = element.readElementText();
0087             if (tagValue == QLatin1StringView("comparator")) {
0088                 qCWarning(LIBKSIEVEUI_LOG) << " comparator support not implemented yet!";
0089             } else {
0090                 auto selectMatchCombobox = w->findChild<SelectMatchTypeComboBox *>(QStringLiteral("matchtypecombobox"));
0091                 selectMatchCombobox->setCode(AutoCreateScriptUtil::tagValueWithCondition(tagValue, notCondition), name(), error);
0092             }
0093         } else if (tagName == QLatin1StringView("str")) {
0094             if (index == 0) {
0095                 auto headerType = w->findChild<SelectHeaderTypeComboBox *>(QStringLiteral("headertype"));
0096                 headerType->setCode(element.readElementText());
0097             } else if (index == 1) {
0098                 auto value = w->findChild<AbstractRegexpEditorLineEdit *>(QStringLiteral("value"));
0099                 QString st = AutoCreateScriptUtil::quoteStr(element.readElementText(), true);
0100                 value->setCode(st);
0101             } else {
0102                 tooManyArguments(tagName, index, 2, error);
0103                 qCDebug(LIBKSIEVEUI_LOG) << " SieveConditionHeader::setParamWidgetValue too many argument " << index;
0104             }
0105             ++index;
0106         } else if (tagName == QLatin1StringView("list")) {
0107             // Header list
0108             if (index == 0) {
0109                 auto headerType = w->findChild<SelectHeaderTypeComboBox *>(QStringLiteral("headertype"));
0110                 headerType->setCode(AutoCreateScriptUtil::listValueToStr(element));
0111             } else if (index == 1) {
0112                 auto value = w->findChild<AbstractRegexpEditorLineEdit *>(QStringLiteral("value"));
0113                 value->setCode(AutoCreateScriptUtil::listValueToStr(element));
0114             } else {
0115                 tooManyArguments(tagName, index, 2, error);
0116                 qCDebug(LIBKSIEVEUI_LOG) << " SieveConditionHeader::setParamWidgetValue too many argument " << index;
0117             }
0118             ++index;
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) << " SieveConditionHeader::setParamWidgetValue unknown tagName " << tagName;
0127         }
0128     }
0129     if (!commentStr.isEmpty()) {
0130         setComment(commentStr);
0131     }
0132 }
0133 
0134 QStringList KSieveUi::SieveConditionHeader::needRequires(QWidget *w) const
0135 {
0136     const SelectMatchTypeComboBox *matchTypeCombo = w->findChild<SelectMatchTypeComboBox *>(QStringLiteral("matchtypecombobox"));
0137     return matchTypeCombo->needRequires();
0138 }
0139 
0140 #include "moc_sieveconditionheader.cpp"