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