KAuth

BackendsManager.cpp
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(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(filePath)) {
44 continue;
45 }
46
47 QPluginLoader loader(filePath);
48 QObject *instance = loader.instance();
49 if (instance) {
50 retlist.append(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(QFile::decodeName(KAUTH_BACKEND_PLUGIN_DIR));
62
63 for (QObject *instance : backends) {
64 auth = qobject_cast<KAuth::AuthBackend *>(instance);
65 if (auth) {
66 break;
67 }
68 }
69
70 // Helper plugin
71 const QList<QObject *> helpers = retrieveInstancesIn(QFile::decodeName(KAUTH_HELPER_PLUGIN_DIR));
72
73 for (QObject *instance : helpers) {
74 helper = qobject_cast<KAuth::HelperProxy *>(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
QString path(const QString &relativePath)
NoDotAndDotDot
QString decodeName(const QByteArray &localFileName)
bool isLibrary(const QString &fileName)
void append(QList< T > &&value)
bool isEmpty() const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Fri Nov 15 2024 11:57:21 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.