File indexing completed on 2024-12-01 04:37:02

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