summaryrefslogtreecommitdiffstats
path: root/common/resource.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'common/resource.cpp')
-rw-r--r--common/resource.cpp55
1 files changed, 55 insertions, 0 deletions
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 @@
20 20
21#include "resource.h" 21#include "resource.h"
22 22
23#include <QCoreApplication>
24#include <QDir>
25#include <QPluginLoader>
26#include <QPointer>
27
23namespace Akonadi2 28namespace Akonadi2
24{ 29{
25 30
@@ -34,6 +39,14 @@ Resource::~Resource()
34 //delete d; 39 //delete d;
35} 40}
36 41
42class ResourceFactory::Private
43{
44public:
45 static QHash<QString, QPointer<ResourceFactory> > s_loadedFactories;
46};
47
48QHash<QString, QPointer<ResourceFactory> > ResourceFactory::Private::s_loadedFactories;
49
37ResourceFactory::ResourceFactory(QObject *parent) 50ResourceFactory::ResourceFactory(QObject *parent)
38 : QObject(parent), 51 : QObject(parent),
39 d(0) 52 d(0)
@@ -46,4 +59,46 @@ ResourceFactory::~ResourceFactory()
46 //delete d; 59 //delete d;
47} 60}
48 61
62ResourceFactory *ResourceFactory::load(const QString &resourceName)
63{
64 ResourceFactory *factory = Private::s_loadedFactories.value(resourceName);
65 if (factory) {
66 return factory;
67 }
68
69 for (auto const &path: QCoreApplication::instance()->libraryPaths()) {
70 if (path.endsWith(QLatin1String("plugins"))) {
71 QDir pluginDir(path);
72 pluginDir.cd(QStringLiteral("akonadi2"));
73
74 for (const QString &fileName: pluginDir.entryList(QDir::Files)) {
75 const QString path = pluginDir.absoluteFilePath(fileName);
76 QPluginLoader loader(path);
77
78 const QString id = loader.metaData()[QStringLiteral("IID")].toString();
79 if (id == resourceName) {
80 QObject *object = loader.instance();
81 if (object) {
82 factory = qobject_cast<ResourceFactory *>(object);
83 if (factory) {
84 Private::s_loadedFactories.insert(resourceName, factory);
85 factory->registerFacades(FacadeFactory::instance());
86 //TODO: if we need more data on it const QJsonObject json = loader.metaData()[QStringLiteral("MetaData")].toObject();
87 return factory;
88 } else {
89 qWarning() << "Plugin for" << resourceName << "from plugin" << loader.fileName() << "produced the wrong object type:" << object;
90 delete object;
91 }
92 } else {
93 qWarning() << "Could not load factory for" << resourceName << "from plugin" << loader.fileName() << "due to the following error:" << loader.errorString();
94 }
95 }
96 }
97 }
98 }
99
100 qWarning() << "Failed to find factory for resource:" << resourceName;
101 return nullptr;
102}
103
49} // namespace Akonadi2 104} // namespace Akonadi2