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>
24 #include "kpluginfactory.h"
29 #include <QtCore/QLibrary>
30 #include <QtCore/QDir>
31 #include <QtCore/QFileInfo>
34 class KPluginLoaderPrivate
36 Q_DECLARE_PUBLIC(KPluginLoader
)
38 KPluginLoaderPrivate(const QString
&libname
)
39 : name(libname
), pluginVersion(~0U), verificationData(0), lib(0)
41 ~KPluginLoaderPrivate()
48 quint32 pluginVersion
;
49 KDEPluginVerificationData
*verificationData
;
55 inline QString
makeLibName( const QString
&libname
)
58 if (!libname
.endsWith(".dll"))
59 return libname
+ ".dll";
62 int pos
= libname
.lastIndexOf('/');
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
];
77 extern QString
fixLibPrefix(const QString
& libname
);
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");
88 kDebug(150) << "plugins should not have a 'lib' prefix:" << libname
;
91 if (QDir::isRelativePath(libname
)) {
92 libfile
= cData
.dirs()->findResource("module", libname
);
93 if (libfile
.isEmpty()) {
95 libname
= fixLibPrefix(libname
);
98 libname
= fileinfo
.path() + QLatin1String("/lib") + fileinfo
.fileName();
100 libfile
= cData
.dirs()->findResource("lib", libname
);
101 if (!libfile
.isEmpty())
102 kDebug(150) << "library" << libname
<< "not found under 'module' but under 'lib'";
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
))
123 KPluginLoader::KPluginLoader(const KService
&service
, const KComponentData
&componentdata
, QObject
*parent
)
124 : QPluginLoader(findLibraryInternal(service
.library(), componentdata
), parent
), d_ptr(new KPluginLoaderPrivate(service
.library()))
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());
137 KPluginLoader::~KPluginLoader()
142 KPluginFactory
*KPluginLoader::factory()
151 return d
->lib
->factory(d
->name
.toUtf8());
154 QObject
*obj
= instance();
159 KPluginFactory
*factory
= qobject_cast
<KPluginFactory
*>(obj
);
163 d
->errorString
= i18n("The library %1 does not offer a KDE 4 compatible factory." , d
->name
);
169 bool KPluginLoader::load()
172 if (!QPluginLoader::load()) {
173 d
->lib
= new KLibrary(d
->name
);
180 Q_ASSERT(!fileName().isEmpty());
181 QLibrary
lib(fileName());
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
);
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");
202 d
->pluginVersion
= *version
;
204 d
->pluginVersion
= ~0U;
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
);
232 #include "kpluginloader.moc"