1/*
2 SPDX-FileCopyrightText: 2009 Dario Freddi <drf@kde.org>
3
4 SPDX-License-Identifier: LGPL-2.1-or-later
5*/
6
7#include "BackendsManager.h"
8
9#include "BackendsConfig.h"
10
11// Include fake backends
12#include "backends/fake/FakeBackend.h"
13#include "backends/fakehelper/FakeHelperProxy.h"
14#include "kauthdebug.h"
15
16#include <QCoreApplication>
17#include <QDir>
18#include <QPluginLoader>
19
20namespace KAuth
21{
22AuthBackend *BackendsManager::auth = nullptr;
23HelperProxy *BackendsManager::helper = nullptr;
24
25BackendsManager::BackendsManager()
26{
27}
28
29QList<QObject *> BackendsManager::retrieveInstancesIn(const QString &path)
30{
31 QList<QObject *> retlist;
32 QDir pluginPath(path);
33 if (!pluginPath.exists() || path.isEmpty()) {
34 return retlist;
35 }
36
37 const QFileInfoList entryList = pluginPath.entryInfoList(filters: QDir::NoDotAndDotDot | QDir::Files);
38
39 for (const QFileInfo &fi : entryList) {
40 const QString filePath = fi.filePath(); // file name with path
41 // QString fileName = fi.fileName(); // just file name
42
43 if (!QLibrary::isLibrary(fileName: filePath)) {
44 continue;
45 }
46
47 QPluginLoader loader(filePath);
48 QObject *instance = loader.instance();
49 if (instance) {
50 retlist.append(t: instance);
51 } else {
52 qCWarning(KAUTH) << "Couldn't load" << filePath << "error:" << loader.errorString();
53 }
54 }
55 return retlist;
56}
57
58void BackendsManager::init()
59{
60 // Backend plugin
61 const QList<QObject *> backends = retrieveInstancesIn(path: QFile::decodeName(KAUTH_BACKEND_PLUGIN_DIR));
62
63 for (QObject *instance : backends) {
64 auth = qobject_cast<KAuth::AuthBackend *>(object: instance);
65 if (auth) {
66 break;
67 }
68 }
69
70 // Helper plugin
71 const QList<QObject *> helpers = retrieveInstancesIn(path: QFile::decodeName(KAUTH_HELPER_PLUGIN_DIR));
72
73 for (QObject *instance : helpers) {
74 helper = qobject_cast<KAuth::HelperProxy *>(object: instance);
75 if (helper) {
76 break;
77 }
78 }
79
80 if (!auth) {
81 // Load the fake auth backend then
82 auth = new FakeBackend;
83#if !KAUTH_COMPILING_FAKE_BACKEND
84 // Spit a fat warning
85 qCWarning(KAUTH) << "WARNING: KAuth was compiled with a working backend, but was unable to load it! Check your installation!";
86#endif
87 }
88
89 if (!helper) {
90 // Load the fake helper backend then
91 helper = new FakeHelperProxy;
92#if !KAUTH_COMPILING_FAKE_BACKEND
93 // Spit a fat warning
94 qCWarning(KAUTH) << "WARNING: KAuth was compiled with a working helper backend, but was unable to load it! "
95 "Check your installation!";
96#endif
97 }
98}
99
100AuthBackend *BackendsManager::authBackend()
101{
102 if (!auth) {
103 init();
104 }
105
106 return auth;
107}
108
109HelperProxy *BackendsManager::helperProxy()
110{
111 if (!helper) {
112 init();
113 }
114
115 return helper;
116}
117
118} // namespace Auth
119

source code of kauth/src/BackendsManager.cpp