• Skip to content
  • Skip to link menu
  • KDE API Reference
  • kdelibs-4.10.1 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 
34 class KLibLoaderPrivate
35 {
36 public:
37  KLibLoader instance;
38  QObjectCleanupHandler cleanuphandler;
39  QString errorString;
40 };
41 
42 K_GLOBAL_STATIC(KLibLoaderPrivate, kLibLoaderPrivate)
43 
44 #define KLIBLOADER_PRIVATE KLibLoaderPrivate *const d = kLibLoaderPrivate
45 
46 KLibLoader* KLibLoader::self()
47 {
48  return &kLibLoaderPrivate->instance;
49 }
50 
51 KLibLoader::KLibLoader()
52  : QObject(0)
53 {
54 }
55 
56 KLibLoader::~KLibLoader()
57 {
58 }
59 
60 extern QString makeLibName( const QString &libname );
61 
62 extern QString findLibrary(const QString &name, const KComponentData &cData);
63 
64 #ifdef Q_OS_WIN
65 // removes "lib" prefix, if present
66 QString 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
84 QString KLibLoader::findLibrary(const QString &_name, const KComponentData &cData)
85 {
86  return ::findLibrary(_name, cData);
87 }
88 
89 KLibrary* 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 
118 QString KLibLoader::lastErrorMessage() const
119 {
120  return kLibLoaderPrivate->errorString;
121 }
122 
123 void KLibLoader::unloadLibrary( const QString &)
124 {
125 }
126 
127 KPluginFactory* 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 
142 QString 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
This file is part of the KDE documentation.
Documentation copyright © 1996-2013 The KDE developers.
Generated on Wed Mar 20 2013 07:14:35 by doxygen 1.8.3.1 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.10.1 API Reference

Skip menu "kdelibs-4.10.1 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