File indexing completed on 2024-11-17 04:51:14

0001 /*
0002   SPDX-FileCopyrightText: 2017 Daniel Vrátil <dvratil@kde.org>
0003 
0004   SPDX-License-Identifier: GPL-2.0-or-later
0005 */
0006 
0007 #include "encryptionwidgethandler.h"
0008 #include <KLazyLocalizedString>
0009 #include <QComboBox>
0010 #include <QLabel>
0011 #include <QStackedWidget>
0012 using namespace MailCommon;
0013 
0014 static const struct {
0015     SearchRule::Function id;
0016     const KLazyLocalizedString displayName;
0017 } EncryptionFunctions[] = {{SearchRule::FuncEquals, kli18n("is")}, {SearchRule::FuncNotEqual, kli18n("is not")}};
0018 static const int EncryptionFunctionCount = sizeof(EncryptionFunctions) / sizeof(*EncryptionFunctions);
0019 
0020 EncryptionWidgetHandler::EncryptionWidgetHandler()
0021     : RuleWidgetHandler()
0022 {
0023 }
0024 
0025 EncryptionWidgetHandler::~EncryptionWidgetHandler() = default;
0026 
0027 QWidget *EncryptionWidgetHandler::createFunctionWidget(int number, QStackedWidget *functionStack, const QObject *receiver, bool isBalooSearch) const
0028 {
0029     Q_UNUSED(isBalooSearch)
0030 
0031     if (number != 0) {
0032         return nullptr;
0033     }
0034 
0035     auto combo = new QComboBox(functionStack);
0036     combo->setMinimumWidth(50);
0037     combo->setObjectName(QLatin1StringView("encryptionRuleFuncCombo"));
0038     for (int i = 0; i < EncryptionFunctionCount; ++i) {
0039         combo->addItem(EncryptionFunctions[i].displayName.toString());
0040     }
0041     combo->adjustSize();
0042     QObject::connect(combo, SIGNAL(activated(int)), receiver, SLOT(slotFunctionChanged()));
0043     return combo;
0044 }
0045 
0046 QWidget *EncryptionWidgetHandler::createValueWidget(int number, QStackedWidget *valueStack, const QObject *receiver) const
0047 {
0048     Q_UNUSED(receiver)
0049 
0050     if (number != 0) {
0051         return nullptr;
0052     }
0053 
0054     auto lbl = new QLabel(i18n("encrypted"), valueStack);
0055     lbl->setObjectName(QLatin1StringView("encryptionRuleValueLabel"));
0056     return lbl;
0057 }
0058 
0059 SearchRule::Function EncryptionWidgetHandler::function(const QByteArray &field, const QStackedWidget *functionStack) const
0060 {
0061     if (!handlesField(field)) {
0062         return SearchRule::FuncNone;
0063     }
0064 
0065     const auto combo = functionStack->findChild<QComboBox *>(QStringLiteral("encryptionRuleFuncCombo"));
0066     if (combo && combo->currentIndex() >= 0) {
0067         return EncryptionFunctions[combo->currentIndex()].id;
0068     }
0069 
0070     return SearchRule::FuncNone;
0071 }
0072 
0073 QString EncryptionWidgetHandler::value(const QByteArray &field, const QStackedWidget *functionStack, const QStackedWidget *valueStack) const
0074 {
0075     Q_UNUSED(functionStack)
0076     Q_UNUSED(valueStack)
0077     if (!handlesField(field)) {
0078         return {};
0079     }
0080     return QStringLiteral("is encrypted"); // dummy value
0081 }
0082 
0083 QString EncryptionWidgetHandler::prettyValue(const QByteArray &field, const QStackedWidget *functionStack, const QStackedWidget *valueStack) const
0084 {
0085     Q_UNUSED(functionStack)
0086     Q_UNUSED(valueStack)
0087     if (!handlesField(field)) {
0088         return {};
0089     }
0090 
0091     return i18n("is encrypted");
0092 }
0093 
0094 bool EncryptionWidgetHandler::handlesField(const QByteArray &field) const
0095 {
0096     return field == "<encryption>";
0097 }
0098 
0099 void EncryptionWidgetHandler::reset(QStackedWidget *functionStack, QStackedWidget *valueStack) const
0100 {
0101     const auto combo = functionStack->findChild<QComboBox *>(QStringLiteral("encryptionRuleFuncCombo"));
0102     if (combo) {
0103         const bool blocked = combo->blockSignals(true);
0104         combo->setCurrentIndex(0);
0105         combo->blockSignals(blocked);
0106     }
0107 
0108     Q_UNUSED(valueStack)
0109 }
0110 
0111 bool EncryptionWidgetHandler::setRule(QStackedWidget *functionStack, QStackedWidget *valueStack, const SearchRule::Ptr rule, bool isBalooSearch) const
0112 {
0113     Q_UNUSED(isBalooSearch)
0114     if (!rule || !handlesField(rule->field())) {
0115         reset(functionStack, valueStack);
0116         return false;
0117     }
0118     update("<encryption>", functionStack, valueStack);
0119 
0120     const auto combo = functionStack->findChild<QComboBox *>(QStringLiteral("encryptionRuleFuncCombo"));
0121     if (combo) {
0122         const bool blocked = combo->blockSignals(true);
0123         for (int i = 0; i < EncryptionFunctionCount; ++i) {
0124             if (EncryptionFunctions[i].id == rule->function()) {
0125                 combo->setCurrentIndex(i);
0126                 break;
0127             }
0128         }
0129         combo->blockSignals(blocked);
0130         return true;
0131     }
0132 
0133     return true;
0134 }
0135 
0136 bool EncryptionWidgetHandler::update(const QByteArray &field, QStackedWidget *functionStack, QStackedWidget *valueStack) const
0137 {
0138     if (!handlesField(field)) {
0139         return false;
0140     }
0141 
0142     functionStack->setCurrentWidget(functionStack->findChild<QWidget *>(QStringLiteral("encryptionRuleFuncCombo")));
0143     valueStack->setCurrentWidget(valueStack->findChild<QWidget *>(QStringLiteral("encryptionRuleValueLabel")));
0144 
0145     return true;
0146 }