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 "sieveconditionmetadataexists.h"
0007 #include "autocreatescripts/autocreatescriptutil_p.h"
0008 #include "editor/sieveeditorutil.h"
0009 #include "util/sieveimapaccountsettings.h"
0010 #include <KLocalizedString>
0011 #include <QLineEdit>
0012 
0013 #include "libksieveui_debug.h"
0014 #include <KLineEditEventHandler>
0015 #include <KSieveUi/AbstractMoveImapFolderWidget>
0016 #include <QGridLayout>
0017 #include <QLabel>
0018 #include <QXmlStreamReader>
0019 
0020 using namespace KSieveUi;
0021 SieveConditionMetaDataExists::SieveConditionMetaDataExists(SieveEditorGraphicalModeWidget *sieveGraphicalModeWidget, QObject *parent)
0022     : SieveCondition(sieveGraphicalModeWidget, QStringLiteral("metadataexists"), i18n("Metadata exists"), parent)
0023 {
0024 }
0025 
0026 QWidget *SieveConditionMetaDataExists::createParamWidget(QWidget *parent) const
0027 {
0028     auto w = new QWidget(parent);
0029     auto grid = new QGridLayout;
0030     grid->setContentsMargins({});
0031     w->setLayout(grid);
0032 
0033     auto lab = new QLabel(i18n("Mailbox:"));
0034     grid->addWidget(lab, 0, 0);
0035 
0036     KSieveUi::AbstractMoveImapFolderWidget *mailbox = AutoCreateScriptUtil::createImapFolderWidget();
0037     mailbox->setSieveImapAccountSettings(sieveImapAccountSettings());
0038     connect(mailbox, &KSieveUi::AbstractMoveImapFolderWidget::textChanged, this, &SieveConditionMetaDataExists::valueChanged);
0039     mailbox->setObjectName(QLatin1StringView("mailbox"));
0040     grid->addWidget(mailbox, 0, 1);
0041 
0042     lab = new QLabel(i18n("Annotation:"));
0043     grid->addWidget(lab, 1, 0);
0044 
0045     auto value = new QLineEdit;
0046     KLineEditEventHandler::catchReturnKey(value);
0047     connect(value, &QLineEdit::textChanged, this, &SieveConditionMetaDataExists::valueChanged);
0048     value->setObjectName(QLatin1StringView("value"));
0049     grid->addWidget(value, 1, 1);
0050 
0051     return w;
0052 }
0053 
0054 QString SieveConditionMetaDataExists::code(QWidget *w) const
0055 {
0056     const KSieveUi::AbstractMoveImapFolderWidget *mailbox = w->findChild<KSieveUi::AbstractMoveImapFolderWidget *>(QStringLiteral("mailbox"));
0057     const QString mailboxStr = mailbox->text();
0058 
0059     const QLineEdit *value = w->findChild<QLineEdit *>(QStringLiteral("value"));
0060     const QString valueStr = value->text();
0061     return QStringLiteral("metadataexists \"%1\" \"%2\"").arg(mailboxStr, valueStr) + AutoCreateScriptUtil::generateConditionComment(comment());
0062 }
0063 
0064 QStringList SieveConditionMetaDataExists::needRequires(QWidget *) const
0065 {
0066     return QStringList() << QStringLiteral("mboxmetadata");
0067 }
0068 
0069 bool SieveConditionMetaDataExists::needCheckIfServerHasCapability() const
0070 {
0071     return true;
0072 }
0073 
0074 QString SieveConditionMetaDataExists::serverNeedsCapability() const
0075 {
0076     return QStringLiteral("mboxmetadata");
0077 }
0078 
0079 QString SieveConditionMetaDataExists::help() const
0080 {
0081     return i18n("The \"metadataexists\" test is true if all of the annotations listed in the \"annotation-names\" argument exist for the specified mailbox.");
0082 }
0083 
0084 void SieveConditionMetaDataExists::setParamWidgetValue(QXmlStreamReader &element, QWidget *w, bool /*notCondition*/, QString &error)
0085 {
0086     int index = 0;
0087     QString commentStr;
0088     while (element.readNextStartElement()) {
0089         const QStringView tagName = element.name();
0090         if (tagName == QLatin1StringView("str")) {
0091             const QString tagValue = element.readElementText();
0092             if (index == 0) {
0093                 auto mailbox = w->findChild<KSieveUi::AbstractMoveImapFolderWidget *>(QStringLiteral("mailbox"));
0094                 mailbox->setText(tagValue);
0095             } else if (index == 1) {
0096                 auto value = w->findChild<QLineEdit *>(QStringLiteral("value"));
0097                 value->setText(AutoCreateScriptUtil::quoteStr(tagValue));
0098             } else {
0099                 tooManyArguments(tagName, index, 2, error);
0100                 qCDebug(LIBKSIEVEUI_LOG) << " SieveConditionServerMetaDataExists::setParamWidgetValue to many attribute " << index;
0101             }
0102             ++index;
0103         } else if (tagName == QLatin1StringView("crlf")) {
0104             element.skipCurrentElement();
0105             // nothing
0106         } else if (tagName == QLatin1StringView("comment")) {
0107             commentStr = AutoCreateScriptUtil::loadConditionComment(commentStr, element.readElementText());
0108         } else {
0109             unknownTag(tagName, error);
0110             qCDebug(LIBKSIEVEUI_LOG) << " SieveConditionServerMetaDataExists::setParamWidgetValue unknown tagName " << tagName;
0111         }
0112     }
0113     if (!commentStr.isEmpty()) {
0114         setComment(commentStr);
0115     }
0116 }
0117 
0118 QUrl SieveConditionMetaDataExists::href() const
0119 {
0120     return SieveEditorUtil::helpUrl(SieveEditorUtil::strToVariableName(name()));
0121 }
0122 
0123 #include "moc_sieveconditionmetadataexists.cpp"