aboutsummaryrefslogtreecommitdiff
path: root/src/about/aboutplugin.cpp
blob: a011b476bfabf36032bac6082763bfa364b55c27 (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
/*
 * 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>

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());
    enable_btn->setText(loader->isLoaded() ? "loaded" : "unloaded");
    ui->tableWidget->setCellWidget(index, 4, enable_btn);

    connect(enable_btn, &QToolButton::clicked, this, [this, loader, enable_btn](bool checked) {
        const bool success = checked ? loader->load() : loader->unload();
        if(!success) {
            enable_btn->setChecked(!checked);
            ui->msg->setText(loader->errorString());
        } else {
            ui->msg->setText(checked ? "Plugin successfully loaded" : "Plugin successfully unloaded");
            enable_btn->setText(checked ? "loaded" : "unloaded");
        }
    });
}