KDECore
klibloader.h
Go to the documentation of this file.
00001 /* This file is part of the KDE libraries 00002 Copyright (C) 1999 Torben Weis <weis@kde.org> 00003 00004 This library is free software; you can redistribute it and/or 00005 modify it under the terms of the GNU Library General Public 00006 License version 2 as published by the Free Software Foundation. 00007 00008 This library is distributed in the hope that it will be useful, 00009 but WITHOUT ANY WARRANTY; without even the implied warranty of 00010 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00011 Library General Public License for more details. 00012 00013 You should have received a copy of the GNU Library General Public License 00014 along with this library; see the file COPYING.LIB. If not, write to 00015 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, 00016 Boston, MA 02110-1301, USA. 00017 */ 00018 #ifndef KLIBLOADER_H 00019 #define KLIBLOADER_H 00020 00021 #include <kglobal.h> 00022 00023 #include <QtCore/QObject> 00024 #include <QtCore/QStringList> 00025 #include <QtCore/QHash> 00026 #include <QtCore/QLibrary> 00027 #include <QtCore/QtPlugin> 00028 00029 #include "kpluginfactory.h" 00030 #include "kpluginloader.h" 00031 #include "klibrary.h" 00032 00033 #ifndef KDE_NO_DEPRECATED 00034 00035 # define K_EXPORT_COMPONENT_FACTORY( libname, factory ) \ 00036 extern "C" { KDE_EXPORT KPluginFactory *init_##libname() { return new factory; } } 00037 00055 class KDECORE_EXPORT KLibLoader : public QObject //krazy:exclude=dpointer (private class is kept as a global static) 00056 { 00057 friend class KLibrary; 00058 friend class KLibraryPrivate; 00059 friend class KLibLoaderPrivate; 00060 00061 Q_OBJECT 00062 public: 00084 KPluginFactory* factory( const QString &libname, QLibrary::LoadHints loadHint = 0); 00085 00107 KLibrary* library( const QString &libname, QLibrary::LoadHints loadHint = 0 ); 00108 00116 QString lastErrorMessage() const; 00117 00132 void unloadLibrary( const QString &libname ); 00133 00144 static KDE_DEPRECATED KLibLoader* self(); 00145 00157 static QString findLibrary(const QString &libname, const KComponentData &cData = KGlobal::mainComponent()); 00158 00165 enum ComponentLoadingError { 00166 ErrNoLibrary = 1, /*< the specified library could not be loaded. Use KLibLoader::lastErrorMessage for details*/ 00167 ErrNoFactory, /*< the library does not export a factory */ 00168 ErrNoComponent, /*< the factory does not support creating components of the specified type */ 00169 ErrServiceProvidesNoLibrary, /*< the specified service provides no shared library (when using KService) */ 00170 ErrNoServiceFound /*< no service implementing the given servicetype and fullfilling the given constraint expression can be found (when using KServiceTypeTrader) */ 00171 }; 00172 00183 static QString errorString( int componentLoadingError ); 00184 00185 00201 template <typename T> 00202 static KDE_DEPRECATED T *createInstance(const QString &keyword, const QString &libname, QObject *parent = 0, 00203 const QVariantList &args = QVariantList(), 00204 int *error = 0 ) 00205 { 00206 KLibrary *library = KLibLoader::self()->library( libname ); 00207 if ( !library ) 00208 { 00209 if ( error ) 00210 *error = ErrNoLibrary; 00211 return 0; 00212 } 00213 KPluginFactory *factory = library->factory(); 00214 if ( !factory ) 00215 { 00216 library->unload(); 00217 if ( error ) 00218 *error = ErrNoFactory; 00219 return 0; 00220 } 00221 QObject *object = factory->template create<T>(keyword, parent, args); 00222 T *res = qobject_cast<T *>( object ); 00223 if ( !res ) 00224 { 00225 delete object; 00226 library->unload(); 00227 if ( error ) 00228 *error = ErrNoComponent; 00229 } 00230 return res; 00231 } 00232 00247 template <typename T> 00248 static KDE_DEPRECATED T *createInstance( const QString &libname, QObject *parent = 0, 00249 const QVariantList &args = QVariantList(), 00250 int *error = 0 ) 00251 { 00252 return createInstance<T>(QString(), libname, parent, args, error); 00253 } 00254 00259 template <typename T> 00260 static KDE_DEPRECATED T *createInstance( const QString &libname, QObject *parent, 00261 const QStringList &args, 00262 int *error = 0 ) 00263 { 00264 KLibrary *library = KLibLoader::self()->library( libname ); 00265 if ( !library ) 00266 { 00267 if ( error ) 00268 *error = ErrNoLibrary; 00269 return 0; 00270 } 00271 KPluginFactory *factory = library->factory(); 00272 if ( !factory ) 00273 { 00274 library->unload(); 00275 if ( error ) 00276 *error = ErrNoFactory; 00277 return 0; 00278 } 00279 QObject *object = factory->template create<T>(parent, args); 00280 T *res = qobject_cast<T *>( object ); 00281 if ( !res ) 00282 { 00283 delete object; 00284 library->unload(); 00285 if ( error ) 00286 *error = ErrNoComponent; 00287 } 00288 return res; 00289 } 00290 00291 private: 00292 ~KLibLoader(); 00293 00294 KLibLoader(); 00295 }; 00296 00297 #endif 00298 #endif
KDE 4.6 API Reference