feat(plugin): support generic addon plugins and build Flatpak by default
The Plugins page replaces the Providers tab and lists every plugin alike: package-manager providers and generic C++ addons, which can be any extension (UI tweak, helper, ...) via IPlugin (onLoad/onUnload/onUiReady) exported with the new FWS_ADDON_PLUGIN macro. Flatpak now ships as a plugin built by default (FWS_BUILD_FLATPAK_PLUGIN=ON), so a plain build is Flatpak-enabled. Generated with Codebuff 🤖 Co-Authored-By: Codebuff <noreply@codebuff.com>
This commit is contained in:
12 files changed
+535
-250
No files matched your search
+93
-41
@@ -14,32 +14,49 @@ static QStringList pluginSearchPaths() {
|
||||
return paths;
|
||||
}
|
||||
|
||||
// Run a plugin's teardown and free what the host owns: the addon/provider
|
||||
// instance (after onUnload) and the library it was compiled into.
|
||||
static void unloadEntry(PluginManager::PluginEntry& entry) {
|
||||
if (entry.plugin) {
|
||||
entry.plugin->onUnload();
|
||||
delete entry.plugin;
|
||||
entry.plugin = nullptr;
|
||||
}
|
||||
if (entry.manager) {
|
||||
delete entry.manager;
|
||||
entry.manager = nullptr;
|
||||
}
|
||||
delete entry.library;
|
||||
entry.library = nullptr;
|
||||
}
|
||||
|
||||
PluginManager::PluginManager() = default;
|
||||
|
||||
PluginManager::~PluginManager() {
|
||||
// Delete managers first (they may run code), then unload the libraries
|
||||
// they were compiled into.
|
||||
for (auto& entry : m_providers) {
|
||||
delete entry.manager;
|
||||
delete entry.library;
|
||||
// Unload plugins first (they may run code), then the libraries they were
|
||||
// compiled into.
|
||||
for (auto& entry : m_plugins) {
|
||||
unloadEntry(entry);
|
||||
}
|
||||
}
|
||||
|
||||
void PluginManager::registerBuiltin(ProviderInfo info, IPackageManager* manager) {
|
||||
void PluginManager::registerBuiltin(PluginInfo info, IPackageManager* manager) {
|
||||
info.builtin = true;
|
||||
info.available = manager->isAvailable();
|
||||
m_providers.push_back({std::move(info), manager, nullptr});
|
||||
m_plugins.push_back({std::move(info), nullptr, manager, nullptr});
|
||||
}
|
||||
|
||||
// Load a single shared library as a provider plugin. Returns false (without
|
||||
// erroring) when the file is not a provider plugin, so unrelated libraries in
|
||||
// the directory are skipped.
|
||||
// Load a single shared library as a plugin. Returns false (without erroring)
|
||||
// when the file is not a plugin, so unrelated libraries in the directory are
|
||||
// skipped. The plugin's kind decides which create function is resolved:
|
||||
// "addon" -> IPlugin (see plugin.h), anything else -> IPackageManager.
|
||||
bool PluginManager::loadPluginFile(const QString& filePath) {
|
||||
typedef const char* (*MetaFn)();
|
||||
typedef IPackageManager* (*CreateFn)();
|
||||
typedef IPackageManager* (*CreateProviderFn)();
|
||||
typedef IPlugin* (*CreatePluginFn)();
|
||||
|
||||
// Own the library for the lifetime of the provider: it must stay loaded
|
||||
// as long as the manager instance created from it exists.
|
||||
// Own the library for the lifetime of the plugin: it must stay loaded
|
||||
// as long as the addon/manager instance created from it exists.
|
||||
QLibrary* library = new QLibrary(filePath);
|
||||
if (!library->load()) {
|
||||
delete library;
|
||||
@@ -51,19 +68,23 @@ bool PluginManager::loadPluginFile(const QString& filePath) {
|
||||
MetaFn versionFn = reinterpret_cast<MetaFn>(library->resolve("fws_plugin_version"));
|
||||
MetaFn descFn = reinterpret_cast<MetaFn>(library->resolve("fws_plugin_description"));
|
||||
MetaFn authorFn = reinterpret_cast<MetaFn>(library->resolve("fws_plugin_author"));
|
||||
CreateFn createFn = reinterpret_cast<CreateFn>(library->resolve("fws_create_provider"));
|
||||
MetaFn kindFn = reinterpret_cast<MetaFn>(library->resolve("fws_plugin_kind"));
|
||||
CreateProviderFn createProviderFn =
|
||||
reinterpret_cast<CreateProviderFn>(library->resolve("fws_create_provider"));
|
||||
CreatePluginFn createPluginFn =
|
||||
reinterpret_cast<CreatePluginFn>(library->resolve("fws_create_plugin"));
|
||||
// Optional: only present when the plugin declares an install hint.
|
||||
MetaFn installHintFn = reinterpret_cast<MetaFn>(library->resolve("fws_plugin_install_hint"));
|
||||
|
||||
if (!idFn || !nameFn || !versionFn || !descFn || !authorFn || !createFn) {
|
||||
if (!idFn || !nameFn || !versionFn || !descFn || !authorFn) {
|
||||
library->unload();
|
||||
delete library;
|
||||
return false;
|
||||
}
|
||||
|
||||
// Reject plugins whose id collides with an already registered provider.
|
||||
// Reject plugins whose id collides with an already registered plugin.
|
||||
const std::string id = idFn();
|
||||
for (const auto& entry : m_providers) {
|
||||
for (const auto& entry : m_plugins) {
|
||||
if (entry.info.id == id) {
|
||||
library->unload();
|
||||
delete library;
|
||||
@@ -71,21 +92,38 @@ bool PluginManager::loadPluginFile(const QString& filePath) {
|
||||
}
|
||||
}
|
||||
|
||||
ProviderInfo info;
|
||||
// Kind defaults to "provider" so legacy plugins without the symbol keep
|
||||
// loading; "addon" requires the matching create function.
|
||||
const std::string kind = kindFn ? kindFn() : "provider";
|
||||
const bool isAddon = (kind == "addon");
|
||||
if ((isAddon && !createPluginFn) || (!isAddon && !createProviderFn)) {
|
||||
library->unload();
|
||||
delete library;
|
||||
return false;
|
||||
}
|
||||
|
||||
PluginInfo info;
|
||||
info.id = id;
|
||||
info.name = nameFn();
|
||||
info.version = versionFn();
|
||||
info.description = descFn();
|
||||
info.author = authorFn();
|
||||
info.kind = isAddon ? PluginKind::Addon : PluginKind::Provider;
|
||||
info.path = filePath.toStdString();
|
||||
if (installHintFn && installHintFn() && *installHintFn()) {
|
||||
info.installHint = installHintFn();
|
||||
}
|
||||
|
||||
IPackageManager* manager = createFn();
|
||||
info.available = manager->isAvailable();
|
||||
|
||||
m_providers.push_back({std::move(info), manager, library});
|
||||
if (isAddon) {
|
||||
IPlugin* plugin = createPluginFn();
|
||||
plugin->onLoad();
|
||||
info.available = plugin->isAvailable();
|
||||
m_plugins.push_back({std::move(info), plugin, nullptr, library});
|
||||
} else {
|
||||
IPackageManager* manager = createProviderFn();
|
||||
info.available = manager->isAvailable();
|
||||
m_plugins.push_back({std::move(info), nullptr, manager, library});
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -110,11 +148,10 @@ void PluginManager::scanDirectory(const QString& dir) {
|
||||
}
|
||||
|
||||
void PluginManager::unloadExternal() {
|
||||
for (auto it = m_providers.begin(); it != m_providers.end();) {
|
||||
for (auto it = m_plugins.begin(); it != m_plugins.end();) {
|
||||
if (!it->info.builtin) {
|
||||
delete it->manager;
|
||||
delete it->library;
|
||||
it = m_providers.erase(it);
|
||||
unloadEntry(*it);
|
||||
it = m_plugins.erase(it);
|
||||
} else {
|
||||
++it;
|
||||
}
|
||||
@@ -133,19 +170,23 @@ void PluginManager::reload() {
|
||||
}
|
||||
|
||||
void PluginManager::refreshAvailability() {
|
||||
for (auto& entry : m_providers) {
|
||||
entry.info.available = entry.manager->isAvailable();
|
||||
for (auto& entry : m_plugins) {
|
||||
if (entry.plugin) {
|
||||
entry.info.available = entry.plugin->isAvailable();
|
||||
} else {
|
||||
entry.info.available = entry.manager->isAvailable();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const std::vector<PluginManager::ProviderEntry>& PluginManager::providers() const {
|
||||
return m_providers;
|
||||
const std::vector<PluginManager::PluginEntry>& PluginManager::plugins() const {
|
||||
return m_plugins;
|
||||
}
|
||||
|
||||
std::vector<const PluginManager::ProviderEntry*> PluginManager::availableProviders() const {
|
||||
std::vector<const ProviderEntry*> result;
|
||||
for (const auto& entry : m_providers) {
|
||||
if (entry.info.available && isEnabled(entry.info.id)) {
|
||||
std::vector<const PluginManager::PluginEntry*> PluginManager::availableProviders() const {
|
||||
std::vector<const PluginEntry*> result;
|
||||
for (const auto& entry : m_plugins) {
|
||||
if (entry.manager && entry.info.available && isEnabled(entry.info.id)) {
|
||||
result.push_back(&entry);
|
||||
}
|
||||
}
|
||||
@@ -154,23 +195,34 @@ std::vector<const PluginManager::ProviderEntry*> PluginManager::availableProvide
|
||||
|
||||
std::vector<IPackageManager*> PluginManager::availableManagers() const {
|
||||
std::vector<IPackageManager*> result;
|
||||
for (const auto& entry : m_providers) {
|
||||
if (entry.info.available && isEnabled(entry.info.id)) {
|
||||
for (const auto& entry : m_plugins) {
|
||||
if (entry.manager && entry.info.available && isEnabled(entry.info.id)) {
|
||||
result.push_back(entry.manager);
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
const PluginManager::ProviderEntry* PluginManager::findAvailable(const std::string& id) const {
|
||||
for (const auto& entry : m_providers) {
|
||||
if (entry.info.id == id && entry.info.available && isEnabled(entry.info.id)) {
|
||||
const PluginManager::PluginEntry* PluginManager::findAvailable(const std::string& id) const {
|
||||
for (const auto& entry : m_plugins) {
|
||||
if (entry.info.id == id && entry.manager && entry.info.available
|
||||
&& isEnabled(entry.info.id)) {
|
||||
return &entry;
|
||||
}
|
||||
}
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
std::vector<IPlugin*> PluginManager::availableAddons() const {
|
||||
std::vector<IPlugin*> result;
|
||||
for (const auto& entry : m_plugins) {
|
||||
if (entry.plugin && entry.info.available && isEnabled(entry.info.id)) {
|
||||
result.push_back(entry.plugin);
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
bool PluginManager::isEnabled(const std::string& id) const {
|
||||
return m_settings.value(QString("plugins/%1/enabled").arg(QString::fromStdString(id)),
|
||||
true).toBool();
|
||||
@@ -181,8 +233,8 @@ void PluginManager::setEnabled(const std::string& id, bool enabled) {
|
||||
}
|
||||
|
||||
IPackageManager* PluginManager::managerFor(const std::string& id) const {
|
||||
for (const auto& entry : m_providers) {
|
||||
for (const auto& entry : m_plugins) {
|
||||
if (entry.info.id == id) return entry.manager;
|
||||
}
|
||||
return nullptr;
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user