KDECore
klibrary.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
00020 #include "klibrary.h"
00021
00022 #include <QtCore/QDir>
00023 #include <QtCore/QPointer>
00024
00025 #include <kcomponentdata.h>
00026 #include <kstandarddirs.h>
00027 #include <kpluginfactory.h>
00028 #include <kdebug.h>
00029
00030 extern QString makeLibName( const QString &libname );
00031
00032 extern QString findLibraryInternal(const QString &name, const KComponentData &cData);
00033
00034
00035 QString findLibrary(const QString &name, const KComponentData &cData)
00036 {
00037 QString libname = findLibraryInternal(name, cData);
00038 #ifdef Q_OS_WIN
00039
00040 if( libname.isEmpty() )
00041 {
00042 libname = name;
00043 QString file, path;
00044
00045 int pos = libname.lastIndexOf( '/' );
00046 if ( pos >= 0 )
00047 {
00048 file = libname.mid( pos + 1 );
00049 path = libname.left( pos );
00050 libname = path + '/' + file.mid( 3 );
00051 }
00052 else
00053 {
00054 file = libname;
00055 libname = file.mid( 3 );
00056 }
00057 if( !file.startsWith( "lib" ) )
00058 return file;
00059
00060 libname = findLibraryInternal(libname, cData);
00061 if( libname.isEmpty() )
00062 libname = name;
00063 }
00064 #endif
00065 return libname;
00066 }
00067
00068
00069 KLibrary::KLibrary(QObject *parent)
00070 : QLibrary(parent), d_ptr(0)
00071 {
00072 }
00073
00074 KLibrary::KLibrary(const QString &name, const KComponentData &cData, QObject *parent)
00075 : QLibrary(findLibrary(name, cData), parent), d_ptr(0)
00076 {
00077 }
00078
00079 KLibrary::KLibrary(const QString &name, int verNum, const KComponentData &cData, QObject *parent)
00080 : QLibrary(findLibrary(name, cData), verNum, parent), d_ptr(0)
00081 {
00082 }
00083
00084 KLibrary::~KLibrary()
00085 {
00086 }
00087
00088 typedef QHash<QString, QPointer<KPluginFactory> > FactoryHash;
00089
00090 K_GLOBAL_STATIC(FactoryHash, s_createdKde3Factories)
00091
00092 static KPluginFactory* kde3Factory(KLibrary *lib, const QByteArray &factoryname)
00093 {
00094 QByteArray symname = "init_";
00095 if(!factoryname.isEmpty()) {
00096 symname += factoryname;
00097 } else {
00098 symname += QFileInfo(lib->fileName()).fileName().split('.').first().toLatin1();
00099 }
00100
00101 const QString hashKey = lib->fileName() + QLatin1Char(':') + QString::fromAscii(symname);
00102 KPluginFactory *factory = s_createdKde3Factories->value(hashKey);
00103 if (factory) {
00104 return factory;
00105 }
00106
00107 typedef KPluginFactory* (*t_func)();
00108 t_func func = reinterpret_cast<t_func>(lib->resolveFunction( symname ));
00109 if ( !func )
00110 {
00111 #ifdef Q_OS_WIN
00112
00113 if (!factoryname.startsWith("lib"))
00114 return kde3Factory(lib, QByteArray("lib")+symname.mid(5 ));
00115 #endif
00116 kDebug(150) << "The library" << lib->fileName() << "does not offer an"
00117 << symname << "function.";
00118 return 0;
00119 }
00120
00121 factory = func();
00122
00123 if( !factory )
00124 {
00125 kDebug(150) << "The library" << lib->fileName() << "does not offer a KDE compatible factory.";
00126 return 0;
00127 }
00128 s_createdKde3Factories->insert(hashKey, factory);
00129
00130 return factory;
00131 }
00132
00133 static KPluginFactory *kde4Factory(KLibrary *lib)
00134 {
00135 const QByteArray symname("qt_plugin_instance");
00136
00137 typedef QObject* (*t_func)();
00138 t_func func = reinterpret_cast<t_func>(lib->resolveFunction(symname));
00139 if ( !func )
00140 {
00141 kDebug(150) << "The library" << lib->fileName() << "does not offer a qt_plugin_instance function.";
00142 return 0;
00143 }
00144
00145 QObject* instance = func();
00146 KPluginFactory *factory = qobject_cast<KPluginFactory *>(instance);
00147
00148 if( !factory )
00149 {
00150 if (instance)
00151 kDebug(150) << "Expected a KPluginFactory, got a" << instance->metaObject()->className();
00152 kDebug(150) << "The library" << lib->fileName() << "does not offer a KDE 4 compatible factory.";
00153 return 0;
00154 }
00155 return factory;
00156 }
00157
00158 KPluginFactory* KLibrary::factory(const char* factoryname)
00159 {
00160 KPluginFactory *factory = kde4Factory(this);
00161 if (!factory)
00162 factory = kde3Factory(this, factoryname);
00163
00164 return factory;
00165 }
00166
00167 void *KLibrary::resolveSymbol( const char* symname )
00168 {
00169 return resolve( symname );
00170 }
00171
00172 KLibrary::void_function_ptr KLibrary::resolveFunction( const char* symname )
00173 {
00174 void *psym = resolve( symname );
00175 if (!psym)
00176 return 0;
00177
00178
00179
00180 ptrdiff_t tmp = reinterpret_cast<ptrdiff_t>(psym);
00181 void_function_ptr sym = reinterpret_cast<void_function_ptr>(tmp);
00182
00183 return sym;
00184 }
00185
00186 void KLibrary::setFileName(const QString &name, const KComponentData &data)
00187 {
00188 QLibrary::setFileName(findLibrary(name, data));
00189 }
00190
00191 #include "klibrary.moc"