| 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 "PluginRegistry.h" |
| 19 | #include <QPointer> |
| 20 | #include <QDebug> |
| 21 | #include <SnapStandardPaths.h> |
| 22 | |
| 23 | Q_LOGGING_CATEGORY(PLUGIN_REGISTRY, "dekko.plugman.registry") |
| 24 | |
| 25 | PluginRegistry::PluginRegistry(QObject *parent) : QObject(parent), |
| 26 | m_loader(new PluginLoader), |
| 27 | m_regConf(SnapStandardPaths::writableLocation(SnapStandardPaths::AppConfigLocation) + |
| 28 | QStringLiteral("/PluginRegistry.conf"), QSettings::IniFormat) |
| 29 | { |
| 30 | m_regConf.setValue("version", "0.1"); |
| 31 | } |
| 32 | |
| 33 | PluginRegistry::~PluginRegistry() |
| 34 | { |
| 35 | qDeleteAll(m_registry); |
| 36 | m_registry.clear(); |
| 37 | } |
| 38 | |
| 39 | static QPointer<PluginRegistry> s_registry; |
| 40 | PluginRegistry *PluginRegistry::instance() |
| 41 | { |
| 42 | if (s_registry.isNull()) { |
| 43 | qCDebug(PLUGIN_REGISTRY) << "Creating instance"; |
| 44 | s_registry = new PluginRegistry(); |
| 45 | } |
| 46 | return s_registry; |
| 47 | } |
| 48 | |
| 49 | Registry PluginRegistry::registry() const |
| 50 | { |
| 51 | return m_registry; |
| 52 | } |
| 53 | |
| 54 | bool PluginRegistry::supportsLocation(const QString &location) |
| 55 | { |
| 56 | return m_locations.contains(location); |
| 57 | } |
| 58 | |
| 59 | QStringList PluginRegistry::pluginLocations() const |
| 60 | { |
| 61 | return m_locations; |
| 62 | } |
| 63 | |
| 64 | void PluginRegistry::setPluginLocations(const QStringList &locations) |
| 65 | { |
| 66 | m_locations = locations; |
| 67 | } |
| 68 | |
| 69 | void PluginRegistry::loadPlugins(const QStringList &paths) |
| 70 | { |
| 71 | qDeleteAll(m_registry); |
| 72 | m_registry.clear(); |
| 73 | if (!m_loader.isNull()) { |
| 74 | m_loader->setPluginPaths(paths); |
| 75 | m_loader->loadPlugins(m_registry); |
| 76 | updateRegistryConf(); |
| 77 | } |
| 78 | } |
| 79 | |
| 80 | QList<PluginInfo *> PluginRegistry::getAllPlugins(const bool enabledOnly) |
| 81 | { |
| 82 | if (enabledOnly) { |
| 83 | QList<PluginInfo *> plugins; |
| 84 | for (auto plugin : m_registry.values()) { |
| 85 | m_regConf.beginGroup(plugin->pluginId()); |
| 86 | const bool enabled = m_regConf.value(QStringLiteral("enabled"), true).toBool(); |
| 87 | m_regConf.endGroup(); |
| 88 | if (enabled) { |
| 89 | plugins << plugin; |
| 90 | } |
| 91 | } |
| 92 | return plugins; |
| 93 | } |
| 94 | return m_registry.values(); |
| 95 | } |
| 96 | |
| 97 | QList<PluginInfo *> PluginRegistry::getByLocation(const QString &key, const bool enabledOnly) |
| 98 | { |
| 99 | if (!m_locations.contains(key)) { |
| 100 | qCWarning(PLUGIN_REGISTRY) << "Location not supported: " << key; |
| 101 | return QList<PluginInfo *>(); |
| 102 | } |
| 103 | if (enabledOnly) { |
| 104 | qCDebug(PLUGIN_REGISTRY) << "Getting all enabled plugins"; |
| 105 | QList<PluginInfo *> plugins; |
| 106 | for (auto plugin : m_registry.values(key)) { |
| 107 | m_regConf.beginGroup(plugin->pluginId()); |
| 108 | bool enabled = m_regConf.value(QStringLiteral("enabled"), true).toBool(); |
| 109 | m_regConf.endGroup(); |
| 110 | if (enabled) { |
| 111 | qCDebug(PLUGIN_REGISTRY) << "Plugin enabled: " << plugin->pluginId(); |
| 112 | enablePlugin(plugin->pluginId()); |
| 113 | plugins << plugin; |
| 114 | } |
| 115 | } |
| 116 | return plugins; |
| 117 | } |
| 118 | return m_registry.values(key); |
| 119 | } |
| 120 | |
| 121 | void PluginRegistry::enablePlugin(const QString &id) |
| 122 | { |
| 123 | m_regConf.beginGroup(id); |
| 124 | m_regConf.setValue("enabled", true); |
| 125 | m_regConf.endGroup(); |
| 126 | } |
| 127 | |
| 128 | void PluginRegistry::disablePlugin(const QString &id) |
| 129 | { |
| 130 | m_regConf.beginGroup(id); |
| 131 | m_regConf.setValue("enabled", false); |
| 132 | m_regConf.endGroup(); |
| 133 | } |
| 134 | |
| 135 | void PluginRegistry::updateRegistryConf() |
| 136 | { |
| 137 | const QStringList keys = m_regConf.childGroups(); |
| 138 | // we enable unknown plugins by default |
| 139 | for (auto plugin : m_registry.values()) { |
| 140 | if (!keys.contains(plugin->pluginId())) { |
| 141 | qCDebug(PLUGIN_REGISTRY) << plugin->pluginId() << "Not in" << keys; |
| 142 | enablePlugin(plugin->pluginId()); |
| 143 | } |
| 144 | } |
| 145 | m_regConf.sync(); |
| 146 | } |
| 147 |