Messagelib

networkurlinterceptorpluginmanager.cpp
1 /*
2  SPDX-FileCopyrightText: 2016-2021 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 
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 (QVector<MailNetworkUrlInterceptorPluginInfo>::iterator it = mPluginList.begin(); it != end; ++it) {
109  loadPlugin(&(*it));
110  }
111 }
112 
113 QVector<WebEngineViewer::NetworkPluginUrlInterceptor *> NetworkUrlInterceptorPluginManagerPrivate::pluginsList() const
114 {
117  for (QVector<MailNetworkUrlInterceptorPluginInfo>::ConstIterator it = mPluginList.constBegin(); it != end; ++it) {
118  if (auto plugin = (*it).plugin) {
119  lst << plugin;
120  }
121  }
122  return lst;
123 }
124 
125 void 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 
136 WebEngineViewer::NetworkPluginUrlInterceptor *NetworkUrlInterceptorPluginManagerPrivate::pluginFromIdentifier(const QString &id)
137 {
139  for (QVector<MailNetworkUrlInterceptorPluginInfo>::ConstIterator it = mPluginList.constBegin(); it != end; ++it) {
140  if ((*it).pluginData.mIdentifier == id) {
141  return (*it).plugin;
142  }
143  }
144  return {};
145 }
146 
147 NetworkUrlInterceptorPluginManager *NetworkUrlInterceptorPluginManager::self()
148 {
150  return &s_self;
151 }
152 
153 NetworkUrlInterceptorPluginManager::NetworkUrlInterceptorPluginManager(QObject *parent)
154  : QObject(parent)
155  , d(new NetworkUrlInterceptorPluginManagerPrivate(this))
156 {
157  d->initializePluginList();
158 }
159 
160 NetworkUrlInterceptorPluginManager::~NetworkUrlInterceptorPluginManager() = default;
161 
162 QVector<WebEngineViewer::NetworkPluginUrlInterceptor *> NetworkUrlInterceptorPluginManager::pluginsList() const
163 {
164  return d->pluginsList();
165 }
166 
167 QString NetworkUrlInterceptorPluginManager::configGroupName() const
168 {
169  return d->configGroupName();
170 }
171 
172 QString NetworkUrlInterceptorPluginManager::configPrefixSettingKey() const
173 {
174  return d->configPrefixSettingKey();
175 }
176 
177 QVector<PimCommon::PluginUtilData> NetworkUrlInterceptorPluginManager::pluginsDataList() const
178 {
179  return d->pluginDataList();
180 }
181 
182 WebEngineViewer::NetworkPluginUrlInterceptor *NetworkUrlInterceptorPluginManager::pluginFromIdentifier(const QString &id)
183 {
184  return d->pluginFromIdentifier(id);
185 }
QString fileName() const
QString name() const
QVector::iterator begin()
The NetworkPluginUrlInterceptor class.
const QList< QKeySequence > & end()
QVector::const_iterator constBegin() const const
static QVector< KPluginMetaData > findPlugins(const QString &directory, std::function< bool(const KPluginMetaData &)> filter={})
QString version() const
This file is part of the KDE documentation.
Documentation copyright © 1996-2021 The KDE developers.
Generated on Sat Dec 4 2021 23:12:54 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.