Libksieve

sieveconditionhasflag.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 "sieveconditionhasflag.h"
7#include "autocreatescripts/autocreatescriptutil_p.h"
8#include "autocreatescripts/commonwidgets/selectmatchtypecombobox.h"
9#include "autocreatescripts/sieveeditorgraphicalmodewidget.h"
10#include "editor/sieveeditorutil.h"
11
12#include <KLineEditEventHandler>
13#include <KLocalizedString>
14#include <QLineEdit>
15
16#include "libksieveui_debug.h"
17#include <QHBoxLayout>
18#include <QLabel>
19#include <QWidget>
20#include <QXmlStreamReader>
21
22using namespace KSieveUi;
23SieveConditionHasFlag::SieveConditionHasFlag(SieveEditorGraphicalModeWidget *sieveGraphicalModeWidget, QObject *parent)
24 : SieveCondition(sieveGraphicalModeWidget, QStringLiteral("hasflag"), i18n("Has Flag"), parent)
25{
26 hasVariableSupport = sieveCapabilities().contains(QLatin1StringView("variables"));
27}
28
29QWidget *SieveConditionHasFlag::createParamWidget(QWidget *parent) const
30{
31 auto w = new QWidget(parent);
32 auto lay = new QHBoxLayout;
33 lay->setContentsMargins({});
34 w->setLayout(lay);
35 auto selecttype = new SelectMatchTypeComboBox(mSieveGraphicalModeWidget);
36 selecttype->setObjectName(QLatin1StringView("matchtype"));
37 connect(selecttype, &SelectMatchTypeComboBox::valueChanged, this, &SieveConditionHasFlag::valueChanged);
38 lay->addWidget(selecttype);
39
40 auto grid = new QGridLayout;
41 grid->setContentsMargins({});
42 lay->addLayout(grid);
43
44 int row = 0;
45 if (hasVariableSupport) {
46 auto lab = new QLabel(i18nc("@label:textbox", "Variable name\n (if empty it uses internal variable):"));
47 grid->addWidget(lab, row, 0);
48
49 auto variableName = new QLineEdit;
51 variableName->setObjectName(QLatin1StringView("variablename"));
52 connect(variableName, &QLineEdit::textChanged, this, &SieveConditionHasFlag::valueChanged);
53 grid->addWidget(variableName, row, 1);
54 ++row;
55 }
56 auto lab = new QLabel(i18nc("@label:textbox", "Value:"));
57 grid->addWidget(lab, row, 0);
58
59 AbstractRegexpEditorLineEdit *value = AutoCreateScriptUtil::createRegexpEditorLineEdit();
60 connect(value, &AbstractRegexpEditorLineEdit::textChanged, this, &SieveConditionHasFlag::valueChanged);
61 connect(selecttype, &SelectMatchTypeComboBox::switchToRegexp, value, &AbstractRegexpEditorLineEdit::switchToRegexpEditorLineEdit);
62 value->setObjectName(QLatin1StringView("value"));
63 grid->addWidget(value, row, 1);
64
65 return w;
66}
67
68QString SieveConditionHasFlag::code(QWidget *w) const
69{
70 const SelectMatchTypeComboBox *matchTypeCombo = w->findChild<SelectMatchTypeComboBox *>(QStringLiteral("matchtype"));
71 bool isNegative = false;
72 const QString matchString = matchTypeCombo->code(isNegative);
73
74 QString result = AutoCreateScriptUtil::negativeString(isNegative) + QStringLiteral("hasflag %1").arg(matchString);
75
76 if (hasVariableSupport) {
77 const QLineEdit *variableName = w->findChild<QLineEdit *>(QStringLiteral("variablename"));
78 const QString variableNameStr = variableName->text();
79 if (!variableNameStr.isEmpty()) {
80 result += QLatin1StringView(" \"") + variableNameStr + QLatin1Char('"');
81 }
82
83 const AbstractRegexpEditorLineEdit *value = w->findChild<AbstractRegexpEditorLineEdit *>(QStringLiteral("value"));
84 const QString valueStr = value->code();
85 result += QLatin1StringView(" \"") + valueStr + QLatin1Char('"');
86 }
87 return result + AutoCreateScriptUtil::generateConditionComment(comment());
88}
89
90QStringList SieveConditionHasFlag::needRequires(QWidget *w) const
91{
92 QStringList lst;
93 if (sieveCapabilities().contains(QLatin1StringView("imap4flags"))) {
94 lst << QStringLiteral("imap4flags");
95 } else {
96 lst << QStringLiteral("imapflags");
97 }
98 if (hasVariableSupport) {
99 lst << QStringLiteral("variables");
100 }
101 const SelectMatchTypeComboBox *matchTypeCombo = w->findChild<SelectMatchTypeComboBox *>(QStringLiteral("matchtype"));
102 lst << matchTypeCombo->needRequires();
103 return lst;
104}
105
106bool SieveConditionHasFlag::needCheckIfServerHasCapability() const
107{
108 return true;
109}
110
111QString SieveConditionHasFlag::serverNeedsCapability() const
112{
113 return QStringLiteral("variables");
114#if 0
115 if (sieveCapabilities().contains(QLatin1StringView("variables"))) {
116 }
117 if (sieveCapabilities().contains(QLatin1StringView("imap4flags"))) {
118 return QStringLiteral("imap4flags");
119 } else {
120 return QStringLiteral("imapflags");
121 }
122#endif
123}
124
125QString SieveConditionHasFlag::help() const
126{
127 return i18n("The hasflag test evaluates to true if any of the variables matches any flag name.");
128}
129
130void SieveConditionHasFlag::setParamWidgetValue(QXmlStreamReader &element, QWidget *w, bool notCondition, QString &error)
131{
132 QStringList strList;
133 QString commentStr;
134 while (element.readNextStartElement()) {
135 const QStringView tagName = element.name();
136 if (tagName == QLatin1StringView("tag")) {
137 auto matchTypeCombo = w->findChild<SelectMatchTypeComboBox *>(QStringLiteral("matchtype"));
138 matchTypeCombo->setCode(AutoCreateScriptUtil::tagValueWithCondition(element.readElementText(), notCondition), name(), error);
139 } else if (tagName == QLatin1StringView("str")) {
140 strList << element.readElementText();
141 } else if (tagName == QLatin1StringView("crlf")) {
142 element.skipCurrentElement();
143 // nothing
144 } else if (tagName == QLatin1StringView("comment")) {
145 commentStr = AutoCreateScriptUtil::loadConditionComment(commentStr, element.readElementText());
146 } else {
147 unknownTag(tagName, error);
148 qCDebug(LIBKSIEVEUI_LOG) << " SieveConditionExists::setParamWidgetValue unknown tagName " << tagName;
149 }
150 }
151 if (!commentStr.isEmpty()) {
152 setComment(commentStr);
153 }
154
155 switch (strList.count()) {
156 case 1: {
157 auto value = w->findChild<AbstractRegexpEditorLineEdit *>(QStringLiteral("value"));
158 value->setCode(strList.at(0));
159 break;
160 }
161 case 2:
162 if (hasVariableSupport) {
163 auto variableName = w->findChild<QLineEdit *>(QStringLiteral("variablename"));
164 variableName->setText(strList.at(0));
165 auto value = w->findChild<AbstractRegexpEditorLineEdit *>(QStringLiteral("value"));
166 value->setCode(strList.at(1));
167 } else {
168 qCDebug(LIBKSIEVEUI_LOG) << " SieveConditionHasFlag has not variable support";
169 }
170 break;
171 default:
172 qCDebug(LIBKSIEVEUI_LOG) << " SieveConditionHasFlag::setParamWidgetValue str list count not correct :" << strList.count();
173 break;
174 }
175}
176
177QUrl SieveConditionHasFlag::href() const
178{
179 return SieveEditorUtil::helpUrl(SieveEditorUtil::strToVariableName(name()));
180}
181
182#include "moc_sieveconditionhasflag.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)
void setContentsMargins(const QMargins &margins)
void textChanged(const QString &text)
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)
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.