| 1 | /* |
| 2 | * Copyright (C) 2021 Damir Porobic <[email protected]> |
| 3 | * |
| 4 | * This program is free software; you can redistribute it and/or modify |
| 5 | * it under the terms of the GNU Lesser General Public License as published by |
| 6 | * the Free Software Foundation; either version 2 of the License, or |
| 7 | * (at your option) any later version. |
| 8 | * |
| 9 | * This program is distributed in the hope that it will be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | * GNU General Public License for more details. |
| 13 | * |
| 14 | * You should have received a copy of the GNU Lesser General Public License |
| 15 | * along with this program; if not, write to the Free Software |
| 16 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, |
| 17 | * Boston, MA 02110-1301, USA. |
| 18 | */ |
| 19 | |
| 20 | #include "PluginManager.h" |
| 21 | |
| 22 | PluginManager::PluginManager(const QSharedPointer<IConfig> &config, const QSharedPointer<IPluginLoader> &loader, const QSharedPointer<ILogger> &logger) : |
| 23 | mConfig(config), |
| 24 | mLoader(loader), |
| 25 | mLogger(logger) |
| 26 | { |
| 27 | loadPlugins(); |
| 28 | |
| 29 | connect(mConfig.data(), &IConfig::pluginsChanged, this, &PluginManager::loadPlugins); |
| 30 | } |
| 31 | |
| 32 | bool PluginManager::isAvailable(PluginType type) const |
| 33 | { |
| 34 | return mPluginMap.contains(type); |
| 35 | } |
| 36 | |
| 37 | void PluginManager::loadPlugins() |
| 38 | { |
| 39 | auto pluginInfos = mConfig->pluginInfos(); |
| 40 | |
| 41 | for (const auto& pluginInfo : pluginInfos) { |
| 42 | auto plugin = QSharedPointer<QObject>(mLoader->load(pluginInfo.path())); |
| 43 | if(plugin.isNull()) { |
| 44 | mLogger->log(QString("Unable to load plugin %1 of type %2").arg(pluginInfo.path(), EnumTranslator::instance()->toString(pluginInfo.type()))); |
| 45 | } else { |
| 46 | mPluginMap[pluginInfo.type()] = plugin; |
| 47 | mPluginPathMap[pluginInfo.type()] = pluginInfo.path(); |
| 48 | } |
| 49 | } |
| 50 | } |
| 51 | |
| 52 | QSharedPointer<QObject> PluginManager::get(PluginType type) const |
| 53 | { |
| 54 | if(isAvailable(type)) { |
| 55 | return mPluginMap[type]; |
| 56 | } else { |
| 57 | mLogger->log(QString("Unavailable plugin requested %1").arg(EnumTranslator::instance()->toString(type))); |
| 58 | |
| 59 | return {}; |
| 60 | } |
| 61 | } |
| 62 | |
| 63 | QString PluginManager::getPath(PluginType type) const |
| 64 | { |
| 65 | if(isAvailable(type)) { |
| 66 | return mPluginPathMap[type]; |
| 67 | } else { |
| 68 | mLogger->log(QString("Unavailable plugin path requested %1").arg(EnumTranslator::instance()->toString(type))); |
| 69 | |
| 70 | return {}; |
| 71 | } |
| 72 | } |