Mailcommon

rulewidgethandlermanager.cpp
1 /*
2 
3  SPDX-FileCopyrightText: 2004 Ingo Kloecker <[email protected]>
4 
5  SPDX-License-Identifier: GPL-2.0-or-later
6 */
7 
8 #include "rulewidgethandlermanager.h"
9 #include "daterulewidgethandler.h"
10 #include "encryptionwidgethandler.h"
11 #include "headersrulerwidgethandler.h"
12 #include "interfaces/rulewidgethandler.h"
13 #include "messagerulewidgethandler.h"
14 #include "numericdoublerulewidgethandler.h"
15 #include "numericrulewidgethandler.h"
16 #include "statusrulewidgethandler.h"
17 #include "tagrulewidgethandler.h"
18 #include "textrulerwidgethandler.h"
19 
20 #include <MessageViewer/Stl_Util>
21 
22 #include <QObject>
23 #include <QStackedWidget>
24 
25 #include <algorithm>
26 using std::for_each;
27 using std::remove;
28 
29 using namespace MailCommon;
30 
31 MailCommon::RuleWidgetHandlerManager *MailCommon::RuleWidgetHandlerManager::self = nullptr;
32 
33 MailCommon::RuleWidgetHandlerManager::RuleWidgetHandlerManager()
34 {
35  registerHandler(new MailCommon::TagRuleWidgetHandler());
36  registerHandler(new MailCommon::DateRuleWidgetHandler());
37  registerHandler(new MailCommon::NumericRuleWidgetHandler());
38  registerHandler(new MailCommon::StatusRuleWidgetHandler());
39  registerHandler(new MailCommon::MessageRuleWidgetHandler());
40  registerHandler(new MailCommon::NumericDoubleRuleWidgetHandler());
41  registerHandler(new MailCommon::HeadersRuleWidgetHandler());
42  registerHandler(new MailCommon::EncryptionWidgetHandler());
43  // the TextRuleWidgetHandler is the fallback handler, so it has to be added
44  // as last handler
45  registerHandler(new MailCommon::TextRuleWidgetHandler());
46 }
47 
48 MailCommon::RuleWidgetHandlerManager::~RuleWidgetHandlerManager()
49 {
50  for_each(mHandlers.begin(), mHandlers.end(), MessageViewer::DeleteAndSetToZero<RuleWidgetHandler>());
51 }
52 
53 void MailCommon::RuleWidgetHandlerManager::setIsAkonadiSearch(bool isBalooSearch)
54 {
55  mIsBalooSearch = isBalooSearch;
56 }
57 
58 void MailCommon::RuleWidgetHandlerManager::registerHandler(const RuleWidgetHandler *handler)
59 {
60  if (!handler) {
61  return;
62  }
63  unregisterHandler(handler); // don't produce duplicates
64  mHandlers.push_back(handler);
65 }
66 
67 void MailCommon::RuleWidgetHandlerManager::unregisterHandler(const RuleWidgetHandler *handler)
68 {
69  // don't delete them, only remove them from the list!
70  mHandlers.erase(remove(mHandlers.begin(), mHandlers.end(), handler), mHandlers.end());
71 }
72 
73 namespace
74 {
75 /**
76  * Returns the number of immediate children of parent with the given object name.
77  * Used by RuleWidgetHandlerManager::createWidgets().
78  */
79 int childCount(const QObject *parent, const QString &objName)
80 {
81  const QObjectList list = parent->children();
82  int count = 0;
83  for (QObject *item : list) {
84  if (item->objectName() == objName) {
85  count++;
86  }
87  }
88  return count;
89 }
90 }
91 
92 void MailCommon::RuleWidgetHandlerManager::createWidgets(QStackedWidget *functionStack, QStackedWidget *valueStack, const QObject *receiver) const
93 {
94  const_iterator end(mHandlers.constEnd());
95  for (const_iterator it = mHandlers.constBegin(); it != end; ++it) {
96  QWidget *w = nullptr;
97  for (int i = 0; (w = (*it)->createFunctionWidget(i, functionStack, receiver, mIsBalooSearch)); ++i) {
98  if (childCount(functionStack, w->objectName()) < 2) {
99  // there wasn't already a widget with this name, so add this widget
100  functionStack->addWidget(w);
101  } else {
102  // there was already a widget with this name, so discard this widget
103  delete w;
104  w = nullptr;
105  }
106  }
107  for (int i = 0; (w = (*it)->createValueWidget(i, valueStack, receiver)); ++i) {
108  if (childCount(valueStack, w->objectName()) < 2) {
109  // there wasn't already a widget with this name, so add this widget
110  valueStack->addWidget(w);
111  } else {
112  // there was already a widget with this name, so discard this widget
113  delete w;
114  w = nullptr;
115  }
116  }
117  }
118 }
119 
120 SearchRule::Function MailCommon::RuleWidgetHandlerManager::function(const QByteArray &field, const QStackedWidget *functionStack) const
121 {
122  const_iterator end(mHandlers.constEnd());
123  for (const_iterator it = mHandlers.constBegin(); it != end; ++it) {
124  const SearchRule::Function func = (*it)->function(field, functionStack);
125  if (func != SearchRule::FuncNone) {
126  return func;
127  }
128  }
129  return SearchRule::FuncNone;
130 }
131 
132 QString MailCommon::RuleWidgetHandlerManager::value(const QByteArray &field, const QStackedWidget *functionStack, const QStackedWidget *valueStack) const
133 {
134  const_iterator end(mHandlers.constEnd());
135  for (const_iterator it = mHandlers.constBegin(); it != end; ++it) {
136  const QString val = (*it)->value(field, functionStack, valueStack);
137  if (!val.isEmpty()) {
138  return val;
139  }
140  }
141  return {};
142 }
143 
144 QString MailCommon::RuleWidgetHandlerManager::prettyValue(const QByteArray &field, const QStackedWidget *functionStack, const QStackedWidget *valueStack) const
145 {
146  const_iterator end(mHandlers.constEnd());
147  for (const_iterator it = mHandlers.constBegin(); it != end; ++it) {
148  const QString val = (*it)->prettyValue(field, functionStack, valueStack);
149  if (!val.isEmpty()) {
150  return val;
151  }
152  }
153  return {};
154 }
155 
156 void MailCommon::RuleWidgetHandlerManager::reset(QStackedWidget *functionStack, QStackedWidget *valueStack) const
157 {
158  const_iterator end(mHandlers.constEnd());
159  for (const_iterator it = mHandlers.constBegin(); it != end; ++it) {
160  (*it)->reset(functionStack, valueStack);
161  }
162  update("", functionStack, valueStack);
163 }
164 
165 void MailCommon::RuleWidgetHandlerManager::setRule(QStackedWidget *functionStack, QStackedWidget *valueStack, const SearchRule::Ptr rule) const
166 {
167  Q_ASSERT(rule);
168  reset(functionStack, valueStack);
169  const_iterator end(mHandlers.constEnd());
170  for (const_iterator it = mHandlers.constBegin(); it != end; ++it) {
171  if ((*it)->setRule(functionStack, valueStack, rule, mIsBalooSearch)) {
172  return;
173  }
174  }
175 }
176 
177 void MailCommon::RuleWidgetHandlerManager::update(const QByteArray &field, QStackedWidget *functionStack, QStackedWidget *valueStack) const
178 {
179  const_iterator end(mHandlers.constEnd());
180  for (const_iterator it = mHandlers.constBegin(); it != end; ++it) {
181  if ((*it)->update(field, functionStack, valueStack)) {
182  return;
183  }
184  }
185 }
std::shared_ptr< SearchRule > Ptr
Defines a pointer to a search rule.
Definition: searchrule.h:29
KIOFILEWIDGETS_EXPORT QStringList list(const QString &fileClass)
bool isEmpty() const const
KGuiItem remove()
Function
Describes operators for comparison of field and contents.
Definition: searchrule.h:40
An interface to filter/search rule widget handlers.
Singleton to manage the list of RuleWidgetHandlers.
void update(Part *part, const QByteArray &data, qint64 dataSize)
KGuiItem reset()
const QList< QKeySequence > & end()
const QObjectList & children() const const
The filter dialog.
int addWidget(QWidget *widget)
This file is part of the KDE documentation.
Documentation copyright © 1996-2023 The KDE developers.
Generated on Tue Sep 26 2023 03:56:33 by doxygen 1.8.17 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.