aboutsummaryrefslogtreecommitdiff
path: root/src/plugin.h
blob: 4de340799f731b1b4c2b840c8dce000dca89aeb6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
/*
 * This file is part of smolbote. It's copyrighted by the contributors recorded
 * in the version control history of the file, available from its original
 * location: https://neueland.iserlohn-fortress.net/smolbote.hg
 *
 * SPDX-License-Identifier: GPL-3.0
 */

#ifndef SMOLBOTE_PLUGIN_H
#define SMOLBOTE_PLUGIN_H

#include <interfaces.h>
#include <QPluginLoader>
#include <QFileInfo>
#include <QDir>

inline Plugin loadPluginFromPath(const QString &path)
{
    Plugin p;
    QPluginLoader loader(path);

    if(loader.load()) {
#ifdef QT_DEBUG
        qDebug("Loading plugin: %s [ok]", qUtf8Printable(path));
#endif

        auto meta = loader.metaData().value("MetaData").toObject();
        p.name = meta.value("name").toString();
        p.author = meta.value("author").toString();
        p.shortcut = QKeySequence::fromString(meta.value("shortcut").toString());

        p.instance = loader.instance();

    } else {
        qDebug("Loading pluing: %s [failed]", qUtf8Printable(path));
        qDebug("%s", qUtf8Printable(loader.errorString()));
    }

    return p;
}

inline QVector<Plugin> loadPlugins(const QString &path)
{
    QVector<Plugin> list;

    // quit if there's nothing to load
    if(path.isEmpty())
        return list;

    // plugins can be a semicolon-separated list
    if(path.contains(';')) {
        auto pluginList = path.split(';');
        for(const auto &pluginPath : pluginList) {
            auto plugin = loadPluginFromPath(pluginPath);
            if(plugin.instance)
                list.append(plugin);
        }

        return list;
    }

    // check if path is path to a file or a folder
    QFileInfo location(path);

    if(!location.exists()) {
        qDebug("Plugin path doesn't exist.");
        return list;
    }

    if(location.isFile()) {
        // only load this one plugin
        auto p = loadPluginFromPath(location.absoluteFilePath());
        if(p.instance)
            list.append(p);

    } else if(location.isDir()) {
        // load all profiles from this directory
        const auto entries = QDir(location.absoluteFilePath()).entryInfoList(QDir::Files | QDir::Readable);
        for(const auto &f : entries) {
            auto p = loadPluginFromPath(f.absoluteFilePath());
            if(p.instance)
                list.append(p);
        }
#ifdef QT_DEBUG
    } else {
        qDebug("Path is neither file nor folder: %s", qUtf8Printable(path));
#endif
    }

    return list;
}

#endif // SMOLBOTE_PLUGIN_H