| 1 | /* Copyright (C) 2017 Dan Chapman <[email protected]> |
| 2 | |
| 3 | This file is part of Dekko email client for Ubuntu devices |
| 4 | |
| 5 | This program is free software; you can redistribute it and/or |
| 6 | modify it under the terms of the GNU General Public License as |
| 7 | published by the Free Software Foundation; either version 2 of |
| 8 | the License or (at your option) version 3 |
| 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 |
| 13 | GNU 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. If not, see <http://www.gnu.org/licenses/>. |
| 17 | */ |
| 18 | #include "ListenerRegistry.h" |
| 19 | #include <QQmlEngine> |
| 20 | #include "PluginRegistry.h" |
| 21 | |
| 22 | Q_LOGGING_CATEGORY(PLUGIN_LISTENER, "dekko.plugman.listener") |
| 23 | |
| 24 | ListenerRegistry::ListenerRegistry(QObject *parent) : QObject(parent) |
| 25 | { |
| 26 | } |
| 27 | |
| 28 | QQmlListProperty<QQuickItem> ListenerRegistry::defaultListeners() |
| 29 | { |
| 30 | return QQmlListProperty<QQuickItem>(this, m_defaults); |
| 31 | } |
| 32 | |
| 33 | QQuickItem *ListenerRegistry::createListenerFromURl(const QString &url) |
| 34 | { |
| 35 | if (url.isEmpty()) { |
| 36 | qCWarning(PLUGIN_LISTENER) << "Invalid component url"; |
| 37 | return Q_NULLPTR; |
| 38 | } |
| 39 | auto engine = qmlEngine(this); |
| 40 | QQmlComponent itemComponent(engine, QUrl::fromLocalFile(url)); |
| 41 | if (itemComponent.isError()) { |
| 42 | for (auto error : itemComponent.errors()) { |
| 43 | qCDebug(PLUGIN_LISTENER) << "Failed loading plugin with error:"; |
| 44 | qCDebug(PLUGIN_LISTENER) << error.toString(); |
| 45 | } |
| 46 | return Q_NULLPTR; |
| 47 | } |
| 48 | return qobject_cast<QQuickItem *>(itemComponent.create(engine->contextForObject(this))); |
| 49 | } |
| 50 | |
| 51 | void ListenerRegistry::componentComplete() |
| 52 | { |
| 53 | auto plugins = PluginRegistry::instance()->getByLocation(QStringLiteral("Dekko::Listener")); |
| 54 | for (auto plugin : plugins) { |
| 55 | if (auto dp = qobject_cast<DekkoPlugin *>(plugin)) { |
| 56 | qCDebug(PLUGIN_LISTENER) << "Loading plugin: " << dp->pluginId(); |
| 57 | QQuickItem *item = createListenerFromURl(dp->component()); |
| 58 | if (item) { |
| 59 | m_plugins << item; |
| 60 | } |
| 61 | } |
| 62 | } |
| 63 | } |
| 64 |