aboutsummaryrefslogtreecommitdiff
path: root/src/about/aboutplugin.cpp
blob: 92e55bb6bd8d915da4eae8c0ba6a0388b4a54d0d (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
/*
 * 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>
#include <QToolButton>

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

    switch(json.type()) {
    case QJsonValue::Bool:
        item->setText(1, json.toBool() ? QLatin1String("true") : QLatin1String("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, QLatin1String("null"));
        break;

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

    return item;
}

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


}

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

void AboutPluginDialog::add(QPluginLoader *loader)
{
    const auto index = ui->tableWidget->rowCount();
    ui->tableWidget->setRowCount(index + 1);

    const auto metadata = loader->metaData()["MetaData"].toObject();
    ui->tableWidget->setItem(index, 0, new QTableWidgetItem(metadata.value("name").toString()));
    ui->tableWidget->setItem(index, 1, new QTableWidgetItem(metadata.value("author").toString()));
    ui->tableWidget->setItem(index, 2, new QTableWidgetItem(metadata.value("license").toString()));
    ui->tableWidget->setItem(index, 3, new QTableWidgetItem(metadata.value("shortcut").toString()));
    ui->tableWidget->setItem(index, 5, new QTableWidgetItem(loader->fileName()));

    auto *enable_btn = new QToolButton(this);
    enable_btn->setCheckable(true);
    enable_btn->setChecked(loader->isLoaded());
    ui->tableWidget->setCellWidget(index, 4, enable_btn);

    connect(enable_btn, &QToolButton::clicked, this, [ loader, enable_btn ](bool checked) {
        const bool success = checked ? loader->load() : loader->unload();
        if(!success) {
            enable_btn->setChecked(!checked);
            //ui->error->setText(loader->errorString());
        }
    });

    for(const QString &key : metadata.keys()) {
        auto *i = createItem(key, metadata.value(key), nullptr);
        if(i != nullptr) {
            ui->details_treeWidget->insertTopLevelItem(0, i);
        }
    }
}