Libksieve

sieveconditionbody.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 "sieveconditionbody.h"
7#include "autocreatescripts/autocreatescriptutil_p.h"
8#include "autocreatescripts/commonwidgets/selectmatchtypecombobox.h"
9#include "editor/sieveeditorutil.h"
10#include "widgets/selectbodytypewidget.h"
11
12#include <KLocalizedString>
13
14#include "libksieveui_debug.h"
15#include <QHBoxLayout>
16#include <QWidget>
17
18using namespace KSieveUi;
19SieveConditionBody::SieveConditionBody(SieveEditorGraphicalModeWidget *sieveGraphicalModeWidget, QObject *parent)
20 : SieveCondition(sieveGraphicalModeWidget, QStringLiteral("body"), i18n("Body"), parent)
21{
22}
23
24QWidget *SieveConditionBody::createParamWidget(QWidget *parent) const
25{
26 auto w = new QWidget(parent);
27 auto lay = new QHBoxLayout;
28 lay->setContentsMargins({});
29 w->setLayout(lay);
30
31 auto bodyType = new SelectBodyTypeWidget;
32 bodyType->setObjectName(QLatin1StringView("bodytype"));
33 connect(bodyType, &SelectBodyTypeWidget::valueChanged, this, &SieveConditionBody::valueChanged);
34 lay->addWidget(bodyType);
35
36 auto matchType = new SelectMatchTypeComboBox(mSieveGraphicalModeWidget);
37 lay->addWidget(matchType);
38 matchType->setObjectName(QLatin1StringView("matchtype"));
39 connect(matchType, &SelectMatchTypeComboBox::valueChanged, this, &SieveConditionBody::valueChanged);
40
41 AbstractRegexpEditorLineEdit *edit = AutoCreateScriptUtil::createRegexpEditorLineEdit();
42 connect(edit, &AbstractRegexpEditorLineEdit::textChanged, this, &SieveConditionBody::valueChanged);
43 connect(matchType, &SelectMatchTypeComboBox::switchToRegexp, edit, &AbstractRegexpEditorLineEdit::switchToRegexpEditorLineEdit);
44 edit->setClearButtonEnabled(true);
45 lay->addWidget(edit);
46 edit->setObjectName(QLatin1StringView("edit"));
47
48 return w;
49}
50
51QString SieveConditionBody::code(QWidget *w) const
52{
53 const SelectBodyTypeWidget *bodyType = w->findChild<SelectBodyTypeWidget *>(QStringLiteral("bodytype"));
54 const QString bodyValue = bodyType->code();
55 const SelectMatchTypeComboBox *matchType = w->findChild<SelectMatchTypeComboBox *>(QStringLiteral("matchtype"));
56 bool isNegative = false;
57 const QString matchValue = matchType->code(isNegative);
58
59 const AbstractRegexpEditorLineEdit *edit = w->findChild<AbstractRegexpEditorLineEdit *>(QStringLiteral("edit"));
60 const QString editValue = AutoCreateScriptUtil::createAddressList(edit->code().trimmed(), false);
61 return AutoCreateScriptUtil::negativeString(isNegative) + QStringLiteral("body %1 %2 %3").arg(bodyValue, matchValue, editValue)
62 + AutoCreateScriptUtil::generateConditionComment(comment());
63}
64
65QStringList SieveConditionBody::needRequires(QWidget *w) const
66{
67 const SelectMatchTypeComboBox *matchType = w->findChild<SelectMatchTypeComboBox *>(QStringLiteral("matchtype"));
68
69 return QStringList() << QStringLiteral("body") << matchType->needRequires();
70}
71
72bool SieveConditionBody::needCheckIfServerHasCapability() const
73{
74 return true;
75}
76
77QString SieveConditionBody::serverNeedsCapability() const
78{
79 return QStringLiteral("body");
80}
81
82QString SieveConditionBody::help() const
83{
84 return i18n(
85 "The body test matches content in the body of an email message, that is, anything following the first empty line after the header. (The empty line "
86 "itself, if present, is not considered to be part of the body.)");
87}
88
89void SieveConditionBody::setParamWidgetValue(QXmlStreamReader &element, QWidget *w, bool notCondition, QString &error)
90{
91 int index = 0;
92 int indexStr = 0;
93 QStringList tagValueList;
94 QStringList strValue;
95
96 bool wasListElement = false;
97 QString commentStr;
98 while (element.readNextStartElement()) {
99 const QStringView tagName = element.name();
100 if (tagName == QLatin1StringView("tag")) {
101 const QString tagValue = element.readElementText();
102 if (index == 0) {
103 tagValueList << AutoCreateScriptUtil::tagValue(tagValue);
104 } else if (index == 1) {
105 tagValueList << AutoCreateScriptUtil::tagValueWithCondition(tagValue, notCondition);
106 } else {
107 tooManyArguments(tagName, index, 2, error);
108 qCDebug(LIBKSIEVEUI_LOG) << " SieveConditionBody::setParamWidgetValue too many argument " << index;
109 }
110 ++index;
111 } else if (tagName == QLatin1StringView("str")) {
112 strValue << element.readElementText();
113 ++indexStr;
114 } else if (tagName == QLatin1StringView("crlf")) {
115 element.skipCurrentElement();
116 // nothing
117 } else if (tagName == QLatin1StringView("comment")) {
118 commentStr = AutoCreateScriptUtil::loadConditionComment(commentStr, element.readElementText());
119 } else if (tagName == QLatin1StringView("list")) {
120 strValue << AutoCreateScriptUtil::listValueToStr(element);
121 wasListElement = true;
122 ++indexStr;
123 } else {
124 unknownTag(tagName, error);
125 qCDebug(LIBKSIEVEUI_LOG) << " SieveConditionBody::setParamWidgetValue unknown tagName " << tagName;
126 }
127 }
128 if (!commentStr.isEmpty()) {
129 setComment(commentStr);
130 }
131 QString errorStr;
132 if (strValue.count() == 1) {
133 auto bodyType = w->findChild<SelectBodyTypeWidget *>(QStringLiteral("bodytype"));
134 bodyType->setCode(tagValueList.at(0), QString(), name(), errorStr);
135 if (errorStr.isEmpty()) {
136 auto matchType = w->findChild<SelectMatchTypeComboBox *>(QStringLiteral("matchtype"));
137 matchType->setCode(tagValueList.at(1), name(), error);
138 } else {
139 auto matchType = w->findChild<SelectMatchTypeComboBox *>(QStringLiteral("matchtype"));
140 if (tagValueList.count() == 1) {
141 matchType->setCode(tagValueList.at(0), name(), error);
142 } else if (tagValueList.count() == 2) {
143 matchType->setCode(tagValueList.at(0), name(), error);
144 bodyType->setCode(tagValueList.at(1), QString(), name(), errorStr);
145 }
146 }
147 auto edit = w->findChild<AbstractRegexpEditorLineEdit *>(QStringLiteral("edit"));
148 edit->setCode(wasListElement ? strValue.at(0) : AutoCreateScriptUtil::quoteStr(strValue.at(0)));
149 } else if (strValue.count() == 2) {
150 auto bodyType = w->findChild<SelectBodyTypeWidget *>(QStringLiteral("bodytype"));
151 bodyType->setCode(tagValueList.at(0), indexStr == 2 ? strValue.at(0) : QString(), name(), errorStr);
152 auto matchType = w->findChild<SelectMatchTypeComboBox *>(QStringLiteral("matchtype"));
153 if (!errorStr.isEmpty()) {
154 matchType->setCode(tagValueList.at(0), name(), error);
155 bodyType->setCode(tagValueList.at(1), indexStr == 2 ? strValue.at(0) : QString(), name(), error);
156 } else {
157 matchType->setCode(tagValueList.at(1), name(), error);
158 }
159 auto edit = w->findChild<AbstractRegexpEditorLineEdit *>(QStringLiteral("edit"));
160 edit->setCode(indexStr == 1 ? AutoCreateScriptUtil::quoteStr(strValue.at(0)) : AutoCreateScriptUtil::quoteStr(strValue.at(1)));
161 }
162}
163
164QUrl SieveConditionBody::href() const
165{
166 return SieveEditorUtil::helpUrl(SieveEditorUtil::strToVariableName(name()));
167}
168
169#include "moc_sieveconditionbody.cpp"
The AbstractRegexpEditorLineEdit class.
QString i18n(const char *text, const TYPE &arg...)
QByteArray tagValue(const Elem &elem, const char *keyName)
void setContentsMargins(const QMargins &margins)
const_reference at(qsizetype i) const const
qsizetype count() const const
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
QString trimmed() 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.