File indexing completed on 2024-04-14 14:20:01

0001 /* This file is part of the KDE libraries
0002    Copyright (C) 1999 Torben Weis <weis@kde.org>
0003    Copyright (C) 2000 Michael Matz <matz@kde.org>
0004 
0005    This library is free software; you can redistribute it and/or
0006    modify it under the terms of the GNU Library General Public
0007    License version 2 as published by the Free Software Foundation.
0008 
0009    This library is distributed in the hope that it will be useful,
0010    but WITHOUT ANY WARRANTY; without even the implied warranty of
0011    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
0012    Library General Public License for more details.
0013 
0014    You should have received a copy of the GNU Library General Public License
0015    along with this library; see the file COPYING.LIB.  If not, write to
0016    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
0017    Boston, MA 02110-1301, USA.
0018 */
0019 #include "klibloader.h"
0020 
0021 #include <QFile>
0022 #include <QDir>
0023 #include <QTimer>
0024 #include <QStack>
0025 #include <QCoreApplication>
0026 #include <QObjectCleanupHandler>
0027 
0028 #include "kcomponentdata.h"
0029 #include "kdebug.h"
0030 #include "klocalizedstring.h"
0031 
0032 class KLibLoaderPrivate
0033 {
0034 public:
0035     KLibLoader instance;
0036     QObjectCleanupHandler cleanuphandler;
0037     QString errorString;
0038 };
0039 
0040 Q_GLOBAL_STATIC(KLibLoaderPrivate, kLibLoaderPrivate)
0041 
0042 #define KLIBLOADER_PRIVATE KLibLoaderPrivate *const d = kLibLoaderPrivate
0043 
0044 KLibLoader *KLibLoader::self()
0045 {
0046     qWarning() << "Using deprecated KLibLoader!";
0047     return &kLibLoaderPrivate()->instance;
0048 }
0049 
0050 KLibLoader::KLibLoader()
0051     : QObject(nullptr)
0052 {
0053 }
0054 
0055 KLibLoader::~KLibLoader()
0056 {
0057 }
0058 
0059 extern QString makeLibName(const QString &libname);
0060 
0061 extern KDELIBS4SUPPORT_DEPRECATED_EXPORT QString findLibrary(const QString &name);
0062 
0063 #ifdef Q_OS_WIN
0064 // removes "lib" prefix, if present
0065 QString fixLibPrefix(const QString &libname)
0066 {
0067     int pos = libname.lastIndexOf(QLatin1Char('/'));
0068     if (pos >= 0) {
0069         QString file = libname.mid(pos + 1);
0070         QString path = libname.left(pos);
0071         if (!file.startsWith(QLatin1String("lib"))) {
0072             return libname;
0073         }
0074         return path + QLatin1Char('/') + file.mid(3);
0075     }
0076     if (!libname.startsWith(QLatin1String("lib"))) {
0077         return libname;
0078     }
0079     return libname.mid(3);
0080 }
0081 #endif
0082 
0083 //static
0084 QString KLibLoader::findLibrary(const QString &_name, const KComponentData &cData)
0085 {
0086     Q_UNUSED(cData); // removed as part of the KF5 changes
0087     return ::findLibrary(_name);
0088 }
0089 
0090 KLibrary *KLibLoader::library(const QString &_name, QLibrary::LoadHints hint)
0091 {
0092     if (_name.isEmpty()) {
0093         return nullptr;
0094     }
0095 
0096     KLibrary *lib = new KLibrary(_name);
0097 
0098     // Klibrary search magic did work?
0099     if (lib->fileName().isEmpty()) {
0100         kLibLoaderPrivate()->errorString = i18n("Library \"%1\" not found", _name);
0101         delete lib;
0102         return nullptr;
0103     }
0104 
0105     lib->setLoadHints(hint);
0106 
0107     lib->load();
0108 
0109     if (!lib->isLoaded()) {
0110         kLibLoaderPrivate()->errorString = lib->errorString();
0111         delete lib;
0112         return nullptr;
0113     }
0114 
0115     kLibLoaderPrivate()->cleanuphandler.add(lib);
0116 
0117     return lib;
0118 }
0119 
0120 QString KLibLoader::lastErrorMessage() const
0121 {
0122     return kLibLoaderPrivate()->errorString;
0123 }
0124 
0125 void KLibLoader::unloadLibrary(const QString &)
0126 {
0127 }
0128 
0129 KPluginFactory *KLibLoader::factory(const QString &_name, QLibrary::LoadHints hint)
0130 {
0131     Q_UNUSED(hint)
0132 
0133     KPluginLoader plugin(_name);
0134     return plugin.factory();
0135 }
0136 
0137 #include "moc_klibloader.cpp"