Libksieve

selectmatchtypecombobox.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 "selectmatchtypecombobox.h"
7#include "autocreatescripts/autocreatescriptutil_p.h"
8#include "autocreatescripts/sieveeditorgraphicalmodewidget.h"
9
10#include <KLocalizedString>
11
12using namespace KSieveUi;
13
14SelectMatchTypeComboBox::SelectMatchTypeComboBox(SieveEditorGraphicalModeWidget *sieveGraphicalModeWidget, QWidget *parent)
15 : QComboBox(parent)
16{
17 mHasRegexCapability = sieveGraphicalModeWidget->sieveCapabilities().contains(QLatin1StringView("regex"));
18 initialize();
19 connect(this, &SelectMatchTypeComboBox::activated, this, &SelectMatchTypeComboBox::slotValueChanged);
20}
21
22SelectMatchTypeComboBox::~SelectMatchTypeComboBox() = default;
23
24void SelectMatchTypeComboBox::slotValueChanged(int val)
25{
26 if (mHasRegexCapability) {
27 const QString value = itemData(val).toString();
28 Q_EMIT switchToRegexp(value.contains(QLatin1StringView("regex")));
29 }
30 Q_EMIT valueChanged();
31}
32
33void SelectMatchTypeComboBox::initialize()
34{
35 addItem(i18n("is"), QStringLiteral(":is"));
36 addItem(i18n("not is"), QStringLiteral("[NOT]:is"));
37 addItem(i18n("contains"), QStringLiteral(":contains"));
38 addItem(i18n("not contains"), QStringLiteral("[NOT]:contains"));
39 addItem(i18n("matches"), QStringLiteral(":matches"));
40 addItem(i18n("not matches"), QStringLiteral("[NOT]:matches"));
41 if (mHasRegexCapability) {
42 addItem(i18n("regex"), QStringLiteral(":regex"));
43 addItem(i18n("not regex"), QStringLiteral("[NOT]:regex"));
44 }
45}
46
47QString SelectMatchTypeComboBox::code(bool &negative) const
48{
50 negative = value.startsWith(QLatin1StringView("[NOT]"));
51 if (negative) {
52 value.remove(QStringLiteral("[NOT]"));
53 }
54 return value;
55}
56
57void SelectMatchTypeComboBox::setCode(const QString &code, const QString &name, QString &error)
58{
59 const int index = findData(code);
60 if (index != -1) {
61 setCurrentIndex(index);
62 if (mHasRegexCapability) {
63 // TODO optimize
64 const QString value = itemData(index).toString();
65 Q_EMIT switchToRegexp(value.contains(QLatin1StringView("regex")));
66 }
67 } else {
68 AutoCreateScriptUtil::comboboxItemNotFound(code, name, error);
70 }
71}
72
73QStringList SelectMatchTypeComboBox::needRequires() const
74{
75 QStringList requireModules;
76 if (mHasRegexCapability) {
77 const QString value = itemData(currentIndex()).toString();
78 if (value.contains(QLatin1StringView("regex"))) {
79 requireModules << QStringLiteral("regex");
80 }
81 }
82 return requireModules;
83}
84
85#include "moc_selectmatchtypecombobox.cpp"
QString i18n(const char *text, const TYPE &arg...)
KCRASH_EXPORT void initialize()
void addItem(const QIcon &icon, const QString &text, const QVariant &userData)
int findData(const QVariant &data, int role, Qt::MatchFlags flags) const const
QVariant itemData(int index, int role) const const
Q_EMITQ_EMIT
bool contains(QChar ch, Qt::CaseSensitivity cs) const const
QString & remove(QChar ch, Qt::CaseSensitivity cs)
bool startsWith(QChar c, Qt::CaseSensitivity cs) const const
QFuture< ArgsType< Signal > > connect(Sender *sender, Signal signal)
QString toString() const const
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.