• Skip to content
  • Skip to link menu
  • KDE API Reference
  • kdelibs-4.14.38 API Reference
  • KDE Home
  • Contact Us
 

KDECore

  • kdecore
  • util
klibrary.cpp
Go to the documentation of this file.
1/* This file is part of the KDE libraries
2 Copyright (C) 1999 Torben Weis <weis@kde.org>
3 Copyright (C) 2000 Michael Matz <matz@kde.org>
4 Copyright (C) 2007 Bernhard Loos <nhuh.put@web.de.org>
5
6 This library is free software; you can redistribute it and/or
7 modify it under the terms of the GNU Library General Public
8 License version 2 as published by the Free Software Foundation.
9
10 This library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Library General Public License for more details.
14
15 You should have received a copy of the GNU Library General Public License
16 along with this library; see the file COPYING.LIB. If not, write to
17 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18 Boston, MA 02110-1301, USA.
19*/
20#include "klibrary.h"
21
22#include <QtCore/QDir>
23#include <QtCore/QPointer>
24
25#include <kcomponentdata.h>
26#include <kstandarddirs.h>
27#include <kpluginfactory.h>
28#include <kdebug.h>
29
30extern QString makeLibName( const QString &libname );
31extern QString findLibraryInternal(const QString &name, const KComponentData &cData);
32
33int kLibraryDebugArea() {
34 static int s_area = KDebug::registerArea("kdecore (KLibrary)");
35 return s_area;
36}
37
38//static
39QString findLibrary(const QString &name, const KComponentData &cData)
40{
41 QString libname = findLibraryInternal(name, cData);
42#ifdef Q_OS_WIN
43 // we don't have 'lib' prefix on windows -> remove it and try again
44 if( libname.isEmpty() )
45 {
46 libname = name;
47 QString file, path;
48
49 int pos = libname.lastIndexOf( QLatin1Char('/') );
50 if ( pos >= 0 )
51 {
52 file = libname.mid( pos + 1 );
53 path = libname.left( pos );
54 libname = path + QLatin1Char('/') + file.mid( 3 );
55 }
56 else
57 {
58 file = libname;
59 libname = file.mid( 3 );
60 }
61 if( !file.startsWith( QLatin1String("lib") ) )
62 return file;
63
64 libname = findLibraryInternal(libname, cData);
65 if( libname.isEmpty() )
66 libname = name;
67 }
68#endif
69 return libname;
70}
71
72
73KLibrary::KLibrary(QObject *parent)
74 : QLibrary(parent), d_ptr(0)
75{
76}
77
78KLibrary::KLibrary(const QString &name, const KComponentData &cData, QObject *parent)
79 : QLibrary(findLibrary(name, cData), parent), d_ptr(0)
80{
81}
82
83KLibrary::KLibrary(const QString &name, int verNum, const KComponentData &cData, QObject *parent)
84 : QLibrary(findLibrary(name, cData), verNum, parent), d_ptr(0)
85{
86}
87
88KLibrary::~KLibrary()
89{
90}
91
92typedef QHash<QString, QPointer<KPluginFactory> > FactoryHash;
93
94K_GLOBAL_STATIC(FactoryHash, s_createdKde3Factories)
95
96static KPluginFactory* kde3Factory(KLibrary *lib, const QByteArray &factoryname)
97{
98 QByteArray symname = "init_";
99 if(!factoryname.isEmpty()) {
100 symname += factoryname;
101 } else {
102 symname += QFileInfo(lib->fileName()).fileName().split(QLatin1Char('.')).first().toLatin1();
103 }
104
105 const QString hashKey = lib->fileName() + QLatin1Char(':') + QString::fromLatin1(symname);
106 KPluginFactory *factory = s_createdKde3Factories->value(hashKey);
107 if (factory) {
108 return factory;
109 }
110
111 typedef KPluginFactory* (*t_func)();
112 t_func func = reinterpret_cast<t_func>(lib->resolveFunction( symname ));
113 if ( !func )
114 {
115#ifdef Q_OS_WIN
116 // a backup for cases when developer has set lib prefix for a plugin name (she should not...)
117 if (!factoryname.startsWith(QByteArray("lib")))
118 return kde3Factory(lib, QByteArray("lib")+symname.mid(5 /*"init_"*/));
119#endif
120 kDebug(kLibraryDebugArea()) << "The library" << lib->fileName() << "does not offer an"
121 << symname << "function.";
122 return 0;
123 }
124
125 factory = func();
126
127 if( !factory )
128 {
129 kDebug(kLibraryDebugArea()) << "The library" << lib->fileName() << "does not offer a KDE compatible factory.";
130 return 0;
131 }
132 s_createdKde3Factories->insert(hashKey, factory);
133
134 return factory;
135}
136
137static KPluginFactory *kde4Factory(KLibrary *lib)
138{
139 const QByteArray symname("qt_plugin_instance");
140
141 typedef QObject* (*t_func)();
142 t_func func = reinterpret_cast<t_func>(lib->resolveFunction(symname));
143 if ( !func )
144 {
145 kDebug(kLibraryDebugArea()) << "The library" << lib->fileName() << "does not offer a qt_plugin_instance function.";
146 return 0;
147 }
148
149 QObject* instance = func();
150 KPluginFactory *factory = qobject_cast<KPluginFactory *>(instance);
151
152 if( !factory )
153 {
154 if (instance)
155 kDebug(kLibraryDebugArea()) << "Expected a KPluginFactory, got a" << instance->metaObject()->className();
156 kDebug(kLibraryDebugArea()) << "The library" << lib->fileName() << "does not offer a KDE 4 compatible factory.";
157 return 0;
158 }
159 return factory;
160}
161
162// deprecated
163KPluginFactory* KLibrary::factory(const char* factoryname)
164{
165 if (fileName().isEmpty()) {
166 return NULL;
167 }
168
169 KPluginFactory *factory = kde4Factory(this);
170 if (!factory)
171 factory = kde3Factory(this, factoryname);
172
173 return factory;
174}
175
176void *KLibrary::resolveSymbol( const char* symname )
177{
178 return resolve( symname );
179}
180
181KLibrary::void_function_ptr KLibrary::resolveFunction( const char* symname )
182{
183 void *psym = resolve( symname );
184 if (!psym)
185 return 0;
186
187 // Cast the void* to non-pointer type first - it's not legal to
188 // cast a pointer-to-object directly to a pointer-to-function.
189 ptrdiff_t tmp = reinterpret_cast<ptrdiff_t>(psym);
190 void_function_ptr sym = reinterpret_cast<void_function_ptr>(tmp);
191
192 return sym;
193}
194
195void KLibrary::setFileName(const QString &name, const KComponentData &data)
196{
197 QLibrary::setFileName(findLibrary(name, data));
198}
199
200#include "klibrary.moc"
KComponentData
Per component data.
Definition: kcomponentdata.h:47
KDebug::registerArea
static int registerArea(const QByteArray &areaName, bool enabled=true)
Definition: kdebug.cpp:856
KLibrary
Thin wrapper around QLibrary; you should rarely use this directly, see KPluginLoader for higher-level...
Definition: klibrary.h:39
KLibrary::~KLibrary
virtual ~KLibrary()
Definition: klibrary.cpp:88
KLibrary::resolveSymbol
void * resolveSymbol(const char *name)
Looks up a symbol from the library.
Definition: klibrary.cpp:176
KLibrary::setFileName
void setFileName(const QString &name, const KComponentData &data=KGlobal::mainComponent())
Definition: klibrary.cpp:195
KLibrary::void_function_ptr
void(* void_function_ptr)()
Definition: klibrary.h:43
KLibrary::resolveFunction
void_function_ptr resolveFunction(const char *name)
Looks up a symbol from the library.
Definition: klibrary.cpp:181
KLibrary::factory
KPluginFactory * factory(const char *factoryname=0)
Returns the factory of the library.
Definition: klibrary.cpp:163
KLibrary::KLibrary
KLibrary(QObject *parent=0)
Definition: klibrary.cpp:73
KLibrary::fileName
QString fileName
Definition: klibrary.h:41
KPluginFactory
If you develop a library that is to be loaded dynamically at runtime, then you should return a pointe...
Definition: kpluginfactory.h:233
QHash
Definition: ksycocafactory.h:28
QLibrary
QObject
QString
K_GLOBAL_STATIC
#define K_GLOBAL_STATIC(TYPE, NAME)
This macro makes it easy to use non-POD types as global statics.
Definition: kglobal.h:221
kDebug
#define kDebug
Definition: kdebug.h:316
kcomponentdata.h
kdebug.h
findLibrary
QString findLibrary(const QString &name, const KComponentData &cData)
Definition: klibrary.cpp:39
kde3Factory
static KPluginFactory * kde3Factory(KLibrary *lib, const QByteArray &factoryname)
Definition: klibrary.cpp:96
findLibrary
QString findLibrary(const QString &name, const KComponentData &cData)
Definition: klibrary.cpp:39
findLibraryInternal
QString findLibraryInternal(const QString &name, const KComponentData &cData)
Definition: kpluginloader.cpp:83
FactoryHash
QHash< QString, QPointer< KPluginFactory > > FactoryHash
Definition: klibrary.cpp:92
kLibraryDebugArea
int kLibraryDebugArea()
Definition: klibrary.cpp:33
makeLibName
QString makeLibName(const QString &libname)
Definition: kpluginloader.cpp:57
kde4Factory
static KPluginFactory * kde4Factory(KLibrary *lib)
Definition: klibrary.cpp:137
klibrary.h
kpluginfactory.h
kstandarddirs.h
This file is part of the KDE documentation.
Documentation copyright © 1996-2023 The KDE developers.
Generated on Mon Feb 20 2023 00:00:00 by doxygen 1.9.6 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

KDECore

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

kdelibs-4.14.38 API Reference

Skip menu "kdelibs-4.14.38 API Reference"
  • DNSSD
  • Interfaces
  •   KHexEdit
  •   KMediaPlayer
  •   KSpeech
  •   KTextEditor
  • kconf_update
  • KDE3Support
  •   KUnitTest
  • KDECore
  • KDED
  • KDEsu
  • KDEUI
  • KDEWebKit
  • KDocTools
  • KFile
  • KHTML
  • KImgIO
  • KInit
  • kio
  • KIOSlave
  • KJS
  •   KJS-API
  •   WTF
  • kjsembed
  • KNewStuff
  • KParts
  • KPty
  • Kross
  • KUnitConversion
  • KUtils
  • Nepomuk
  • Plasma
  • Solid
  • Sonnet
  • ThreadWeaver
Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal