| 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 "ActionRegistry.h" |
| 19 | #include <QtQml> |
| 20 | #include <QQmlError> |
| 21 | #include <QQmlEngine> |
| 22 | #include <QQmlComponent> |
| 23 | #include "PluginRegistry.h" |
| 24 | |
| 25 | Q_LOGGING_CATEGORY(PLUGIN_ACTION, "dekko.plugman.action") |
| 26 | |
| 27 | ActionRegistry::ActionRegistry(QObject *parent) : QObject(parent) |
| 28 | { |
| 29 | connect(this, &ActionRegistry::locationChanged, this, &ActionRegistry::loadActions); |
| 30 | } |
| 31 | |
| 32 | ActionRegistry::~ActionRegistry() |
| 33 | { |
| 34 | qDeleteAll(m_actions); |
| 35 | } |
| 36 | |
| 37 | QList<QObject *> ActionRegistry::actions() const |
| 38 | { |
| 39 | return m_actions; |
| 40 | } |
| 41 | |
| 42 | QString ActionRegistry::location() const |
| 43 | { |
| 44 | return m_location; |
| 45 | } |
| 46 | |
| 47 | QQmlListProperty<QObject> ActionRegistry::defaultActions() |
| 48 | { |
| 49 | return QQmlListProperty<QObject>(this, m_defaults); |
| 50 | } |
| 51 | |
| 52 | void ActionRegistry::setLocation(QString location) |
| 53 | { |
| 54 | if (m_location == location) |
| 55 | return; |
| 56 | |
| 57 | m_location = location; |
| 58 | emit locationChanged(location); |
| 59 | } |
| 60 | |
| 61 | void ActionRegistry::loadActions() |
| 62 | { |
| 63 | qCDebug(PLUGIN_ACTION) << "Loading actions for: " << m_location; |
| 64 | if (!m_actions.isEmpty()) { |
| 65 | qDeleteAll(m_actions); |
| 66 | m_actions.clear(); |
| 67 | } |
| 68 | m_actions << m_defaults; |
| 69 | auto plugins = PluginRegistry::instance()->getByLocation(m_location); |
| 70 | for (auto plugin : plugins) { |
| 71 | if (auto dp = qobject_cast<DekkoPlugin *>(plugin)) { |
| 72 | PluginIncubator *incubator = new PluginIncubator(this); |
| 73 | connect(incubator, &PluginIncubator::objectReady, this, &ActionRegistry::finishLoading); |
| 74 | connect(incubator, &PluginIncubator::error, this, &ActionRegistry::handleError); |
| 75 | incubator->setSourceUrl(qmlEngine(this), QUrl::fromLocalFile(dp->component())); |
| 76 | m_incubators << incubator; |
| 77 | } |
| 78 | } |
| 79 | emit actionsChanged(m_actions); |
| 80 | } |
| 81 | |
| 82 | void ActionRegistry::finishLoading() |
| 83 | { |
| 84 | PluginIncubator *incubator = qobject_cast<PluginIncubator *>(sender()); |
| 85 | if (incubator->status() == QQmlIncubator::Ready) { |
| 86 | QObject *action = incubator->object(); |
| 87 | qCDebug(PLUGIN_ACTION) << "Finished incubating"; |
| 88 | m_actions << action; |
| 89 | emit actionsChanged(m_actions); |
| 90 | qCDebug(PLUGIN_ACTION) << "Action loaded for " << m_location; |
| 91 | } |
| 92 | } |
| 93 | |
| 94 | void ActionRegistry::handleError() |
| 95 | { |
| 96 | PluginIncubator *incubator = qobject_cast<PluginIncubator *>(sender()); |
| 97 | for (auto error : incubator->errors()) { |
| 98 | qCDebug(PLUGIN_ACTION) << "Incubator Error: " << error.toString(); |
| 99 | } |
| 100 | } |
| 101 |