Messagelib

networkurlinterceptorpluginmanager.cpp
1/*
2 SPDX-FileCopyrightText: 2016-2024 Laurent Montel <montel@kde.org>
3
4 SPDX-License-Identifier: LGPL-2.0-or-later
5*/
6
7#include "networkurlinterceptorpluginmanager.h"
8#include "networkpluginurlinterceptor.h"
9#include "webengineviewer_debug.h"
10
11#include <KPluginFactory>
12#include <KPluginMetaData>
13#include <QFileInfo>
14#include <QVariant>
15
16using namespace WebEngineViewer;
17
18class MailNetworkUrlInterceptorPluginInfo
19{
20public:
21 MailNetworkUrlInterceptorPluginInfo() = default;
22
23 KPluginMetaData data;
24 QString metaDataFileNameBaseName;
25 QString metaDataFileName;
28 bool isEnabled = false;
29};
30
31namespace
32{
33QString pluginVersion()
34{
35 return QStringLiteral("1.0");
36}
37}
38
39class WebEngineViewer::NetworkUrlInterceptorPluginManagerPrivate
40{
41public:
42 explicit NetworkUrlInterceptorPluginManagerPrivate(NetworkUrlInterceptorPluginManager *qq)
43 : q(qq)
44 {
45 }
46
47 void initializePluginList();
48
49 void loadPlugin(MailNetworkUrlInterceptorPluginInfo *item);
50 [[nodiscard]] QList<WebEngineViewer::NetworkPluginUrlInterceptor *> pluginsList() const;
51 [[nodiscard]] QString configGroupName() const;
52 [[nodiscard]] QString configPrefixSettingKey() const;
53 [[nodiscard]] QList<PimCommon::PluginUtilData> pluginDataList() const;
54 [[nodiscard]] NetworkPluginUrlInterceptor *pluginFromIdentifier(const QString &id);
55
56private:
60};
61
62QString NetworkUrlInterceptorPluginManagerPrivate::configGroupName() const
63{
64 return QStringLiteral("NetworkUrlInterceptorPlugins");
65}
66
67QString NetworkUrlInterceptorPluginManagerPrivate::configPrefixSettingKey() const
68{
69 return QStringLiteral("PluginsNetworkUrlInterceptor");
70}
71
72QList<PimCommon::PluginUtilData> NetworkUrlInterceptorPluginManagerPrivate::pluginDataList() const
73{
74 return mPluginDataList;
75}
76
77void NetworkUrlInterceptorPluginManagerPrivate::initializePluginList()
78{
79 const QList<KPluginMetaData> plugins = KPluginMetaData::findPlugins(QStringLiteral("pim6/webengineviewer/urlinterceptor"));
80
82 i.toBack();
83 const QPair<QStringList, QStringList> pair = PimCommon::PluginUtil::loadPluginSetting(configGroupName(), configPrefixSettingKey());
84
85 while (i.hasPrevious()) {
86 MailNetworkUrlInterceptorPluginInfo info;
87
88 const KPluginMetaData data = i.previous();
89
90 // 1) get plugin data => name/description etc.
91 info.pluginData = PimCommon::PluginUtil::createPluginMetaData(data);
92 // 2) look at if plugin is activated
93 const bool isPluginActivated =
94 PimCommon::PluginUtil::isPluginActivated(pair.first, pair.second, info.pluginData.mEnableByDefault, info.pluginData.mIdentifier);
95 info.isEnabled = isPluginActivated;
96 info.metaDataFileNameBaseName = QFileInfo(data.fileName()).baseName();
97 info.metaDataFileName = data.fileName();
98 info.data = data;
99
100 if (pluginVersion() == data.version()) {
101 info.plugin = nullptr;
102 mPluginList.push_back(info);
103 } else {
104 qCWarning(WEBENGINEVIEWER_LOG) << "Plugin " << data.name() << " doesn't have correction plugin version. It will not be loaded.";
105 }
106 }
108 for (QList<MailNetworkUrlInterceptorPluginInfo>::iterator it = mPluginList.begin(); it != end; ++it) {
109 loadPlugin(&(*it));
110 }
111}
112
113QList<WebEngineViewer::NetworkPluginUrlInterceptor *> NetworkUrlInterceptorPluginManagerPrivate::pluginsList() const
114{
117 for (QList<MailNetworkUrlInterceptorPluginInfo>::ConstIterator it = mPluginList.constBegin(); it != end; ++it) {
118 if (auto plugin = (*it).plugin) {
119 lst << plugin;
120 }
121 }
122 return lst;
123}
124
125void NetworkUrlInterceptorPluginManagerPrivate::loadPlugin(MailNetworkUrlInterceptorPluginInfo *item)
126{
127 if (auto plugin =
128 KPluginFactory::instantiatePlugin<WebEngineViewer::NetworkPluginUrlInterceptor>(item->data, q, QVariantList() << item->metaDataFileName).plugin) {
129 item->plugin = plugin;
130 item->plugin->setIsEnabled(item->isEnabled);
131 item->pluginData.mHasConfigureDialog = item->plugin->hasConfigureDialog();
132 mPluginDataList.append(item->pluginData);
133 }
134}
135
136WebEngineViewer::NetworkPluginUrlInterceptor *NetworkUrlInterceptorPluginManagerPrivate::pluginFromIdentifier(const QString &id)
137{
139 for (QList<MailNetworkUrlInterceptorPluginInfo>::ConstIterator it = mPluginList.constBegin(); it != end; ++it) {
140 if ((*it).pluginData.mIdentifier == id) {
141 return (*it).plugin;
142 }
143 }
144 return {};
145}
146
147NetworkUrlInterceptorPluginManager *NetworkUrlInterceptorPluginManager::self()
148{
150 return &s_self;
151}
152
153NetworkUrlInterceptorPluginManager::NetworkUrlInterceptorPluginManager(QObject *parent)
154 : QObject(parent)
155 , d(new NetworkUrlInterceptorPluginManagerPrivate(this))
156{
157 d->initializePluginList();
158}
159
160NetworkUrlInterceptorPluginManager::~NetworkUrlInterceptorPluginManager() = default;
161
162QList<WebEngineViewer::NetworkPluginUrlInterceptor *> NetworkUrlInterceptorPluginManager::pluginsList() const
163{
164 return d->pluginsList();
165}
166
167QString NetworkUrlInterceptorPluginManager::configGroupName() const
168{
169 return d->configGroupName();
170}
171
172QString NetworkUrlInterceptorPluginManager::configPrefixSettingKey() const
173{
174 return d->configPrefixSettingKey();
175}
176
177QList<PimCommon::PluginUtilData> NetworkUrlInterceptorPluginManager::pluginsDataList() const
178{
179 return d->pluginDataList();
180}
181
182WebEngineViewer::NetworkPluginUrlInterceptor *NetworkUrlInterceptorPluginManager::pluginFromIdentifier(const QString &id)
183{
184 return d->pluginFromIdentifier(id);
185}
186
187#include "moc_networkurlinterceptorpluginmanager.cpp"
QString fileName() const
static QList< KPluginMetaData > findPlugins(const QString &directory, std::function< bool(const KPluginMetaData &)> filter={}, KPluginMetaDataOptions options={})
QString name() const
QString version() const
const QList< QKeySequence > & end()
QString baseName() const const
void append(const T &value)
QList::iterator begin()
QList::const_iterator constBegin() const const
QList::const_iterator constEnd() const const
QList::iterator end()
void push_back(const T &value)
T qobject_cast(QObject *object)
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Sun Feb 25 2024 18:37:31 by doxygen 1.10.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.