plugincontroller.cpp 3.46 KB
Newer Older
Sandro Andrade's avatar
Sandro Andrade committed
1
2
3
4
5
6
7
8
9
/****************************************************************************
**
** Copyright (C) 2016 by Sandro S. Andrade <sandroandrade@kde.org>
**
** This program is free software; you can redistribute it and/or
** modify it under the terms of the GNU General Public License as
** published by the Free Software Foundation; either version 2 of
** the License or (at your option) version 3 or any later version
** accepted by the membership of KDE e.V. (or its successor approved
10
** by the membership of KDE e.V.), which shall act as a proxy
Sandro Andrade's avatar
Sandro Andrade committed
11
12
13
14
15
16
17
18
19
20
21
22
** defined in Section 14 of version 3 of the license.
**
** This program is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
** GNU General Public License for more details.
**
** You should have received a copy of the GNU General Public License
** along with this program.  If not, see <http://www.gnu.org/licenses/>.
**
****************************************************************************/

23
#include "plugincontroller.h"
24

Sandro Andrade's avatar
Sandro Andrade committed
25
#include "core.h"
26

27
28
29
30
#if defined(Q_OS_ANDROID)
#include "../plugins/csoundsoundcontroller/csoundsoundcontroller.h"
#endif

31
#include <interfaces/iplugin.h>
32
#include <interfaces/isoundcontroller.h>
33

34
#if !defined(Q_OS_ANDROID)
Sandro Andrade's avatar
Sandro Andrade committed
35
#include <KPluginLoader>
36
#endif
37
38
39

#include <QDebug>

40
41
42
43
44
namespace Minuet
{
PluginController::PluginController(QObject *parent)
    : IPluginController(parent)
{
45
#if !defined(Q_OS_ANDROID)
46
47
    m_plugins = KPluginLoader::findPlugins(QStringLiteral("minuet"), [&](const KPluginMetaData &meta) {
        if (!meta.serviceTypes().contains(QStringLiteral("Minuet/Plugin"))) {
48
49
50
            qWarning() << "Plugin" << meta.fileName()
                       << "is installed into the minuet plugin directory, but does not have"
                          " \"Minuet/Plugin\" set as the service type. This plugin will not be loaded.";
51
52
53
54
            return false;
        }
        return true;
    });
55
#endif
56
}
57

58
59
PluginController::~PluginController()
{
60
#if !defined(Q_OS_ANDROID)
61
62
    qDeleteAll(m_loadedPlugins.values().begin(), m_loadedPlugins.values().end());
    m_loadedPlugins.clear();
63
#endif
64
}
65

66
bool PluginController::initialize(Core *core)
67
{
68
    m_errorString.clear();
69
#if !defined(Q_OS_ANDROID)
70
    ISoundController *soundController = 0;
71
    foreach (const KPluginMetaData &pluginMetaData, m_plugins) {
72
73
74
75
76
        if (m_loadedPlugins.value(pluginMetaData))
            continue;

        KPluginLoader loader(pluginMetaData.fileName());
        IPlugin *plugin = qobject_cast<IPlugin *>(loader.instance());
77
        if (plugin) {
78
            m_loadedPlugins.insert(pluginMetaData, plugin);
79
            if (!core->soundController() && (soundController = qobject_cast<ISoundController *>(plugin))) {
80
                qInfo() << "Setting soundcontroller to" << soundController->metaObject()->className();
81
                core->setSoundController(soundController);
82
83
            }
        }
84
    }
85
86
87
88
    if (!soundController) {
        m_errorString = QStringLiteral("Could not find a suitable SoundController plugin!");
        return false;
    }
89
#else
90
91
92
93
94
    ISoundController *soundController = 0;
    if (!core->soundController() && (soundController = new CsoundSoundController)) {
        qInfo() << "Setting soundcontroller to" << soundController->metaObject()->className();
        core->setSoundController(soundController);
    }
95
#endif
96
97
98
    return true;
}

99
100
101
102
103
QString PluginController::errorString() const
{
    return m_errorString;
}

Sandro Andrade's avatar
Sandro Andrade committed
104
}