Libksieve

selectmatchtypecombobox.cpp
1 /*
2  Copyright (C) 2013-2020 Laurent Montel <[email protected]>
3 
4  This library is free software; you can redistribute it and/or
5  modify it under the terms of the GNU Library General Public
6  License as published by the Free Software Foundation; either
7  version 2 of the License, or (at your option) any later version.
8 
9  This library is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  Library General Public License for more details.
13 
14  You should have received a copy of the GNU Library General Public License
15  along with this library; see the file COPYING.LIB. If not, write to
16  the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17  Boston, MA 02110-1301, USA.
18 */
19 #include "selectmatchtypecombobox.h"
20 #include "autocreatescripts/sieveeditorgraphicalmodewidget.h"
21 #include "autocreatescripts/autocreatescriptutil_p.h"
22 
23 #include <KLocalizedString>
24 
25 using namespace KSieveUi;
26 
27 SelectMatchTypeComboBox::SelectMatchTypeComboBox(SieveEditorGraphicalModeWidget *sieveGraphicalModeWidget, QWidget *parent)
28  : QComboBox(parent)
29 {
30  mHasRegexCapability = sieveGraphicalModeWidget->sieveCapabilities().contains(QLatin1String("regex"));
31  initialize();
32  connect(this, QOverload<int>::of(&SelectMatchTypeComboBox::activated), this, &SelectMatchTypeComboBox::slotValueChanged);
33 }
34 
35 SelectMatchTypeComboBox::~SelectMatchTypeComboBox()
36 {
37 }
38 
39 void SelectMatchTypeComboBox::slotValueChanged(int val)
40 {
41  if (mHasRegexCapability) {
42  const QString value = itemData(val).toString();
43  Q_EMIT switchToRegexp(value.contains(QLatin1String("regex")));
44  }
45  Q_EMIT valueChanged();
46 }
47 
48 void SelectMatchTypeComboBox::initialize()
49 {
50  addItem(i18n("is"), QStringLiteral(":is"));
51  addItem(i18n("not is"), QStringLiteral("[NOT]:is"));
52  addItem(i18n("contains"), QStringLiteral(":contains"));
53  addItem(i18n("not contains"), QStringLiteral("[NOT]:contains"));
54  addItem(i18n("matches"), QStringLiteral(":matches"));
55  addItem(i18n("not matches"), QStringLiteral("[NOT]:matches"));
56  if (mHasRegexCapability) {
57  addItem(i18n("regex"), QStringLiteral(":regex"));
58  addItem(i18n("not regex"), QStringLiteral("[NOT]:regex"));
59  }
60 }
61 
62 QString SelectMatchTypeComboBox::code(bool &negative) const
63 {
64  QString value = itemData(currentIndex()).toString();
65  negative = value.startsWith(QLatin1String("[NOT]"));
66  if (negative) {
67  value.remove(QStringLiteral("[NOT]"));
68  }
69  return value;
70 }
71 
72 void SelectMatchTypeComboBox::setCode(const QString &code, const QString &name, QString &error)
73 {
74  const int index = findData(code);
75  if (index != -1) {
76  setCurrentIndex(index);
77  if (mHasRegexCapability) {
78  //TODO optimize
79  const QString value = itemData(index).toString();
80  Q_EMIT switchToRegexp(value.contains(QLatin1String("regex")));
81  }
82  } else {
83  AutoCreateScriptUtil::comboboxItemNotFound(code, name, error);
84  setCurrentIndex(0);
85  }
86 }
87 
88 QStringList SelectMatchTypeComboBox::needRequires() const
89 {
90  QStringList requireModules;
91  if (mHasRegexCapability) {
92  const QString value = itemData(currentIndex()).toString();
93  if (value.contains(QLatin1String("regex"))) {
94  requireModules << QStringLiteral("regex");
95  }
96  }
97  return requireModules;
98 }
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-2020 The KDE developers.
Generated on Sat Jul 11 2020 23:08:17 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.