File indexing completed on 2025-01-05 04:58:21

0001 /*
0002    SPDX-FileCopyrightText: 2017-2024 Laurent Montel <montel@kde.org>
0003 
0004    SPDX-License-Identifier: LGPL-2.0-or-later
0005 */
0006 
0007 #include "addressesslineeditpluginmanager.h"
0008 #include "addressesslineeditabstractplugin.h"
0009 #include "pimcommonakonadi_debug.h"
0010 #include <KPluginFactory>
0011 #include <KPluginMetaData>
0012 
0013 #include <QFileInfo>
0014 
0015 using namespace PimCommon;
0016 
0017 class AddressessLineEditPluginInfo
0018 {
0019 public:
0020     AddressessLineEditPluginInfo() = default;
0021 
0022     KPluginMetaData data;
0023     QString metaDataFileNameBaseName;
0024     QString metaDataFileName;
0025     PimCommon::AddressessLineEditAbstractPlugin *plugin = nullptr;
0026 };
0027 
0028 namespace
0029 {
0030 QString pluginVersion()
0031 {
0032     return QStringLiteral("1.0");
0033 }
0034 }
0035 
0036 class PimCommon::AddressessLineEditPluginManagerPrivate
0037 {
0038 public:
0039     explicit AddressessLineEditPluginManagerPrivate(AddressessLineEditPluginManager *qq)
0040         : q(qq)
0041     {
0042         initializePlugins();
0043     }
0044 
0045     void loadPlugin(AddressessLineEditPluginInfo *item);
0046     [[nodiscard]] QList<PimCommon::AddressessLineEditAbstractPlugin *> pluginsList() const;
0047     QList<AddressessLineEditPluginInfo> mPluginList;
0048     bool initializePlugins();
0049 
0050 private:
0051     AddressessLineEditPluginManager *const q;
0052 };
0053 
0054 bool AddressessLineEditPluginManagerPrivate::initializePlugins()
0055 {
0056     if (!mPluginList.isEmpty()) {
0057         return true;
0058     }
0059     const QList<KPluginMetaData> plugins = KPluginMetaData::findPlugins(QStringLiteral("addressline"));
0060     QListIterator<KPluginMetaData> i(plugins);
0061     i.toBack();
0062     while (i.hasPrevious()) {
0063         AddressessLineEditPluginInfo info;
0064         const KPluginMetaData data = i.previous();
0065 
0066         info.metaDataFileNameBaseName = QFileInfo(data.fileName()).baseName();
0067         info.metaDataFileName = data.fileName();
0068         info.data = data;
0069         if (pluginVersion() == data.version()) {
0070             info.plugin = nullptr;
0071             mPluginList.push_back(info);
0072         } else {
0073             qCWarning(PIMCOMMONAKONADI_LOG) << "Plugin " << data.name() << " doesn't have correction plugin version. It will not be loaded.";
0074         }
0075     }
0076     const QList<AddressessLineEditPluginInfo>::iterator end(mPluginList.end());
0077     for (QList<AddressessLineEditPluginInfo>::iterator it = mPluginList.begin(); it != end; ++it) {
0078         loadPlugin(&(*it));
0079     }
0080     return true;
0081 }
0082 
0083 void AddressessLineEditPluginManagerPrivate::loadPlugin(AddressessLineEditPluginInfo *item)
0084 {
0085     if (auto plugin =
0086             KPluginFactory::instantiatePlugin<PimCommon::AddressessLineEditAbstractPlugin>(item->data, q, QVariantList() << item->metaDataFileNameBaseName)
0087                 .plugin) {
0088         item->plugin = plugin;
0089     }
0090 }
0091 
0092 QList<PimCommon::AddressessLineEditAbstractPlugin *> AddressessLineEditPluginManagerPrivate::pluginsList() const
0093 {
0094     QList<PimCommon::AddressessLineEditAbstractPlugin *> lst;
0095     const QList<AddressessLineEditPluginInfo>::ConstIterator end(mPluginList.constEnd());
0096     for (QList<AddressessLineEditPluginInfo>::ConstIterator it = mPluginList.constBegin(); it != end; ++it) {
0097         if (auto plugin = (*it).plugin) {
0098             lst << plugin;
0099         }
0100     }
0101     return lst;
0102 }
0103 
0104 AddressessLineEditPluginManager::AddressessLineEditPluginManager(QObject *parent)
0105     : QObject(parent)
0106     , d(new AddressessLineEditPluginManagerPrivate(this))
0107 {
0108 }
0109 
0110 AddressessLineEditPluginManager::~AddressessLineEditPluginManager() = default;
0111 
0112 AddressessLineEditPluginManager *AddressessLineEditPluginManager::self()
0113 {
0114     static AddressessLineEditPluginManager s_self;
0115     return &s_self;
0116 }
0117 
0118 QList<PimCommon::AddressessLineEditAbstractPlugin *> AddressessLineEditPluginManager::pluginsList() const
0119 {
0120     return d->pluginsList();
0121 }
0122 
0123 PimCommon::AddressessLineEditAbstractPlugin *AddressessLineEditPluginManager::plugin(const QString &identifier)
0124 {
0125     const QList<PimCommon::AddressessLineEditAbstractPlugin *> lstPlugins = pluginsList();
0126     for (PimCommon::AddressessLineEditAbstractPlugin *p : lstPlugins) {
0127         const QList<AddressessLineEditAbstractPluginInfo> lstPluginsInfo = p->names();
0128         for (const AddressessLineEditAbstractPluginInfo &info : lstPluginsInfo) {
0129             if (info.identifier == identifier) {
0130                 return p;
0131             }
0132         }
0133     }
0134     return {};
0135 }
0136 
0137 #include "moc_addressesslineeditpluginmanager.cpp"