Messagelib

messageviewerconfiguresettingspluginmanager.cpp
1 /*
2  SPDX-FileCopyrightText: 2018-2021 Laurent Montel <[email protected]>
3 
4  SPDX-License-Identifier: LGPL-2.0-or-later
5 */
6 
7 #include "messageviewerconfiguresettingspluginmanager.h"
8 #include "messageviewer_debug.h"
9 #include "messageviewerconfiguresettingsplugin.h"
10 #include <KPluginFactory>
11 #include <KPluginMetaData>
12 #include <QFileInfo>
13 
14 using namespace MessageViewer;
15 
16 class ConfigureSettingsPluginInfo
17 {
18 public:
19  ConfigureSettingsPluginInfo() = default;
20 
21  KPluginMetaData data;
22  PimCommon::PluginUtilData pluginData;
23  QString metaDataFileNameBaseName;
24  QString metaDataFileName;
26 };
27 
28 class MessageViewer::MessageViewerConfigureSettingsPluginManagerPrivate
29 {
30 public:
31  explicit MessageViewerConfigureSettingsPluginManagerPrivate(MessageViewerConfigureSettingsPluginManager *qq)
32  : q(qq)
33  {
34  }
35 
36  Q_REQUIRED_RESULT QVector<MessageViewer::MessageViewerConfigureSettingsPlugin *> pluginsList() const;
37  Q_REQUIRED_RESULT QVector<PimCommon::PluginUtilData> pluginDataList() const;
38  void initializePluginList();
39  void loadPlugin(ConfigureSettingsPluginInfo *item);
40  Q_REQUIRED_RESULT QString configGroupName() const;
41  Q_REQUIRED_RESULT QString configPrefixSettingKey() const;
42  MessageViewerConfigureSettingsPlugin *pluginFromIdentifier(const QString &id);
43 
44 private:
45  QVector<PimCommon::PluginUtilData> mPluginDataList;
48 };
49 
50 namespace
51 {
52 QString configurePluginVersion()
53 {
54  return QStringLiteral("1.0");
55 }
56 }
57 
58 QVector<PimCommon::PluginUtilData> MessageViewerConfigureSettingsPluginManagerPrivate::pluginDataList() const
59 {
60  return mPluginDataList;
61 }
62 
63 QString MessageViewerConfigureSettingsPluginManagerPrivate::configGroupName() const
64 {
65  return QStringLiteral("MessageViewerConfigureSettingsPlugins");
66 }
67 
68 QString MessageViewerConfigureSettingsPluginManagerPrivate::configPrefixSettingKey() const
69 {
70  return QStringLiteral("MessageViewerConfigureSettingsPlugin");
71 }
72 
73 void MessageViewerConfigureSettingsPluginManagerPrivate::initializePluginList()
74 {
75  const QVector<KPluginMetaData> plugins = KPluginMetaData::findPlugins(QStringLiteral("messageviewer/configuresettings"));
76 
78  i.toBack();
79  const QPair<QStringList, QStringList> pair = PimCommon::PluginUtil::loadPluginSetting(configGroupName(), configPrefixSettingKey());
80  QVector<int> listOrder;
81  while (i.hasPrevious()) {
82  ConfigureSettingsPluginInfo info;
83 
84  const KPluginMetaData data = i.previous();
85 
86  // 1) get plugin data => name/description etc.
87  info.pluginData = PimCommon::PluginUtil::createPluginMetaData(data);
88  // 2) look at if plugin is activated
89  info.metaDataFileNameBaseName = QFileInfo(data.fileName()).baseName();
90  info.metaDataFileName = data.fileName();
91  info.data = data;
92  const QString version = data.version();
93  if (configurePluginVersion() == version) {
94  const QVariant p = data.rawData().value(QStringLiteral("X-KDE-MessageViewer-Configure-Order")).toVariant();
95  int order = -1;
96  if (p.isValid()) {
97  order = p.toInt();
98  }
99  int pos = 0;
100  for (; pos < listOrder.count(); ++pos) {
101  if (listOrder.at(pos) > order) {
102  pos--;
103  break;
104  }
105  }
106  pos = qMax(0, pos);
107  listOrder.insert(pos, order);
108  info.plugin = nullptr;
109  mPluginList.insert(pos, info);
110  } else {
111  qCWarning(MESSAGEVIEWER_LOG) << "Plugin " << data.name() << " doesn't have correction plugin version. It will not be loaded.";
112  }
113  }
115  for (QVector<ConfigureSettingsPluginInfo>::iterator it = mPluginList.begin(); it != end; ++it) {
116  loadPlugin(&(*it));
117  }
118 }
119 
120 QVector<MessageViewer::MessageViewerConfigureSettingsPlugin *> MessageViewerConfigureSettingsPluginManagerPrivate::pluginsList() const
121 {
124  for (QVector<ConfigureSettingsPluginInfo>::ConstIterator it = mPluginList.constBegin(); it != end; ++it) {
125  if (auto plugin = (*it).plugin) {
126  lst << plugin;
127  }
128  }
129  return lst;
130 }
131 
132 void MessageViewerConfigureSettingsPluginManagerPrivate::loadPlugin(ConfigureSettingsPluginInfo *item)
133 {
134  if (auto plugin =
135  KPluginFactory::instantiatePlugin<MessageViewer::MessageViewerConfigureSettingsPlugin>(item->data, q, QVariantList() << item->metaDataFileName)
136  .plugin) {
137  item->plugin = plugin;
138  // By default it's true
139  item->pluginData.mHasConfigureDialog = true;
140  mPluginDataList.append(item->pluginData);
141  }
142 }
143 
144 MessageViewerConfigureSettingsPlugin *MessageViewerConfigureSettingsPluginManagerPrivate::pluginFromIdentifier(const QString &id)
145 {
147  for (QVector<ConfigureSettingsPluginInfo>::ConstIterator it = mPluginList.constBegin(); it != end; ++it) {
148  if ((*it).pluginData.mIdentifier == id) {
149  return (*it).plugin;
150  }
151  }
152  return {};
153 }
154 
155 MessageViewerConfigureSettingsPluginManager *MessageViewerConfigureSettingsPluginManager::self()
156 {
158  return &s_self;
159 }
160 
161 MessageViewerConfigureSettingsPluginManager::MessageViewerConfigureSettingsPluginManager(QObject *parent)
162  : QObject(parent)
163  , d(new MessageViewer::MessageViewerConfigureSettingsPluginManagerPrivate(this))
164 {
165  d->initializePluginList();
166 }
167 
168 MessageViewerConfigureSettingsPluginManager::~MessageViewerConfigureSettingsPluginManager() = default;
169 
170 QVector<MessageViewer::MessageViewerConfigureSettingsPlugin *> MessageViewerConfigureSettingsPluginManager::pluginsList() const
171 {
172  return d->pluginsList();
173 }
174 
175 QString MessageViewerConfigureSettingsPluginManager::configGroupName() const
176 {
177  return d->configGroupName();
178 }
179 
180 QString MessageViewerConfigureSettingsPluginManager::configPrefixSettingKey() const
181 {
182  return d->configPrefixSettingKey();
183 }
184 
185 QVector<PimCommon::PluginUtilData> MessageViewerConfigureSettingsPluginManager::pluginsDataList() const
186 {
187  return d->pluginDataList();
188 }
189 
190 MessageViewerConfigureSettingsPlugin *MessageViewerConfigureSettingsPluginManager::pluginFromIdentifier(const QString &id)
191 {
192  return d->pluginFromIdentifier(id);
193 }
QString fileName() const
QString name() const
QVector::iterator begin()
void insert(int i, T &&value)
int toInt(bool *ok) const const
QJsonObject rawData() const
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 Fri Nov 26 2021 23:16:42 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.