The shop was a catalogue served over HTTP that the application fetched and listed, which put a network round trip between a user and a plugin that was almost always one file they already had. It also had to keep its catalogue URLs in step with every release, since an entry that pointed at a missing asset was indistinguishable from one that never worked. A plugin is now distributed as a .fwspacm package: a ZIP archive holding a plugin.json manifest and the shared library, which the user drops anywhere on the window. The file is the distribution, so there is no catalogue to publish, no network, and no catalogue URL setting to explain. The manifest tells the user what they are about to run, and its checksum is verified before anything is written, so a package whose library is not the one it declares never reaches the plugin directory. Qt exposes no public ZIP API, so the archive is read directly (central directory plus zlib for the deflated entries) rather than through a private header that could move between toolchains. The reader stays in the logic layer: app-core-test still links no QtWidgets, and a unit test builds its own archives to check both compression methods and every refusal path.
128 lines
5.4 KiB
C++
128 lines
5.4 KiB
C++
#pragma once
|
|
|
|
#include <QObject>
|
|
#include <QString>
|
|
|
|
#include <vector>
|
|
|
|
#include "zip-archive.h"
|
|
|
|
// A plugin package (.fwspacm): a ZIP archive holding a plugin and the manifest
|
|
// describing it.
|
|
//
|
|
// The package is built by a plugin author with the script at the repo root
|
|
// (make-plugin-package.py) and installed by dropping the file anywhere on the
|
|
// window. There is no catalogue and no network: what the user installs is a
|
|
// file they already have, which is the whole point of the format.
|
|
//
|
|
// Layout of the archive:
|
|
//
|
|
// plugin.json the manifest (required)
|
|
// <library> the shared library (.so / .dll / .dylib)
|
|
//
|
|
// The manifest names what the plugin is, so the application can tell the user
|
|
// what it is about to run before it is written anywhere:
|
|
//
|
|
// { "format": 1, "id": "flatpak", "name": "Flatpak", "kind": "provider",
|
|
// "version": "1.0.0", "author": "FWS", "description": "…",
|
|
// "library": "libflatpak-provider.so", "sha256": "…" }
|
|
//
|
|
// Installing a package runs third-party code inside this process, so the
|
|
// reader stays deliberately strict: the archive has to be a ZIP, the manifest
|
|
// has to be a well-formed object, the library has to be one of the entries, its
|
|
// name has to be safe to write, and its checksum — when the manifest announces
|
|
// one — has to match before anything lands in the plugin directory. The library
|
|
// is written through QSaveFile, so it appears there complete, under its final
|
|
// name, and never half-installed.
|
|
class PluginPackage : public QObject {
|
|
Q_OBJECT
|
|
public:
|
|
// What the manifest declares about a plugin. Everything but the id is
|
|
// there for the user: the plugin itself still reports its own identity
|
|
// through the C ABI once loaded, and the registry stays the authority on
|
|
// what is actually installed.
|
|
struct Manifest {
|
|
int format = 0;
|
|
QString id;
|
|
QString name;
|
|
QString description;
|
|
QString version;
|
|
QString author;
|
|
QString kind; // "provider" or "addon"; empty when unstated
|
|
QString library; // file name to write in the plugin directory
|
|
QString sha256; // optional checksum of that library
|
|
};
|
|
|
|
// Outcome of reading a package, before anything is written: what the
|
|
// manifest says and which library it points at.
|
|
struct Contents {
|
|
Manifest manifest;
|
|
QByteArray library;
|
|
QString libraryName; // name the library is written under
|
|
};
|
|
|
|
explicit PluginPackage(QObject* parent = nullptr);
|
|
|
|
// ---- Format ----
|
|
// The extension a package file carries.
|
|
static QString suffix();
|
|
// Whether a path looks like a package. Used by the window to accept a drop
|
|
// without having to open the file first.
|
|
static bool isPackagePath(const QString& path);
|
|
|
|
// ---- Installation ----
|
|
// Directory packages are installed into (created on demand). Defaults to the
|
|
// per-user plugin directory PluginManager already scans; overridable so the
|
|
// tests never touch the user's real one.
|
|
QString installDirectory() const;
|
|
void setInstallDirectory(const QString& path);
|
|
|
|
// Installs a package, then emits installFinished(). The file is only
|
|
// written once the manifest, the library and the checksum have all been
|
|
// accepted, so a refused package leaves the plugin directory untouched.
|
|
void install(const QString& path);
|
|
// Removes a package this class installed, and emits removeFinished(). A
|
|
// library the user copied in by hand is never touched.
|
|
void remove(const QString& id);
|
|
|
|
// True when a package with that id was installed here and its file is
|
|
// still on disk.
|
|
bool isInstalled(const QString& id) const;
|
|
// Absolute path of the library installed for that id (empty when none).
|
|
QString installedPath(const QString& id) const;
|
|
// Every id installed from a package, for the page that lists them.
|
|
QStringList installedIds() const;
|
|
|
|
// Reads a package without writing anything: `error` is filled (and the
|
|
// result empty) when the file is not a usable package. Pure logic, so it is
|
|
// unit-tested without touching the disk.
|
|
static Contents read(const QString& path, QString* error);
|
|
|
|
// Parses a manifest. `error` is filled when the document is not a manifest
|
|
// or when it lacks what an install needs (an id, and a library the archive
|
|
// actually contains).
|
|
static Manifest parseManifest(const QByteArray& json, const ZipArchive& archive,
|
|
QString* error);
|
|
|
|
// The name a library entry is written under in the plugin directory. An
|
|
// entry the manifest does not name is found by its library suffix, so a
|
|
// package does not have to agree with the script on the name.
|
|
static QString targetFileName(const Manifest& manifest, const ZipArchive& archive,
|
|
QString* error);
|
|
|
|
// The platform's shared-library suffix.
|
|
static QString librarySuffix();
|
|
|
|
signals:
|
|
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 library installed for a package id.
|
|
static QString installedKey(const QString& id);
|
|
// Settings key prefix listing every installed package.
|
|
static QString installedKeyPrefix();
|
|
|
|
QString m_installDirectory; // empty = the per-user plugin directory
|
|
};
|