Libksieve

sieveconditionheader.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 "sieveconditionheader.h"
7#include "autocreatescripts/autocreatescriptutil_p.h"
8#include "autocreatescripts/commonwidgets/selectmatchtypecombobox.h"
9#include "widgets/selectheadertypecombobox.h"
10
11#include <KLocalizedString>
12
13#include "libksieveui_debug.h"
14#include <QHBoxLayout>
15#include <QLabel>
16#include <QXmlStreamReader>
17
18using namespace KSieveUi;
19
20SieveConditionHeader::SieveConditionHeader(SieveEditorGraphicalModeWidget *sieveGraphicalModeWidget, QObject *parent)
21 : SieveCondition(sieveGraphicalModeWidget, QStringLiteral("header"), i18n("Header"), parent)
22{
23}
24
25QWidget *SieveConditionHeader::createParamWidget(QWidget *parent) const
26{
27 auto w = new QWidget(parent);
28 auto lay = new QHBoxLayout;
29 lay->setContentsMargins({});
30 w->setLayout(lay);
31
32 auto matchTypeCombo = new SelectMatchTypeComboBox(mSieveGraphicalModeWidget);
33 matchTypeCombo->setObjectName(QLatin1StringView("matchtypecombobox"));
34 connect(matchTypeCombo, &SelectMatchTypeComboBox::valueChanged, this, &SieveConditionHeader::valueChanged);
35 lay->addWidget(matchTypeCombo);
36
37 auto grid = new QGridLayout;
38 lay->addLayout(grid);
39
40 auto headerType = new SelectHeaderTypeComboBox;
41 headerType->setObjectName(QLatin1StringView("headertype"));
42 connect(headerType, &SelectHeaderTypeComboBox::valueChanged, this, &SieveConditionHeader::valueChanged);
43 grid->addWidget(headerType, 0, 0, 1, 2);
44
45 auto lab = new QLabel(i18nc("@label:textbox", "With value:"));
46 grid->addWidget(lab, 1, 0);
47
48 AbstractRegexpEditorLineEdit *value = AutoCreateScriptUtil::createRegexpEditorLineEdit();
49 connect(value, &AbstractRegexpEditorLineEdit::textChanged, this, &SieveConditionHeader::valueChanged);
50 connect(matchTypeCombo, &SelectMatchTypeComboBox::switchToRegexp, value, &AbstractRegexpEditorLineEdit::switchToRegexpEditorLineEdit);
51 value->setObjectName(QLatin1StringView("value"));
52 grid->addWidget(value, 1, 1);
53 return w;
54}
55
56QString SieveConditionHeader::code(QWidget *w) const
57{
58 const SelectMatchTypeComboBox *matchTypeCombo = w->findChild<SelectMatchTypeComboBox *>(QStringLiteral("matchtypecombobox"));
59 bool isNegative = false;
60 const QString matchString = matchTypeCombo->code(isNegative);
61
62 const SelectHeaderTypeComboBox *headerType = w->findChild<SelectHeaderTypeComboBox *>(QStringLiteral("headertype"));
63 const QString headerStr = headerType->code();
64
65 const AbstractRegexpEditorLineEdit *edit = w->findChild<AbstractRegexpEditorLineEdit *>(QStringLiteral("value"));
66 QString valueStr = edit->code();
67
68 valueStr = AutoCreateScriptUtil::fixListValue(valueStr);
69 return AutoCreateScriptUtil::negativeString(isNegative) + QStringLiteral("header %1 %2 %3").arg(matchString, headerStr, valueStr)
70 + AutoCreateScriptUtil::generateConditionComment(comment());
71}
72
73QString SieveConditionHeader::help() const
74{
75 return i18n("The \"header\" test evaluates to true if the value of any of the named headers, ignoring leading and trailing whitespace, matches any key.");
76}
77
78void SieveConditionHeader::setParamWidgetValue(QXmlStreamReader &element, QWidget *w, bool notCondition, QString &error)
79{
80 int index = 0;
81 QString commentStr;
82 while (element.readNextStartElement()) {
83 const QStringView tagName = element.name();
84
85 if (tagName == QLatin1StringView("tag")) {
86 const QString tagValue = element.readElementText();
87 if (tagValue == QLatin1StringView("comparator")) {
88 qCWarning(LIBKSIEVEUI_LOG) << " comparator support not implemented yet!";
89 } else {
90 auto selectMatchCombobox = w->findChild<SelectMatchTypeComboBox *>(QStringLiteral("matchtypecombobox"));
91 selectMatchCombobox->setCode(AutoCreateScriptUtil::tagValueWithCondition(tagValue, notCondition), name(), error);
92 }
93 } else if (tagName == QLatin1StringView("str")) {
94 if (index == 0) {
95 auto headerType = w->findChild<SelectHeaderTypeComboBox *>(QStringLiteral("headertype"));
96 headerType->setCode(element.readElementText());
97 } else if (index == 1) {
98 auto value = w->findChild<AbstractRegexpEditorLineEdit *>(QStringLiteral("value"));
99 QString st = AutoCreateScriptUtil::quoteStr(element.readElementText(), true);
100 value->setCode(st);
101 } else {
102 tooManyArguments(tagName, index, 2, error);
103 qCDebug(LIBKSIEVEUI_LOG) << " SieveConditionHeader::setParamWidgetValue too many argument " << index;
104 }
105 ++index;
106 } else if (tagName == QLatin1StringView("list")) {
107 // Header list
108 if (index == 0) {
109 auto headerType = w->findChild<SelectHeaderTypeComboBox *>(QStringLiteral("headertype"));
110 headerType->setCode(AutoCreateScriptUtil::listValueToStr(element));
111 } else if (index == 1) {
112 auto value = w->findChild<AbstractRegexpEditorLineEdit *>(QStringLiteral("value"));
113 value->setCode(AutoCreateScriptUtil::listValueToStr(element));
114 } else {
115 tooManyArguments(tagName, index, 2, error);
116 qCDebug(LIBKSIEVEUI_LOG) << " SieveConditionHeader::setParamWidgetValue too many argument " << index;
117 }
118 ++index;
119 } else if (tagName == QLatin1StringView("crlf")) {
120 element.skipCurrentElement();
121 // nothing
122 } else if (tagName == QLatin1StringView("comment")) {
123 commentStr = AutoCreateScriptUtil::loadConditionComment(commentStr, element.readElementText());
124 } else {
125 unknownTag(tagName, error);
126 qCDebug(LIBKSIEVEUI_LOG) << " SieveConditionHeader::setParamWidgetValue unknown tagName " << tagName;
127 }
128 }
129 if (!commentStr.isEmpty()) {
130 setComment(commentStr);
131 }
132}
133
134QStringList KSieveUi::SieveConditionHeader::needRequires(QWidget *w) const
135{
136 const SelectMatchTypeComboBox *matchTypeCombo = w->findChild<SelectMatchTypeComboBox *>(QStringLiteral("matchtypecombobox"));
137 return matchTypeCombo->needRequires();
138}
139
140#include "moc_sieveconditionheader.cpp"
The AbstractRegexpEditorLineEdit class.
QString i18nc(const char *context, const char *text, const TYPE &arg...)
QString i18n(const char *text, const TYPE &arg...)
QByteArray tagValue(const Elem &elem, const char *keyName)
void addLayout(QLayout *layout, int row, int column, Qt::Alignment alignment)
void setContentsMargins(const QMargins &margins)
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.