Mailcommon

statusrulewidgethandler.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 "statusrulewidgethandler.h"
8 #include "search/searchrule/searchrulestatus.h"
9 
10 #include <KLazyLocalizedString>
11 #include <QComboBox>
12 #include <QIcon>
13 #include <QStackedWidget>
14 using namespace MailCommon;
15 
16 static const struct {
18  const KLazyLocalizedString displayName;
19 } StatusFunctions[] = {{SearchRule::FuncContains, kli18n("is")}, {SearchRule::FuncContainsNot, kli18n("is not")}};
20 static const int StatusFunctionCount = sizeof(StatusFunctions) / sizeof(*StatusFunctions);
21 
22 struct MessageStatusInfo {
23  const KLazyLocalizedString text;
24  const char *icon;
25 };
26 
27 // If you change the ordering here; also do it in the enum below
28 static const MessageStatusInfo StatusValues[] = {
29  {kli18nc("message status", "Important"), "emblem-important"},
30  {kli18nc("message status", "Action Item"), "mail-task"},
31  {kli18nc("message status", "Unread"), "mail-unread"},
32  {kli18nc("message status", "Read"), "mail-read"},
33  {kli18nc("message status", "Deleted"), "mail-deleted"},
34  {kli18nc("message status", "Replied"), "mail-replied"},
35  {kli18nc("message status", "Forwarded"), "mail-forwarded"},
36  {kli18nc("message status", "Queued"), "mail-queued"},
37  {kli18nc("message status", "Sent"), "mail-sent"},
38  {kli18nc("message status", "Watched"), "mail-thread-watch"},
39  {kli18nc("message status", "Ignored"), "mail-thread-ignored"},
40  {kli18nc("message status", "Spam"), "mail-mark-junk"},
41  {kli18nc("message status", "Ham"), "mail-mark-notjunk"},
42  {kli18nc("message status", "Has Attachment"), "mail-attachment"} // must be last
43 };
44 
45 static const int StatusValueCount = sizeof(StatusValues) / sizeof(MessageStatusInfo);
46 // we want to show all status entries in the quick search bar, but only the
47 // ones up to attachment in the search/filter dialog, because there the
48 // attachment case is handled separately.
49 static const int StatusValueCountWithoutHidden = StatusValueCount - 1;
50 
51 //---------------------------------------------------------------------------
52 
53 QWidget *StatusRuleWidgetHandler::createFunctionWidget(int number, QStackedWidget *functionStack, const QObject *receiver, bool /*isBalooSearch*/) const
54 {
55  if (number != 0) {
56  return nullptr;
57  }
58 
59  auto funcCombo = new QComboBox(functionStack);
60  funcCombo->setMinimumWidth(50);
61  funcCombo->setObjectName(QStringLiteral("statusRuleFuncCombo"));
62  for (int i = 0; i < StatusFunctionCount; ++i) {
63  funcCombo->addItem(StatusFunctions[i].displayName.toString());
64  }
65  funcCombo->adjustSize();
66  QObject::connect(funcCombo, SIGNAL(activated(int)), receiver, SLOT(slotFunctionChanged()));
67  return funcCombo;
68 }
69 
70 //---------------------------------------------------------------------------
71 
72 QWidget *StatusRuleWidgetHandler::createValueWidget(int number, QStackedWidget *valueStack, const QObject *receiver) const
73 {
74  if (number != 0) {
75  return nullptr;
76  }
77 
78  auto statusCombo = new QComboBox(valueStack);
79  statusCombo->setMinimumWidth(50);
80  statusCombo->setObjectName(QStringLiteral("statusRuleValueCombo"));
81  for (int i = 0; i < StatusValueCountWithoutHidden; ++i) {
82  if (StatusValues[i].icon != nullptr) {
83  statusCombo->addItem(QIcon::fromTheme(QLatin1String(StatusValues[i].icon)), StatusValues[i].text.toString());
84  } else {
85  statusCombo->addItem(StatusValues[i].text.toString());
86  }
87  }
88  statusCombo->adjustSize();
89  QObject::connect(statusCombo, SIGNAL(activated(int)), receiver, SLOT(slotValueChanged()));
90  return statusCombo;
91 }
92 
93 //---------------------------------------------------------------------------
94 
95 SearchRule::Function StatusRuleWidgetHandler::currentFunction(const QStackedWidget *functionStack) const
96 {
97  const auto funcCombo = functionStack->findChild<QComboBox *>(QStringLiteral("statusRuleFuncCombo"));
98 
99  if (funcCombo && funcCombo->currentIndex() >= 0) {
100  return StatusFunctions[funcCombo->currentIndex()].id;
101  }
102 
103  return SearchRule::FuncNone;
104 }
105 
106 //---------------------------------------------------------------------------
107 
108 SearchRule::Function StatusRuleWidgetHandler::function(const QByteArray &field, const QStackedWidget *functionStack) const
109 {
110  if (!handlesField(field)) {
111  return SearchRule::FuncNone;
112  }
113 
114  return currentFunction(functionStack);
115 }
116 
117 //---------------------------------------------------------------------------
118 
119 int StatusRuleWidgetHandler::currentStatusValue(const QStackedWidget *valueStack) const
120 {
121  const auto statusCombo = valueStack->findChild<QComboBox *>(QStringLiteral("statusRuleValueCombo"));
122 
123  if (statusCombo) {
124  return statusCombo->currentIndex();
125  }
126 
127  return -1;
128 }
129 
130 //---------------------------------------------------------------------------
131 
132 QString StatusRuleWidgetHandler::value(const QByteArray &field, const QStackedWidget *, const QStackedWidget *valueStack) const
133 {
134  if (!handlesField(field)) {
135  return {};
136  }
137 
138  const int status = currentStatusValue(valueStack);
139  if (status != -1) {
140  return StatusValues[status].text.untranslatedText();
141  } else {
142  return {};
143  }
144 }
145 
146 //---------------------------------------------------------------------------
147 
148 QString StatusRuleWidgetHandler::prettyValue(const QByteArray &field, const QStackedWidget *, const QStackedWidget *valueStack) const
149 {
150  if (!handlesField(field)) {
151  return {};
152  }
153 
154  const int status = currentStatusValue(valueStack);
155  if (status != -1) {
156  return StatusValues[status].text.toString();
157  } else {
158  return {};
159  }
160 }
161 
162 //---------------------------------------------------------------------------
163 
164 bool StatusRuleWidgetHandler::handlesField(const QByteArray &field) const
165 {
166  return field == "<status>";
167 }
168 
169 //---------------------------------------------------------------------------
170 
171 void StatusRuleWidgetHandler::reset(QStackedWidget *functionStack, QStackedWidget *valueStack) const
172 {
173  // reset the function combo box
174  auto funcCombo = functionStack->findChild<QComboBox *>(QStringLiteral("statusRuleFuncCombo"));
175 
176  if (funcCombo) {
177  funcCombo->blockSignals(true);
178  funcCombo->setCurrentIndex(0);
179  funcCombo->blockSignals(false);
180  }
181 
182  // reset the status value combo box
183  auto statusCombo = valueStack->findChild<QComboBox *>(QStringLiteral("statusRuleValueCombo"));
184 
185  if (statusCombo) {
186  statusCombo->blockSignals(true);
187  statusCombo->setCurrentIndex(0);
188  statusCombo->blockSignals(false);
189  }
190 }
191 
192 //---------------------------------------------------------------------------
193 
194 bool StatusRuleWidgetHandler::setRule(QStackedWidget *functionStack, QStackedWidget *valueStack, const SearchRule::Ptr rule, bool /*isBalooSearch*/) const
195 {
196  if (!rule || !handlesField(rule->field())) {
197  reset(functionStack, valueStack);
198  return false;
199  }
200 
201  // set the function
202  const SearchRule::Function func = rule->function();
203  int funcIndex = 0;
204  for (; funcIndex < StatusFunctionCount; ++funcIndex) {
205  if (func == StatusFunctions[funcIndex].id) {
206  break;
207  }
208  }
209 
210  const auto funcCombo = functionStack->findChild<QComboBox *>(QStringLiteral("statusRuleFuncCombo"));
211 
212  if (funcCombo) {
213  funcCombo->blockSignals(true);
214  if (funcIndex < StatusFunctionCount) {
215  funcCombo->setCurrentIndex(funcIndex);
216  } else {
217  funcCombo->setCurrentIndex(0);
218  }
219  funcCombo->blockSignals(false);
220  functionStack->setCurrentWidget(funcCombo);
221  }
222 
223  // set the value
224  const QString value = rule->contents();
225  int valueIndex = 0;
226  for (; valueIndex < StatusValueCountWithoutHidden; ++valueIndex) {
227  if (value == StatusValues[valueIndex].text.untranslatedText()) {
228  break;
229  }
230  }
231 
232  const auto statusCombo = valueStack->findChild<QComboBox *>(QStringLiteral("statusRuleValueCombo"));
233 
234  if (statusCombo) {
235  statusCombo->blockSignals(true);
236  if (valueIndex < StatusValueCountWithoutHidden) {
237  statusCombo->setCurrentIndex(valueIndex);
238  } else {
239  statusCombo->setCurrentIndex(0);
240  }
241  statusCombo->blockSignals(false);
242  valueStack->setCurrentWidget(statusCombo);
243  }
244  return true;
245 }
246 
247 //---------------------------------------------------------------------------
248 
249 bool StatusRuleWidgetHandler::update(const QByteArray &field, QStackedWidget *functionStack, QStackedWidget *valueStack) const
250 {
251  if (!handlesField(field)) {
252  return false;
253  }
254 
255  // raise the correct function widget
256  functionStack->setCurrentWidget(functionStack->findChild<QWidget *>(QStringLiteral("statusRuleFuncCombo")));
257 
258  // raise the correct value widget
259  valueStack->setCurrentWidget(valueStack->findChild<QWidget *>(QStringLiteral("statusRuleValueCombo")));
260 
261  return true;
262 }
void setCurrentWidget(QWidget *widget)
std::shared_ptr< SearchRule > Ptr
Defines a pointer to a search rule.
Definition: searchrule.h:29
QIcon fromTheme(const QString &name)
QMetaObject::Connection connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
QString toString() const
bool blockSignals(bool block)
NETWORKMANAGERQT_EXPORT NetworkManager::Status status()
Function
Describes operators for comparison of field and contents.
Definition: searchrule.h:40
T findChild(const QString &name, Qt::FindChildOptions options) const const
KGuiItem reset()
The filter dialog.
This file is part of the KDE documentation.
Documentation copyright © 1996-2022 The KDE developers.
Generated on Sat Sep 24 2022 03:58:15 by doxygen 1.8.17 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.