Files
FWS-PacManage/include/plugin-shop.h
T
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

108 lines
4.5 KiB
C++

#pragma once
#include <QByteArray>
#include <QNetworkAccessManager>
#include <QObject>
#include <QString>
#include <vector>
class QNetworkReply;
// The plugin shop: a catalogue of installable plugins, browsable and
// installable from the Plugins page.
//
// The catalogue is a small JSON document served over HTTP — by default the one
// shipped in the project's own repository (see catalogUrl() and
// doc/plugin-shop.md for the format). Each entry names a plugin, what it does
// and the shared library to fetch. The shop downloads it into the per-user
// plugin directory (<home>/.local/share/PacManager/plugins, the path
// PluginManager already scans), so a plugin can be installed without root
// rights and becomes usable as soon as the plugin registry is reloaded.
//
// Installing a plugin runs third-party code inside this process, so the shop
// stays deliberately minimal: it only ever fetches a plugin the user picked
// from the catalogue, checks the announced checksum before writing anything,
// remembers which files it wrote, and can therefore remove exactly those again.
// A library the user dropped in by hand is never touched by an uninstall.
class PluginShop : public QObject {
Q_OBJECT
public:
struct CatalogEntry {
QString id;
QString name;
QString description;
QString version;
QString author;
// "provider" or "addon" when the catalogue says so; empty otherwise
// (the real kind is the one the library declares when it is loaded).
QString kind;
QString url; // shared library to download
QString sha256; // optional checksum of that file
QString fileName; // name to write in the plugin directory
};
explicit PluginShop(QObject* parent = nullptr);
// ---- Catalogue ----
const std::vector<CatalogEntry>& catalog() const { return m_catalog; }
bool catalogLoaded() const { return m_catalogLoaded; }
QString lastError() const { return m_lastError; }
// Where the catalogue is read from. Defaults to the catalogue shipped with
// the project; setCatalogUrl() lets a fork point the shop at its own, and
// the value is remembered in the settings.
QString catalogUrl() const;
void setCatalogUrl(const QString& url);
// Fetches and parses the catalogue, then emits catalogChanged().
void refresh();
// ---- Installed plugins ----
// Directory the shop installs into (created on demand). Overridable so the
// tests never touch the user's real plugin directory.
QString installDirectory() const;
void setInstallDirectory(const QString& path);
// True when the shop installed this catalogue entry and its file is still
// there.
bool isInstalled(const QString& id) const;
// Absolute path of what the shop installed for `id` (empty when it did not).
QString installedPath(const QString& id) const;
// Downloads the entry and writes it into the plugin directory, then emits
// installFinished(). A checksum mismatch aborts the install: a library that
// is not the one the catalogue announced is never loaded.
void install(const QString& id);
// Removes a plugin the shop installed and emits removeFinished().
void remove(const QString& id);
// Parses a catalogue document. Pure logic, exposed so it is unit-tested
// without any network access: `error` is filled (and the result empty) when
// the document is not a catalogue at all. Entries missing what is needed to
// install them are skipped, so one broken entry never hides the others.
static std::vector<CatalogEntry> parseCatalog(const QByteArray& json, QString* error);
signals:
void catalogChanged();
void installFinished(const QString& id, bool success, const QString& message);
void removeFinished(const QString& id, bool success, const QString& message);
private:
// Settings key remembering the file installed for a catalogue entry.
static QString installedKey(const QString& id);
static QString librarySuffix();
const CatalogEntry* entry(const QString& id) const;
// Name the downloaded file is written under: the catalogue's own name when
// it has one, otherwise the last segment of the URL with the platform's
// library suffix.
static QString targetFileName(const CatalogEntry& entry);
QString targetPath(const CatalogEntry& entry) const;
QNetworkAccessManager m_network;
std::vector<CatalogEntry> m_catalog;
bool m_catalogLoaded = false;
QString m_lastError;
QString m_installDirectory; // empty = the per-user plugin directory
};