Libksieve

sieveconditionmetadata.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 "sieveconditionmetadata.h"
7#include "autocreatescripts/autocreatescriptutil_p.h"
8#include "autocreatescripts/commonwidgets/selectmatchtypecombobox.h"
9#include "editor/sieveeditorutil.h"
10#include "util/sieveimapaccountsettings.h"
11#include <KLocalizedString>
12#include <QLineEdit>
13
14#include "libksieveui_debug.h"
15#include <KLineEditEventHandler>
16#include <KSieveUi/AbstractMoveImapFolderWidget>
17#include <QHBoxLayout>
18#include <QLabel>
19#include <QWidget>
20#include <QXmlStreamReader>
21
22using namespace KSieveUi;
23SieveConditionMetaData::SieveConditionMetaData(SieveEditorGraphicalModeWidget *sieveGraphicalModeWidget, QObject *parent)
24 : SieveCondition(sieveGraphicalModeWidget, QStringLiteral("metadata"), i18n("Meta Data"), parent)
25{
26}
27
28QWidget *SieveConditionMetaData::createParamWidget(QWidget *parent) const
29{
30 auto w = new QWidget(parent);
31 auto lay = new QHBoxLayout;
32 lay->setContentsMargins({});
33 w->setLayout(lay);
34
35 auto selectType = new SelectMatchTypeComboBox(mSieveGraphicalModeWidget);
36 selectType->setObjectName(QLatin1StringView("selecttype"));
37 connect(selectType, &SelectMatchTypeComboBox::valueChanged, this, &SieveConditionMetaData::valueChanged);
38 lay->addWidget(selectType);
39
40 auto grid = new QGridLayout;
41 grid->setContentsMargins({});
42 lay->addLayout(grid);
43
44 auto lab = new QLabel(i18nc("@label:textbox", "Mailbox:"));
45 grid->addWidget(lab, 0, 0);
46
47 KSieveUi::AbstractMoveImapFolderWidget *mailbox = AutoCreateScriptUtil::createImapFolderWidget();
48 mailbox->setSieveImapAccountSettings(sieveImapAccountSettings());
49
50 connect(mailbox, &KSieveUi::AbstractMoveImapFolderWidget::textChanged, this, &SieveConditionMetaData::valueChanged);
51 mailbox->setObjectName(QLatin1StringView("mailbox"));
52 grid->addWidget(mailbox, 0, 1);
53
54 lab = new QLabel(i18nc("@label:textbox", "Annotations:"));
55 grid->addWidget(lab, 1, 0);
56
57 auto annotation = new QLineEdit;
59 connect(annotation, &QLineEdit::textChanged, this, &SieveConditionMetaData::valueChanged);
60 annotation->setObjectName(QLatin1StringView("annotation"));
61 grid->addWidget(annotation, 1, 1);
62
63 lab = new QLabel(i18nc("@label:textbox", "Value:"));
64 grid->addWidget(lab, 2, 0);
65
66 AbstractRegexpEditorLineEdit *value = AutoCreateScriptUtil::createRegexpEditorLineEdit();
67 connect(value, &AbstractRegexpEditorLineEdit::textChanged, this, &SieveConditionMetaData::valueChanged);
68 connect(selectType, &SelectMatchTypeComboBox::switchToRegexp, value, &AbstractRegexpEditorLineEdit::switchToRegexpEditorLineEdit);
69 value->setObjectName(QLatin1StringView("value"));
70 grid->addWidget(value, 2, 1);
71
72 return w;
73}
74
75QString SieveConditionMetaData::code(QWidget *w) const
76{
77 const SelectMatchTypeComboBox *selectType = w->findChild<SelectMatchTypeComboBox *>(QStringLiteral("selecttype"));
78 bool isNegative = false;
79 const QString matchString = selectType->code(isNegative);
80
81 QString result = AutoCreateScriptUtil::negativeString(isNegative) + QStringLiteral("metadata %1 ").arg(matchString);
82
83 const KSieveUi::AbstractMoveImapFolderWidget *mailbox = w->findChild<KSieveUi::AbstractMoveImapFolderWidget *>(QStringLiteral("mailbox"));
84 const QString mailboxStr = mailbox->text();
85
86 result += QStringLiteral("\"%1\" ").arg(mailboxStr);
87
88 const QLineEdit *annotation = w->findChild<QLineEdit *>(QStringLiteral("annotation"));
89 const QString annotationStr = annotation->text();
90
91 result += QStringLiteral("\"%1\" ").arg(annotationStr);
92
93 const AbstractRegexpEditorLineEdit *value = w->findChild<AbstractRegexpEditorLineEdit *>(QStringLiteral("value"));
94 const QString valueStr = value->code();
95
96 result += QStringLiteral("\"%1\"").arg(valueStr);
97 return result + AutoCreateScriptUtil::generateConditionComment(comment());
98}
99
100QStringList SieveConditionMetaData::needRequires(QWidget *w) const
101{
102 const SelectMatchTypeComboBox *selectType = w->findChild<SelectMatchTypeComboBox *>(QStringLiteral("selecttype"));
103 return QStringList() << QStringLiteral("mboxmetadata") << selectType->needRequires();
104}
105
106bool SieveConditionMetaData::needCheckIfServerHasCapability() const
107{
108 return true;
109}
110
111QString SieveConditionMetaData::serverNeedsCapability() const
112{
113 return QStringLiteral("mboxmetadata");
114}
115
116QString SieveConditionMetaData::help() const
117{
118 return i18n(
119 "This test retrieves the value of the mailbox annotation \"annotation-name\" for the mailbox \"mailbox\". The retrieved value is compared to the "
120 "\"key-list\". The test returns true if the annotation exists and its value matches any of the keys.");
121}
122
123void SieveConditionMetaData::setParamWidgetValue(QXmlStreamReader &element, QWidget *w, bool notCondition, QString &error)
124{
125 int index = 0;
126 QString commentStr;
127 while (element.readNextStartElement()) {
128 const QStringView tagName = element.name();
129 if (tagName == QLatin1StringView("str")) {
130 const QString tagValue = element.readElementText();
131 switch (index) {
132 case 0: {
133 auto mailbox = w->findChild<KSieveUi::AbstractMoveImapFolderWidget *>(QStringLiteral("mailbox"));
134 mailbox->setText(tagValue);
135 break;
136 }
137 case 1: {
138 auto annotation = w->findChild<QLineEdit *>(QStringLiteral("annotation"));
139 annotation->setText(AutoCreateScriptUtil::quoteStr(tagValue));
140 break;
141 }
142 case 2: {
143 auto value = w->findChild<AbstractRegexpEditorLineEdit *>(QStringLiteral("value"));
144 value->setCode(AutoCreateScriptUtil::quoteStr(tagValue));
145 break;
146 }
147 default:
148 tooManyArguments(tagName, index, 3, error);
149 qCDebug(LIBKSIEVEUI_LOG) << " SieveConditionMetaData::setParamWidgetValue too many argument " << index;
150 break;
151 }
152 ++index;
153 } else if (tagName == QLatin1StringView("tag")) {
154 auto selectType = w->findChild<SelectMatchTypeComboBox *>(QStringLiteral("selecttype"));
155 selectType->setCode(AutoCreateScriptUtil::tagValueWithCondition(element.readElementText(), notCondition), name(), error);
156 } else if (tagName == QLatin1StringView("crlf")) {
157 element.skipCurrentElement();
158 // nothing
159 } else if (tagName == QLatin1StringView("comment")) {
160 commentStr = AutoCreateScriptUtil::loadConditionComment(commentStr, element.readElementText());
161 } else {
162 unknownTag(tagName, error);
163 qCDebug(LIBKSIEVEUI_LOG) << " SieveConditionMetaData::setParamWidgetValue unknown tagName " << tagName;
164 }
165 }
166 if (!commentStr.isEmpty()) {
167 setComment(commentStr);
168 }
169}
170
171QUrl SieveConditionMetaData::href() const
172{
173 return SieveEditorUtil::helpUrl(SieveEditorUtil::strToVariableName(name()));
174}
175
176#include "moc_sieveconditionmetadata.cpp"
The AbstractMoveImapFolderWidget class.
The AbstractRegexpEditorLineEdit 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)
QString arg(Args &&... args) const const
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 Fri Nov 22 2024 12:09:40 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.