Messagelib

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

KDE's Doxygen guidelines are available online.