File indexing completed on 2025-02-16 04:55:55

0001 /*
0002    SPDX-FileCopyrightText: 2013-2024 Laurent Montel <montel@kde.org>
0003 
0004    SPDX-License-Identifier: LGPL-2.0-or-later
0005 */
0006 #include "selectmatchtypecombobox.h"
0007 #include "autocreatescripts/autocreatescriptutil_p.h"
0008 #include "autocreatescripts/sieveeditorgraphicalmodewidget.h"
0009 
0010 #include <KLocalizedString>
0011 
0012 using namespace KSieveUi;
0013 
0014 SelectMatchTypeComboBox::SelectMatchTypeComboBox(SieveEditorGraphicalModeWidget *sieveGraphicalModeWidget, QWidget *parent)
0015     : QComboBox(parent)
0016 {
0017     mHasRegexCapability = sieveGraphicalModeWidget->sieveCapabilities().contains(QLatin1StringView("regex"));
0018     initialize();
0019     connect(this, &SelectMatchTypeComboBox::activated, this, &SelectMatchTypeComboBox::slotValueChanged);
0020 }
0021 
0022 SelectMatchTypeComboBox::~SelectMatchTypeComboBox() = default;
0023 
0024 void SelectMatchTypeComboBox::slotValueChanged(int val)
0025 {
0026     if (mHasRegexCapability) {
0027         const QString value = itemData(val).toString();
0028         Q_EMIT switchToRegexp(value.contains(QLatin1StringView("regex")));
0029     }
0030     Q_EMIT valueChanged();
0031 }
0032 
0033 void SelectMatchTypeComboBox::initialize()
0034 {
0035     addItem(i18n("is"), QStringLiteral(":is"));
0036     addItem(i18n("not is"), QStringLiteral("[NOT]:is"));
0037     addItem(i18n("contains"), QStringLiteral(":contains"));
0038     addItem(i18n("not contains"), QStringLiteral("[NOT]:contains"));
0039     addItem(i18n("matches"), QStringLiteral(":matches"));
0040     addItem(i18n("not matches"), QStringLiteral("[NOT]:matches"));
0041     if (mHasRegexCapability) {
0042         addItem(i18n("regex"), QStringLiteral(":regex"));
0043         addItem(i18n("not regex"), QStringLiteral("[NOT]:regex"));
0044     }
0045 }
0046 
0047 QString SelectMatchTypeComboBox::code(bool &negative) const
0048 {
0049     QString value = itemData(currentIndex()).toString();
0050     negative = value.startsWith(QLatin1StringView("[NOT]"));
0051     if (negative) {
0052         value.remove(QStringLiteral("[NOT]"));
0053     }
0054     return value;
0055 }
0056 
0057 void SelectMatchTypeComboBox::setCode(const QString &code, const QString &name, QString &error)
0058 {
0059     const int index = findData(code);
0060     if (index != -1) {
0061         setCurrentIndex(index);
0062         if (mHasRegexCapability) {
0063             // TODO optimize
0064             const QString value = itemData(index).toString();
0065             Q_EMIT switchToRegexp(value.contains(QLatin1StringView("regex")));
0066         }
0067     } else {
0068         AutoCreateScriptUtil::comboboxItemNotFound(code, name, error);
0069         setCurrentIndex(0);
0070     }
0071 }
0072 
0073 QStringList SelectMatchTypeComboBox::needRequires() const
0074 {
0075     QStringList requireModules;
0076     if (mHasRegexCapability) {
0077         const QString value = itemData(currentIndex()).toString();
0078         if (value.contains(QLatin1StringView("regex"))) {
0079             requireModules << QStringLiteral("regex");
0080         }
0081     }
0082     return requireModules;
0083 }
0084 
0085 #include "moc_selectmatchtypecombobox.cpp"