Mailcommon

encryptionwidgethandler.cpp
1 /*
2  SPDX-FileCopyrightText: 2017 Daniel Vr├ítil <[email protected]>
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>
12 using namespace MailCommon;
13 
14 static const struct {
16  const KLazyLocalizedString displayName;
17 } EncryptionFunctions[] = {{SearchRule::FuncEquals, kli18n("is")}, {SearchRule::FuncNotEqual, kli18n("is not")}};
18 static const int EncryptionFunctionCount = sizeof(EncryptionFunctions) / sizeof(*EncryptionFunctions);
19 
20 EncryptionWidgetHandler::EncryptionWidgetHandler()
22 {
23 }
24 
25 EncryptionWidgetHandler::~EncryptionWidgetHandler() = default;
26 
27 QWidget *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(QStringLiteral("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 
46 QWidget *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(i18n("encrypted"), valueStack);
55  lbl->setObjectName(QStringLiteral("encryptionRuleValueLabel"));
56  return lbl;
57 }
58 
59 SearchRule::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 
73 QString 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 
83 QString 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 
94 bool EncryptionWidgetHandler::handlesField(const QByteArray &field) const
95 {
96  return field == "<encryption>";
97 }
98 
99 void 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 
111 bool 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 
136 bool 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 }
void setCurrentWidget(QWidget *widget)
std::shared_ptr< SearchRule > Ptr
Defines a pointer to a search rule.
Definition: searchrule.h:29
QMetaObject::Connection connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
QString toString() const
QString i18n(const char *text, const TYPE &arg...)
bool blockSignals(bool block)
Function
Describes operators for comparison of field and contents.
Definition: searchrule.h:40
T findChild(const QString &name, Qt::FindChildOptions options) const const
An interface to filter/search rule widget handlers.
void update(Part *part, const QByteArray &data, qint64 dataSize)
KGuiItem reset()
The filter dialog.
This file is part of the KDE documentation.
Documentation copyright © 1996-2022 The KDE developers.
Generated on Sat Oct 1 2022 04:00:52 by doxygen 1.8.17 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.