Mailcommon

messagerulewidgethandler.cpp
1 /*
2  SPDX-FileCopyrightText: 2013-2022 Laurent Montel <[email protected]>
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>
16 using namespace MailCommon;
17 
18 // also see SearchRule::matches() and SearchRule::Function
19 // if you change the following strings!
20 static 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 };
31 static const int MessageFunctionCount = sizeof(MessageFunctions) / sizeof(*MessageFunctions);
32 
33 //---------------------------------------------------------------------------
34 
35 QWidget *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(QStringLiteral("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 
56 QWidget *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(QStringLiteral("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(QStringLiteral("textRuleValueHider"));
73  label->setBuddy(valueStack);
74  return label;
75  }
76 
77  return nullptr;
78 }
79 
80 //---------------------------------------------------------------------------
81 
82 SearchRule::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 
95 SearchRule::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 
106 QString 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 
119 QString 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 
137 QString 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 
155 bool MessageRuleWidgetHandler::handlesField(const QByteArray &field) const
156 {
157  return field == "<message>";
158 }
159 
160 //---------------------------------------------------------------------------
161 
162 void 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 
188 bool 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 
242 bool 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 }
void setCurrentWidget(QWidget *widget)
std::shared_ptr< SearchRule > Ptr
Defines a pointer to a search rule.
Definition: searchrule.h:29
virtual void setText(const QString &)
void setClearButtonEnabled(bool enable)
QMetaObject::Connection connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
QString toString() const
void clear()
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
QString label(StandardShortcut id)
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:53 by doxygen 1.8.17 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.