Mailcommon

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

KDE's Doxygen guidelines are available online.