diff --git a/src/common/plugin.cpp b/src/common/plugin.cpp index a8709a3e2..eb705822f 100644 --- a/src/common/plugin.cpp +++ b/src/common/plugin.cpp @@ -20,52 +20,14 @@ #include "config.h" -#include -#include -#include - -Q_LOGGING_CATEGORY(lcPluginLoader, "pluginLoader", QtInfoMsg) - namespace OCC { PluginFactory::~PluginFactory() = default; -QString PluginLoader::pluginName(const QString &type, const QString &name) +QString pluginFileName(const QString &type, const QString &name) { return QString(QLatin1String("%1sync_%2_%3")) .arg(APPLICATION_EXECUTABLE, type, name); } -bool PluginLoader::isAvailable(const QString &type, const QString &name) -{ - QPluginLoader loader(pluginName(type, name)); - return loader.load(); -} - -QObject *PluginLoader::load(const QString &type, const QString &name) -{ - QString fileName = pluginName(type, name); - QPluginLoader pluginLoader(fileName); - if (pluginLoader.load()) { - qCInfo(lcPluginLoader) << "Loaded plugin" << fileName; - } else { - qCWarning(lcPluginLoader) << "Could not load plugin" - << fileName <<":" - << pluginLoader.errorString() - << "from" << QDir::currentPath(); - } - - return pluginLoader.instance(); -} - -QObject *PluginLoader::create(const QString &type, const QString &name, QObject *parent) -{ - auto factory = qobject_cast(load(type, name)); - if (!factory) { - return nullptr; - } else { - return factory->create(parent); - } -} - } diff --git a/src/common/plugin.h b/src/common/plugin.h index c9f9fc918..df17ccc5c 100644 --- a/src/common/plugin.h +++ b/src/common/plugin.h @@ -20,7 +20,6 @@ #include "ocsynclib.h" #include -#include namespace OCC { @@ -41,15 +40,8 @@ public: } }; -class OCSYNC_EXPORT PluginLoader -{ -public: - static QString pluginName(const QString &type, const QString &name); - - bool isAvailable(const QString &type, const QString &name); - QObject *load(const QString &type, const QString &name); - QObject *create(const QString &type, const QString &name, QObject *parent = nullptr); -}; +/// Return the expected name of a plugin, for use with QPluginLoader +QString pluginFileName(const QString &type, const QString &name); } diff --git a/src/common/vfs.cpp b/src/common/vfs.cpp index 896b31db0..9809ff4a6 100644 --- a/src/common/vfs.cpp +++ b/src/common/vfs.cpp @@ -19,6 +19,9 @@ #include "vfs.h" #include "plugin.h" +#include +#include + using namespace OCC; Vfs::Vfs(QObject* parent) @@ -69,7 +72,7 @@ bool OCC::isVfsPluginAvailable(Vfs::Mode mode) auto name = modeToPluginName(mode); if (name.isEmpty()) return false; - return PluginLoader().load("vfs", name); + return QPluginLoader(pluginFileName("vfs", name)).load(); } Vfs::Mode OCC::bestAvailableVfsMode() @@ -82,10 +85,34 @@ Vfs::Mode OCC::bestAvailableVfsMode() return Vfs::Off; } +Q_LOGGING_CATEGORY(lcPlugin, "plugins", QtInfoMsg) + Vfs *OCC::createVfsFromPlugin(Vfs::Mode mode, QObject *parent) { auto name = modeToPluginName(mode); if (name.isEmpty()) return nullptr; - return qobject_cast(PluginLoader().create("vfs", name, parent)); + + auto pluginPath = pluginFileName("vfs", name); + QPluginLoader loader(pluginPath); + auto plugin = loader.instance(); + if (!plugin) { + qCWarning(lcPlugin) << "Could not load plugin" << pluginPath << loader.errorString(); + return nullptr; + } + + auto factory = qobject_cast(plugin); + if (!factory) { + qCWarning(lcPlugin) << "Plugin" << pluginPath << "does not implement PluginFactory"; + return nullptr; + } + + auto vfs = qobject_cast(factory->create(parent)); + if (!vfs) { + qCWarning(lcPlugin) << "Plugin" << pluginPath << "does not create a Vfs instance"; + return nullptr; + } + + qCInfo(lcPlugin) << "Created VFS instance from plugin" << pluginPath; + return vfs; }