File indexing completed on 2024-05-19 15:07:33

0001 /*
0002     SPDX-FileCopyrightText: 2009 Dario Freddi <drf@kde.org>
0003 
0004     SPDX-License-Identifier: LGPL-2.1-or-later
0005 */
0006 
0007 #include "BackendsManager.h"
0008 
0009 #include "BackendsConfig.h"
0010 
0011 // Include fake backends
0012 #include "backends/fake/FakeBackend.h"
0013 #include "backends/fakehelper/FakeHelperProxy.h"
0014 #include "kauthdebug.h"
0015 
0016 #include <QCoreApplication>
0017 #include <QDir>
0018 #include <QPluginLoader>
0019 
0020 namespace KAuth
0021 {
0022 AuthBackend *BackendsManager::auth = nullptr;
0023 HelperProxy *BackendsManager::helper = nullptr;
0024 
0025 BackendsManager::BackendsManager()
0026 {
0027 }
0028 
0029 QList<QObject *> BackendsManager::retrieveInstancesIn(const QString &path)
0030 {
0031     QList<QObject *> retlist;
0032     QDir pluginPath(path);
0033     if (!pluginPath.exists() || path.isEmpty()) {
0034         return retlist;
0035     }
0036 
0037     const QFileInfoList entryList = pluginPath.entryInfoList(QDir::NoDotAndDotDot | QDir::Files);
0038 
0039     for (const QFileInfo &fi : entryList) {
0040         const QString filePath = fi.filePath(); // file name with path
0041         // QString fileName = fi.fileName(); // just file name
0042 
0043         if (!QLibrary::isLibrary(filePath)) {
0044             continue;
0045         }
0046 
0047         QPluginLoader loader(filePath);
0048         QObject *instance = loader.instance();
0049         if (instance) {
0050             retlist.append(instance);
0051         } else {
0052             qCWarning(KAUTH) << "Couldn't load" << filePath << "error:" << loader.errorString();
0053         }
0054     }
0055     return retlist;
0056 }
0057 
0058 void BackendsManager::init()
0059 {
0060     // Backend plugin
0061     const QList<QObject *> backends = retrieveInstancesIn(QFile::decodeName(KAUTH_BACKEND_PLUGIN_DIR));
0062 
0063     for (QObject *instance : backends) {
0064         auth = qobject_cast<KAuth::AuthBackend *>(instance);
0065         if (auth) {
0066             break;
0067         }
0068     }
0069 
0070     // Helper plugin
0071     const QList<QObject *> helpers = retrieveInstancesIn(QFile::decodeName(KAUTH_HELPER_PLUGIN_DIR));
0072 
0073     for (QObject *instance : helpers) {
0074         helper = qobject_cast<KAuth::HelperProxy *>(instance);
0075         if (helper) {
0076             break;
0077         }
0078     }
0079 
0080     if (!auth) {
0081         // Load the fake auth backend then
0082         auth = new FakeBackend;
0083 #if !KAUTH_COMPILING_FAKE_BACKEND
0084         // Spit a fat warning
0085         qCWarning(KAUTH) << "WARNING: KAuth was compiled with a working backend, but was unable to load it! Check your installation!";
0086 #endif
0087     }
0088 
0089     if (!helper) {
0090         // Load the fake helper backend then
0091         helper = new FakeHelperProxy;
0092 #if !KAUTH_COMPILING_FAKE_BACKEND
0093         // Spit a fat warning
0094         qCWarning(KAUTH) << "WARNING: KAuth was compiled with a working helper backend, but was unable to load it! "
0095                             "Check your installation!";
0096 #endif
0097     }
0098 }
0099 
0100 AuthBackend *BackendsManager::authBackend()
0101 {
0102     if (!auth) {
0103         init();
0104     }
0105 
0106     return auth;
0107 }
0108 
0109 HelperProxy *BackendsManager::helperProxy()
0110 {
0111     if (!helper) {
0112         init();
0113     }
0114 
0115     return helper;
0116 }
0117 
0118 } // namespace Auth