• Skip to content
  • Skip to link menu
KDE 4.3 API Reference
  • KDE API Reference
  • kdelibs
  • Sitemap
  • Contact Us
 

KDECore

klibrary.cpp

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    Copyright (C) 2000 Michael Matz <matz@kde.org>
00004    Copyright (C) 2007 Bernhard Loos <nhuh.put@web.de.org>
00005 
00006    This library is free software; you can redistribute it and/or
00007    modify it under the terms of the GNU Library General Public
00008    License version 2 as published by the Free Software Foundation.
00009 
00010    This library is distributed in the hope that it will be useful,
00011    but WITHOUT ANY WARRANTY; without even the implied warranty of
00012    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013    Library General Public License for more details.
00014 
00015    You should have received a copy of the GNU Library General Public License
00016    along with this library; see the file COPYING.LIB.  If not, write to
00017    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
00018    Boston, MA 02110-1301, USA.
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 //static
00035 QString findLibrary(const QString &name, const KComponentData &cData)
00036 {
00037     QString libname = findLibraryInternal(name, cData);
00038 #ifdef Q_OS_WIN
00039     // we don't have 'lib' prefix on windows -> remove it and try again
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         // a backup for cases when developer has set lib prefix for a plugin name (she should not...)
00113         if (!factoryname.startsWith("lib"))
00114             return kde3Factory(lib, QByteArray("lib")+symname.mid(5 /*"init_"*/));
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     // Cast the void* to non-pointer type first - it's not legal to
00179     // cast a pointer-to-object directly to a pointer-to-function.
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"

KDECore

Skip menu "KDECore"
  • Main Page
  • Modules
  • Namespace List
  • Class Hierarchy
  • Alphabetical List
  • Class List
  • File List
  • Namespace Members
  • Class Members
  • Related Pages

kdelibs

Skip menu "kdelibs"
  • DNSSD
  • Interfaces
  •   KHexEdit
  •   KMediaPlayer
  •   KSpeech
  •   KTextEditor
  • Kate
  • kconf_update
  • KDE3Support
  •   KUnitTest
  • KDECore
  • KDED
  • KDEsu
  • KDEUI
  • KDocTools
  • KFile
  • KHTML
  • KImgIO
  • KInit
  • kio
  • KIOSlave
  • KJS
  •   KJS-API
  •   WTF
  • kjsembed
  • KNewStuff
  • KParts
  • KPty
  • Kross
  • KUtils
  • Nepomuk
  • Plasma
  • Solid
  • Sonnet
  • ThreadWeaver
Generated for kdelibs by doxygen 1.6.1
This website is maintained by Adriaan de Groot and Allen Winter.
KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal