KDELibs4Support

klibloader.cpp
1 /* This file is part of the KDE libraries
2  Copyright (C) 1999 Torben Weis <[email protected]>
3  Copyright (C) 2000 Michael Matz <[email protected]>
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 <QFile>
22 #include <QDir>
23 #include <QTimer>
24 #include <QStack>
25 #include <QCoreApplication>
26 #include <QObjectCleanupHandler>
27 
28 #include "kcomponentdata.h"
29 #include "kdebug.h"
30 #include "klocalizedstring.h"
31 
32 class KLibLoaderPrivate
33 {
34 public:
35  KLibLoader instance;
36  QObjectCleanupHandler cleanuphandler;
37  QString errorString;
38 };
39 
40 Q_GLOBAL_STATIC(KLibLoaderPrivate, kLibLoaderPrivate)
41 
42 #define KLIBLOADER_PRIVATE KLibLoaderPrivate *const d = kLibLoaderPrivate
43 
45 {
46  qWarning() << "Using deprecated KLibLoader!";
47  return &kLibLoaderPrivate()->instance;
48 }
49 
50 KLibLoader::KLibLoader()
51  : QObject(nullptr)
52 {
53 }
54 
55 KLibLoader::~KLibLoader()
56 {
57 }
58 
59 extern QString makeLibName(const QString &libname);
60 
61 extern KDELIBS4SUPPORT_DEPRECATED_EXPORT QString findLibrary(const QString &name);
62 
63 #ifdef Q_OS_WIN
64 // removes "lib" prefix, if present
65 QString fixLibPrefix(const QString &libname)
66 {
67  int pos = libname.lastIndexOf(QLatin1Char('/'));
68  if (pos >= 0) {
69  QString file = libname.mid(pos + 1);
70  QString path = libname.left(pos);
71  if (!file.startsWith(QLatin1String("lib"))) {
72  return libname;
73  }
74  return path + QLatin1Char('/') + file.mid(3);
75  }
76  if (!libname.startsWith(QLatin1String("lib"))) {
77  return libname;
78  }
79  return libname.mid(3);
80 }
81 #endif
82 
83 //static
85 {
86  Q_UNUSED(cData); // removed as part of the KF5 changes
87  return ::findLibrary(_name);
88 }
89 
91 {
92  if (_name.isEmpty()) {
93  return nullptr;
94  }
95 
96  KLibrary *lib = new KLibrary(_name);
97 
98  // Klibrary search magic did work?
99  if (lib->fileName().isEmpty()) {
100  kLibLoaderPrivate()->errorString = i18n("Library \"%1\" not found", _name);
101  delete lib;
102  return nullptr;
103  }
104 
105  lib->setLoadHints(hint);
106 
107  lib->load();
108 
109  if (!lib->isLoaded()) {
110  kLibLoaderPrivate()->errorString = lib->errorString();
111  delete lib;
112  return nullptr;
113  }
114 
115  kLibLoaderPrivate()->cleanuphandler.add(lib);
116 
117  return lib;
118 }
119 
121 {
122  return kLibLoaderPrivate()->errorString;
123 }
124 
126 {
127 }
128 
130 {
131  Q_UNUSED(hint)
132 
133  KPluginLoader plugin(_name);
134  return plugin.factory();
135 }
136 
KPluginFactory * factory(const QString &libname, QLibrary::LoadHints loadHint={})
Loads a factory from a plugin.
Definition: klibloader.cpp:129
void setLoadHints(QLibrary::LoadHints hints)
QString lastErrorMessage() const
Returns an error message that can be useful to debug the problem.
Definition: klibloader.cpp:120
int lastIndexOf(QChar ch, int from, Qt::CaseSensitivity cs) const const
bool isEmpty() const const
bool startsWith(const QString &s, Qt::CaseSensitivity cs) const const
KLibrary searches for libraries in the same way that KPluginLoader searches for plugins.
Definition: klibrary.h:37
KLibrary * library(const QString &libname, QLibrary::LoadHints loadHint={})
Loads and initializes a library.
Definition: klibloader.cpp:90
static QString findLibrary(const QString &libname, const KComponentData &cData=KGlobal::mainComponent())
Definition: klibloader.cpp:84
typedef LoadHints
bool isLoaded() const const
bool load()
void unloadLibrary(const QString &libname)
Definition: klibloader.cpp:125
KPluginFactory * factory()
QString i18n(const char *text, const TYPE &arg...)
static KLibLoader * self()
Returns a pointer to the factory.
Definition: klibloader.cpp:44
QString mid(int position, int n) const const
QString left(int n) const const
Per component data.
QString errorString() const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2021 The KDE developers.
Generated on Thu Jun 17 2021 22:58:16 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.