aboutsummaryrefslogtreecommitdiff
path: root/lib/about/aboutplugin.cpp
blob: b345b75c3e1055b49091fa73107f03c2735ca9ae (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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
/*
 * 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/gitea/aqua/smolbote
 *
 * SPDX-License-Identifier: GPL-3.0
 */

#include "aboutplugin.h"
#include "ui_aboutplugin.h"
#include <QJsonArray>
#include <QPluginLoader>

QTreeWidgetItem *createItem(const QString &key, const QJsonValue &json, QTreeWidgetItem *parent)
{
    auto *item = new QTreeWidgetItem(parent, { key, QLatin1Literal("---") });

    switch(json.type()) {
    case QJsonValue::Bool:
        item->setText(1, json.toBool() ? QLatin1Literal("true") : QLatin1Literal("false"));
        break;

    case QJsonValue::Double:
        item->setText(1, QString::number(json.toDouble()));
        break;

    case QJsonValue::String:
        item->setText(1, json.toString());
        break;

    case QJsonValue::Array:
        item->setText(1, QString());
        for(const auto &v : json.toArray()) {
            createItem(QString(), v, item);
        }
        break;

    case QJsonValue::Object:
        item->setText(1, QString());
        for(const QString &k : json.toObject().keys()) {
            createItem(k, json.toObject()[k], item);
        }
        break;

    case QJsonValue::Null:
        item->setText(1, QLatin1Literal("null"));
        break;

    case QJsonValue::Undefined:
        item->setText(1, QLatin1Literal("undefined"));
        break;
    }

    return item;
}

AboutPluginDialog::AboutPluginDialog(QPluginLoader *loader, QWidget *parent)
    : QDialog(parent)
    , ui(new Ui::AboutPluginDialog)
{
    setAttribute(Qt::WA_DeleteOnClose, true);
    ui->setupUi(this);

    // load button icon
    {
        QIcon load_icon;
        load_icon.addPixmap(style()->standardPixmap(QStyle::SP_MediaPlay), QIcon::Normal, QIcon::On);
        load_icon.addPixmap(style()->standardPixmap(QStyle::SP_MediaStop), QIcon::Normal, QIcon::Off);
        ui->load->setIcon(load_icon);
    }

    auto metaData = loader->metaData()["MetaData"].toObject();

    this->setWindowTitle(metaData["name"].toString());

    ui->path->setText(loader->fileName());
    ui->load->setChecked(loader->isLoaded());

    connect(ui->load, &QToolButton::clicked, this, [this, loader](bool checked) {
        if(checked) {
            // load plugin
            if(!loader->load()) {
                ui->load->setChecked(false);
                ui->error->setText(loader->errorString());
            }
        } else {
            // unload plugin
            if(!loader->unload()) {
                ui->load->setChecked(true);
                ui->error->setText(loader->errorString());
            }
        }
    });

    ui->name->setText(metaData[QLatin1Literal("name")].toString());
    ui->author->setText(metaData[QLatin1Literal("author")].toString());
    ui->license->setText(metaData[QLatin1Literal("license")].toString());
    ui->shortcut->setText(metaData[QLatin1Literal("shortcut")].toString());

    for(const QString &key : loader->metaData().keys()) {
        auto *i = createItem(key, loader->metaData()[key], nullptr);

        if(i != nullptr)
            ui->details_treeWidget->insertTopLevelItem(0, i);
    }
}

AboutPluginDialog::~AboutPluginDialog()
{
    delete ui;
}