Mailcommon

encryptionwidgethandler.cpp
1/*
2 SPDX-FileCopyrightText: 2017 Daniel Vrátil <dvratil@kde.org>
3
4 SPDX-License-Identifier: GPL-2.0-or-later
5*/
6
7#include "encryptionwidgethandler.h"
8#include <KLazyLocalizedString>
9#include <QComboBox>
10#include <QLabel>
11#include <QStackedWidget>
12using namespace MailCommon;
13
14static const struct {
16 const KLazyLocalizedString displayName;
17} EncryptionFunctions[] = {{SearchRule::FuncEquals, kli18n("is")}, {SearchRule::FuncNotEqual, kli18n("is not")}};
18static const int EncryptionFunctionCount = sizeof(EncryptionFunctions) / sizeof(*EncryptionFunctions);
19
20EncryptionWidgetHandler::EncryptionWidgetHandler()
22{
23}
24
25EncryptionWidgetHandler::~EncryptionWidgetHandler() = default;
26
27QWidget *EncryptionWidgetHandler::createFunctionWidget(int number, QStackedWidget *functionStack, const QObject *receiver, bool isBalooSearch) const
28{
29 Q_UNUSED(isBalooSearch)
30
31 if (number != 0) {
32 return nullptr;
33 }
34
35 auto combo = new QComboBox(functionStack);
36 combo->setMinimumWidth(50);
37 combo->setObjectName(QLatin1StringView("encryptionRuleFuncCombo"));
38 for (int i = 0; i < EncryptionFunctionCount; ++i) {
39 combo->addItem(EncryptionFunctions[i].displayName.toString());
40 }
41 combo->adjustSize();
42 QObject::connect(combo, SIGNAL(activated(int)), receiver, SLOT(slotFunctionChanged()));
43 return combo;
44}
45
46QWidget *EncryptionWidgetHandler::createValueWidget(int number, QStackedWidget *valueStack, const QObject *receiver) const
47{
48 Q_UNUSED(receiver)
49
50 if (number != 0) {
51 return nullptr;
52 }
53
54 auto lbl = new QLabel(i18nc("@label:textbox", "encrypted"), valueStack);
55 lbl->setObjectName(QLatin1StringView("encryptionRuleValueLabel"));
56 return lbl;
57}
58
59SearchRule::Function EncryptionWidgetHandler::function(const QByteArray &field, const QStackedWidget *functionStack) const
60{
61 if (!handlesField(field)) {
62 return SearchRule::FuncNone;
63 }
64
65 const auto combo = functionStack->findChild<QComboBox *>(QStringLiteral("encryptionRuleFuncCombo"));
66 if (combo && combo->currentIndex() >= 0) {
67 return EncryptionFunctions[combo->currentIndex()].id;
68 }
69
70 return SearchRule::FuncNone;
71}
72
73QString EncryptionWidgetHandler::value(const QByteArray &field, const QStackedWidget *functionStack, const QStackedWidget *valueStack) const
74{
75 Q_UNUSED(functionStack)
76 Q_UNUSED(valueStack)
77 if (!handlesField(field)) {
78 return {};
79 }
80 return QStringLiteral("is encrypted"); // dummy value
81}
82
83QString EncryptionWidgetHandler::prettyValue(const QByteArray &field, const QStackedWidget *functionStack, const QStackedWidget *valueStack) const
84{
85 Q_UNUSED(functionStack)
86 Q_UNUSED(valueStack)
87 if (!handlesField(field)) {
88 return {};
89 }
90
91 return i18n("is encrypted");
92}
93
94bool EncryptionWidgetHandler::handlesField(const QByteArray &field) const
95{
96 return field == "<encryption>";
97}
98
99void EncryptionWidgetHandler::reset(QStackedWidget *functionStack, QStackedWidget *valueStack) const
100{
101 const auto combo = functionStack->findChild<QComboBox *>(QStringLiteral("encryptionRuleFuncCombo"));
102 if (combo) {
103 const bool blocked = combo->blockSignals(true);
104 combo->setCurrentIndex(0);
105 combo->blockSignals(blocked);
106 }
107
108 Q_UNUSED(valueStack)
109}
110
111bool EncryptionWidgetHandler::setRule(QStackedWidget *functionStack, QStackedWidget *valueStack, const SearchRule::Ptr rule, bool isBalooSearch) const
112{
113 Q_UNUSED(isBalooSearch)
114 if (!rule || !handlesField(rule->field())) {
115 reset(functionStack, valueStack);
116 return false;
117 }
118 update("<encryption>", functionStack, valueStack);
119
120 const auto combo = functionStack->findChild<QComboBox *>(QStringLiteral("encryptionRuleFuncCombo"));
121 if (combo) {
122 const bool blocked = combo->blockSignals(true);
123 for (int i = 0; i < EncryptionFunctionCount; ++i) {
124 if (EncryptionFunctions[i].id == rule->function()) {
125 combo->setCurrentIndex(i);
126 break;
127 }
128 }
129 combo->blockSignals(blocked);
130 return true;
131 }
132
133 return true;
134}
135
136bool EncryptionWidgetHandler::update(const QByteArray &field, QStackedWidget *functionStack, QStackedWidget *valueStack) const
137{
138 if (!handlesField(field)) {
139 return false;
140 }
141
142 functionStack->setCurrentWidget(functionStack->findChild<QWidget *>(QStringLiteral("encryptionRuleFuncCombo")));
143 valueStack->setCurrentWidget(valueStack->findChild<QWidget *>(QStringLiteral("encryptionRuleValueLabel")));
144
145 return true;
146}
QString toString() const
An interface to filter/search rule widget handlers.
std::shared_ptr< SearchRule > Ptr
Defines a pointer to a search rule.
Definition searchrule.h:29
Function
Describes operators for comparison of field and contents.
Definition searchrule.h:40
QString i18nc(const char *context, const char *text, const TYPE &arg...)
QString i18n(const char *text, const TYPE &arg...)
The filter dialog.
QMetaObject::Connection connect(const QObject *sender, PointerToMemberFunction signal, Functor functor)
T findChild(const QString &name, Qt::FindChildOptions options) const const
void setCurrentWidget(QWidget *widget)
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Sat Dec 21 2024 17:00:25 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.