File indexing completed on 2025-01-26 04:57:24

0001 /*
0002    SPDX-FileCopyrightText: 2016-2024 Laurent Montel <montel@kde.org>
0003 
0004    SPDX-License-Identifier: LGPL-2.0-or-later
0005 */
0006 
0007 #include "networkurlinterceptorpluginmanager.h"
0008 #include "networkpluginurlinterceptor.h"
0009 #include "webengineviewer_debug.h"
0010 
0011 #include <KPluginFactory>
0012 #include <KPluginMetaData>
0013 #include <QFileInfo>
0014 #include <QVariant>
0015 
0016 using namespace WebEngineViewer;
0017 
0018 class MailNetworkUrlInterceptorPluginInfo
0019 {
0020 public:
0021     MailNetworkUrlInterceptorPluginInfo() = default;
0022 
0023     KPluginMetaData data;
0024     QString metaDataFileNameBaseName;
0025     QString metaDataFileName;
0026     PimCommon::PluginUtilData pluginData;
0027     WebEngineViewer::NetworkPluginUrlInterceptor *plugin = nullptr;
0028     bool isEnabled = false;
0029 };
0030 
0031 namespace
0032 {
0033 QString pluginVersion()
0034 {
0035     return QStringLiteral("1.0");
0036 }
0037 }
0038 
0039 class WebEngineViewer::NetworkUrlInterceptorPluginManagerPrivate
0040 {
0041 public:
0042     explicit NetworkUrlInterceptorPluginManagerPrivate(NetworkUrlInterceptorPluginManager *qq)
0043         : q(qq)
0044     {
0045     }
0046 
0047     void initializePluginList();
0048 
0049     void loadPlugin(MailNetworkUrlInterceptorPluginInfo *item);
0050     [[nodiscard]] QList<WebEngineViewer::NetworkPluginUrlInterceptor *> pluginsList() const;
0051     [[nodiscard]] QString configGroupName() const;
0052     [[nodiscard]] QString configPrefixSettingKey() const;
0053     [[nodiscard]] QList<PimCommon::PluginUtilData> pluginDataList() const;
0054     [[nodiscard]] NetworkPluginUrlInterceptor *pluginFromIdentifier(const QString &id);
0055 
0056 private:
0057     QList<MailNetworkUrlInterceptorPluginInfo> mPluginList;
0058     QList<PimCommon::PluginUtilData> mPluginDataList;
0059     NetworkUrlInterceptorPluginManager *const q;
0060 };
0061 
0062 QString NetworkUrlInterceptorPluginManagerPrivate::configGroupName() const
0063 {
0064     return QStringLiteral("NetworkUrlInterceptorPlugins");
0065 }
0066 
0067 QString NetworkUrlInterceptorPluginManagerPrivate::configPrefixSettingKey() const
0068 {
0069     return QStringLiteral("PluginsNetworkUrlInterceptor");
0070 }
0071 
0072 QList<PimCommon::PluginUtilData> NetworkUrlInterceptorPluginManagerPrivate::pluginDataList() const
0073 {
0074     return mPluginDataList;
0075 }
0076 
0077 void NetworkUrlInterceptorPluginManagerPrivate::initializePluginList()
0078 {
0079     const QList<KPluginMetaData> plugins = KPluginMetaData::findPlugins(QStringLiteral("pim6/webengineviewer/urlinterceptor"));
0080 
0081     QListIterator<KPluginMetaData> i(plugins);
0082     i.toBack();
0083     const QPair<QStringList, QStringList> pair = PimCommon::PluginUtil::loadPluginSetting(configGroupName(), configPrefixSettingKey());
0084 
0085     while (i.hasPrevious()) {
0086         MailNetworkUrlInterceptorPluginInfo info;
0087 
0088         const KPluginMetaData data = i.previous();
0089 
0090         // 1) get plugin data => name/description etc.
0091         info.pluginData = PimCommon::PluginUtil::createPluginMetaData(data);
0092         // 2) look at if plugin is activated
0093         const bool isPluginActivated =
0094             PimCommon::PluginUtil::isPluginActivated(pair.first, pair.second, info.pluginData.mEnableByDefault, info.pluginData.mIdentifier);
0095         info.isEnabled = isPluginActivated;
0096         info.metaDataFileNameBaseName = QFileInfo(data.fileName()).baseName();
0097         info.metaDataFileName = data.fileName();
0098         info.data = data;
0099 
0100         if (pluginVersion() == data.version()) {
0101             info.plugin = nullptr;
0102             mPluginList.push_back(info);
0103         } else {
0104             qCWarning(WEBENGINEVIEWER_LOG) << "Plugin " << data.name() << " doesn't have correction plugin version. It will not be loaded.";
0105         }
0106     }
0107     QList<MailNetworkUrlInterceptorPluginInfo>::iterator end(mPluginList.end());
0108     for (QList<MailNetworkUrlInterceptorPluginInfo>::iterator it = mPluginList.begin(); it != end; ++it) {
0109         loadPlugin(&(*it));
0110     }
0111 }
0112 
0113 QList<WebEngineViewer::NetworkPluginUrlInterceptor *> NetworkUrlInterceptorPluginManagerPrivate::pluginsList() const
0114 {
0115     QList<WebEngineViewer::NetworkPluginUrlInterceptor *> lst;
0116     QList<MailNetworkUrlInterceptorPluginInfo>::ConstIterator end(mPluginList.constEnd());
0117     for (QList<MailNetworkUrlInterceptorPluginInfo>::ConstIterator it = mPluginList.constBegin(); it != end; ++it) {
0118         if (auto plugin = (*it).plugin) {
0119             lst << plugin;
0120         }
0121     }
0122     return lst;
0123 }
0124 
0125 void NetworkUrlInterceptorPluginManagerPrivate::loadPlugin(MailNetworkUrlInterceptorPluginInfo *item)
0126 {
0127     if (auto plugin =
0128             KPluginFactory::instantiatePlugin<WebEngineViewer::NetworkPluginUrlInterceptor>(item->data, q, QVariantList() << item->metaDataFileName).plugin) {
0129         item->plugin = plugin;
0130         item->plugin->setIsEnabled(item->isEnabled);
0131         item->pluginData.mHasConfigureDialog = item->plugin->hasConfigureDialog();
0132         mPluginDataList.append(item->pluginData);
0133     }
0134 }
0135 
0136 WebEngineViewer::NetworkPluginUrlInterceptor *NetworkUrlInterceptorPluginManagerPrivate::pluginFromIdentifier(const QString &id)
0137 {
0138     QList<MailNetworkUrlInterceptorPluginInfo>::ConstIterator end(mPluginList.constEnd());
0139     for (QList<MailNetworkUrlInterceptorPluginInfo>::ConstIterator it = mPluginList.constBegin(); it != end; ++it) {
0140         if ((*it).pluginData.mIdentifier == id) {
0141             return (*it).plugin;
0142         }
0143     }
0144     return {};
0145 }
0146 
0147 NetworkUrlInterceptorPluginManager *NetworkUrlInterceptorPluginManager::self()
0148 {
0149     static NetworkUrlInterceptorPluginManager s_self;
0150     return &s_self;
0151 }
0152 
0153 NetworkUrlInterceptorPluginManager::NetworkUrlInterceptorPluginManager(QObject *parent)
0154     : QObject(parent)
0155     , d(new NetworkUrlInterceptorPluginManagerPrivate(this))
0156 {
0157     d->initializePluginList();
0158 }
0159 
0160 NetworkUrlInterceptorPluginManager::~NetworkUrlInterceptorPluginManager() = default;
0161 
0162 QList<WebEngineViewer::NetworkPluginUrlInterceptor *> NetworkUrlInterceptorPluginManager::pluginsList() const
0163 {
0164     return d->pluginsList();
0165 }
0166 
0167 QString NetworkUrlInterceptorPluginManager::configGroupName() const
0168 {
0169     return d->configGroupName();
0170 }
0171 
0172 QString NetworkUrlInterceptorPluginManager::configPrefixSettingKey() const
0173 {
0174     return d->configPrefixSettingKey();
0175 }
0176 
0177 QList<PimCommon::PluginUtilData> NetworkUrlInterceptorPluginManager::pluginsDataList() const
0178 {
0179     return d->pluginDataList();
0180 }
0181 
0182 WebEngineViewer::NetworkPluginUrlInterceptor *NetworkUrlInterceptorPluginManager::pluginFromIdentifier(const QString &id)
0183 {
0184     return d->pluginFromIdentifier(id);
0185 }
0186 
0187 #include "moc_networkurlinterceptorpluginmanager.cpp"