PluginManager.cpp 1.58 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
/*  This file was part of the KDE libraries

    SPDX-FileCopyrightText: 2021 Tomaz Canabrava <tcanabrava@kde.org>

    SPDX-License-Identifier: GPL-2.0-or-later
*/

#include "PluginManager.h"

#include "IKonsolePlugin.h"
#include "MainWindow.h"

Nicolas Fella's avatar
Nicolas Fella committed
13
#include <KPluginLoader>
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#include <KPluginMetaData>

namespace Konsole {

struct PluginManagerPrivate {
    std::vector<IKonsolePlugin*> plugins;
};

PluginManager::PluginManager()
: d(std::make_unique<PluginManagerPrivate>())
{
}

PluginManager::~PluginManager()
{
    qDeleteAll(d->plugins);
}

void PluginManager::loadAllPlugins()
{
    QVector<KPluginMetaData> pluginMetaData = KPluginLoader::findPlugins(QStringLiteral("konsoleplugins"));
    for(const auto &metaData : pluginMetaData) {
        KPluginLoader pluginLoader(metaData.fileName());
        KPluginFactory *factory = pluginLoader.factory();
        if (!factory) {
            continue;
        }

        auto *plugin = factory->create<IKonsolePlugin>();
        if (!plugin) {
            continue;
        }

        d->plugins.push_back(plugin);
    }
}

void PluginManager::registerMainWindow(Konsole::MainWindow* window)
{
    window->unplugActionList(QStringLiteral("plugin-submenu"));

    QList<QAction*> internalPluginSubmenus;
    for (auto *plugin : d->plugins) {
        plugin->addMainWindow(window);
        internalPluginSubmenus.append(plugin->menuBarActions(window));
        window->addPlugin(plugin);
    }

    window->plugActionList(QStringLiteral("plugin-submenu"), internalPluginSubmenus);
}

std::vector<IKonsolePlugin*> PluginManager::plugins() const
{
    return d->plugins;
}

}