Mailcommon

messagerulewidgethandler.cpp
1/*
2 SPDX-FileCopyrightText: 2013-2024 Laurent Montel <montel@kde.org>
3
4 SPDX-License-Identifier: GPL-2.0-or-later
5*/
6
7#include "messagerulewidgethandler.h"
8#include "search/searchpattern.h"
9
10#include <KLazyLocalizedString>
11#include <KLineEdit>
12#include <KLocalizedString>
13#include <QComboBox>
14#include <QLabel>
15#include <QStackedWidget>
16using namespace MailCommon;
17
18// also see SearchRule::matches() and SearchRule::Function
19// if you change the following strings!
20static const struct {
22 const KLazyLocalizedString displayName;
23} MessageFunctions[] = {
24 {SearchRule::FuncContains, kli18n("contains")},
25 {SearchRule::FuncContainsNot, kli18n("does not contain")},
26 {SearchRule::FuncRegExp, kli18n("matches regular expr.")},
27 {SearchRule::FuncNotRegExp, kli18n("does not match reg. expr.")},
28 {SearchRule::FuncHasAttachment, kli18n("has an attachment")},
29 {SearchRule::FuncHasNoAttachment, kli18n("has no attachment")},
30};
31static const int MessageFunctionCount = sizeof(MessageFunctions) / sizeof(*MessageFunctions);
32
33//---------------------------------------------------------------------------
34
35QWidget *MessageRuleWidgetHandler::createFunctionWidget(int number, QStackedWidget *functionStack, const QObject *receiver, bool isBalooSearch) const
36{
37 if (number != 0) {
38 return nullptr;
39 }
40
41 auto funcCombo = new QComboBox(functionStack);
42 funcCombo->setMinimumWidth(50);
43 funcCombo->setObjectName(QLatin1StringView("messageRuleFuncCombo"));
44 for (int i = 0; i < MessageFunctionCount; ++i) {
45 if (!(isBalooSearch && (MessageFunctions[i].id == SearchRule::FuncHasAttachment || MessageFunctions[i].id == SearchRule::FuncHasNoAttachment))) {
46 funcCombo->addItem(MessageFunctions[i].displayName.toString());
47 }
48 }
49 funcCombo->adjustSize();
50 QObject::connect(funcCombo, SIGNAL(activated(int)), receiver, SLOT(slotFunctionChanged()));
51 return funcCombo;
52}
53
54//---------------------------------------------------------------------------
55
56QWidget *MessageRuleWidgetHandler::createValueWidget(int number, QStackedWidget *valueStack, const QObject *receiver) const
57{
58 if (number == 0) {
59 auto lineEdit = new KLineEdit(valueStack);
60 lineEdit->setClearButtonEnabled(true);
61 lineEdit->setTrapReturnKey(true);
62
63 lineEdit->setObjectName(QLatin1StringView("regExpLineEdit"));
64 QObject::connect(lineEdit, SIGNAL(textChanged(QString)), receiver, SLOT(slotValueChanged()));
65 QObject::connect(lineEdit, SIGNAL(returnPressed()), receiver, SLOT(slotReturnPressed()));
66 return lineEdit;
67 }
68
69 // blank QLabel to hide value widget for has-attachment rule
70 if (number == 1) {
71 auto label = new QLabel(valueStack);
72 label->setObjectName(QLatin1StringView("textRuleValueHider"));
73 label->setBuddy(valueStack);
74 return label;
75 }
76
77 return nullptr;
78}
79
80//---------------------------------------------------------------------------
81
82SearchRule::Function MessageRuleWidgetHandler::currentFunction(const QStackedWidget *functionStack) const
83{
84 const auto funcCombo = functionStack->findChild<QComboBox *>(QStringLiteral("messageRuleFuncCombo"));
85
86 if (funcCombo && funcCombo->currentIndex() >= 0) {
87 return MessageFunctions[funcCombo->currentIndex()].id;
88 }
89
90 return SearchRule::FuncNone;
91}
92
93//---------------------------------------------------------------------------
94
95SearchRule::Function MessageRuleWidgetHandler::function(const QByteArray &field, const QStackedWidget *functionStack) const
96{
97 if (!handlesField(field)) {
98 return SearchRule::FuncNone;
99 }
100
101 return currentFunction(functionStack);
102}
103
104//---------------------------------------------------------------------------
105
106QString MessageRuleWidgetHandler::currentValue(const QStackedWidget *valueStack, SearchRule::Function) const
107{
108 const KLineEdit *lineEdit = valueStack->findChild<KLineEdit *>(QStringLiteral("regExpLineEdit"));
109
110 if (lineEdit) {
111 return lineEdit->text();
112 }
113
114 return {};
115}
116
117//---------------------------------------------------------------------------
118
119QString MessageRuleWidgetHandler::value(const QByteArray &field, const QStackedWidget *functionStack, const QStackedWidget *valueStack) const
120{
121 if (!handlesField(field)) {
122 return {};
123 }
124
125 SearchRule::Function func = currentFunction(functionStack);
126 if (func == SearchRule::FuncHasAttachment) {
127 return QStringLiteral("has an attachment"); // just a non-empty dummy value
128 } else if (func == SearchRule::FuncHasNoAttachment) {
129 return QStringLiteral("has no attachment"); // just a non-empty dummy value
130 } else {
131 return currentValue(valueStack, func);
132 }
133}
134
135//---------------------------------------------------------------------------
136
137QString MessageRuleWidgetHandler::prettyValue(const QByteArray &field, const QStackedWidget *functionStack, const QStackedWidget *valueStack) const
138{
139 if (!handlesField(field)) {
140 return {};
141 }
142
143 SearchRule::Function func = currentFunction(functionStack);
144 if (func == SearchRule::FuncHasAttachment) {
145 return i18n("has an attachment");
146 } else if (func == SearchRule::FuncHasNoAttachment) {
147 return i18n("has no attachment");
148 } else {
149 return currentValue(valueStack, func);
150 }
151}
152
153//---------------------------------------------------------------------------
154
155bool MessageRuleWidgetHandler::handlesField(const QByteArray &field) const
156{
157 return field == "<message>";
158}
159
160//---------------------------------------------------------------------------
161
162void MessageRuleWidgetHandler::reset(QStackedWidget *functionStack, QStackedWidget *valueStack) const
163{
164 // reset the function combo box
165 const auto funcCombo = functionStack->findChild<QComboBox *>(QStringLiteral("messageRuleFuncCombo"));
166
167 if (funcCombo) {
168 funcCombo->blockSignals(true);
169 funcCombo->setCurrentIndex(0);
170 funcCombo->blockSignals(false);
171 }
172
173 // reset the value widget
174 auto lineEdit = valueStack->findChild<KLineEdit *>(QStringLiteral("regExpLineEdit"));
175
176 if (lineEdit) {
177 lineEdit->blockSignals(true);
178 lineEdit->clear();
179 lineEdit->blockSignals(false);
180 lineEdit->setClearButtonEnabled(false);
181 lineEdit->setClearButtonEnabled(true);
182 valueStack->setCurrentWidget(lineEdit);
183 }
184}
185
186//---------------------------------------------------------------------------
187
188bool MessageRuleWidgetHandler::setRule(QStackedWidget *functionStack, QStackedWidget *valueStack, const SearchRule::Ptr rule, bool isBalooSearch) const
189{
190 if (!rule || !handlesField(rule->field())) {
191 reset(functionStack, valueStack);
192 return false;
193 }
194
195 const SearchRule::Function func = rule->function();
196
197 if ((isBalooSearch && (func == SearchRule::FuncHasAttachment || func == SearchRule::FuncHasNoAttachment))) {
198 reset(functionStack, valueStack);
199 return false;
200 }
201
202 int i = 0;
203 for (; i < MessageFunctionCount; ++i) {
204 if (func == MessageFunctions[i].id) {
205 break;
206 }
207 }
208
209 const auto funcCombo = functionStack->findChild<QComboBox *>(QStringLiteral("messageRuleFuncCombo"));
210
211 if (funcCombo) {
212 funcCombo->blockSignals(true);
213 if (i < MessageFunctionCount) {
214 funcCombo->setCurrentIndex(i);
215 } else {
216 funcCombo->setCurrentIndex(0);
217 }
218 funcCombo->blockSignals(false);
219 functionStack->setCurrentWidget(funcCombo);
220 }
221
222 if (func == SearchRule::FuncHasAttachment || func == SearchRule::FuncHasNoAttachment) {
223 auto w = valueStack->findChild<QWidget *>(QStringLiteral("textRuleValueHider"));
224 valueStack->setCurrentWidget(w);
225 } else {
226 auto lineEdit = valueStack->findChild<KLineEdit *>(QStringLiteral("regExpLineEdit"));
227
228 if (lineEdit) {
229 lineEdit->blockSignals(true);
230 lineEdit->setText(rule->contents());
231 lineEdit->blockSignals(false);
232 lineEdit->setClearButtonEnabled(false);
233 lineEdit->setClearButtonEnabled(true);
234 valueStack->setCurrentWidget(lineEdit);
235 }
236 }
237 return true;
238}
239
240//---------------------------------------------------------------------------
241
242bool MessageRuleWidgetHandler::update(const QByteArray &field, QStackedWidget *functionStack, QStackedWidget *valueStack) const
243{
244 if (!handlesField(field)) {
245 return false;
246 }
247
248 // raise the correct function widget
249 functionStack->setCurrentWidget(functionStack->findChild<QWidget *>(QStringLiteral("messageRuleFuncCombo")));
250
251 // raise the correct value widget
252 SearchRule::Function func = currentFunction(functionStack);
253 if (func == SearchRule::FuncHasAttachment || func == SearchRule::FuncHasNoAttachment) {
254 auto w = valueStack->findChild<QWidget *>(QStringLiteral("textRuleValueHider"));
255 valueStack->setCurrentWidget(w);
256 } else {
257 auto lineEdit = valueStack->findChild<KLineEdit *>(QStringLiteral("regExpLineEdit"));
258
259 if (lineEdit) {
260 valueStack->setCurrentWidget(lineEdit);
261 }
262 }
263 return true;
264}
QString toString() const
virtual void setText(const QString &)
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 i18n(const char *text, const TYPE &arg...)
QString label(StandardShortcut id)
The filter dialog.
void clear()
void setClearButtonEnabled(bool enable)
bool blockSignals(bool block)
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 Fri Dec 6 2024 12:02:04 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.