| 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 "ServiceRegistry.h" |
| 19 | #include "PluginRegistry.h" |
| 20 | |
| 21 | Q_LOGGING_CATEGORY(PLUGIN_SERVICE, "dekko.plugman.service") |
| 22 | |
| 23 | ServiceRegistry::ServiceRegistry(QObject *parent) : QObject(parent) |
| 24 | { |
| 25 | connect(this, &ServiceRegistry::serviceKeyChanged, this, &ServiceRegistry::loadServices); |
| 26 | } |
| 27 | |
| 28 | ServiceRegistry::~ServiceRegistry() |
| 29 | { |
| 30 | clear(); |
| 31 | } |
| 32 | |
| 33 | void ServiceRegistry::setServiceKey(const QString &key) |
| 34 | { |
| 35 | m_key = key; |
| 36 | loadServices(); |
| 37 | } |
| 38 | |
| 39 | void ServiceRegistry::startServices() |
| 40 | { |
| 41 | for (auto service : m_services) { |
| 42 | service->start(); |
| 43 | } |
| 44 | } |
| 45 | |
| 46 | void ServiceRegistry::killServices() |
| 47 | { |
| 48 | for (auto service : m_services) { |
| 49 | service->stop(); |
| 50 | } |
| 51 | } |
| 52 | |
| 53 | void ServiceRegistry::clear() |
| 54 | { |
| 55 | killServices(); |
| 56 | qDeleteAll(m_services); |
| 57 | m_services.clear(); |
| 58 | } |
| 59 | |
| 60 | void ServiceRegistry::sendMessage(const QString &pluginId, const QString &key, const QVariant &value) |
| 61 | { |
| 62 | for (auto service : m_services) { |
| 63 | if (service->pluginId() == pluginId) { |
| 64 | service->messageReceived(key, value); |
| 65 | return; |
| 66 | } |
| 67 | } |
| 68 | } |
| 69 | |
| 70 | void ServiceRegistry::loadServices() |
| 71 | { |
| 72 | auto plugins = PluginRegistry::instance()->getByLocation(m_key); |
| 73 | for (auto plugin : plugins) { |
| 74 | qCDebug(PLUGIN_SERVICE) << "Examining Service plugin " << plugin->pluginId(); |
| 75 | auto servicePlugin = qobject_cast<ServicePlugin *>(plugin); |
| 76 | if (servicePlugin) { |
| 77 | qCDebug(PLUGIN_SERVICE) << "We have a service plugin!!!" << servicePlugin->location(); |
| 78 | m_services.append(servicePlugin); |
| 79 | } else { |
| 80 | qCWarning(PLUGIN_SERVICE) << plugin->pluginId() << "is not a service plugin"; |
| 81 | } |
| 82 | } |
| 83 | } |