File indexing completed on 2024-11-17 04:51:15

0001 /*
0002 
0003   SPDX-FileCopyrightText: 2004 Ingo Kloecker <kloecker@kde.org>
0004 
0005   SPDX-License-Identifier: GPL-2.0-or-later
0006 */
0007 
0008 #include "rulewidgethandlermanager.h"
0009 #include "daterulewidgethandler.h"
0010 #include "encryptionwidgethandler.h"
0011 #include "headersrulerwidgethandler.h"
0012 #include "interfaces/rulewidgethandler.h"
0013 #include "messagerulewidgethandler.h"
0014 #include "numericdoublerulewidgethandler.h"
0015 #include "numericrulewidgethandler.h"
0016 #include "statusrulewidgethandler.h"
0017 #include "tagrulewidgethandler.h"
0018 #include "textrulerwidgethandler.h"
0019 
0020 #include <MessageViewer/Stl_Util>
0021 
0022 #include <QObject>
0023 #include <QStackedWidget>
0024 
0025 #include <algorithm>
0026 using std::for_each;
0027 using std::remove;
0028 
0029 using namespace MailCommon;
0030 
0031 MailCommon::RuleWidgetHandlerManager *MailCommon::RuleWidgetHandlerManager::self = nullptr;
0032 
0033 MailCommon::RuleWidgetHandlerManager::RuleWidgetHandlerManager()
0034 {
0035     registerHandler(new MailCommon::TagRuleWidgetHandler());
0036     registerHandler(new MailCommon::DateRuleWidgetHandler());
0037     registerHandler(new MailCommon::NumericRuleWidgetHandler());
0038     registerHandler(new MailCommon::StatusRuleWidgetHandler());
0039     registerHandler(new MailCommon::MessageRuleWidgetHandler());
0040     registerHandler(new MailCommon::NumericDoubleRuleWidgetHandler());
0041     registerHandler(new MailCommon::HeadersRuleWidgetHandler());
0042     registerHandler(new MailCommon::EncryptionWidgetHandler());
0043     // the TextRuleWidgetHandler is the fallback handler, so it has to be added
0044     // as last handler
0045     registerHandler(new MailCommon::TextRuleWidgetHandler());
0046 }
0047 
0048 MailCommon::RuleWidgetHandlerManager::~RuleWidgetHandlerManager()
0049 {
0050     for_each(mHandlers.begin(), mHandlers.end(), MessageViewer::DeleteAndSetToZero<RuleWidgetHandler>());
0051 }
0052 
0053 void MailCommon::RuleWidgetHandlerManager::setIsAkonadiSearch(bool isBalooSearch)
0054 {
0055     mIsBalooSearch = isBalooSearch;
0056 }
0057 
0058 void MailCommon::RuleWidgetHandlerManager::registerHandler(const RuleWidgetHandler *handler)
0059 {
0060     if (!handler) {
0061         return;
0062     }
0063     unregisterHandler(handler); // don't produce duplicates
0064     mHandlers.push_back(handler);
0065 }
0066 
0067 void MailCommon::RuleWidgetHandlerManager::unregisterHandler(const RuleWidgetHandler *handler)
0068 {
0069     // don't delete them, only remove them from the list!
0070     mHandlers.erase(remove(mHandlers.begin(), mHandlers.end(), handler), mHandlers.end());
0071 }
0072 
0073 namespace
0074 {
0075 /**
0076  * Returns the number of immediate children of parent with the given object name.
0077  * Used by RuleWidgetHandlerManager::createWidgets().
0078  */
0079 int childCount(const QObject *parent, const QString &objName)
0080 {
0081     const QObjectList list = parent->children();
0082     int count = 0;
0083     for (QObject *item : list) {
0084         if (item->objectName() == objName) {
0085             count++;
0086         }
0087     }
0088     return count;
0089 }
0090 }
0091 
0092 void MailCommon::RuleWidgetHandlerManager::createWidgets(QStackedWidget *functionStack, QStackedWidget *valueStack, const QObject *receiver) const
0093 {
0094     const_iterator end(mHandlers.constEnd());
0095     for (const_iterator it = mHandlers.constBegin(); it != end; ++it) {
0096         QWidget *w = nullptr;
0097         for (int i = 0; (w = (*it)->createFunctionWidget(i, functionStack, receiver, mIsBalooSearch)); ++i) {
0098             if (childCount(functionStack, w->objectName()) < 2) {
0099                 // there wasn't already a widget with this name, so add this widget
0100                 functionStack->addWidget(w);
0101             } else {
0102                 // there was already a widget with this name, so discard this widget
0103                 delete w;
0104                 w = nullptr;
0105             }
0106         }
0107         for (int i = 0; (w = (*it)->createValueWidget(i, valueStack, receiver)); ++i) {
0108             if (childCount(valueStack, w->objectName()) < 2) {
0109                 // there wasn't already a widget with this name, so add this widget
0110                 valueStack->addWidget(w);
0111             } else {
0112                 // there was already a widget with this name, so discard this widget
0113                 delete w;
0114                 w = nullptr;
0115             }
0116         }
0117     }
0118 }
0119 
0120 SearchRule::Function MailCommon::RuleWidgetHandlerManager::function(const QByteArray &field, const QStackedWidget *functionStack) const
0121 {
0122     const_iterator end(mHandlers.constEnd());
0123     for (const_iterator it = mHandlers.constBegin(); it != end; ++it) {
0124         const SearchRule::Function func = (*it)->function(field, functionStack);
0125         if (func != SearchRule::FuncNone) {
0126             return func;
0127         }
0128     }
0129     return SearchRule::FuncNone;
0130 }
0131 
0132 QString MailCommon::RuleWidgetHandlerManager::value(const QByteArray &field, const QStackedWidget *functionStack, const QStackedWidget *valueStack) const
0133 {
0134     const_iterator end(mHandlers.constEnd());
0135     for (const_iterator it = mHandlers.constBegin(); it != end; ++it) {
0136         const QString val = (*it)->value(field, functionStack, valueStack);
0137         if (!val.isEmpty()) {
0138             return val;
0139         }
0140     }
0141     return {};
0142 }
0143 
0144 QString MailCommon::RuleWidgetHandlerManager::prettyValue(const QByteArray &field, const QStackedWidget *functionStack, const QStackedWidget *valueStack) const
0145 {
0146     const_iterator end(mHandlers.constEnd());
0147     for (const_iterator it = mHandlers.constBegin(); it != end; ++it) {
0148         const QString val = (*it)->prettyValue(field, functionStack, valueStack);
0149         if (!val.isEmpty()) {
0150             return val;
0151         }
0152     }
0153     return {};
0154 }
0155 
0156 void MailCommon::RuleWidgetHandlerManager::reset(QStackedWidget *functionStack, QStackedWidget *valueStack) const
0157 {
0158     const_iterator end(mHandlers.constEnd());
0159     for (const_iterator it = mHandlers.constBegin(); it != end; ++it) {
0160         (*it)->reset(functionStack, valueStack);
0161     }
0162     update("", functionStack, valueStack);
0163 }
0164 
0165 void MailCommon::RuleWidgetHandlerManager::setRule(QStackedWidget *functionStack, QStackedWidget *valueStack, const SearchRule::Ptr rule) const
0166 {
0167     Q_ASSERT(rule);
0168     reset(functionStack, valueStack);
0169     const_iterator end(mHandlers.constEnd());
0170     for (const_iterator it = mHandlers.constBegin(); it != end; ++it) {
0171         if ((*it)->setRule(functionStack, valueStack, rule, mIsBalooSearch)) {
0172             return;
0173         }
0174     }
0175 }
0176 
0177 void MailCommon::RuleWidgetHandlerManager::update(const QByteArray &field, QStackedWidget *functionStack, QStackedWidget *valueStack) const
0178 {
0179     const_iterator end(mHandlers.constEnd());
0180     for (const_iterator it = mHandlers.constBegin(); it != end; ++it) {
0181         if ((*it)->update(field, functionStack, valueStack)) {
0182             return;
0183         }
0184     }
0185 }