From 8c80b142925840a6ae74ad227f5d7d07cae1fef4 Mon Sep 17 00:00:00 2001 From: Aaron Seigo Date: Tue, 16 Dec 2014 09:03:24 +0100 Subject: first run at a plugin loader --- common/resource.cpp | 55 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 55 insertions(+) (limited to 'common/resource.cpp') diff --git a/common/resource.cpp b/common/resource.cpp index 26d57d5..62c521d 100644 --- a/common/resource.cpp +++ b/common/resource.cpp @@ -20,6 +20,11 @@ #include "resource.h" +#include +#include +#include +#include + namespace Akonadi2 { @@ -34,6 +39,14 @@ Resource::~Resource() //delete d; } +class ResourceFactory::Private +{ +public: + static QHash > s_loadedFactories; +}; + +QHash > ResourceFactory::Private::s_loadedFactories; + ResourceFactory::ResourceFactory(QObject *parent) : QObject(parent), d(0) @@ -46,4 +59,46 @@ ResourceFactory::~ResourceFactory() //delete d; } +ResourceFactory *ResourceFactory::load(const QString &resourceName) +{ + ResourceFactory *factory = Private::s_loadedFactories.value(resourceName); + if (factory) { + return factory; + } + + for (auto const &path: QCoreApplication::instance()->libraryPaths()) { + if (path.endsWith(QLatin1String("plugins"))) { + QDir pluginDir(path); + pluginDir.cd(QStringLiteral("akonadi2")); + + for (const QString &fileName: pluginDir.entryList(QDir::Files)) { + const QString path = pluginDir.absoluteFilePath(fileName); + QPluginLoader loader(path); + + const QString id = loader.metaData()[QStringLiteral("IID")].toString(); + if (id == resourceName) { + QObject *object = loader.instance(); + if (object) { + factory = qobject_cast(object); + if (factory) { + Private::s_loadedFactories.insert(resourceName, factory); + factory->registerFacades(FacadeFactory::instance()); + //TODO: if we need more data on it const QJsonObject json = loader.metaData()[QStringLiteral("MetaData")].toObject(); + return factory; + } else { + qWarning() << "Plugin for" << resourceName << "from plugin" << loader.fileName() << "produced the wrong object type:" << object; + delete object; + } + } else { + qWarning() << "Could not load factory for" << resourceName << "from plugin" << loader.fileName() << "due to the following error:" << loader.errorString(); + } + } + } + } + } + + qWarning() << "Failed to find factory for resource:" << resourceName; + return nullptr; +} + } // namespace Akonadi2 -- cgit v1.2.3