File indexing completed on 2024-10-06 10:24:23

0001 /*
0002    SPDX-FileCopyrightText: 2018-2024 Laurent Montel <montel@kde.org>
0003 
0004    SPDX-License-Identifier: LGPL-2.0-or-later
0005 */
0006 
0007 #include "authenticationmanager.h"
0008 #include <KPluginFactory>
0009 #include <KPluginMetaData>
0010 #include <QFileInfo>
0011 
0012 #include "plugins/pluginauthentication.h"
0013 
0014 AuthenticationManager::AuthenticationManager(QObject *parent)
0015     : QObject(parent)
0016 {
0017     initializePluginList();
0018 }
0019 
0020 AuthenticationManager::~AuthenticationManager() = default;
0021 
0022 AuthenticationManager *AuthenticationManager::self()
0023 {
0024     static AuthenticationManager s_self;
0025     return &s_self;
0026 }
0027 
0028 void AuthenticationManager::initializePluginList()
0029 {
0030     const QVector<KPluginMetaData> plugins = KPluginMetaData::findPlugins(QStringLiteral("ruqolaplugins/authentication"));
0031 
0032     QVectorIterator<KPluginMetaData> i(plugins);
0033     i.toBack();
0034     QSet<QString> unique;
0035     while (i.hasPrevious()) {
0036         AuthenticationManagerInfo info;
0037         const KPluginMetaData data = i.previous();
0038 
0039         // 1) get plugin data => name/description etc.
0040         info.pluginData = createPluginMetaData(data);
0041         // 2) look at if plugin is activated
0042         info.data = data;
0043         info.metaDataFileNameBaseName = QFileInfo(data.fileName()).baseName();
0044         info.metaDataFileName = data.fileName();
0045         // only load plugins once, even if found multiple times!
0046         if (unique.contains(info.metaDataFileNameBaseName)) {
0047             continue;
0048         }
0049         info.plugin = nullptr;
0050         mPluginList.push_back(info);
0051         unique.insert(info.metaDataFileNameBaseName);
0052     }
0053     QVector<AuthenticationManagerInfo>::iterator end(mPluginList.end());
0054     for (QVector<AuthenticationManagerInfo>::iterator it = mPluginList.begin(); it != end; ++it) {
0055         loadPlugin(&(*it));
0056     }
0057 }
0058 
0059 void AuthenticationManager::loadPlugin(AuthenticationManagerInfo *item)
0060 {
0061     if (auto plugin = KPluginFactory::instantiatePlugin<PluginAuthentication>(item->data, this, QVariantList() << item->metaDataFileNameBaseName).plugin) {
0062         item->plugin = plugin;
0063         mPluginDataList.append(item->pluginData);
0064     }
0065 }
0066 
0067 QVector<PluginAuthentication *> AuthenticationManager::pluginsList() const
0068 {
0069     QVector<PluginAuthentication *> lst;
0070     QVector<AuthenticationManagerInfo>::ConstIterator end(mPluginList.constEnd());
0071     for (QVector<AuthenticationManagerInfo>::ConstIterator it = mPluginList.constBegin(); it != end; ++it) {
0072         if (auto plugin = (*it).plugin) {
0073             lst << plugin;
0074         }
0075     }
0076     return lst;
0077 }
0078 
0079 PluginUtilData AuthenticationManager::createPluginMetaData(const KPluginMetaData &metaData)
0080 {
0081     PluginUtilData pluginData;
0082     pluginData.mName = metaData.name();
0083     pluginData.mIdentifier = metaData.pluginId();
0084     return pluginData;
0085 }
0086 
0087 #include "moc_authenticationmanager.cpp"