Messagelib

headerstylepluginmanager.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 "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 
78 #if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
80 #else
82 #endif
83  i.toBack();
84  const QPair<QStringList, QStringList> pair = PimCommon::PluginUtil::loadPluginSetting(configGroupName(), configPrefixSettingKey());
85  QVector<int> listOrder;
86  while (i.hasPrevious()) {
87  HeaderStylePluginInfo info;
88 
89  const KPluginMetaData data = i.previous();
90 
91  // 1) get plugin data => name/description etc.
92  info.pluginData = PimCommon::PluginUtil::createPluginMetaData(data);
93  // 2) look at if plugin is activated
94  const bool isPluginActivated =
95  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  info.data = data;
100  const QString version = data.version();
101  if (pluginVersion() == version) {
102  const QVariant p = data.rawData().value(QStringLiteral("X-KDE-MessageViewer-Header-Order")).toVariant();
103  int order = -1;
104  if (p.isValid()) {
105  order = p.toInt();
106  }
107  int pos = 0;
108  for (; pos < listOrder.count(); ++pos) {
109  if (listOrder.at(pos) > order) {
110  pos--;
111  break;
112  }
113  }
114  pos = qMax(0, pos);
115  listOrder.insert(pos, order);
116  info.plugin = nullptr;
117  mPluginList.insert(pos, info);
118  } else {
119  qCWarning(MESSAGEVIEWER_LOG) << "Plugin " << data.name() << " doesn't have correction plugin version. It will not be loaded.";
120  }
121  }
123  for (QVector<HeaderStylePluginInfo>::iterator it = mPluginList.begin(); it != end; ++it) {
124  loadPlugin(&(*it));
125  }
126 }
127 
128 QVector<MessageViewer::HeaderStylePlugin *> HeaderStylePluginManagerPrivate::pluginsList() const
129 {
131  QVector<HeaderStylePluginInfo>::ConstIterator end(mPluginList.constEnd());
132  for (QVector<HeaderStylePluginInfo>::ConstIterator it = mPluginList.constBegin(); it != end; ++it) {
133  if (auto plugin = (*it).plugin) {
134  lst << plugin;
135  }
136  }
137  return lst;
138 }
139 
140 void HeaderStylePluginManagerPrivate::loadPlugin(HeaderStylePluginInfo *item)
141 {
142  if (auto plugin = KPluginFactory::instantiatePlugin<MessageViewer::HeaderStylePlugin>(item->data, q, QVariantList() << item->metaDataFileName).plugin) {
143  item->plugin = plugin;
144  item->plugin->setIsEnabled(item->isEnabled);
145  item->pluginData.mHasConfigureDialog = false;
146  mPluginDataList.append(item->pluginData);
147  }
148 }
149 
150 MessageViewer::HeaderStylePlugin *HeaderStylePluginManagerPrivate::pluginFromIdentifier(const QString &id)
151 {
152  QVector<HeaderStylePluginInfo>::ConstIterator end(mPluginList.constEnd());
153  for (QVector<HeaderStylePluginInfo>::ConstIterator it = mPluginList.constBegin(); it != end; ++it) {
154  if ((*it).pluginData.mIdentifier == id) {
155  return (*it).plugin;
156  }
157  }
158  return {};
159 }
160 
161 HeaderStylePluginManager *HeaderStylePluginManager::self()
162 {
163  static HeaderStylePluginManager s_self;
164  return &s_self;
165 }
166 
167 HeaderStylePluginManager::HeaderStylePluginManager(QObject *parent)
168  : QObject(parent)
169  , d(new MessageViewer::HeaderStylePluginManagerPrivate(this))
170 {
171  d->initializePluginList();
172 }
173 
174 HeaderStylePluginManager::~HeaderStylePluginManager() = default;
175 
176 QVector<MessageViewer::HeaderStylePlugin *> HeaderStylePluginManager::pluginsList() const
177 {
178  return d->pluginsList();
179 }
180 
181 QStringList HeaderStylePluginManager::pluginListName() const
182 {
183  QStringList lst;
184  lst.reserve(d->pluginsList().count());
185  const auto pluginsList{d->pluginsList()};
186  for (MessageViewer::HeaderStylePlugin *plugin : pluginsList) {
187  lst << plugin->name();
188  }
189  return lst;
190 }
191 
192 QString HeaderStylePluginManager::configGroupName() const
193 {
194  return d->configGroupName();
195 }
196 
197 QString HeaderStylePluginManager::configPrefixSettingKey() const
198 {
199  return d->configPrefixSettingKey();
200 }
201 
202 QVector<PimCommon::PluginUtilData> HeaderStylePluginManager::pluginsDataList() const
203 {
204  return d->pluginDataList();
205 }
206 
207 MessageViewer::HeaderStylePlugin *HeaderStylePluginManager::pluginFromIdentifier(const QString &id)
208 {
209  return d->pluginFromIdentifier(id);
210 }
QJsonObject rawData() const
bool isValid() const const
QVector::iterator begin()
void insert(int i, T &&value)
QString fileName() const
The HeaderStylePluginManager class.
KCOREADDONS_EXPORT unsigned int version()
QString version() const
void reserve(int alloc)
const T & at(int i) const const
QString name() const
QString baseName() const const
int toInt(bool *ok) const const
QJsonValue value(const QString &key) const const
QVariant toVariant() const const
static QVector< KPluginMetaData > findPlugins(const QString &directory, std::function< bool(const KPluginMetaData &)> filter, KPluginMetaDataOption option)
The HeaderStylePlugin class.
int count(const T &value) 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 Tue May 24 2022 04:08:03 by doxygen 1.8.17 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.