File indexing completed on 2024-04-14 04:53:33

0001 /* This file is part of the KDE project
0002    Copyright (C) 2009 Collabora Ltd <info@collabora.co.uk>
0003     @author George Goldberg <george.goldberg@collabora.co.uk>
0004 
0005    This program is free software; you can redistribute it and/or
0006    modify it under the terms of the GNU General Public
0007    License as published by the Free Software Foundation; either
0008    version 2 of the License, or (at your option) any later version.
0009 
0010    This program is distributed in the hope that it will be useful,
0011    but WITHOUT ANY WARRANTY; without even the implied warranty of
0012    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
0013    General Public License for more details.
0014 
0015    You should have received a copy of the GNU General Public License
0016    along with this program; see the file COPYING.  If not, write to
0017    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
0018    Boston, MA 02110-1301, USA.
0019 */
0020 
0021 #include "eventsmanager.h"
0022 
0023 #include "eventsplugin.h"
0024 #include "krfbconfig.h"
0025 #include "rfbservermanager.h"
0026 #include "krfbdebug.h"
0027 
0028 #include <QGlobalStatic>
0029 
0030 #include <KPluginFactory>
0031 #include <KPluginMetaData>
0032 
0033 
0034 class EventsManagerStatic
0035 {
0036 public:
0037     EventsManager instance;
0038 };
0039 
0040 Q_GLOBAL_STATIC(EventsManagerStatic, eventsManagerStatic)
0041 
0042 EventsManager::EventsManager()
0043 {
0044     const QVector<KPluginMetaData> plugins = KPluginMetaData::findPlugins(QStringLiteral("krfb/events"), {}, KPluginMetaData::AllowEmptyMetaData);
0045     for (const KPluginMetaData &data : plugins) {
0046         const KPluginFactory::Result<EventsPlugin> result = KPluginFactory::instantiatePlugin<EventsPlugin>(data);
0047         if (result.plugin) {
0048             m_plugins.insert(data.pluginId(), result.plugin);
0049             qCDebug(KRFB) << "Loaded plugin with name " << data.pluginId();
0050         } else {
0051             qCDebug(KRFB) << "unable to load plugin for " << data.fileName() << result.errorString;
0052         }
0053     }
0054 }
0055 
0056 EventsManager::~EventsManager() = default;
0057 
0058 EventsManager *EventsManager::instance()
0059 {
0060     return &eventsManagerStatic->instance;
0061 }
0062 
0063 QSharedPointer<EventHandler> EventsManager::eventHandler()
0064 {
0065     for (auto it = m_plugins.cbegin(); it != m_plugins.constEnd(); it++) {
0066         QSharedPointer<EventHandler> eventHandler(it.value()->eventHandler());
0067         if (eventHandler) {
0068             eventHandler->setFrameBufferPlugin(RfbServerManager::instance()->framebuffer());
0069             return eventHandler;
0070         }
0071     }
0072 
0073     // No valid events plugin found.
0074     qCDebug(KRFB) << "No valid event handlers found. returning null.";
0075     return QSharedPointer<EventHandler>();
0076 }