Changeset View
Changeset View
Standalone View
Standalone View
krfb/eventsmanager.cpp
- This file was added.
1 | /* This file is part of the KDE project | ||||
---|---|---|---|---|---|
2 | Copyright (C) 2009 Collabora Ltd <info@collabora.co.uk> | ||||
3 | @author George Goldberg <george.goldberg@collabora.co.uk> | ||||
4 | | ||||
5 | This program is free software; you can redistribute it and/or | ||||
6 | modify it under the terms of the GNU General Public | ||||
7 | License as published by the Free Software Foundation; either | ||||
8 | version 2 of the License, or (at your option) any later version. | ||||
9 | | ||||
10 | This program is distributed in the hope that it will be useful, | ||||
11 | but WITHOUT ANY WARRANTY; without even the implied warranty of | ||||
12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||||
13 | General Public License for more details. | ||||
14 | | ||||
15 | You should have received a copy of the GNU General Public License | ||||
16 | along with this program; see the file COPYING. If not, write to | ||||
17 | the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, | ||||
18 | Boston, MA 02110-1301, USA. | ||||
19 | */ | ||||
20 | | ||||
21 | #include "eventsmanager.h" | ||||
22 | | ||||
23 | #include "eventsplugin.h" | ||||
24 | #include "krfbconfig.h" | ||||
25 | #include "rfbservermanager.h" | ||||
26 | | ||||
27 | #include <QDebug> | ||||
28 | #include <QGlobalStatic> | ||||
29 | | ||||
30 | #include <KPluginFactory> | ||||
31 | #include <KPluginLoader> | ||||
32 | #include <KPluginMetaData> | ||||
33 | | ||||
34 | #include <QtCore/QSharedPointer> | ||||
35 | | ||||
36 | class EventsManagerStatic | ||||
37 | { | ||||
38 | public: | ||||
39 | EventsManager instance; | ||||
40 | }; | ||||
41 | | ||||
42 | Q_GLOBAL_STATIC(EventsManagerStatic, eventsManagerStatic) | ||||
43 | | ||||
44 | EventsManager::EventsManager() | ||||
45 | { | ||||
46 | //qDebug(); | ||||
47 | | ||||
48 | loadPlugins(); | ||||
49 | } | ||||
50 | | ||||
51 | EventsManager::~EventsManager() | ||||
52 | { | ||||
53 | //qDebug(); | ||||
54 | } | ||||
55 | | ||||
56 | EventsManager *EventsManager::instance() | ||||
57 | { | ||||
58 | //qDebug(); | ||||
59 | | ||||
60 | return &eventsManagerStatic->instance; | ||||
61 | } | ||||
62 | | ||||
63 | void EventsManager::loadPlugins() | ||||
64 | { | ||||
65 | //qDebug(); | ||||
66 | | ||||
67 | const QVector<KPluginMetaData> plugins = KPluginLoader::findPlugins(QStringLiteral("krfb"), [](const KPluginMetaData & md) { | ||||
68 | return md.serviceTypes().contains(QStringLiteral("krfb/events")); | ||||
69 | }); | ||||
70 | | ||||
71 | QVectorIterator<KPluginMetaData> i(plugins); | ||||
72 | i.toBack(); | ||||
73 | QSet<QString> unique; | ||||
74 | while (i.hasPrevious()) { | ||||
75 | KPluginMetaData data = i.previous(); | ||||
76 | // only load plugins once, even if found multiple times! | ||||
77 | if (unique.contains(data.name())) | ||||
78 | continue; | ||||
79 | KPluginFactory *factory = KPluginLoader(data.fileName()).factory(); | ||||
80 | | ||||
81 | if (!factory) { | ||||
82 | qDebug() << "KPluginFactory could not load the plugin:" << data.fileName(); | ||||
83 | } else { | ||||
84 | qDebug() << "found plugin at " << data.fileName(); | ||||
85 | } | ||||
86 | | ||||
87 | EventsPlugin *plugin = factory->create<EventsPlugin>(this); | ||||
88 | if (plugin) { | ||||
89 | m_plugins.insert(data.pluginId(), plugin); | ||||
90 | qDebug() << "Loaded plugin with name " << data.pluginId(); | ||||
91 | } else { | ||||
92 | qDebug() << "unable to load plugin for " << data.fileName(); | ||||
93 | } | ||||
94 | unique.insert (data.name()); | ||||
95 | } | ||||
96 | } | ||||
97 | | ||||
98 | QSharedPointer<EventHandler> EventsManager::eventHandler() | ||||
99 | { | ||||
100 | QMap<QString, EventsPlugin *>::const_iterator iter = m_plugins.constBegin(); | ||||
101 | | ||||
102 | while (iter != m_plugins.constEnd()) { | ||||
103 | | ||||
104 | QSharedPointer<EventHandler> eventHandler(iter.value()->eventHandler()); | ||||
105 | | ||||
106 | if (eventHandler) { | ||||
107 | eventHandler->setFrameBufferPlugin(RfbServerManager::instance()->framebuffer()); | ||||
108 | return eventHandler; | ||||
109 | } | ||||
110 | | ||||
111 | ++iter; | ||||
112 | } | ||||
113 | | ||||
114 | // No valid events plugin found. | ||||
115 | qDebug() << "No valid event handlers found. returning null."; | ||||
116 | return QSharedPointer<EventHandler>(); | ||||
117 | } | ||||
118 | | ||||
119 | | ||||
120 | #include "eventsmanager.moc" |