Libksieve

sieveconditionaddress.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 "sieveconditionaddress.h"
7#include "autocreatescripts/autocreatescriptutil_p.h"
8#include "autocreatescripts/commonwidgets/selectmatchtypecombobox.h"
9#include "editor/sieveeditorutil.h"
10#include "widgets/selectaddresspartcombobox.h"
11#include "widgets/selectheadertypecombobox.h"
12
13#include <KLocalizedString>
14
15#include "libksieveui_debug.h"
16#include <QHBoxLayout>
17#include <QLabel>
18#include <QXmlStreamReader>
19
20using namespace KSieveUi;
21
22SieveConditionAddress::SieveConditionAddress(SieveEditorGraphicalModeWidget *sieveGraphicalModeWidget, QObject *parent)
23 : SieveCondition(sieveGraphicalModeWidget, QStringLiteral("address"), i18n("Address"), parent)
24{
25}
26
27QWidget *SieveConditionAddress::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 selectAddressPart = new SelectAddressPartComboBox(mSieveGraphicalModeWidget);
35 connect(selectAddressPart, &SelectAddressPartComboBox::valueChanged, this, &SieveConditionAddress::valueChanged);
36 selectAddressPart->setObjectName(QLatin1StringView("addresspartcombobox"));
37 lay->addWidget(selectAddressPart);
38
39 auto grid = new QGridLayout;
40 grid->setContentsMargins({});
41 lay->addLayout(grid);
42 auto selectMatchCombobox = new SelectMatchTypeComboBox(mSieveGraphicalModeWidget);
43 connect(selectMatchCombobox, &SelectMatchTypeComboBox::valueChanged, this, &SieveConditionAddress::valueChanged);
44 selectMatchCombobox->setObjectName(QLatin1StringView("matchtypecombobox"));
45 grid->addWidget(selectMatchCombobox, 0, 0);
46
47 auto selectHeaderType = new SelectHeaderTypeComboBox(true);
48 connect(selectHeaderType, &SelectHeaderTypeComboBox::valueChanged, this, &SieveConditionAddress::valueChanged);
49 selectHeaderType->setObjectName(QLatin1StringView("headertypecombobox"));
50 grid->addWidget(selectHeaderType, 0, 1);
51
52 auto lab = new QLabel(i18nc("@label:textbox", "address:"));
53 grid->addWidget(lab, 1, 0);
54
55 AbstractRegexpEditorLineEdit *edit = AutoCreateScriptUtil::createRegexpEditorLineEdit();
56 connect(edit, &AbstractRegexpEditorLineEdit::textChanged, this, &SieveConditionAddress::valueChanged);
57 connect(selectMatchCombobox, &SelectMatchTypeComboBox::switchToRegexp, edit, &AbstractRegexpEditorLineEdit::switchToRegexpEditorLineEdit);
58 edit->setClearButtonEnabled(true);
59 edit->setPlaceholderText(i18nc("@info:placeholder", "Use ; to separate emails"));
60 grid->addWidget(edit, 1, 1);
61 edit->setObjectName(QLatin1StringView("editaddress"));
62 return w;
63}
64
65QString SieveConditionAddress::code(QWidget *w) const
66{
67 const SelectMatchTypeComboBox *selectMatchCombobox = w->findChild<SelectMatchTypeComboBox *>(QStringLiteral("matchtypecombobox"));
68 bool isNegative = false;
69 const QString matchTypeStr = selectMatchCombobox->code(isNegative);
70
71 const SelectAddressPartComboBox *selectAddressPart = w->findChild<SelectAddressPartComboBox *>(QStringLiteral("addresspartcombobox"));
72 const QString selectAddressPartStr = selectAddressPart->code();
73
74 const SelectHeaderTypeComboBox *selectHeaderType = w->findChild<SelectHeaderTypeComboBox *>(QStringLiteral("headertypecombobox"));
75 const QString selectHeaderTypeStr = selectHeaderType->code();
76
77 const AbstractRegexpEditorLineEdit *edit = w->findChild<AbstractRegexpEditorLineEdit *>(QStringLiteral("editaddress"));
78 const QString addressStr = AutoCreateScriptUtil::createAddressList(edit->code().trimmed(), false);
79 return AutoCreateScriptUtil::negativeString(isNegative)
80 + QStringLiteral("address %1 %2 %3 %4").arg(selectAddressPartStr, matchTypeStr, selectHeaderTypeStr, addressStr)
81 + AutoCreateScriptUtil::generateConditionComment(comment());
82}
83
84QStringList SieveConditionAddress::needRequires(QWidget *w) const
85{
86 const SelectAddressPartComboBox *selectAddressPart = w->findChild<SelectAddressPartComboBox *>(QStringLiteral("addresspartcombobox"));
87 const SelectMatchTypeComboBox *selectMatchCombobox = w->findChild<SelectMatchTypeComboBox *>(QStringLiteral("matchtypecombobox"));
88
89 return selectAddressPart->extraRequire() + selectMatchCombobox->needRequires();
90}
91
92QString SieveConditionAddress::help() const
93{
94 return i18n(
95 "The \"address\" test matches Internet addresses in structured headers that contain addresses. It returns true if any header contains any key in the "
96 "specified part of the address, as modified by the comparator and the match keyword.");
97}
98
99void SieveConditionAddress::setParamWidgetValue(QXmlStreamReader &element, QWidget *w, bool notCondition, QString &error)
100{
101 int index = 0;
102 int indexStr = 0;
103 QStringList lstTagValue;
104 QString commentStr;
105 while (element.readNextStartElement()) {
106 const QStringView tagName = element.name();
107 if (tagName == QLatin1StringView("tag")) {
108 lstTagValue << element.readElementText();
109 ++index;
110 } else if (tagName == QLatin1StringView("str")) {
111 if (indexStr == 0) {
112 auto selectHeaderType = w->findChild<SelectHeaderTypeComboBox *>(QStringLiteral("headertypecombobox"));
113 selectHeaderType->setCode(element.readElementText());
114 } else if (indexStr == 1) {
115 auto edit = w->findChild<AbstractRegexpEditorLineEdit *>(QStringLiteral("editaddress"));
116 edit->setCode(AutoCreateScriptUtil::quoteStr(element.readElementText()));
117 } else {
118 tooManyArguments(tagName, indexStr, 2, error);
119 qCDebug(LIBKSIEVEUI_LOG) << " SieveConditionAddress::setParamWidgetValue too many argument :" << index;
120 }
121 ++indexStr;
122 } else if (tagName == QLatin1StringView("list")) {
123 if (indexStr == 0) {
124 auto selectHeaderType = w->findChild<SelectHeaderTypeComboBox *>(QStringLiteral("headertypecombobox"));
125 selectHeaderType->setCode(AutoCreateScriptUtil::listValueToStr(element));
126 } else if (indexStr == 1) {
127 auto edit = w->findChild<AbstractRegexpEditorLineEdit *>(QStringLiteral("editaddress"));
128 edit->setCode(AutoCreateScriptUtil::listValueToStr(element));
129 } else {
130 tooManyArguments(tagName, indexStr, 2, error);
131 qCDebug(LIBKSIEVEUI_LOG) << " SieveConditionAddress::setParamWidgetValue too many argument :" << index;
132 }
133 ++indexStr;
134 } else if (tagName == QLatin1StringView("crlf")) {
135 element.skipCurrentElement();
136 // nothing
137 } else if (tagName == QLatin1StringView("comment")) {
138 commentStr = AutoCreateScriptUtil::loadConditionComment(commentStr, element.readElementText());
139 } else {
140 unknownTag(tagName, error);
141 qCDebug(LIBKSIEVEUI_LOG) << " SieveConditionAddress::setParamWidgetValue unknown tagName " << tagName;
142 }
143 }
144 if (!commentStr.isEmpty()) {
145 setComment(commentStr);
146 }
147 if (lstTagValue.count() == 1) {
148 QString specificError;
149 auto selectMatchCombobox = w->findChild<SelectMatchTypeComboBox *>(QStringLiteral("matchtypecombobox"));
150 selectMatchCombobox->setCode(AutoCreateScriptUtil::tagValueWithCondition(lstTagValue.at(0), notCondition), name(), specificError);
151 if (!specificError.isEmpty()) { // Test if default match type == is
152 auto selectAddressPart = w->findChild<SelectAddressPartComboBox *>(QStringLiteral("addresspartcombobox"));
153 selectAddressPart->setCode(AutoCreateScriptUtil::tagValue(lstTagValue.at(0)), name(), error);
154 }
155 } else if (lstTagValue.count() == 2) {
156 QString errorStr;
157 auto selectAddressPart = w->findChild<SelectAddressPartComboBox *>(QStringLiteral("addresspartcombobox"));
158 selectAddressPart->setCode(AutoCreateScriptUtil::tagValue(lstTagValue.at(0)), name(), errorStr);
159 if (errorStr.isEmpty()) {
160 auto selectMatchCombobox = w->findChild<SelectMatchTypeComboBox *>(QStringLiteral("matchtypecombobox"));
161 selectMatchCombobox->setCode(AutoCreateScriptUtil::tagValueWithCondition(lstTagValue.at(1), notCondition), name(), error);
162 } else { // Problem with order
163 auto selectMatchCombobox = w->findChild<SelectMatchTypeComboBox *>(QStringLiteral("matchtypecombobox"));
164 selectMatchCombobox->setCode(AutoCreateScriptUtil::tagValueWithCondition(lstTagValue.at(0), notCondition), name(), error);
165 selectAddressPart->setCode(AutoCreateScriptUtil::tagValue(lstTagValue.at(1)), name(), error);
166 }
167 } else if (lstTagValue.count() > 2) {
168 tooManyArguments(QStringLiteral("tag"), lstTagValue.count(), 2, error);
169 qCDebug(LIBKSIEVEUI_LOG) << "SieveConditionAddress::setParamWidgetValue too many argument :" << lstTagValue.count();
170 }
171}
172
173QUrl SieveConditionAddress::href() const
174{
175 return SieveEditorUtil::helpUrl(SieveEditorUtil::strToVariableName(name()));
176}
177
178#include "moc_sieveconditionaddress.cpp"
The AbstractRegexpEditorLineEdit class.
QString i18nc(const char *context, const char *text, const TYPE &arg...)
QString i18n(const char *text, const TYPE &arg...)
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.