Files
FWS-PacManage/pages/plugins-page.cpp
BlackAngelTV 5033c4e940 feat(plugins): add a plugin shop fed by a JSON catalogue
Installing a plugin meant compiling a shared library and dropping it in a
directory by hand. The shop reads a catalogue (plugins/catalog.json, one
entry per plugin with its description and download URL), lists what can be
installed next to what already is, and installs an entry into
~/.local/share/PacManager/plugins — the directory the registry already
scans — so a plugin becomes usable without root rights and without a
restart. Installing runs third-party code in this process, so the shop only
fetches what the user picked, checks the announced sha256 before writing
anything, and a removal deletes only the files the shop wrote itself.

🤖 Generated with Codebuff
Co-Authored-By: Codebuff <noreply@codebuff.com>
2026-09-16 22:41:11 +02:00

389 lines
16 KiB
C++

#include "plugins-page.h"
#include <QColor>
#include <QFrame>
#include <QHBoxLayout>
#include <QInputDialog>
#include <QLabel>
#include <QLineEdit>
#include <QPushButton>
#include <QTableWidget>
#include <QVBoxLayout>
#include "plugin-shop.h"
#include "ui-kit.h"
PluginsPage::PluginsPage(AppCore* core, QWidget* parent) : Page(core, parent) {
QVBoxLayout* layout = new QVBoxLayout(this);
layout->setContentsMargins(24, 20, 24, 20);
layout->setSpacing(12);
QFrame* hintCard = ui::card();
QVBoxLayout* hintLayout = new QVBoxLayout(hintCard);
hintLayout->setContentsMargins(18, 16, 18, 16);
hintLayout->setSpacing(4);
QLabel* hintTitle = new QLabel("AJOUTER UN PLUGIN");
hintTitle->setObjectName("cardTitle");
hintLayout->addWidget(hintTitle);
QLabel* hint = new QLabel(
"Pacman est intégré et le plugin Flatpak est livré par défaut. Si un "
"fournisseur est « Indisponible », sélectionnez-le puis cliquez sur "
"« Installer l'outil » pour l'installer via pacman. Un plugin peut être "
"un gestionnaire de paquets (apt, dnf, ...) ou n'importe quelle extension "
"(addon graphique, helper, ...) tant qu'il est écrit en C++ : compilez une "
"bibliothèque partagée (voir plugins/example-provider et "
"plugins/example-addon) et déposez-la dans "
"~/.local/share/PacManager/plugins/ ou dans le dossier plugins/ à côté de "
"l'exécutable, puis cliquez sur « Recharger ».");
hint->setWordWrap(true);
hint->setStyleSheet("font-size: 12px; color: #94a3b8;");
hintLayout->addWidget(hint);
layout->addWidget(hintCard);
m_table = ui::table({"Plugin", "Type", "Version", "Auteur", "Statut"});
connect(m_table, &QTableWidget::currentCellChanged, this,
[this](int currentRow, int, int, int) { onSelected(currentRow); });
layout->addWidget(m_table, 1);
m_description = new QLabel("");
m_description->setWordWrap(true);
m_description->setStyleSheet(
"font-size: 13px; color: #94a3b8; background: transparent; padding: 2px 4px;");
layout->addWidget(m_description);
QHBoxLayout* buttonBar = new QHBoxLayout();
buttonBar->addStretch();
// Installs the OS package backing a provider whose tool is missing
// (e.g. pacman -S flatpak), then rescans so it becomes available.
m_installButton = new QPushButton("Installer l'outil");
m_installButton->setProperty("variant", "primary");
m_installButton->setEnabled(false);
m_installButton->setVisible(false);
connect(m_installButton, &QPushButton::clicked, this, [this, core] {
if (m_selectedId.empty()) return;
start(core->installPluginTool(m_selectedId), tr("Installation de l'outil"));
});
buttonBar->addWidget(m_installButton);
m_toggleButton = new QPushButton("Activer");
m_toggleButton->setEnabled(false);
connect(m_toggleButton, &QPushButton::clicked, this, [this, core] {
if (m_selectedId.empty()) return;
const std::string id = m_selectedId;
const bool enabled = !core->plugins()->isEnabled(id);
core->setPluginEnabled(id, enabled);
// A freshly enabled addon gets the window so it can attach its UI (it
// may have been disabled when onUiReady ran at launch). The core already
// announced the change, which refreshed this table.
if (enabled) {
for (const auto& entry : core->plugins()->plugins()) {
if (entry.info.id == id && entry.plugin) {
entry.plugin->onUiReady(mainWindow());
break;
}
}
}
});
buttonBar->addWidget(m_toggleButton);
QPushButton* rescanButton = new QPushButton("Recharger");
rescanButton->setIcon(ui::icon("refresh"));
rescanButton->setToolTip("Rescanner les dossiers de plugins");
connect(rescanButton, &QPushButton::clicked, this, [this, core] {
core->reloadPlugins();
for (IPlugin* addon : core->plugins()->availableAddons()) {
addon->onUiReady(mainWindow());
}
});
buttonBar->addWidget(rescanButton);
layout->addLayout(buttonBar);
// ---- Shop: the catalogue of installable plugins ----
QFrame* shopCard = ui::card();
QVBoxLayout* shopLayout = new QVBoxLayout(shopCard);
shopLayout->setContentsMargins(18, 16, 18, 16);
shopLayout->setSpacing(8);
QLabel* shopTitle = new QLabel("MAGASIN DE PLUGINS");
shopTitle->setObjectName("cardTitle");
shopLayout->addWidget(shopTitle);
m_shopHint = new QLabel();
m_shopHint->setTextFormat(Qt::RichText);
m_shopHint->setWordWrap(true);
m_shopHint->setStyleSheet("font-size: 12px; color: #94a3b8;");
shopLayout->addWidget(m_shopHint);
m_shopTable = ui::table({"Plugin", "Type", "Version", "Auteur", "État"});
connect(m_shopTable, &QTableWidget::currentCellChanged, this,
[this](int currentRow, int, int, int) { onShopSelected(currentRow); });
shopLayout->addWidget(m_shopTable, 1);
QHBoxLayout* shopBar = new QHBoxLayout();
shopBar->setSpacing(8);
QPushButton* catalogButton = new QPushButton("Charger le catalogue");
catalogButton->setProperty("variant", "primary");
catalogButton->setIcon(ui::icon("refresh"));
catalogButton->setToolTip("Lire la liste des plugins disponibles sur le catalogue");
connect(catalogButton, &QPushButton::clicked, core, &AppCore::loadPluginCatalog);
shopBar->addWidget(catalogButton);
m_shopInstallButton = new QPushButton("Installer");
m_shopInstallButton->setProperty("variant", "success");
m_shopInstallButton->setIcon(ui::icon("plugin"));
m_shopInstallButton->setEnabled(false);
m_shopInstallButton->setToolTip(
"Télécharger le plugin dans ~/.local/share/PacManager/plugins ; il est "
"chargé immédiatement");
connect(m_shopInstallButton, &QPushButton::clicked, this, [this, core] {
if (!m_shopSelectedId.isEmpty()) core->installShopPlugin(m_shopSelectedId);
});
shopBar->addWidget(m_shopInstallButton);
m_shopRemoveButton = new QPushButton("Désinstaller");
m_shopRemoveButton->setProperty("variant", "danger");
m_shopRemoveButton->setIcon(ui::icon("trash"));
m_shopRemoveButton->setEnabled(false);
m_shopRemoveButton->setToolTip("Supprimer le plugin installé par le magasin");
connect(m_shopRemoveButton, &QPushButton::clicked, this, [this, core] {
if (!m_shopSelectedId.isEmpty()) core->removeShopPlugin(m_shopSelectedId);
});
shopBar->addWidget(m_shopRemoveButton);
QPushButton* sourceButton = new QPushButton("Source du catalogue...");
sourceButton->setToolTip("Pointer le magasin vers un autre catalogue");
connect(sourceButton, &QPushButton::clicked, this, [this, core] {
bool ok = false;
const QString url = QInputDialog::getText(
this, "Catalogue de plugins", "Adresse du catalogue JSON :",
QLineEdit::Normal, core->shop()->catalogUrl(), &ok);
if (!ok || url.trimmed().isEmpty()) return;
core->shop()->setCatalogUrl(url);
updateShopHint();
emit message(QString("Catalogue : %1").arg(core->shop()->catalogUrl()));
});
shopBar->addWidget(sourceButton);
shopBar->addStretch();
shopLayout->addLayout(shopBar);
layout->addWidget(shopCard, 1);
connect(core, &AppCore::pluginsChanged, this, &PluginsPage::refresh);
// The core reloads the registry itself after a shop install or removal;
// this page only renders the catalogue and reports what happened.
connect(core, &AppCore::pluginCatalogChanged, this, [this, core] {
refreshShopTable();
const PluginShop* shop = core->shop();
emit message(shop->catalogLoaded()
? QString("Catalogue chargé : %1 plugin(s) disponible(s)")
.arg(static_cast<int>(shop->catalog().size()))
: shop->lastError());
});
connect(core, &AppCore::shopInstallFinished, this,
[this](const QString& id, bool success, const QString& detail) {
refreshShopTable();
emit message(success ? QString("Plugin « %1 » installé").arg(id)
: QString("Échec pour « %1 » : %2").arg(id, detail));
});
refreshTable();
updateShopHint();
refreshShopTable();
}
void PluginsPage::refresh() {
refreshTable();
updateShopHint();
refreshShopTable();
}
// Source line of the shop: where the catalogue is read from, and what installing
// actually does. Kept next to the table so the two never disagree.
void PluginsPage::updateShopHint() {
m_shopHint->setText(QString(
"Le magasin lit un catalogue JSON et installe les plugins choisis dans "
"<code>~/.local/share/PacManager/plugins</code>, sans mot de passe ; ils sont "
"chargés immédiatement, sans redémarrer. Catalogue actuel : <code>%1</code>.")
.arg(core()->shop()->catalogUrl().toHtmlEscaped()));
}
void PluginsPage::refreshShopTable() {
const PluginShop* shop = core()->shop();
const auto& catalog = shop->catalog();
QFont nameFont;
nameFont.setWeight(QFont::DemiBold);
ui::TableFillBatch batch(m_shopTable);
m_shopTable->setRowCount(0);
m_shopTable->setRowCount(static_cast<int>(catalog.size()));
for (int i = 0; i < static_cast<int>(catalog.size()); ++i) {
const PluginShop::CatalogEntry& entry = catalog[i];
QTableWidgetItem* nameItem =
new QTableWidgetItem(entry.name.isEmpty() ? entry.id : entry.name);
nameItem->setFont(nameFont);
nameItem->setData(Qt::UserRole, entry.id);
if (!entry.description.isEmpty()) nameItem->setToolTip(entry.description);
m_shopTable->setItem(i, 0, nameItem);
// The catalogue's own kind label; the authoritative one is what the
// library declares once the registry loads it.
const QString kind = entry.kind == QLatin1String("provider") ? QStringLiteral("Fournisseur")
: entry.kind == QLatin1String("addon") ? QStringLiteral("Extension")
: QStringLiteral("—");
QTableWidgetItem* typeItem = new QTableWidgetItem(kind);
typeItem->setForeground(QColor("#94a3b8"));
m_shopTable->setItem(i, 1, typeItem);
m_shopTable->setItem(i, 2, new QTableWidgetItem(
entry.version.isEmpty() ? QStringLiteral("—") : entry.version));
m_shopTable->setItem(i, 3, new QTableWidgetItem(
entry.author.isEmpty() ? QStringLiteral("—") : entry.author));
const bool installed = shop->isInstalled(entry.id);
QTableWidgetItem* stateItem =
new QTableWidgetItem(installed ? QStringLiteral("Installé") : QStringLiteral("Disponible"));
stateItem->setForeground(QColor(installed ? "#34d399" : "#38bdf8"));
stateItem->setIcon(ui::icon(installed ? "check" : "package"));
m_shopTable->setItem(i, 4, stateItem);
}
batch.release();
if (!catalog.empty()) m_shopTable->selectRow(0);
onShopSelected(m_shopTable->currentRow());
}
void PluginsPage::onShopSelected(int row) {
const auto& catalog = core()->shop()->catalog();
if (row < 0 || row >= static_cast<int>(catalog.size())) {
m_shopSelectedId.clear();
m_shopInstallButton->setEnabled(false);
m_shopRemoveButton->setEnabled(false);
return;
}
m_shopSelectedId = catalog[row].id;
// An entry is either to install or installed: only one of the two buttons
// ever makes sense for the selected row.
const bool installed = core()->shop()->isInstalled(m_shopSelectedId);
m_shopInstallButton->setEnabled(!installed);
m_shopRemoveButton->setEnabled(installed);
}
void PluginsPage::refreshTable() {
const auto& plugins = core()->plugins()->plugins();
QFont nameFont;
nameFont.setWeight(QFont::DemiBold);
ui::TableFillBatch batch(m_table);
m_table->setRowCount(0);
m_table->setRowCount(static_cast<int>(plugins.size()));
for (int i = 0; i < static_cast<int>(plugins.size()); ++i) {
const auto& entry = plugins[i];
const bool enabled = core()->plugins()->isEnabled(entry.info.id);
QTableWidgetItem* nameItem =
new QTableWidgetItem(QString::fromStdString(entry.info.name));
nameItem->setFont(nameFont);
nameItem->setData(Qt::UserRole, QString::fromStdString(entry.info.id));
m_table->setItem(i, 0, nameItem);
// Type column distinguishes package-manager providers from generic
// addons; the build origin is appended for external plugins.
const QString kindText = entry.info.kind == PluginKind::Addon ? "Extension"
: "Fournisseur";
QTableWidgetItem* typeItem =
new QTableWidgetItem(entry.info.builtin ? kindText : kindText + " (externe)");
typeItem->setForeground(QColor("#94a3b8"));
m_table->setItem(i, 1, typeItem);
m_table->setItem(i, 2, new QTableWidgetItem(QString::fromStdString(entry.info.version)));
m_table->setItem(i, 3, new QTableWidgetItem(QString::fromStdString(entry.info.author)));
QString statusText;
QColor statusColor;
if (!entry.info.available) {
statusText = "Indisponible";
statusColor = QColor("#64748b");
} else if (enabled) {
statusText = "Actif";
statusColor = QColor("#34d399");
} else {
statusText = "Désactivé";
statusColor = QColor("#fbbf24");
}
QTableWidgetItem* statusItem = new QTableWidgetItem(statusText);
statusItem->setForeground(statusColor);
statusItem->setIcon(ui::icon(entry.info.available ? (enabled ? "check" : "close")
: "alert"));
m_table->setItem(i, 4, statusItem);
}
batch.release();
if (plugins.empty()) {
m_toggleButton->setEnabled(false);
m_installButton->setVisible(false);
m_description->clear();
return;
}
// Keep the previously selected plugin selected when possible.
int row = 0;
for (int i = 0; i < static_cast<int>(plugins.size()); ++i) {
if (plugins[i].info.id == m_selectedId) {
row = i;
break;
}
}
m_table->selectRow(row);
onSelected(row);
}
void PluginsPage::onSelected(int row) {
const auto& plugins = core()->plugins()->plugins();
if (row < 0 || row >= static_cast<int>(plugins.size())) {
m_toggleButton->setEnabled(false);
m_installButton->setVisible(false);
m_description->clear();
return;
}
const auto& entry = plugins[row];
m_selectedId = entry.info.id;
m_description->setText(QString::fromStdString(entry.info.description));
const bool toggleable = entry.info.available;
m_toggleButton->setEnabled(toggleable);
m_toggleButton->setToolTip(
toggleable ? QString()
: QString("Outil « %1 » non détecté sur ce système.")
.arg(QString::fromStdString(entry.info.name)));
m_toggleButton->setText(core()->plugins()->isEnabled(entry.info.id) ? "Désactiver"
: "Activer");
// Offer to install the backing tool (e.g. flatpak via pacman) when it is
// missing and we know which OS package provides it.
const bool pacmanAvailable = core()->plugins()->findAvailable("pacman") != nullptr;
const bool installable = !entry.info.available && !entry.info.installHint.empty()
&& pacmanAvailable;
m_installButton->setVisible(!entry.info.available);
m_installButton->setEnabled(installable);
m_installButton->setToolTip(
installable
? QString("sudo pacman -S %1").arg(QString::fromStdString(entry.info.installHint))
: QString());
}