diff options
author | Aaron Seigo <aseigo@kde.org> | 2014-12-16 09:03:24 +0100 |
---|---|---|
committer | Aaron Seigo <aseigo@kde.org> | 2014-12-16 09:03:24 +0100 |
commit | 8c80b142925840a6ae74ad227f5d7d07cae1fef4 (patch) | |
tree | 3cfdad12156a3aaa163b632298dce26df454d506 /common/resource.cpp | |
parent | a092eff6d12e6dff5ecba3deb63820a7735ad4b9 (diff) | |
download | sink-8c80b142925840a6ae74ad227f5d7d07cae1fef4.tar.gz sink-8c80b142925840a6ae74ad227f5d7d07cae1fef4.zip |
first run at a plugin loader
Diffstat (limited to 'common/resource.cpp')
-rw-r--r-- | common/resource.cpp | 55 |
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 | |||
23 | namespace Akonadi2 | 28 | namespace Akonadi2 |
24 | { | 29 | { |
25 | 30 | ||
@@ -34,6 +39,14 @@ Resource::~Resource() | |||
34 | //delete d; | 39 | //delete d; |
35 | } | 40 | } |
36 | 41 | ||
42 | class ResourceFactory::Private | ||
43 | { | ||
44 | public: | ||
45 | static QHash<QString, QPointer<ResourceFactory> > s_loadedFactories; | ||
46 | }; | ||
47 | |||
48 | QHash<QString, QPointer<ResourceFactory> > ResourceFactory::Private::s_loadedFactories; | ||
49 | |||
37 | ResourceFactory::ResourceFactory(QObject *parent) | 50 | ResourceFactory::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 | ||
62 | ResourceFactory *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 |