Libksieve

sieveconditionmetadataexists.cpp
1/*
2 SPDX-FileCopyrightText: 2013-2024 Laurent Montel <montel@kde.org>
3
4 SPDX-License-Identifier: LGPL-2.0-or-later
5*/
6#include "sieveconditionmetadataexists.h"
7#include "autocreatescripts/autocreatescriptutil_p.h"
8#include "editor/sieveeditorutil.h"
9#include "util/sieveimapaccountsettings.h"
10#include <KLocalizedString>
11#include <QLineEdit>
12
13#include "libksieveui_debug.h"
14#include <KLineEditEventHandler>
15#include <KSieveUi/AbstractMoveImapFolderWidget>
16#include <QGridLayout>
17#include <QLabel>
18#include <QXmlStreamReader>
19
20using namespace KSieveUi;
21SieveConditionMetaDataExists::SieveConditionMetaDataExists(SieveEditorGraphicalModeWidget *sieveGraphicalModeWidget, QObject *parent)
22 : SieveCondition(sieveGraphicalModeWidget, QStringLiteral("metadataexists"), i18n("Metadata exists"), parent)
23{
24}
25
26QWidget *SieveConditionMetaDataExists::createParamWidget(QWidget *parent) const
27{
28 auto w = new QWidget(parent);
29 auto grid = new QGridLayout;
30 grid->setContentsMargins({});
31 w->setLayout(grid);
32
33 auto lab = new QLabel(i18nc("@label:textbox", "Mailbox:"));
34 grid->addWidget(lab, 0, 0);
35
36 KSieveUi::AbstractMoveImapFolderWidget *mailbox = AutoCreateScriptUtil::createImapFolderWidget();
37 mailbox->setSieveImapAccountSettings(sieveImapAccountSettings());
38 connect(mailbox, &KSieveUi::AbstractMoveImapFolderWidget::textChanged, this, &SieveConditionMetaDataExists::valueChanged);
39 mailbox->setObjectName(QLatin1StringView("mailbox"));
40 grid->addWidget(mailbox, 0, 1);
41
42 lab = new QLabel(i18nc("@label:textbox", "Annotation:"));
43 grid->addWidget(lab, 1, 0);
44
45 auto value = new QLineEdit;
47 connect(value, &QLineEdit::textChanged, this, &SieveConditionMetaDataExists::valueChanged);
48 value->setObjectName(QLatin1StringView("value"));
49 grid->addWidget(value, 1, 1);
50
51 return w;
52}
53
54QString SieveConditionMetaDataExists::code(QWidget *w) const
55{
56 const KSieveUi::AbstractMoveImapFolderWidget *mailbox = w->findChild<KSieveUi::AbstractMoveImapFolderWidget *>(QStringLiteral("mailbox"));
57 const QString mailboxStr = mailbox->text();
58
59 const QLineEdit *value = w->findChild<QLineEdit *>(QStringLiteral("value"));
60 const QString valueStr = value->text();
61 return QStringLiteral("metadataexists \"%1\" \"%2\"").arg(mailboxStr, valueStr) + AutoCreateScriptUtil::generateConditionComment(comment());
62}
63
64QStringList SieveConditionMetaDataExists::needRequires(QWidget *) const
65{
66 return QStringList() << QStringLiteral("mboxmetadata");
67}
68
69bool SieveConditionMetaDataExists::needCheckIfServerHasCapability() const
70{
71 return true;
72}
73
74QString SieveConditionMetaDataExists::serverNeedsCapability() const
75{
76 return QStringLiteral("mboxmetadata");
77}
78
79QString SieveConditionMetaDataExists::help() const
80{
81 return i18n("The \"metadataexists\" test is true if all of the annotations listed in the \"annotation-names\" argument exist for the specified mailbox.");
82}
83
84void SieveConditionMetaDataExists::setParamWidgetValue(QXmlStreamReader &element, QWidget *w, bool /*notCondition*/, QString &error)
85{
86 int index = 0;
87 QString commentStr;
88 while (element.readNextStartElement()) {
89 const QStringView tagName = element.name();
90 if (tagName == QLatin1StringView("str")) {
91 const QString tagValue = element.readElementText();
92 if (index == 0) {
93 auto mailbox = w->findChild<KSieveUi::AbstractMoveImapFolderWidget *>(QStringLiteral("mailbox"));
94 mailbox->setText(tagValue);
95 } else if (index == 1) {
96 auto value = w->findChild<QLineEdit *>(QStringLiteral("value"));
97 value->setText(AutoCreateScriptUtil::quoteStr(tagValue));
98 } else {
99 tooManyArguments(tagName, index, 2, error);
100 qCDebug(LIBKSIEVEUI_LOG) << " SieveConditionServerMetaDataExists::setParamWidgetValue to many attribute " << index;
101 }
102 ++index;
103 } else if (tagName == QLatin1StringView("crlf")) {
104 element.skipCurrentElement();
105 // nothing
106 } else if (tagName == QLatin1StringView("comment")) {
107 commentStr = AutoCreateScriptUtil::loadConditionComment(commentStr, element.readElementText());
108 } else {
109 unknownTag(tagName, error);
110 qCDebug(LIBKSIEVEUI_LOG) << " SieveConditionServerMetaDataExists::setParamWidgetValue unknown tagName " << tagName;
111 }
112 }
113 if (!commentStr.isEmpty()) {
114 setComment(commentStr);
115 }
116}
117
118QUrl SieveConditionMetaDataExists::href() const
119{
120 return SieveEditorUtil::helpUrl(SieveEditorUtil::strToVariableName(name()));
121}
122
123#include "moc_sieveconditionmetadataexists.cpp"
The AbstractMoveImapFolderWidget class.
QString i18nc(const char *context, const char *text, const TYPE &arg...)
QString i18n(const char *text, const TYPE &arg...)
void catchReturnKey(QObject *lineEdit)
QByteArray tagValue(const Elem &elem, const char *keyName)
void setContentsMargins(const QMargins &margins)
void textChanged(const QString &text)
QMetaObject::Connection connect(const QObject *sender, PointerToMemberFunction signal, Functor functor)
T findChild(const QString &name, Qt::FindChildOptions options) const const
QObject * parent() const const
void setObjectName(QAnyStringView name)
bool isEmpty() const const
QStringView name() const const
QString readElementText(ReadElementTextBehaviour behaviour)
bool readNextStartElement()
void skipCurrentElement()
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Sat Dec 21 2024 16:57:10 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.