File indexing completed on 2024-04-21 03:50:57

0001 /****************************************************************************
0002 **
0003 ** Copyright (C) 2016 by Sandro S. Andrade <sandroandrade@kde.org>
0004 **
0005 ** This program is free software; you can redistribute it and/or
0006 ** modify it under the terms of the GNU General Public License as
0007 ** published by the Free Software Foundation; either version 2 of
0008 ** the License or (at your option) version 3 or any later version
0009 ** accepted by the membership of KDE e.V. (or its successor approved
0010 ** by the membership of KDE e.V.), which shall act as a proxy
0011 ** defined in Section 14 of version 3 of the license.
0012 **
0013 ** This program is distributed in the hope that it will be useful,
0014 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
0015 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
0016 ** GNU General Public License for more details.
0017 **
0018 ** You should have received a copy of the GNU General Public License
0019 ** along with this program.  If not, see <http://www.gnu.org/licenses/>.
0020 **
0021 ****************************************************************************/
0022 
0023 #include "plugincontroller.h"
0024 
0025 #include "core.h"
0026 
0027 #if defined(Q_OS_ANDROID)
0028 #include "../plugins/csoundsoundcontroller/csoundsoundcontroller.h"
0029 #endif
0030 
0031 #include <interfaces/iplugin.h>
0032 #include <interfaces/isoundcontroller.h>
0033 
0034 #if !defined(Q_OS_ANDROID)
0035 #include <QPluginLoader>
0036 #include <KPluginMetaData>
0037 #endif
0038 
0039 #include <QDebug>
0040 
0041 namespace Minuet
0042 {
0043 PluginController::PluginController(QObject *parent) : IPluginController(parent)
0044 {
0045 #if !defined(Q_OS_ANDROID)
0046     m_plugins = KPluginMetaData::findPlugins(QStringLiteral("minuet"));
0047 #endif
0048 }
0049 
0050 PluginController::~PluginController()
0051 {
0052 #if !defined(Q_OS_ANDROID)
0053     const auto &loadedPlugins = m_loadedPlugins.values();
0054     qDeleteAll(loadedPlugins.begin(), loadedPlugins.end());
0055     m_loadedPlugins.clear();
0056 #endif
0057 }
0058 
0059 bool PluginController::initialize(Core *core)
0060 {
0061     m_errorString.clear();
0062 #if !defined(Q_OS_ANDROID)
0063     ISoundController *soundController = nullptr;
0064     foreach (const KPluginMetaData &pluginMetaData, m_plugins) {
0065         if (m_loadedPlugins.value(pluginMetaData)) {
0066             continue;
0067         }
0068 
0069         QPluginLoader loader(pluginMetaData.fileName());
0070         IPlugin *plugin = qobject_cast<IPlugin *>(loader.instance());
0071         if (plugin) {
0072             m_loadedPlugins.insert(pluginMetaData, plugin);
0073             if (!core->soundController()
0074                 && (soundController = qobject_cast<ISoundController *>(plugin))) {
0075                 qInfo() << "Setting soundcontroller to"
0076                         << soundController->metaObject()->className();
0077                 core->setSoundController(soundController);
0078             }
0079         }
0080     }
0081     if (!soundController) {
0082         m_errorString = QStringLiteral("Could not find a suitable SoundController plugin!");
0083         return false;
0084     }
0085 #else
0086     ISoundController *soundController = 0;
0087     if (!core->soundController() && (soundController = new CsoundSoundController)) {
0088         qInfo() << "Setting soundcontroller to" << soundController->metaObject()->className();
0089         core->setSoundController(soundController);
0090     }
0091 #endif
0092     return true;
0093 }
0094 
0095 QString PluginController::errorString() const
0096 {
0097     return m_errorString;
0098 }
0099 
0100 }
0101 
0102 #include "moc_plugincontroller.cpp"