File indexing completed on 2025-01-26 04:47:11

0001 /*
0002    SPDX-FileCopyrightText: 2016-2024 Laurent Montel <montel@kde.org>
0003 
0004    SPDX-License-Identifier: GPL-2.0-or-later
0005 */
0006 
0007 #include "urlhandlerwebenginemanager.h"
0008 #include "urlhandlerwebengine.h"
0009 
0010 #include "articleviewer-ng/webengine/articleviewerwebengine.h"
0011 
0012 using std::remove;
0013 
0014 using namespace Akregator;
0015 
0016 URLHandlerWebEngineManager *URLHandlerWebEngineManager::self = nullptr;
0017 
0018 URLHandlerWebEngineManager::URLHandlerWebEngineManager(QObject *parent)
0019     : QObject(parent)
0020 {
0021     registerHandler(new AkregatorConfigHandler());
0022     registerHandler(new MailToURLHandlerWebEngine());
0023     registerHandler(new ActionURLHandlerWebEngine());
0024 }
0025 
0026 URLHandlerWebEngineManager::~URLHandlerWebEngineManager() = default;
0027 
0028 URLHandlerWebEngineManager *URLHandlerWebEngineManager::instance()
0029 {
0030     if (!self) {
0031         self = new URLHandlerWebEngineManager();
0032     }
0033     return self;
0034 }
0035 
0036 void URLHandlerWebEngineManager::registerHandler(const URLHandlerWebEngine *handler)
0037 {
0038     if (!handler) {
0039         return;
0040     }
0041     unregisterHandler(handler); // don't produce duplicates
0042     mHandlers.push_back(handler);
0043 }
0044 
0045 void URLHandlerWebEngineManager::unregisterHandler(const URLHandlerWebEngine *handler)
0046 {
0047     // don't delete them, only remove them from the list!
0048     mHandlers.erase(remove(mHandlers.begin(), mHandlers.end(), handler), mHandlers.end());
0049 }
0050 
0051 bool URLHandlerWebEngineManager::handleClick(const QUrl &url, ArticleViewerWebEngine *w) const
0052 {
0053     HandlerList::const_iterator end(mHandlers.constEnd());
0054     for (HandlerList::const_iterator it = mHandlers.constBegin(); it != end; ++it) {
0055         if ((*it)->handleClick(url, w)) {
0056             return true;
0057         }
0058     }
0059     return false;
0060 }
0061 
0062 bool URLHandlerWebEngineManager::handleContextMenuRequest(const QUrl &url, const QPoint &p, ArticleViewerWebEngine *w) const
0063 {
0064     HandlerList::const_iterator end(mHandlers.constEnd());
0065     for (HandlerList::const_iterator it = mHandlers.constBegin(); it != end; ++it) {
0066         if ((*it)->handleContextMenuRequest(url, p, w)) {
0067             return true;
0068         }
0069     }
0070     return false;
0071 }
0072 
0073 QString URLHandlerWebEngineManager::statusBarMessage(const QUrl &url, ArticleViewerWebEngine *w) const
0074 {
0075     HandlerList::const_iterator end(mHandlers.constEnd());
0076     for (HandlerList::const_iterator it = mHandlers.constBegin(); it != end; ++it) {
0077         const QString msg = (*it)->statusBarMessage(url, w);
0078         if (!msg.isEmpty()) {
0079             return msg;
0080         }
0081     }
0082     return {};
0083 }
0084 
0085 #include "moc_urlhandlerwebenginemanager.cpp"