KDECore
klibloader.cpp
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019 #include "klibloader.h"
00020
00021 #include <QtCore/QFile>
00022 #include <QtCore/QDir>
00023 #include <QtCore/QTimer>
00024 #include <QtCore/QLibrary>
00025 #include <QStack>
00026 #include <QtCore/QCoreApplication>
00027 #include <QtCore/QObjectCleanupHandler>
00028
00029 #include "kstandarddirs.h"
00030 #include "kcomponentdata.h"
00031 #include "kdebug.h"
00032 #include "klocale.h"
00033
00034 class KLibLoaderPrivate
00035 {
00036 public:
00037 KLibLoader instance;
00038 QObjectCleanupHandler cleanuphandler;
00039 QString errorString;
00040 };
00041
00042 K_GLOBAL_STATIC(KLibLoaderPrivate, kLibLoaderPrivate)
00043
00044 #define KLIBLOADER_PRIVATE KLibLoaderPrivate *const d = kLibLoaderPrivate
00045
00046 KLibLoader* KLibLoader::self()
00047 {
00048 return &kLibLoaderPrivate->instance;
00049 }
00050
00051 KLibLoader::KLibLoader()
00052 : QObject(0)
00053 {
00054 }
00055
00056 KLibLoader::~KLibLoader()
00057 {
00058 }
00059
00060 extern QString makeLibName( const QString &libname );
00061
00062 extern QString findLibrary(const QString &name, const KComponentData &cData);
00063
00064 #ifdef Q_OS_WIN
00065
00066 QString fixLibPrefix(const QString& libname)
00067 {
00068 int pos = libname.lastIndexOf( '/' );
00069 if ( pos >= 0 )
00070 {
00071 QString file = libname.mid( pos + 1 );
00072 QString path = libname.left( pos );
00073 if( !file.startsWith( "lib" ) )
00074 return libname;
00075 return path + '/' + file.mid( 3 );
00076 }
00077 if( !libname.startsWith( "lib" ) )
00078 return libname;
00079 return libname.mid( 3 );
00080 }
00081 #endif
00082
00083
00084 QString KLibLoader::findLibrary(const QString &_name, const KComponentData &cData)
00085 {
00086 return ::findLibrary(_name, cData);
00087 }
00088
00089 KLibrary* KLibLoader::library( const QString &_name, QLibrary::LoadHints hint )
00090 {
00091 if (_name.isEmpty())
00092 return 0;
00093
00094 KLibrary *lib = new KLibrary(_name);
00095
00096
00097 if (lib->fileName().isEmpty()) {
00098 kLibLoaderPrivate->errorString = i18n("Library \"%1\" not found",_name);
00099 delete lib;
00100 return 0;
00101 }
00102
00103 lib->setLoadHints(hint);
00104
00105 lib->load();
00106
00107 if (!lib->isLoaded()) {
00108 kLibLoaderPrivate->errorString = lib->errorString();
00109 delete lib;
00110 return 0;
00111 }
00112
00113 kLibLoaderPrivate->cleanuphandler.add(lib);
00114
00115 return lib;
00116 }
00117
00118 QString KLibLoader::lastErrorMessage() const
00119 {
00120 return kLibLoaderPrivate->errorString;
00121 }
00122
00123 void KLibLoader::unloadLibrary( const QString &)
00124 {
00125 }
00126
00127 KPluginFactory* KLibLoader::factory( const QString &_name, QLibrary::LoadHints hint )
00128 {
00129 KLibrary* lib = library( _name, hint);
00130 if ( !lib )
00131 return 0;
00132
00133 KPluginFactory* fac = lib->factory();
00134 if ( !fac ) {
00135 kLibLoaderPrivate->errorString = errorString( ErrNoFactory );
00136 return 0;
00137 }
00138
00139 return fac;
00140 }
00141
00142 QString KLibLoader::errorString( int componentLoadingError )
00143 {
00144 switch ( componentLoadingError ) {
00145 case ErrNoServiceFound:
00146 return i18n( "No service matching the requirements was found." );
00147 case ErrServiceProvidesNoLibrary:
00148 return i18n( "The service provides no library, the Library key is missing in the .desktop file." );
00149 case ErrNoLibrary:
00150 return kLibLoaderPrivate->instance.lastErrorMessage();
00151 case ErrNoFactory:
00152 return i18n( "The library does not export a factory for creating components." );
00153 case ErrNoComponent:
00154 return i18n( "The factory does not support creating components of the specified type." );
00155 default:
00156 return i18n( "KLibLoader: Unknown error" );
00157 }
00158 }
00159
00160 #include "klibloader.moc"
00161