Messagelib

messageviewerconfiguresettingspluginmanager.cpp
1 /*
2  SPDX-FileCopyrightText: 2018-2022 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 
77 #if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
79 #else
81 #endif
82  i.toBack();
83  const QPair<QStringList, QStringList> pair = PimCommon::PluginUtil::loadPluginSetting(configGroupName(), configPrefixSettingKey());
84  QVector<int> listOrder;
85  while (i.hasPrevious()) {
86  ConfigureSettingsPluginInfo info;
87 
88  const KPluginMetaData data = i.previous();
89 
90  // 1) get plugin data => name/description etc.
91  info.pluginData = PimCommon::PluginUtil::createPluginMetaData(data);
92  // 2) look at if plugin is activated
93  info.metaDataFileNameBaseName = QFileInfo(data.fileName()).baseName();
94  info.metaDataFileName = data.fileName();
95  info.data = data;
96  const QString version = data.version();
97  if (configurePluginVersion() == version) {
98  const QVariant p = data.rawData().value(QStringLiteral("X-KDE-MessageViewer-Configure-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<ConfigureSettingsPluginInfo>::iterator it = mPluginList.begin(); it != end; ++it) {
120  loadPlugin(&(*it));
121  }
122 }
123 
124 QVector<MessageViewer::MessageViewerConfigureSettingsPlugin *> MessageViewerConfigureSettingsPluginManagerPrivate::pluginsList() const
125 {
128  for (QVector<ConfigureSettingsPluginInfo>::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 MessageViewerConfigureSettingsPluginManagerPrivate::loadPlugin(ConfigureSettingsPluginInfo *item)
137 {
138  if (auto plugin =
139  KPluginFactory::instantiatePlugin<MessageViewer::MessageViewerConfigureSettingsPlugin>(item->data, q, QVariantList() << item->metaDataFileName)
140  .plugin) {
141  item->plugin = plugin;
142  // By default it's true
143  item->pluginData.mHasConfigureDialog = true;
144  mPluginDataList.append(item->pluginData);
145  }
146 }
147 
148 MessageViewerConfigureSettingsPlugin *MessageViewerConfigureSettingsPluginManagerPrivate::pluginFromIdentifier(const QString &id)
149 {
151  for (QVector<ConfigureSettingsPluginInfo>::ConstIterator it = mPluginList.constBegin(); it != end; ++it) {
152  if ((*it).pluginData.mIdentifier == id) {
153  return (*it).plugin;
154  }
155  }
156  return {};
157 }
158 
159 MessageViewerConfigureSettingsPluginManager *MessageViewerConfigureSettingsPluginManager::self()
160 {
162  return &s_self;
163 }
164 
165 MessageViewerConfigureSettingsPluginManager::MessageViewerConfigureSettingsPluginManager(QObject *parent)
166  : QObject(parent)
167  , d(new MessageViewer::MessageViewerConfigureSettingsPluginManagerPrivate(this))
168 {
169  d->initializePluginList();
170 }
171 
172 MessageViewerConfigureSettingsPluginManager::~MessageViewerConfigureSettingsPluginManager() = default;
173 
174 QVector<MessageViewer::MessageViewerConfigureSettingsPlugin *> MessageViewerConfigureSettingsPluginManager::pluginsList() const
175 {
176  return d->pluginsList();
177 }
178 
179 QString MessageViewerConfigureSettingsPluginManager::configGroupName() const
180 {
181  return d->configGroupName();
182 }
183 
184 QString MessageViewerConfigureSettingsPluginManager::configPrefixSettingKey() const
185 {
186  return d->configPrefixSettingKey();
187 }
188 
189 QVector<PimCommon::PluginUtilData> MessageViewerConfigureSettingsPluginManager::pluginsDataList() const
190 {
191  return d->pluginDataList();
192 }
193 
194 MessageViewerConfigureSettingsPlugin *MessageViewerConfigureSettingsPluginManager::pluginFromIdentifier(const QString &id)
195 {
196  return d->pluginFromIdentifier(id);
197 }
QJsonObject rawData() const
bool isValid() const const
QVector::iterator begin()
void insert(int i, T &&value)
QString fileName() const
QString version() const
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)
KDB_EXPORT KDbVersionInfo version()
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 Wed May 25 2022 03:55:40 by doxygen 1.8.17 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.