Libksieve

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