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

KDECore

  • kdecore
  • util
klibloader.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
5 This library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Library General Public
7 License version 2 as published by the Free Software Foundation.
8
9 This library is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Library General Public License for more details.
13
14 You should have received a copy of the GNU Library General Public License
15 along with this library; see the file COPYING.LIB. If not, write to
16 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17 Boston, MA 02110-1301, USA.
18*/
19#include "klibloader.h"
20
21#include <QtCore/QFile>
22#include <QtCore/QDir>
23#include <QtCore/QTimer>
24#include <QtCore/QLibrary>
25#include <QStack>
26#include <QtCore/QCoreApplication>
27#include <QtCore/QObjectCleanupHandler>
28
29#include "kstandarddirs.h"
30#include "kcomponentdata.h"
31#include "kdebug.h"
32#include "klocale.h"
33
34class KLibLoaderPrivate
35{
36public:
37 KLibLoader instance;
38 QObjectCleanupHandler cleanuphandler;
39 QString errorString;
40};
41
42K_GLOBAL_STATIC(KLibLoaderPrivate, kLibLoaderPrivate)
43
44#define KLIBLOADER_PRIVATE KLibLoaderPrivate *const d = kLibLoaderPrivate
45
46KLibLoader* KLibLoader::self()
47{
48 return &kLibLoaderPrivate->instance;
49}
50
51KLibLoader::KLibLoader()
52 : QObject(0)
53{
54}
55
56KLibLoader::~KLibLoader()
57{
58}
59
60extern QString makeLibName( const QString &libname );
61
62extern QString findLibrary(const QString &name, const KComponentData &cData);
63
64#ifdef Q_OS_WIN
65// removes "lib" prefix, if present
66QString fixLibPrefix(const QString& libname)
67{
68 int pos = libname.lastIndexOf( QLatin1Char('/') );
69 if ( pos >= 0 )
70 {
71 QString file = libname.mid( pos + 1 );
72 QString path = libname.left( pos );
73 if( !file.startsWith( QLatin1String("lib") ) )
74 return libname;
75 return path + QLatin1Char('/') + file.mid( 3 );
76 }
77 if( !libname.startsWith( QLatin1String("lib") ) )
78 return libname;
79 return libname.mid( 3 );
80}
81#endif
82
83//static
84QString KLibLoader::findLibrary(const QString &_name, const KComponentData &cData)
85{
86 return ::findLibrary(_name, cData);
87}
88
89KLibrary* KLibLoader::library( const QString &_name, QLibrary::LoadHints hint )
90{
91 if (_name.isEmpty())
92 return 0;
93
94 KLibrary *lib = new KLibrary(_name);
95
96 // Klibrary search magic did work?
97 if (lib->fileName().isEmpty()) {
98 kLibLoaderPrivate->errorString = i18n("Library \"%1\" not found",_name);
99 delete lib;
100 return 0;
101 }
102
103 lib->setLoadHints(hint);
104
105 lib->load();
106
107 if (!lib->isLoaded()) {
108 kLibLoaderPrivate->errorString = lib->errorString();
109 delete lib;
110 return 0;
111 }
112
113 kLibLoaderPrivate->cleanuphandler.add(lib);
114
115 return lib;
116}
117
118QString KLibLoader::lastErrorMessage() const
119{
120 return kLibLoaderPrivate->errorString;
121}
122
123void KLibLoader::unloadLibrary( const QString &)
124{
125}
126
127KPluginFactory* KLibLoader::factory( const QString &_name, QLibrary::LoadHints hint )
128{
129 KLibrary* lib = library( _name, hint);
130 if ( !lib )
131 return 0;
132
133 KPluginFactory* fac = lib->factory();
134 if ( !fac ) {
135 kLibLoaderPrivate->errorString = errorString( ErrNoFactory );
136 return 0;
137 }
138
139 return fac;
140}
141
142QString KLibLoader::errorString( int componentLoadingError )
143{
144 switch ( componentLoadingError ) {
145 case ErrNoServiceFound:
146 return i18n( "No service matching the requirements was found." );
147 case ErrServiceProvidesNoLibrary:
148 return i18n( "The service provides no library, the Library key is missing in the .desktop file." );
149 case ErrNoLibrary:
150 return kLibLoaderPrivate->instance.lastErrorMessage();
151 case ErrNoFactory:
152 return i18n( "The library does not export a factory for creating components." );
153 case ErrNoComponent:
154 return i18n( "The factory does not support creating components of the specified type." );
155 default:
156 return i18n( "KLibLoader: Unknown error" );
157 }
158}
159
160#include "klibloader.moc"
161// vim: sw=4 sts=4 et
KComponentData
Per component data.
Definition: kcomponentdata.h:47
KLibLoader
The KLibLoader allows you to load libraries dynamically at runtime.
Definition: klibloader.h:56
KLibLoader::self
static KLibLoader * self()
Returns a pointer to the factory.
Definition: klibloader.cpp:46
KLibLoader::findLibrary
static QString findLibrary(const QString &libname, const KComponentData &cData=KGlobal::mainComponent())
Helper method which looks for a library in the standard paths ("module" and "lib" resources).
Definition: klibloader.cpp:84
KLibLoader::errorString
static QString errorString(int componentLoadingError)
Converts a numerical error code into a human-readable error message.
Definition: klibloader.cpp:142
KLibLoader::unloadLibrary
void unloadLibrary(const QString &libname)
Unloads the library with the given name.
Definition: klibloader.cpp:123
KLibLoader::factory
KPluginFactory * factory(const QString &libname, QLibrary::LoadHints loadHint=0)
Loads and initializes a library.
Definition: klibloader.cpp:127
KLibLoader::ErrServiceProvidesNoLibrary
@ ErrServiceProvidesNoLibrary
Definition: klibloader.h:169
KLibLoader::ErrNoServiceFound
@ ErrNoServiceFound
Definition: klibloader.h:170
KLibLoader::ErrNoComponent
@ ErrNoComponent
Definition: klibloader.h:168
KLibLoader::ErrNoFactory
@ ErrNoFactory
Definition: klibloader.h:167
KLibLoader::ErrNoLibrary
@ ErrNoLibrary
Definition: klibloader.h:166
KLibLoader::lastErrorMessage
QString lastErrorMessage() const
Returns an error message that can be useful to debug the problem.
Definition: klibloader.cpp:118
KLibLoader::library
KLibrary * library(const QString &libname, QLibrary::LoadHints loadHint=0)
Loads and initializes a library.
Definition: klibloader.cpp:89
KLibrary
Thin wrapper around QLibrary; you should rarely use this directly, see KPluginLoader for higher-level...
Definition: klibrary.h:39
KLibrary::factory
KPluginFactory * factory(const char *factoryname=0)
Returns the factory of the library.
Definition: klibrary.cpp:163
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
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
kcomponentdata.h
kdebug.h
findLibrary
QString findLibrary(const QString &name, const KComponentData &cData)
Definition: klibrary.cpp:39
fixLibPrefix
QString fixLibPrefix(const QString &libname)
Definition: klibloader.cpp:66
makeLibName
QString makeLibName(const QString &libname)
Definition: kpluginloader.cpp:57
klibloader.h
klocale.h
i18n
QString i18n(const char *text)
Returns a localized version of a string.
Definition: klocalizedstring.h:630
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