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

0001 /*
0002    SPDX-FileCopyrightText: 2013-2024 Laurent Montel <montel@kde.org>
0003 
0004    SPDX-License-Identifier: LGPL-2.0-or-later
0005 */
0006 #include "sieveconditionservermetadata.h"
0007 #include "autocreatescripts/autocreatescriptutil_p.h"
0008 #include "autocreatescripts/commonwidgets/selectmatchtypecombobox.h"
0009 #include "editor/sieveeditorutil.h"
0010 
0011 #include <KLineEditEventHandler>
0012 #include <KLocalizedString>
0013 #include <QLineEdit>
0014 
0015 #include "libksieveui_debug.h"
0016 #include <QHBoxLayout>
0017 #include <QLabel>
0018 #include <QWidget>
0019 #include <QXmlStreamReader>
0020 
0021 using namespace KSieveUi;
0022 SieveConditionServerMetaData::SieveConditionServerMetaData(SieveEditorGraphicalModeWidget *sieveGraphicalModeWidget, QObject *parent)
0023     : SieveCondition(sieveGraphicalModeWidget, QStringLiteral("servermetadata"), i18n("Server Meta Data"), parent)
0024 {
0025 }
0026 
0027 QWidget *SieveConditionServerMetaData::createParamWidget(QWidget *parent) const
0028 {
0029     auto w = new QWidget(parent);
0030     auto lay = new QHBoxLayout;
0031     lay->setContentsMargins({});
0032     w->setLayout(lay);
0033 
0034     auto selectType = new SelectMatchTypeComboBox(mSieveGraphicalModeWidget);
0035     selectType->setObjectName(QLatin1StringView("selecttype"));
0036     connect(selectType, &SelectMatchTypeComboBox::valueChanged, this, &SieveConditionServerMetaData::valueChanged);
0037     lay->addWidget(selectType);
0038 
0039     auto grid = new QGridLayout;
0040     grid->setContentsMargins({});
0041     lay->addLayout(grid);
0042 
0043     auto lab = new QLabel(i18n("Mailbox:"));
0044     grid->addWidget(lab, 0, 0);
0045 
0046     auto mailbox = new QLineEdit;
0047     KLineEditEventHandler::catchReturnKey(mailbox);
0048     mailbox->setObjectName(QLatin1StringView("mailbox"));
0049     connect(mailbox, &QLineEdit::textChanged, this, &SieveConditionServerMetaData::valueChanged);
0050     grid->addWidget(mailbox, 0, 1);
0051 
0052     lab = new QLabel(i18n("Annotations:"));
0053     grid->addWidget(lab, 1, 0);
0054 
0055     auto annotation = new QLineEdit;
0056     KLineEditEventHandler::catchReturnKey(annotation);
0057     connect(annotation, &QLineEdit::textChanged, this, &SieveConditionServerMetaData::valueChanged);
0058     annotation->setObjectName(QLatin1StringView("annotation"));
0059     grid->addWidget(annotation, 1, 1);
0060 
0061     lab = new QLabel(i18n("Value:"));
0062     grid->addWidget(lab, 2, 0);
0063 
0064     AbstractRegexpEditorLineEdit *value = AutoCreateScriptUtil::createRegexpEditorLineEdit();
0065     connect(value, &AbstractRegexpEditorLineEdit::textChanged, this, &SieveConditionServerMetaData::valueChanged);
0066     connect(selectType, &SelectMatchTypeComboBox::switchToRegexp, value, &AbstractRegexpEditorLineEdit::switchToRegexpEditorLineEdit);
0067     value->setObjectName(QLatin1StringView("value"));
0068     grid->addWidget(value, 2, 1);
0069 
0070     return w;
0071 }
0072 
0073 QString SieveConditionServerMetaData::code(QWidget *w) const
0074 {
0075     const SelectMatchTypeComboBox *selectType = w->findChild<SelectMatchTypeComboBox *>(QStringLiteral("selecttype"));
0076     bool isNegative = false;
0077     const QString matchString = selectType->code(isNegative);
0078 
0079     QString result = (isNegative ? QStringLiteral("not ") : QString()) + QStringLiteral("servermetadata %1 ").arg(matchString);
0080 
0081     const QLineEdit *mailbox = w->findChild<QLineEdit *>(QStringLiteral("mailbox"));
0082     const QString mailboxStr = mailbox->text();
0083 
0084     result += QStringLiteral("\"%1\" ").arg(mailboxStr);
0085 
0086     const QLineEdit *annotation = w->findChild<QLineEdit *>(QStringLiteral("annotation"));
0087     const QString annotationStr = annotation->text();
0088 
0089     result += QStringLiteral("\"%1\" ").arg(annotationStr);
0090 
0091     const AbstractRegexpEditorLineEdit *value = w->findChild<AbstractRegexpEditorLineEdit *>(QStringLiteral("value"));
0092     const QString valueStr = value->code();
0093 
0094     result += QStringLiteral("\"%1\"").arg(valueStr);
0095     return result + AutoCreateScriptUtil::generateConditionComment(comment());
0096 }
0097 
0098 QStringList SieveConditionServerMetaData::needRequires(QWidget *w) const
0099 {
0100     const SelectMatchTypeComboBox *selectType = w->findChild<SelectMatchTypeComboBox *>(QStringLiteral("selecttype"));
0101     return QStringList() << QStringLiteral("servermetadata") << selectType->needRequires();
0102 }
0103 
0104 bool SieveConditionServerMetaData::needCheckIfServerHasCapability() const
0105 {
0106     return true;
0107 }
0108 
0109 QString SieveConditionServerMetaData::serverNeedsCapability() const
0110 {
0111     return QStringLiteral("servermetadata");
0112 }
0113 
0114 QString SieveConditionServerMetaData::help() const
0115 {
0116     return i18n(
0117         "This test retrieves the value of the server annotation \"annotation-name\".  The retrieved value is compared to the \"key-list\". The test returns "
0118         "true if the annotation exists and its value matches any of the keys.");
0119 }
0120 
0121 void SieveConditionServerMetaData::setParamWidgetValue(QXmlStreamReader &element, QWidget *w, bool notCondition, QString &error)
0122 {
0123     int index = 0;
0124     QString commentStr;
0125     while (element.readNextStartElement()) {
0126         const QStringView tagName = element.name();
0127         if (tagName == QLatin1StringView("str")) {
0128             const QString tagValue = element.readElementText();
0129             switch (index) {
0130             case 0: {
0131                 auto mailbox = w->findChild<QLineEdit *>(QStringLiteral("mailbox"));
0132                 mailbox->setText(tagValue);
0133                 break;
0134             }
0135             case 1: {
0136                 auto annotation = w->findChild<QLineEdit *>(QStringLiteral("annotation"));
0137                 annotation->setText(tagValue);
0138                 break;
0139             }
0140             case 2: {
0141                 auto value = w->findChild<AbstractRegexpEditorLineEdit *>(QStringLiteral("value"));
0142                 value->setCode(tagValue);
0143                 break;
0144             }
0145             default:
0146                 tooManyArguments(tagName, index, 3, error);
0147                 qCDebug(LIBKSIEVEUI_LOG) << " SieveConditionServerMetaData::setParamWidgetValue too many argument " << index;
0148                 break;
0149             }
0150             ++index;
0151         } else if (tagName == QLatin1StringView("tag")) {
0152             auto selectType = w->findChild<SelectMatchTypeComboBox *>(QStringLiteral("selecttype"));
0153             selectType->setCode(AutoCreateScriptUtil::tagValueWithCondition(element.readElementText(), notCondition), name(), error);
0154         } else if (tagName == QLatin1StringView("crlf")) {
0155             element.skipCurrentElement();
0156             // nothing
0157         } else if (tagName == QLatin1StringView("comment")) {
0158             commentStr = AutoCreateScriptUtil::loadConditionComment(commentStr, element.readElementText());
0159         } else {
0160             unknownTag(tagName, error);
0161             qCDebug(LIBKSIEVEUI_LOG) << " SieveConditionServerMetaData::setParamWidgetValue unknown tagName " << tagName;
0162         }
0163     }
0164     if (!commentStr.isEmpty()) {
0165         setComment(commentStr);
0166     }
0167 }
0168 
0169 QUrl SieveConditionServerMetaData::href() const
0170 {
0171     return SieveEditorUtil::helpUrl(SieveEditorUtil::strToVariableName(name()));
0172 }
0173 
0174 #include "moc_sieveconditionservermetadata.cpp"