Messagelib

viewerpluginmanager.cpp
1 /*
2  SPDX-FileCopyrightText: 2015-2021 Laurent Montel <[email protected]>
3 
4  SPDX-License-Identifier: LGPL-2.0-or-later
5 */
6 
7 #include "viewerpluginmanager.h"
8 #include "messageviewer_debug.h"
9 #include "viewerplugin.h"
10 
11 #include <KPluginFactory>
12 #include <KPluginMetaData>
13 #include <KSharedConfig>
14 #include <QFileInfo>
15 
16 using namespace MessageViewer;
17 
18 class ViewerPluginInfo
19 {
20 public:
21  ViewerPluginInfo() = default;
22 
23  KPluginMetaData data;
24  QString metaDataFileNameBaseName;
25  QString metaDataFileName;
26  PimCommon::PluginUtilData pluginData;
27  MessageViewer::ViewerPlugin *plugin = nullptr;
28  bool isEnabled = false;
29 };
30 
31 class MessageViewer::ViewerPluginManagerPrivate
32 {
33 public:
34  explicit ViewerPluginManagerPrivate(ViewerPluginManager *qq)
35  : q(qq)
36  {
37  }
38 
39  bool initializePluginList();
40  void loadPlugin(ViewerPluginInfo *item);
41  Q_REQUIRED_RESULT QVector<MessageViewer::ViewerPlugin *> pluginsList() const;
42  Q_REQUIRED_RESULT QVector<PimCommon::PluginUtilData> pluginDataList() const;
43 
44  QString pluginDirectory;
45  QString pluginName;
46 
47  Q_REQUIRED_RESULT QString configGroupName() const;
48  Q_REQUIRED_RESULT QString configPrefixSettingKey() const;
49  ViewerPlugin *pluginFromIdentifier(const QString &id);
50 
51 private:
52  QVector<ViewerPluginInfo> mPluginList;
53  QVector<PimCommon::PluginUtilData> mPluginDataList;
54  ViewerPluginManager *const q;
55 };
56 
57 namespace
58 {
59 QString pluginVersion()
60 {
61  return QStringLiteral("2.0");
62 }
63 }
64 
65 QString ViewerPluginManagerPrivate::configGroupName() const
66 {
67  return QStringLiteral("PluginMessageViewer%1").arg(pluginName);
68 }
69 
70 QString ViewerPluginManagerPrivate::configPrefixSettingKey() const
71 {
72  return QStringLiteral("MessageViewerPlugins");
73 }
74 
75 bool ViewerPluginManagerPrivate::initializePluginList()
76 {
77  if (!mPluginList.isEmpty()) {
78  return true;
79  }
80  if (pluginDirectory.isEmpty()) {
81  return false;
82  }
83 
84  QVector<KPluginMetaData> plugins = KPluginMetaData::findPlugins(pluginDirectory);
85 
86  // We need common plugin to avoid to duplicate code between akregator/kmail
87  plugins += KPluginMetaData::findPlugins(QStringLiteral("messageviewer/viewercommonplugin"));
88 
89  const QPair<QStringList, QStringList> pair = PimCommon::PluginUtil::loadPluginSetting(configGroupName(), configPrefixSettingKey());
91  i.toBack();
92  while (i.hasPrevious()) {
93  ViewerPluginInfo info;
94 
95  const KPluginMetaData data = i.previous();
96 
97  // 1) get plugin data => name/description etc.
98  info.pluginData = PimCommon::PluginUtil::createPluginMetaData(data);
99  // 2) look at if plugin is activated
100  const bool isPluginActivated =
101  PimCommon::PluginUtil::isPluginActivated(pair.first, pair.second, info.pluginData.mEnableByDefault, info.pluginData.mIdentifier);
102  info.isEnabled = isPluginActivated;
103  info.metaDataFileNameBaseName = QFileInfo(data.fileName()).baseName();
104  info.metaDataFileName = data.fileName();
105  info.data = data;
106 
107  if (pluginVersion() == data.version()) {
108  info.plugin = nullptr;
109  mPluginList.push_back(info);
110  } else {
111  qCWarning(MESSAGEVIEWER_LOG) << "Plugin name :" << data.name() << " doesn't have correct plugin version. Please update it";
112  }
113  }
114  QVector<ViewerPluginInfo>::iterator end(mPluginList.end());
115  for (QVector<ViewerPluginInfo>::iterator it = mPluginList.begin(); it != end; ++it) {
116  loadPlugin(&(*it));
117  }
118  return true;
119 }
120 
121 void ViewerPluginManagerPrivate::loadPlugin(ViewerPluginInfo *item)
122 {
123  if (auto plugin = KPluginFactory::instantiatePlugin<MessageViewer::ViewerPlugin>(item->data, q, QVariantList() << item->metaDataFileName).plugin) {
124  item->plugin = plugin;
125  item->plugin->setIsEnabled(item->isEnabled);
126  item->pluginData.mHasConfigureDialog = item->plugin->hasConfigureDialog();
127  mPluginDataList.append(item->pluginData);
128  }
129 }
130 
131 QVector<ViewerPlugin *> ViewerPluginManagerPrivate::pluginsList() const
132 {
134  QVector<ViewerPluginInfo>::ConstIterator end(mPluginList.constEnd());
135  for (QVector<ViewerPluginInfo>::ConstIterator it = mPluginList.constBegin(); it != end; ++it) {
136  if (auto plugin = (*it).plugin) {
137  lst << plugin;
138  }
139  }
140  return lst;
141 }
142 
143 QVector<PimCommon::PluginUtilData> ViewerPluginManagerPrivate::pluginDataList() const
144 {
145  return mPluginDataList;
146 }
147 
148 ViewerPlugin *ViewerPluginManagerPrivate::pluginFromIdentifier(const QString &id)
149 {
150  QVector<ViewerPluginInfo>::ConstIterator end(mPluginList.constEnd());
151  for (QVector<ViewerPluginInfo>::ConstIterator it = mPluginList.constBegin(); it != end; ++it) {
152  if ((*it).pluginData.mIdentifier == id) {
153  return (*it).plugin;
154  }
155  }
156  return {};
157 }
158 
159 ViewerPluginManager::ViewerPluginManager(QObject *parent)
160  : QObject(parent)
161  , d(new MessageViewer::ViewerPluginManagerPrivate(this))
162 {
163 }
164 
165 MessageViewer::ViewerPluginManager::~ViewerPluginManager() = default;
166 
167 bool ViewerPluginManager::initializePluginList()
168 {
169  return d->initializePluginList();
170 }
171 
172 ViewerPluginManager *ViewerPluginManager::self()
173 {
174  static ViewerPluginManager s_self;
175  return &s_self;
176 }
177 
178 QVector<MessageViewer::ViewerPlugin *> ViewerPluginManager::pluginsList() const
179 {
180  return d->pluginsList();
181 }
182 
183 void ViewerPluginManager::setPluginDirectory(const QString &directory)
184 {
185  d->pluginDirectory = directory;
186 }
187 
188 QString ViewerPluginManager::pluginDirectory() const
189 {
190  return d->pluginDirectory;
191 }
192 
193 QVector<PimCommon::PluginUtilData> ViewerPluginManager::pluginsDataList() const
194 {
195  return d->pluginDataList();
196 }
197 
198 QString ViewerPluginManager::configGroupName() const
199 {
200  return d->configGroupName();
201 }
202 
203 QString ViewerPluginManager::configPrefixSettingKey() const
204 {
205  return d->configPrefixSettingKey();
206 }
207 
208 MessageViewer::ViewerPlugin *ViewerPluginManager::pluginFromIdentifier(const QString &id)
209 {
210  return d->pluginFromIdentifier(id);
211 }
212 
213 void ViewerPluginManager::setPluginName(const QString &pluginName)
214 {
215  d->pluginName = pluginName;
216 }
217 
218 QString ViewerPluginManager::pluginName() const
219 {
220  return d->pluginName;
221 }
QString fileName() const
QString name() const
QVector::iterator begin()
The ViewerPlugin class.
Definition: viewerplugin.h:22
The ViewerPluginManager class.
const QList< QKeySequence > & end()
QVector::const_iterator constBegin() const const
QString arg(qlonglong a, int fieldWidth, int base, QChar fillChar) 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 Fri Nov 26 2021 23:16:44 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.