Libksieve

selectmatchtypecombobox.cpp
1 /*
2  SPDX-FileCopyrightText: 2013-2021 Laurent Montel <[email protected]>
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 
12 using namespace KSieveUi;
13 
14 SelectMatchTypeComboBox::SelectMatchTypeComboBox(SieveEditorGraphicalModeWidget *sieveGraphicalModeWidget, QWidget *parent)
15  : QComboBox(parent)
16 {
17  mHasRegexCapability = sieveGraphicalModeWidget->sieveCapabilities().contains(QLatin1String("regex"));
18  initialize();
19  connect(this, QOverload<int>::of(&SelectMatchTypeComboBox::activated), this, &SelectMatchTypeComboBox::slotValueChanged);
20 }
21 
22 SelectMatchTypeComboBox::~SelectMatchTypeComboBox()
23 {
24 }
25 
26 void SelectMatchTypeComboBox::slotValueChanged(int val)
27 {
28  if (mHasRegexCapability) {
29  const QString value = itemData(val).toString();
30  Q_EMIT switchToRegexp(value.contains(QLatin1String("regex")));
31  }
32  Q_EMIT valueChanged();
33 }
34 
35 void SelectMatchTypeComboBox::initialize()
36 {
37  addItem(i18n("is"), QStringLiteral(":is"));
38  addItem(i18n("not is"), QStringLiteral("[NOT]:is"));
39  addItem(i18n("contains"), QStringLiteral(":contains"));
40  addItem(i18n("not contains"), QStringLiteral("[NOT]:contains"));
41  addItem(i18n("matches"), QStringLiteral(":matches"));
42  addItem(i18n("not matches"), QStringLiteral("[NOT]:matches"));
43  if (mHasRegexCapability) {
44  addItem(i18n("regex"), QStringLiteral(":regex"));
45  addItem(i18n("not regex"), QStringLiteral("[NOT]:regex"));
46  }
47 }
48 
49 QString SelectMatchTypeComboBox::code(bool &negative) const
50 {
51  QString value = itemData(currentIndex()).toString();
52  negative = value.startsWith(QLatin1String("[NOT]"));
53  if (negative) {
54  value.remove(QStringLiteral("[NOT]"));
55  }
56  return value;
57 }
58 
59 void SelectMatchTypeComboBox::setCode(const QString &code, const QString &name, QString &error)
60 {
61  const int index = findData(code);
62  if (index != -1) {
63  setCurrentIndex(index);
64  if (mHasRegexCapability) {
65  // TODO optimize
66  const QString value = itemData(index).toString();
67  Q_EMIT switchToRegexp(value.contains(QLatin1String("regex")));
68  }
69  } else {
70  AutoCreateScriptUtil::comboboxItemNotFound(code, name, error);
71  setCurrentIndex(0);
72  }
73 }
74 
75 QStringList SelectMatchTypeComboBox::needRequires() const
76 {
77  QStringList requireModules;
78  if (mHasRegexCapability) {
79  const QString value = itemData(currentIndex()).toString();
80  if (value.contains(QLatin1String("regex"))) {
81  requireModules << QStringLiteral("regex");
82  }
83  }
84  return requireModules;
85 }
KCRASH_EXPORT void initialize()
QString & remove(int position, int n)
bool startsWith(const QString &s, Qt::CaseSensitivity cs) const const
bool contains(QChar ch, Qt::CaseSensitivity cs) const const
QString i18n(const char *text, const TYPE &arg...)
This file is part of the KDE documentation.
Documentation copyright © 1996-2021 The KDE developers.
Generated on Sun Apr 11 2021 23:09:36 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.