pages/ was a flat row of eight screens, so a change to one meant scanning the whole folder to see what else was in it. The screens now sit in packages/ (browse, cleanup), overview/ (dashboard, history), system/ (logs, settings) and extensions/ (plugins), with the scaffolding they all build on — the Page base and the shared widgets — in base/.
370 lines
14 KiB
C++
370 lines
14 KiB
C++
#include "plugins-page.h"
|
|
|
|
#include <QColor>
|
|
#include <QFileDialog>
|
|
#include <QFileInfo>
|
|
#include <QFrame>
|
|
#include <QHBoxLayout>
|
|
#include <QLabel>
|
|
#include <QPushButton>
|
|
#include <QTableWidget>
|
|
#include <QVBoxLayout>
|
|
|
|
#include "plugin-package.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(tr("AJOUTER UN PLUGIN"));
|
|
hintTitle->setObjectName("cardTitle");
|
|
hintLayout->addWidget(hintTitle);
|
|
|
|
QLabel* hint = new QLabel(QString(
|
|
tr("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), empaquetez-la avec ./make-plugin-package.py, puis "
|
|
"déposez le fichier %1 obtenu n'importe où sur la fenêtre."))
|
|
.arg(PluginPackage::suffix()));
|
|
hint->setWordWrap(true);
|
|
hint->setStyleSheet("font-size: 12px; color: #94a3b8;");
|
|
hintLayout->addWidget(hint);
|
|
|
|
layout->addWidget(hintCard);
|
|
|
|
m_table = ui::table({tr("Plugin"), tr("Type"), tr("Version"), tr("Auteur"), tr("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(tr("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(tr("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(tr("Recharger"));
|
|
rescanButton->setIcon(ui::icon("refresh"));
|
|
rescanButton->setToolTip(tr("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);
|
|
|
|
// ---- Plugin packages: the drop zone and what was installed from one ----
|
|
QFrame* packageCard = ui::card();
|
|
QVBoxLayout* packageLayout = new QVBoxLayout(packageCard);
|
|
packageLayout->setContentsMargins(18, 16, 18, 16);
|
|
packageLayout->setSpacing(8);
|
|
|
|
QLabel* packageTitle = new QLabel(tr("PAQUETS DE PLUGIN"));
|
|
packageTitle->setObjectName("cardTitle");
|
|
packageLayout->addWidget(packageTitle);
|
|
|
|
m_packageHint = new QLabel();
|
|
m_packageHint->setTextFormat(Qt::RichText);
|
|
m_packageHint->setWordWrap(true);
|
|
m_packageHint->setStyleSheet("font-size: 12px; color: #94a3b8;");
|
|
packageLayout->addWidget(m_packageHint);
|
|
|
|
m_packageTable = ui::table({tr("Paquet"), tr("Fichier installé")});
|
|
connect(m_packageTable, &QTableWidget::currentCellChanged, this,
|
|
[this](int currentRow, int, int, int) { onPackageSelected(currentRow); });
|
|
packageLayout->addWidget(m_packageTable, 1);
|
|
|
|
QHBoxLayout* packageBar = new QHBoxLayout();
|
|
packageBar->setSpacing(8);
|
|
|
|
m_browseButton = new QPushButton(tr("Ouvrir un paquet..."));
|
|
m_browseButton->setProperty("variant", "primary");
|
|
m_browseButton->setIcon(ui::icon("package"));
|
|
m_browseButton->setToolTip(
|
|
tr("Choisir un fichier de paquet de plugin à installer (le glisser-déposer fait "
|
|
"la même chose)"));
|
|
connect(m_browseButton, &QPushButton::clicked, this, &PluginsPage::browsePackage);
|
|
packageBar->addWidget(m_browseButton);
|
|
|
|
m_removeButton = new QPushButton(tr("Supprimer le paquet"));
|
|
m_removeButton->setProperty("variant", "danger");
|
|
m_removeButton->setIcon(ui::icon("trash"));
|
|
m_removeButton->setEnabled(false);
|
|
m_removeButton->setToolTip(
|
|
tr("Supprimer la bibliothèque installée depuis un paquet (jamais une bibliothèque "
|
|
"copiée à la main)"));
|
|
connect(m_removeButton, &QPushButton::clicked, this, [this, core] {
|
|
if (!m_selectedPackageId.isEmpty()) core->removePluginPackage(m_selectedPackageId);
|
|
});
|
|
packageBar->addWidget(m_removeButton);
|
|
|
|
packageBar->addStretch();
|
|
packageLayout->addLayout(packageBar);
|
|
|
|
layout->addWidget(packageCard, 1);
|
|
|
|
connect(core, &AppCore::pluginsChanged, this, &PluginsPage::refresh);
|
|
// The core reloads the registry itself after a package is installed or
|
|
// removed; this page only renders the list and reports what happened.
|
|
connect(core, &AppCore::pluginPackageInstalled, this,
|
|
[this](const QString& id, bool success, const QString& detail) {
|
|
refreshPackageList();
|
|
emit message(success
|
|
? QString(tr("Paquet « %1 » installé (%2)")).arg(id, detail)
|
|
: QString(tr("Échec pour « %1 » : %2")).arg(id, detail));
|
|
});
|
|
connect(core, &AppCore::pluginPackageRemoved, this,
|
|
[this](const QString& id, bool success, const QString& detail) {
|
|
refreshPackageList();
|
|
emit message(success ? QString(tr("Paquet « %1 » supprimé")).arg(id)
|
|
: QString(tr("Échec pour « %1 » : %2")).arg(id, detail));
|
|
});
|
|
|
|
refreshTable();
|
|
updateDropHint();
|
|
refreshPackageList();
|
|
}
|
|
|
|
void PluginsPage::browsePackage() {
|
|
const QString path = QFileDialog::getOpenFileName(
|
|
this, tr("Ouvrir un paquet de plugin"), QString(),
|
|
tr("Paquets de plugin (*%1);;Tous les fichiers (*)")
|
|
.arg(PluginPackage::suffix()));
|
|
if (path.isEmpty()) return;
|
|
core()->installPluginPackage(path);
|
|
}
|
|
|
|
void PluginsPage::refresh() {
|
|
refreshTable();
|
|
updateDropHint();
|
|
refreshPackageList();
|
|
}
|
|
|
|
// What the drop zone does, next to the zone itself so the two cannot disagree.
|
|
void PluginsPage::updateDropHint() {
|
|
m_packageHint->setText(QString(
|
|
tr("Déposez un fichier <code>%1</code> n'importe où sur la fenêtre : son "
|
|
"bibliothèque est extraite dans <code>%2</code>, sans mot de passe, et le "
|
|
"plugin est chargé immédiatement, sans redémarrer. Le paquet contient un "
|
|
"manifeste <code>plugin.json</code> et la bibliothèque ; il se crée avec "
|
|
"<code>./make-plugin-package.py</code> (voir doc/plugins.md)."))
|
|
.arg(PluginPackage::suffix().toHtmlEscaped(),
|
|
core()->packages()->installDirectory().toHtmlEscaped()));
|
|
}
|
|
|
|
void PluginsPage::refreshPackageList() {
|
|
const PluginPackage* packages = core()->packages();
|
|
const QStringList ids = packages->installedIds();
|
|
|
|
QFont nameFont;
|
|
nameFont.setWeight(QFont::DemiBold);
|
|
|
|
ui::TableFillBatch batch(m_packageTable);
|
|
m_packageTable->setRowCount(0);
|
|
m_packageTable->setRowCount(static_cast<int>(ids.size()));
|
|
|
|
for (int i = 0; i < static_cast<int>(ids.size()); ++i) {
|
|
const QString& id = ids[i];
|
|
const QString path = packages->installedPath(id);
|
|
|
|
QTableWidgetItem* idItem = new QTableWidgetItem(id);
|
|
idItem->setFont(nameFont);
|
|
idItem->setData(Qt::UserRole, id);
|
|
idItem->setToolTip(id);
|
|
m_packageTable->setItem(i, 0, idItem);
|
|
|
|
QTableWidgetItem* fileItem = new QTableWidgetItem(QFileInfo(path).fileName());
|
|
fileItem->setForeground(QColor("#94a3b8"));
|
|
fileItem->setToolTip(path);
|
|
m_packageTable->setItem(i, 1, fileItem);
|
|
}
|
|
batch.release();
|
|
|
|
if (ids.isEmpty()) {
|
|
m_selectedPackageId.clear();
|
|
m_removeButton->setEnabled(false);
|
|
return;
|
|
}
|
|
|
|
// Keep the same package selected across a reload, so installing a second
|
|
// package does not move the selection away from what the user was looking at.
|
|
int row = 0;
|
|
for (int i = 0; i < static_cast<int>(ids.size()); ++i) {
|
|
if (ids[i] == m_selectedPackageId) {
|
|
row = i;
|
|
break;
|
|
}
|
|
}
|
|
m_packageTable->selectRow(row);
|
|
onPackageSelected(row);
|
|
}
|
|
|
|
void PluginsPage::onPackageSelected(int row) {
|
|
const QStringList ids = core()->packages()->installedIds();
|
|
if (row < 0 || row >= static_cast<int>(ids.size())) {
|
|
m_selectedPackageId.clear();
|
|
m_removeButton->setEnabled(false);
|
|
return;
|
|
}
|
|
m_selectedPackageId = ids[row];
|
|
m_removeButton->setEnabled(true);
|
|
}
|
|
|
|
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 ? tr("Extension")
|
|
: tr("Fournisseur");
|
|
QTableWidgetItem* typeItem =
|
|
new QTableWidgetItem(entry.info.builtin ? kindText : kindText + tr(" (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 = tr("Actif");
|
|
statusColor = QColor("#34d399");
|
|
} else {
|
|
statusText = tr("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(tr("Outil « %1 » non détecté sur ce système."))
|
|
.arg(QString::fromStdString(entry.info.name)));
|
|
m_toggleButton->setText(core()->plugins()->isEnabled(entry.info.id) ? tr("Désactiver")
|
|
: tr("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());
|
|
}
|