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 <QLibrary>
25 #include <QStack>
26 #include <QCoreApplication>
27 #include <QObjectCleanupHandler>
28 
29 #include "kcomponentdata.h"
30 #include "kdebug.h"
31 #include "klocalizedstring.h"
32 
33 class KLibLoaderPrivate
34 {
35 public:
36  KLibLoader instance;
37  QObjectCleanupHandler cleanuphandler;
38  QString errorString;
39 };
40 
41 Q_GLOBAL_STATIC(KLibLoaderPrivate, kLibLoaderPrivate)
42 
43 #define KLIBLOADER_PRIVATE KLibLoaderPrivate *const d = kLibLoaderPrivate
44 
46 {
47  qWarning() << "Using deprecated KLibLoader!";
48  return &kLibLoaderPrivate()->instance;
49 }
50 
51 KLibLoader::KLibLoader()
52  : QObject(nullptr)
53 {
54 }
55 
56 KLibLoader::~KLibLoader()
57 {
58 }
59 
60 extern QString makeLibName(const QString &libname);
61 
62 extern KDELIBS4SUPPORT_DEPRECATED_EXPORT QString findLibrary(const QString &name);
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  QString file = libname.mid(pos + 1);
71  QString path = libname.left(pos);
72  if (!file.startsWith(QLatin1String("lib"))) {
73  return libname;
74  }
75  return path + QLatin1Char('/') + file.mid(3);
76  }
77  if (!libname.startsWith(QLatin1String("lib"))) {
78  return libname;
79  }
80  return libname.mid(3);
81 }
82 #endif
83 
84 //static
86 {
87  Q_UNUSED(cData); // removed as part of the KF5 changes
88  return ::findLibrary(_name);
89 }
90 
92 {
93  if (_name.isEmpty()) {
94  return nullptr;
95  }
96 
97  KLibrary *lib = new KLibrary(_name);
98 
99  // Klibrary search magic did work?
100  if (lib->fileName().isEmpty()) {
101  kLibLoaderPrivate()->errorString = i18n("Library \"%1\" not found", _name);
102  delete lib;
103  return nullptr;
104  }
105 
106  lib->setLoadHints(hint);
107 
108  lib->load();
109 
110  if (!lib->isLoaded()) {
111  kLibLoaderPrivate()->errorString = lib->errorString();
112  delete lib;
113  return nullptr;
114  }
115 
116  kLibLoaderPrivate()->cleanuphandler.add(lib);
117 
118  return lib;
119 }
120 
122 {
123  return kLibLoaderPrivate()->errorString;
124 }
125 
127 {
128 }
129 
131 {
132  Q_UNUSED(hint)
133 
134  KPluginLoader plugin(_name);
135  return plugin.factory();
136 }
137 
KPluginFactory * factory(const QString &libname, QLibrary::LoadHints loadHint={})
Loads a factory from a plugin.
Definition: klibloader.cpp:130
void setLoadHints(QLibrary::LoadHints hints)
QString lastErrorMessage() const
Returns an error message that can be useful to debug the problem.
Definition: klibloader.cpp:121
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:91
static QString findLibrary(const QString &libname, const KComponentData &cData=KGlobal::mainComponent())
Definition: klibloader.cpp:85
typedef LoadHints
bool isLoaded() const const
bool load()
void unloadLibrary(const QString &libname)
Definition: klibloader.cpp:126
KPluginFactory * factory()
QString i18n(const char *text, const TYPE &arg...)
static KLibLoader * self()
Returns a pointer to the factory.
Definition: klibloader.cpp:45
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-2020 The KDE developers.
Generated on Fri Aug 14 2020 22:58:20 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.