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

KDE's Doxygen guidelines are available online.