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>
This commit is contained in:
1 parent
b26e907221
commit
5033c4e940
9 files changed
+872
-6
No files matched your search
@@ -12,6 +12,7 @@
|
||||
#include <sstream>
|
||||
|
||||
#include "app-logging.h"
|
||||
#include "plugin-shop.h"
|
||||
#include "workers.h"
|
||||
|
||||
namespace {
|
||||
@@ -94,6 +95,22 @@ AppCore::AppCore(QObject* parent) : QObject(parent) {
|
||||
// Non-owning handle kept for sudo credentials and the database lock.
|
||||
m_pacman = dynamic_cast<PacmanManager*>(m_plugins->managerFor("pacman"));
|
||||
|
||||
// Plugin shop: the catalogue, the downloads and the bookkeeping of what it
|
||||
// installed. What it fetches lands in the directory the registry scans, so
|
||||
// a successful install only needs that registry reloaded.
|
||||
m_shop = new PluginShop(this);
|
||||
connect(m_shop, &PluginShop::catalogChanged, this, &AppCore::pluginCatalogChanged);
|
||||
connect(m_shop, &PluginShop::installFinished, this,
|
||||
[this](const QString& id, bool success, const QString& message) {
|
||||
if (success) reloadPlugins();
|
||||
emit shopInstallFinished(id, success, message);
|
||||
});
|
||||
connect(m_shop, &PluginShop::removeFinished, this,
|
||||
[this](const QString& id, bool success, const QString& message) {
|
||||
if (success) reloadPlugins();
|
||||
emit shopInstallFinished(id, success, message);
|
||||
});
|
||||
|
||||
m_history = new HistoryManager();
|
||||
m_update = new UpdateManager(this);
|
||||
m_credentialMode =
|
||||
@@ -888,6 +905,20 @@ void AppCore::reloadPlugins() {
|
||||
emit statusMessage(QStringLiteral("Plugins rechargés."));
|
||||
}
|
||||
|
||||
void AppCore::loadPluginCatalog() {
|
||||
emit statusMessage(QStringLiteral("Chargement du catalogue de plugins..."));
|
||||
m_shop->refresh();
|
||||
}
|
||||
|
||||
void AppCore::installShopPlugin(const QString& id) {
|
||||
emit statusMessage(QStringLiteral("Téléchargement du plugin %1...").arg(id));
|
||||
m_shop->install(id);
|
||||
}
|
||||
|
||||
void AppCore::removeShopPlugin(const QString& id) {
|
||||
m_shop->remove(id);
|
||||
}
|
||||
|
||||
void AppCore::clearHistory() {
|
||||
m_history->clear();
|
||||
emit historyChanged();
|
||||
|
||||
@@ -0,0 +1,283 @@
|
||||
#include "plugin-shop.h"
|
||||
|
||||
#include <QCryptographicHash>
|
||||
#include <QDir>
|
||||
#include <QFile>
|
||||
#include <QFileInfo>
|
||||
#include <QJsonArray>
|
||||
#include <QJsonDocument>
|
||||
#include <QJsonObject>
|
||||
#include <QNetworkReply>
|
||||
#include <QNetworkRequest>
|
||||
#include <QSaveFile>
|
||||
#include <QSettings>
|
||||
#include <QUrl>
|
||||
|
||||
#include "app-logging.h"
|
||||
|
||||
// Catalogue shipped with the project: publishing a plugin to the shop means
|
||||
// adding an entry to this file (see doc/plugin-shop.md for the format).
|
||||
static const char* kDefaultCatalogUrl =
|
||||
"https://gitea.nocode-var.tech/FWS/FWS-PacManage/raw/branch/main/plugins/catalog.json";
|
||||
|
||||
// The per-user plugin directory, shared with PluginManager's search paths.
|
||||
static const char* kUserPluginDir = "/.local/share/PacManager/plugins";
|
||||
|
||||
PluginShop::PluginShop(QObject* parent) : QObject(parent) {}
|
||||
|
||||
QString PluginShop::installDirectory() const {
|
||||
if (!m_installDirectory.isEmpty()) return m_installDirectory;
|
||||
return QDir::homePath() + QLatin1String(kUserPluginDir);
|
||||
}
|
||||
|
||||
void PluginShop::setInstallDirectory(const QString& path) {
|
||||
m_installDirectory = path;
|
||||
}
|
||||
|
||||
QString PluginShop::catalogUrl() const {
|
||||
QSettings settings;
|
||||
const QString configured = settings.value(QStringLiteral("shop/catalogUrl")).toString().trimmed();
|
||||
// An empty setting means "the catalogue this build ships with": the key is
|
||||
// only written once someone actually points the shop elsewhere.
|
||||
return configured.isEmpty() ? QString::fromLatin1(kDefaultCatalogUrl) : configured;
|
||||
}
|
||||
|
||||
void PluginShop::setCatalogUrl(const QString& url) {
|
||||
QSettings settings;
|
||||
const QString trimmed = url.trimmed();
|
||||
if (trimmed.isEmpty() || trimmed == QLatin1String(kDefaultCatalogUrl)) {
|
||||
settings.remove(QStringLiteral("shop/catalogUrl"));
|
||||
return;
|
||||
}
|
||||
settings.setValue(QStringLiteral("shop/catalogUrl"), trimmed);
|
||||
}
|
||||
|
||||
void PluginShop::refresh() {
|
||||
QNetworkRequest request{QUrl(catalogUrl())};
|
||||
request.setHeader(QNetworkRequest::UserAgentHeader,
|
||||
QStringLiteral("PacManager-plugin-shop"));
|
||||
request.setAttribute(QNetworkRequest::RedirectPolicyAttribute,
|
||||
QNetworkRequest::NoLessSafeRedirectPolicy);
|
||||
|
||||
QNetworkReply* reply = m_network.get(request);
|
||||
connect(reply, &QNetworkReply::finished, this, [this, reply]() {
|
||||
reply->deleteLater();
|
||||
|
||||
if (reply->error() != QNetworkReply::NoError) {
|
||||
m_catalogLoaded = false;
|
||||
m_lastError = QStringLiteral("Catalogue indisponible : %1").arg(reply->errorString());
|
||||
qCWarning(pacmgrPlugin) << "plugin catalog fetch failed:" << reply->errorString();
|
||||
emit catalogChanged();
|
||||
return;
|
||||
}
|
||||
|
||||
QString error;
|
||||
std::vector<CatalogEntry> entries = parseCatalog(reply->readAll(), &error);
|
||||
if (!error.isEmpty()) {
|
||||
m_catalogLoaded = false;
|
||||
m_lastError = error;
|
||||
qCWarning(pacmgrPlugin) << "plugin catalog rejected:" << error;
|
||||
emit catalogChanged();
|
||||
return;
|
||||
}
|
||||
|
||||
m_catalog = std::move(entries);
|
||||
m_catalogLoaded = true;
|
||||
m_lastError.clear();
|
||||
qCInfo(pacmgrPlugin) << "plugin catalog loaded,"
|
||||
<< static_cast<int>(m_catalog.size()) << "entrie(s)";
|
||||
emit catalogChanged();
|
||||
});
|
||||
}
|
||||
|
||||
std::vector<PluginShop::CatalogEntry> PluginShop::parseCatalog(const QByteArray& json,
|
||||
QString* error) {
|
||||
auto fail = [error](const QString& message) {
|
||||
if (error) *error = message;
|
||||
return std::vector<CatalogEntry>{};
|
||||
};
|
||||
|
||||
QJsonParseError parseError{};
|
||||
const QJsonDocument document = QJsonDocument::fromJson(json, &parseError);
|
||||
if (parseError.error != QJsonParseError::NoError) {
|
||||
return fail(QStringLiteral("Catalogue illisible : %1").arg(parseError.errorString()));
|
||||
}
|
||||
if (!document.isObject()) {
|
||||
return fail(QStringLiteral("Catalogue illisible : document JSON attendu."));
|
||||
}
|
||||
|
||||
const QJsonObject root = document.object();
|
||||
if (!root.value(QStringLiteral("plugins")).isArray()) {
|
||||
return fail(QStringLiteral("Catalogue invalide : tableau « plugins » manquant."));
|
||||
}
|
||||
|
||||
std::vector<CatalogEntry> entries;
|
||||
const QJsonArray plugins = root.value(QStringLiteral("plugins")).toArray();
|
||||
for (const QJsonValue& value : plugins) {
|
||||
if (!value.isObject()) continue;
|
||||
const QJsonObject object = value.toObject();
|
||||
|
||||
CatalogEntry entry;
|
||||
entry.id = object.value(QStringLiteral("id")).toString().trimmed();
|
||||
entry.name = object.value(QStringLiteral("name")).toString().trimmed();
|
||||
entry.description = object.value(QStringLiteral("description")).toString().trimmed();
|
||||
entry.version = object.value(QStringLiteral("version")).toString().trimmed();
|
||||
entry.author = object.value(QStringLiteral("author")).toString().trimmed();
|
||||
entry.kind = object.value(QStringLiteral("kind")).toString().trimmed().toLower();
|
||||
entry.url = object.value(QStringLiteral("url")).toString().trimmed();
|
||||
entry.sha256 = object.value(QStringLiteral("sha256")).toString().trimmed().toLower();
|
||||
entry.fileName = object.value(QStringLiteral("file")).toString().trimmed();
|
||||
|
||||
// Without an id and a download URL an entry cannot be installed; the
|
||||
// rest of the catalogue stays usable instead of failing as a whole.
|
||||
if (entry.id.isEmpty() || entry.name.isEmpty() || entry.url.isEmpty()) continue;
|
||||
// Any other kind is left empty on purpose: the real kind is the one the
|
||||
// library declares when the registry loads it.
|
||||
if (entry.kind != QLatin1String("provider") && entry.kind != QLatin1String("addon")) {
|
||||
entry.kind.clear();
|
||||
}
|
||||
entries.push_back(entry);
|
||||
}
|
||||
|
||||
if (error) error->clear();
|
||||
return entries;
|
||||
}
|
||||
|
||||
QString PluginShop::librarySuffix() {
|
||||
#ifdef Q_OS_WIN
|
||||
return QStringLiteral(".dll");
|
||||
#elif defined(Q_OS_MACOS)
|
||||
return QStringLiteral(".dylib");
|
||||
#else
|
||||
return QStringLiteral(".so");
|
||||
#endif
|
||||
}
|
||||
|
||||
QString PluginShop::targetFileName(const CatalogEntry& entry) {
|
||||
if (!entry.fileName.isEmpty()) return entry.fileName;
|
||||
|
||||
QString name = QFileInfo(QUrl(entry.url).fileName()).fileName();
|
||||
if (name.isEmpty()) name = entry.id;
|
||||
// The plugin directory is scanned for shared libraries only, so a URL that
|
||||
// points at something else still lands as one.
|
||||
if (!name.endsWith(librarySuffix(), Qt::CaseInsensitive)) name += librarySuffix();
|
||||
return name;
|
||||
}
|
||||
|
||||
QString PluginShop::targetPath(const CatalogEntry& entry) const {
|
||||
return QDir(installDirectory()).filePath(targetFileName(entry));
|
||||
}
|
||||
|
||||
QString PluginShop::installedKey(const QString& id) {
|
||||
return QStringLiteral("shop/installed/%1").arg(id);
|
||||
}
|
||||
|
||||
const PluginShop::CatalogEntry* PluginShop::entry(const QString& id) const {
|
||||
for (const CatalogEntry& candidate : m_catalog) {
|
||||
if (candidate.id == id) return &candidate;
|
||||
}
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
QString PluginShop::installedPath(const QString& id) const {
|
||||
QSettings settings;
|
||||
const QString path = settings.value(installedKey(id)).toString();
|
||||
// A recorded file that was deleted by hand is not installed any more.
|
||||
if (path.isEmpty() || !QFile::exists(path)) return QString();
|
||||
return path;
|
||||
}
|
||||
|
||||
bool PluginShop::isInstalled(const QString& id) const {
|
||||
return !installedPath(id).isEmpty();
|
||||
}
|
||||
|
||||
void PluginShop::install(const QString& id) {
|
||||
const CatalogEntry* found = entry(id);
|
||||
if (!found) {
|
||||
emit installFinished(id, false, QStringLiteral("Plugin absent du catalogue."));
|
||||
return;
|
||||
}
|
||||
// Copied: the catalogue can be refreshed while the download is running.
|
||||
const CatalogEntry selected = *found;
|
||||
|
||||
QNetworkRequest request{QUrl(selected.url)};
|
||||
request.setHeader(QNetworkRequest::UserAgentHeader,
|
||||
QStringLiteral("PacManager-plugin-shop"));
|
||||
request.setAttribute(QNetworkRequest::RedirectPolicyAttribute,
|
||||
QNetworkRequest::NoLessSafeRedirectPolicy);
|
||||
|
||||
QNetworkReply* reply = m_network.get(request);
|
||||
connect(reply, &QNetworkReply::finished, this, [this, reply, selected]() {
|
||||
reply->deleteLater();
|
||||
|
||||
if (reply->error() != QNetworkReply::NoError) {
|
||||
emit installFinished(selected.id, false,
|
||||
QStringLiteral("Téléchargement impossible : %1").arg(reply->errorString()));
|
||||
return;
|
||||
}
|
||||
|
||||
const QByteArray payload = reply->readAll();
|
||||
if (payload.isEmpty()) {
|
||||
emit installFinished(selected.id, false,
|
||||
QStringLiteral("Fichier téléchargé vide."));
|
||||
return;
|
||||
}
|
||||
|
||||
if (!selected.sha256.isEmpty()) {
|
||||
const QString digest = QString::fromLatin1(
|
||||
QCryptographicHash::hash(payload, QCryptographicHash::Sha256).toHex());
|
||||
if (digest.compare(selected.sha256, Qt::CaseInsensitive) != 0) {
|
||||
// The whole point of the checksum: a library that is not the one
|
||||
// the catalogue announced never reaches the plugin directory.
|
||||
emit installFinished(selected.id, false,
|
||||
QStringLiteral("Somme de contrôle invalide (attendu %1).")
|
||||
.arg(selected.sha256));
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
const QString directory = installDirectory();
|
||||
if (!QDir().mkpath(directory)) {
|
||||
emit installFinished(selected.id, false,
|
||||
QStringLiteral("Impossible de créer %1.").arg(directory));
|
||||
return;
|
||||
}
|
||||
|
||||
const QString path = targetPath(selected);
|
||||
// Written through QSaveFile so the shared library only ever appears in
|
||||
// the scanned directory complete, under its final name.
|
||||
QSaveFile file(path);
|
||||
if (!file.open(QIODevice::WriteOnly) || file.write(payload) != payload.size()
|
||||
|| !file.commit()) {
|
||||
emit installFinished(selected.id, false,
|
||||
QStringLiteral("Impossible d'écrire %1.").arg(path));
|
||||
return;
|
||||
}
|
||||
|
||||
QSettings settings;
|
||||
settings.setValue(installedKey(selected.id), path);
|
||||
qCInfo(pacmgrPlugin) << "plugin installed from the shop:" << selected.id << path;
|
||||
emit installFinished(selected.id, true, path);
|
||||
});
|
||||
}
|
||||
|
||||
void PluginShop::remove(const QString& id) {
|
||||
// Only a file the shop wrote is removed: a plugin the user dropped into the
|
||||
// directory by hand is not the shop's to delete.
|
||||
const QString path = installedPath(id);
|
||||
if (path.isEmpty()) {
|
||||
emit removeFinished(id, false,
|
||||
QStringLiteral("Ce plugin n'a pas été installé par le magasin."));
|
||||
return;
|
||||
}
|
||||
|
||||
if (QFile::exists(path) && !QFile::remove(path)) {
|
||||
emit removeFinished(id, false, QStringLiteral("Impossible de supprimer %1.").arg(path));
|
||||
return;
|
||||
}
|
||||
|
||||
QSettings settings;
|
||||
settings.remove(installedKey(id));
|
||||
qCInfo(pacmgrPlugin) << "plugin removed from the shop:" << id << path;
|
||||
emit removeFinished(id, true, path);
|
||||
}
|
||||
Reference in new issue
Block a user