Messagelib

headerstylepluginmanager.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 "headerstylepluginmanager.h"
8 #include "headerstyleplugin.h"
9 #include "messageviewer_debug.h"
10 #include <KPluginFactory>
11 #include <KPluginMetaData>
12 #include <QFileInfo>
13 
14 using namespace MessageViewer;
15 
16 class HeaderStylePluginInfo
17 {
18 public:
19  HeaderStylePluginInfo() = default;
20 
21  PimCommon::PluginUtilData pluginData;
22  QString metaDataFileNameBaseName;
23  QString metaDataFileName;
24  KPluginMetaData data;
25  MessageViewer::HeaderStylePlugin *plugin = nullptr;
26  bool isEnabled = false;
27 };
28 
29 class MessageViewer::HeaderStylePluginManagerPrivate
30 {
31 public:
32  HeaderStylePluginManagerPrivate(HeaderStylePluginManager *qq)
33  : q(qq)
34  {
35  }
36 
37  Q_REQUIRED_RESULT QVector<MessageViewer::HeaderStylePlugin *> pluginsList() const;
38  Q_REQUIRED_RESULT QVector<PimCommon::PluginUtilData> pluginDataList() const;
39  void initializePluginList();
40  void loadPlugin(HeaderStylePluginInfo *item);
41  Q_REQUIRED_RESULT QString configGroupName() const;
42  Q_REQUIRED_RESULT QString configPrefixSettingKey() const;
43  MessageViewer::HeaderStylePlugin *pluginFromIdentifier(const QString &id);
44 
45 private:
46  QVector<PimCommon::PluginUtilData> mPluginDataList;
48  HeaderStylePluginManager *const q;
49 };
50 
51 namespace
52 {
53 QString pluginVersion()
54 {
55  return QStringLiteral("1.0");
56 }
57 }
58 
59 QVector<PimCommon::PluginUtilData> HeaderStylePluginManagerPrivate::pluginDataList() const
60 {
61  return mPluginDataList;
62 }
63 
64 QString HeaderStylePluginManagerPrivate::configGroupName() const
65 {
66  return QStringLiteral("HeaderStylePlugins");
67 }
68 
69 QString HeaderStylePluginManagerPrivate::configPrefixSettingKey() const
70 {
71  return QStringLiteral("PluginHeaderStyle");
72 }
73 
74 void HeaderStylePluginManagerPrivate::initializePluginList()
75 {
76  const QVector<KPluginMetaData> plugins = KPluginMetaData::findPlugins(QStringLiteral("messageviewer/headerstyle"));
77 
79  i.toBack();
80  const QPair<QStringList, QStringList> pair = PimCommon::PluginUtil::loadPluginSetting(configGroupName(), configPrefixSettingKey());
81  QVector<int> listOrder;
82  while (i.hasPrevious()) {
83  HeaderStylePluginInfo info;
84 
85  const KPluginMetaData data = i.previous();
86 
87  // 1) get plugin data => name/description etc.
88  info.pluginData = PimCommon::PluginUtil::createPluginMetaData(data);
89  // 2) look at if plugin is activated
90  const bool isPluginActivated =
91  PimCommon::PluginUtil::isPluginActivated(pair.first, pair.second, info.pluginData.mEnableByDefault, info.pluginData.mIdentifier);
92  info.isEnabled = isPluginActivated;
93  info.metaDataFileNameBaseName = QFileInfo(data.fileName()).baseName();
94  info.metaDataFileName = data.fileName();
95  info.data = data;
96  const QString version = data.version();
97  if (pluginVersion() == version) {
98  const QVariant p = data.rawData().value(QStringLiteral("X-KDE-MessageViewer-Header-Order")).toVariant();
99  int order = -1;
100  if (p.isValid()) {
101  order = p.toInt();
102  }
103  int pos = 0;
104  for (; pos < listOrder.count(); ++pos) {
105  if (listOrder.at(pos) > order) {
106  pos--;
107  break;
108  }
109  }
110  pos = qMax(0, pos);
111  listOrder.insert(pos, order);
112  info.plugin = nullptr;
113  mPluginList.insert(pos, info);
114  } else {
115  qCWarning(MESSAGEVIEWER_LOG) << "Plugin " << data.name() << " doesn't have correction plugin version. It will not be loaded.";
116  }
117  }
119  for (QVector<HeaderStylePluginInfo>::iterator it = mPluginList.begin(); it != end; ++it) {
120  loadPlugin(&(*it));
121  }
122 }
123 
124 QVector<MessageViewer::HeaderStylePlugin *> HeaderStylePluginManagerPrivate::pluginsList() const
125 {
127  QVector<HeaderStylePluginInfo>::ConstIterator end(mPluginList.constEnd());
128  for (QVector<HeaderStylePluginInfo>::ConstIterator it = mPluginList.constBegin(); it != end; ++it) {
129  if (auto plugin = (*it).plugin) {
130  lst << plugin;
131  }
132  }
133  return lst;
134 }
135 
136 void HeaderStylePluginManagerPrivate::loadPlugin(HeaderStylePluginInfo *item)
137 {
138  if (auto plugin = KPluginFactory::instantiatePlugin<MessageViewer::HeaderStylePlugin>(item->data, q, QVariantList() << item->metaDataFileName).plugin) {
139  item->plugin = plugin;
140  item->plugin->setIsEnabled(item->isEnabled);
141  item->pluginData.mHasConfigureDialog = false;
142  mPluginDataList.append(item->pluginData);
143  }
144 }
145 
146 MessageViewer::HeaderStylePlugin *HeaderStylePluginManagerPrivate::pluginFromIdentifier(const QString &id)
147 {
148  QVector<HeaderStylePluginInfo>::ConstIterator end(mPluginList.constEnd());
149  for (QVector<HeaderStylePluginInfo>::ConstIterator it = mPluginList.constBegin(); it != end; ++it) {
150  if ((*it).pluginData.mIdentifier == id) {
151  return (*it).plugin;
152  }
153  }
154  return {};
155 }
156 
157 HeaderStylePluginManager *HeaderStylePluginManager::self()
158 {
159  static HeaderStylePluginManager s_self;
160  return &s_self;
161 }
162 
163 HeaderStylePluginManager::HeaderStylePluginManager(QObject *parent)
164  : QObject(parent)
165  , d(new MessageViewer::HeaderStylePluginManagerPrivate(this))
166 {
167  d->initializePluginList();
168 }
169 
170 HeaderStylePluginManager::~HeaderStylePluginManager() = default;
171 
172 QVector<MessageViewer::HeaderStylePlugin *> HeaderStylePluginManager::pluginsList() const
173 {
174  return d->pluginsList();
175 }
176 
177 QStringList HeaderStylePluginManager::pluginListName() const
178 {
179  QStringList lst;
180  lst.reserve(d->pluginsList().count());
181  const auto pluginsList{d->pluginsList()};
182  for (MessageViewer::HeaderStylePlugin *plugin : pluginsList) {
183  lst << plugin->name();
184  }
185  return lst;
186 }
187 
188 QString HeaderStylePluginManager::configGroupName() const
189 {
190  return d->configGroupName();
191 }
192 
193 QString HeaderStylePluginManager::configPrefixSettingKey() const
194 {
195  return d->configPrefixSettingKey();
196 }
197 
198 QVector<PimCommon::PluginUtilData> HeaderStylePluginManager::pluginsDataList() const
199 {
200  return d->pluginDataList();
201 }
202 
203 MessageViewer::HeaderStylePlugin *HeaderStylePluginManager::pluginFromIdentifier(const QString &id)
204 {
205  return d->pluginFromIdentifier(id);
206 }
QString fileName() const
QString name() const
QVector::iterator begin()
void reserve(int alloc)
void insert(int i, T &&value)
int toInt(bool *ok) const const
QJsonObject rawData() const
The HeaderStylePluginManager class.
The HeaderStylePlugin class.
QVariant toVariant() const const
const QList< QKeySequence > & end()
const T & at(int i) const const
QVector::const_iterator constBegin() const const
int count(const T &value) const const
static QVector< KPluginMetaData > findPlugins(const QString &directory, std::function< bool(const KPluginMetaData &)> filter={})
bool isValid() const const
QJsonValue value(const QString &key) const const
QString version() const
KDB_EXPORT KDbVersionInfo version()
This file is part of the KDE documentation.
Documentation copyright © 1996-2021 The KDE developers.
Generated on Tue Nov 30 2021 23:05:46 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.