fix logic
[personal-kdelibs.git] / kdecore / util / kpluginloader.cpp
blobbc528ce819c36389fd1391fad6985607717655c5
1 /* This file is part of the KDE project
2 Copyright (C) 2007 Bernhard Loos <nhuh.put@web.de>
4 This library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Library General Public
6 License version 2 as published by the Free Software Foundation.
8 This library is distributed in the hope that it will be useful,
9 but WITHOUT ANY WARRANTY; without even the implied warranty of
10 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 Library General Public License for more details.
13 You should have received a copy of the GNU Library General Public License
14 along with this library; see the file COPYING.LIB. If not, write to
15 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
16 Boston, MA 02110-1301, USA.
19 #include "kpluginloader.h"
21 #include <kcomponentdata.h>
22 #include <kstandarddirs.h>
23 #include <klocale.h>
24 #include "kpluginfactory.h"
25 #include <kservice.h>
26 #include "klibrary.h"
27 #include <kdebug.h>
29 #include <QtCore/QLibrary>
30 #include <QtCore/QDir>
31 #include <QtCore/QFileInfo>
34 class KPluginLoaderPrivate
36 Q_DECLARE_PUBLIC(KPluginLoader)
37 protected:
38 KPluginLoaderPrivate(const QString &libname)
39 : name(libname), pluginVersion(~0U), verificationData(0), lib(0)
41 ~KPluginLoaderPrivate()
43 delete lib;
46 KPluginLoader *q_ptr;
47 const QString name;
48 quint32 pluginVersion;
49 KDEPluginVerificationData *verificationData;
50 QString errorString;
52 KLibrary *lib;
55 inline QString makeLibName( const QString &libname )
57 #ifdef Q_OS_WIN
58 if (!libname.endsWith(".dll"))
59 return libname + ".dll";
60 return libname;
61 #else
62 int pos = libname.lastIndexOf('/');
63 if (pos < 0)
64 pos = 0;
65 if (libname.indexOf('.', pos) < 0) {
66 const char* const extList[] = { ".so", ".dylib", ".bundle", ".sl" };
67 for (uint i = 0; i < sizeof(extList) / sizeof(*extList); ++i) {
68 if (QLibrary::isLibrary(libname + extList[i]))
69 return libname + extList[i];
72 return libname;
73 #endif
76 #ifdef Q_OS_WIN
77 extern QString fixLibPrefix(const QString& libname);
78 #endif
80 inline QString findLibraryInternal(const QString &name, const KComponentData &cData)
82 QString libname = makeLibName(name);
84 QFileInfo fileinfo(name);
85 bool hasPrefix = fileinfo.fileName().startsWith("lib");
87 if (hasPrefix)
88 kDebug(150) << "plugins should not have a 'lib' prefix:" << libname;
90 QString libfile;
91 if (QDir::isRelativePath(libname)) {
92 libfile = cData.dirs()->findResource("module", libname);
93 if (libfile.isEmpty()) {
94 #ifdef Q_OS_WIN
95 libname = fixLibPrefix(libname);
96 #else
97 if (!hasPrefix)
98 libname = fileinfo.path() + QLatin1String("/lib") + fileinfo.fileName();
99 #endif
100 libfile = cData.dirs()->findResource("lib", libname);
101 if (!libfile.isEmpty())
102 kDebug(150) << "library" << libname << "not found under 'module' but under 'lib'";
105 else {
106 libfile = libname;
108 return libfile;
111 bool KPluginLoader::isLoaded() const
113 return QPluginLoader::isLoaded() || d_ptr->lib;
116 KPluginLoader::KPluginLoader(const QString &plugin, const KComponentData &componentdata, QObject *parent)
117 : QPluginLoader(findLibraryInternal(plugin, componentdata), parent), d_ptr(new KPluginLoaderPrivate(plugin))
119 d_ptr->q_ptr = this;
120 load();
123 KPluginLoader::KPluginLoader(const KService &service, const KComponentData &componentdata, QObject *parent)
124 : QPluginLoader(findLibraryInternal(service.library(), componentdata), parent), d_ptr(new KPluginLoaderPrivate(service.library()))
126 d_ptr->q_ptr = this;
127 Q_D(KPluginLoader);
128 Q_ASSERT(service.isValid());
130 if (service.library().isEmpty()) {
131 d->errorString = i18n("The service '%1' provides no library or the Library key is missing in the .desktop file.", service.name());
132 return;
134 load();
137 KPluginLoader::~KPluginLoader()
139 delete d_ptr;
142 KPluginFactory *KPluginLoader::factory()
144 Q_D(KPluginLoader);
146 if (!isLoaded())
147 return 0;
150 if (d->lib) {
151 return d->lib->factory(d->name.toUtf8());
154 QObject *obj = instance();
156 if (!obj)
157 return 0;
159 KPluginFactory *factory = qobject_cast<KPluginFactory *>(obj);
161 if (factory == 0) {
162 delete obj;
163 d->errorString = i18n("The library %1 does not offer a KDE 4 compatible factory." , d->name);
166 return factory;
169 bool KPluginLoader::load()
171 Q_D(KPluginLoader);
172 if (!QPluginLoader::load()) {
173 d->lib = new KLibrary(d->name);
174 if (d->lib->load())
175 return true;
177 return false;
180 Q_ASSERT(!fileName().isEmpty());
181 QLibrary lib(fileName());
182 lib.load();
183 Q_ASSERT(lib.isLoaded());
185 d->verificationData = (KDEPluginVerificationData *) lib.resolve("kde_plugin_verification_data");
186 if (d->verificationData) {
187 if (d->verificationData->dataVersion < KDEPluginVerificationData::PluginVerificationDataVersion
188 || (d->verificationData->KDEVersion > KDE_VERSION)
189 || (KDE_VERSION_MAJOR << 16 != (d->verificationData->KDEVersion & 0xFF0000)))
191 d->errorString = i18n("The plugin '%1' uses an incompatible KDE library (%2).", d->name, d->verificationData->KDEVersionString);
192 lib.unload();
193 unload();
194 return false;
196 } else {
197 kDebug(150) << "The plugin" << d->name << "doesn't contain a kde_plugin_verification_data structure";
200 quint32 *version = (quint32 *) lib.resolve("kde_plugin_version");
201 if (version)
202 d->pluginVersion = *version;
203 else
204 d->pluginVersion = ~0U;
206 lib.unload();
208 return true;
211 QString KPluginLoader::errorString() const
213 Q_D(const KPluginLoader);
214 if (!d->errorString.isEmpty())
215 return d->errorString;
217 return QPluginLoader::errorString();
220 quint32 KPluginLoader::pluginVersion() const
222 Q_D(const KPluginLoader);
223 return d->pluginVersion;
226 QString KPluginLoader::pluginName() const
228 Q_D(const KPluginLoader);
229 return d->name;
232 #include "kpluginloader.moc"