Messagelib

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

KDE's Doxygen guidelines are available online.