Messagelib

viewerpluginmanager.cpp
1 /*
2  SPDX-FileCopyrightText: 2015-2022 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());
90 #if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
92 #else
94 #endif
95  i.toBack();
96  while (i.hasPrevious()) {
97  ViewerPluginInfo info;
98 
99  const KPluginMetaData data = i.previous();
100 
101  // 1) get plugin data => name/description etc.
102  info.pluginData = PimCommon::PluginUtil::createPluginMetaData(data);
103  // 2) look at if plugin is activated
104  const bool isPluginActivated =
105  PimCommon::PluginUtil::isPluginActivated(pair.first, pair.second, info.pluginData.mEnableByDefault, info.pluginData.mIdentifier);
106  info.isEnabled = isPluginActivated;
107  info.metaDataFileNameBaseName = QFileInfo(data.fileName()).baseName();
108  info.metaDataFileName = data.fileName();
109  info.data = data;
110 
111  if (pluginVersion() == data.version()) {
112  info.plugin = nullptr;
113  mPluginList.push_back(info);
114  } else {
115  qCWarning(MESSAGEVIEWER_LOG) << "Plugin name :" << data.name() << " doesn't have correct plugin version. Please update it";
116  }
117  }
118  QVector<ViewerPluginInfo>::iterator end(mPluginList.end());
119  for (QVector<ViewerPluginInfo>::iterator it = mPluginList.begin(); it != end; ++it) {
120  loadPlugin(&(*it));
121  }
122  return true;
123 }
124 
125 void ViewerPluginManagerPrivate::loadPlugin(ViewerPluginInfo *item)
126 {
127  if (auto plugin = KPluginFactory::instantiatePlugin<MessageViewer::ViewerPlugin>(item->data, q, QVariantList() << item->metaDataFileName).plugin) {
128  item->plugin = plugin;
129  item->plugin->setIsEnabled(item->isEnabled);
130  item->pluginData.mHasConfigureDialog = item->plugin->hasConfigureDialog();
131  mPluginDataList.append(item->pluginData);
132  }
133 }
134 
135 QVector<ViewerPlugin *> ViewerPluginManagerPrivate::pluginsList() const
136 {
138  QVector<ViewerPluginInfo>::ConstIterator end(mPluginList.constEnd());
139  for (QVector<ViewerPluginInfo>::ConstIterator it = mPluginList.constBegin(); it != end; ++it) {
140  if (auto plugin = (*it).plugin) {
141  lst << plugin;
142  }
143  }
144  return lst;
145 }
146 
147 QVector<PimCommon::PluginUtilData> ViewerPluginManagerPrivate::pluginDataList() const
148 {
149  return mPluginDataList;
150 }
151 
152 ViewerPlugin *ViewerPluginManagerPrivate::pluginFromIdentifier(const QString &id)
153 {
154  QVector<ViewerPluginInfo>::ConstIterator end(mPluginList.constEnd());
155  for (QVector<ViewerPluginInfo>::ConstIterator it = mPluginList.constBegin(); it != end; ++it) {
156  if ((*it).pluginData.mIdentifier == id) {
157  return (*it).plugin;
158  }
159  }
160  return {};
161 }
162 
163 ViewerPluginManager::ViewerPluginManager(QObject *parent)
164  : QObject(parent)
165  , d(new MessageViewer::ViewerPluginManagerPrivate(this))
166 {
167 }
168 
169 MessageViewer::ViewerPluginManager::~ViewerPluginManager() = default;
170 
171 bool ViewerPluginManager::initializePluginList()
172 {
173  return d->initializePluginList();
174 }
175 
176 ViewerPluginManager *ViewerPluginManager::self()
177 {
178  static ViewerPluginManager s_self;
179  return &s_self;
180 }
181 
182 QVector<MessageViewer::ViewerPlugin *> ViewerPluginManager::pluginsList() const
183 {
184  return d->pluginsList();
185 }
186 
187 void ViewerPluginManager::setPluginDirectory(const QString &directory)
188 {
189  d->pluginDirectory = directory;
190 }
191 
192 QString ViewerPluginManager::pluginDirectory() const
193 {
194  return d->pluginDirectory;
195 }
196 
197 QVector<PimCommon::PluginUtilData> ViewerPluginManager::pluginsDataList() const
198 {
199  return d->pluginDataList();
200 }
201 
202 QString ViewerPluginManager::configGroupName() const
203 {
204  return d->configGroupName();
205 }
206 
207 QString ViewerPluginManager::configPrefixSettingKey() const
208 {
209  return d->configPrefixSettingKey();
210 }
211 
212 MessageViewer::ViewerPlugin *ViewerPluginManager::pluginFromIdentifier(const QString &id)
213 {
214  return d->pluginFromIdentifier(id);
215 }
216 
217 void ViewerPluginManager::setPluginName(const QString &pluginName)
218 {
219  d->pluginName = pluginName;
220 }
221 
222 QString ViewerPluginManager::pluginName() const
223 {
224  return d->pluginName;
225 }
The ViewerPluginManager class.
QVector::iterator begin()
QString fileName() const
QString version() const
QString name() const
QString baseName() const const
The ViewerPlugin class.
Definition: viewerplugin.h:22
static QVector< KPluginMetaData > findPlugins(const QString &directory, std::function< bool(const KPluginMetaData &)> filter, KPluginMetaDataOption option)
QString arg(qlonglong a, int fieldWidth, int base, QChar fillChar) const const
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 Wed May 25 2022 03:55:41 by doxygen 1.8.17 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.