Messagelib

messageviewercheckbeforedeletingpluginmanager.cpp
1 /*
2  SPDX-FileCopyrightText: 2021 Laurent Montel <[email protected]>
3 
4  SPDX-License-Identifier: LGPL-2.0-or-later
5 */
6 
7 #include "messageviewercheckbeforedeletingpluginmanager.h"
8 #include "messageviewer_debug.h"
9 #include "messageviewercheckbeforedeletingplugin.h"
10 #include <KPluginFactory>
11 #include <KPluginMetaData>
12 #include <QFileInfo>
13 
14 using namespace MessageViewer;
15 
16 class CheckBeforeDeletingPluginInfo
17 {
18 public:
19  CheckBeforeDeletingPluginInfo() = default;
20 
21  KPluginMetaData data;
22  PimCommon::PluginUtilData pluginData;
23  QString metaDataFileNameBaseName;
24  QString metaDataFileName;
25  bool isEnabled = true;
27 };
28 
29 class MessageViewer::MessageViewerCheckBeforeDeletingPluginManagerPrivate
30 {
31 public:
32  explicit MessageViewerCheckBeforeDeletingPluginManagerPrivate(MessageViewerCheckBeforeDeletingPluginManager *qq)
33  : q(qq)
34  {
35  }
36 
37  Q_REQUIRED_RESULT QVector<MessageViewer::MessageViewerCheckBeforeDeletingPlugin *> pluginsList() const;
38  Q_REQUIRED_RESULT QVector<PimCommon::PluginUtilData> pluginDataList() const;
39  void initializePluginList();
40  void loadPlugin(CheckBeforeDeletingPluginInfo *item);
41  Q_REQUIRED_RESULT QString configGroupName() const;
42  Q_REQUIRED_RESULT QString configPrefixSettingKey() const;
43  Q_REQUIRED_RESULT MessageViewerCheckBeforeDeletingPlugin *pluginFromIdentifier(const QString &id);
44 
45 private:
46  QVector<PimCommon::PluginUtilData> mPluginDataList;
49 };
50 
51 namespace
52 {
53 QString pluginVersion()
54 {
55  return QStringLiteral("1.0");
56 }
57 }
58 
59 QVector<PimCommon::PluginUtilData> MessageViewerCheckBeforeDeletingPluginManagerPrivate::pluginDataList() const
60 {
61  return mPluginDataList;
62 }
63 
64 QString MessageViewerCheckBeforeDeletingPluginManagerPrivate::configGroupName() const
65 {
66  return QStringLiteral("MessageViewerCheckBeforeDeletingPlugins");
67 }
68 
69 QString MessageViewerCheckBeforeDeletingPluginManagerPrivate::configPrefixSettingKey() const
70 {
71  return QStringLiteral("MessageViewerCheckBeforeDeletingPlugin");
72 }
73 
74 void MessageViewerCheckBeforeDeletingPluginManagerPrivate::initializePluginList()
75 {
76  const QVector<KPluginMetaData> plugins = KPluginMetaData::findPlugins(QStringLiteral("messageviewer/checkbeforedeleting"));
78  i.toBack();
79  const QPair<QStringList, QStringList> pair = PimCommon::PluginUtil::loadPluginSetting(configGroupName(), configPrefixSettingKey());
80  QVector<int> listOrder;
81  while (i.hasPrevious()) {
82  CheckBeforeDeletingPluginInfo 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  const bool isPluginActivated =
90  PimCommon::PluginUtil::isPluginActivated(pair.first, pair.second, info.pluginData.mEnableByDefault, info.pluginData.mIdentifier);
91  info.isEnabled = isPluginActivated;
92  info.data = data;
93 
94  info.metaDataFileNameBaseName = QFileInfo(data.fileName()).baseName();
95  info.metaDataFileName = data.fileName();
96  const QString version = data.version();
97  if (pluginVersion() == 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<CheckBeforeDeletingPluginInfo>::iterator it = mPluginList.begin(); it != end; ++it) {
120  loadPlugin(&(*it));
121  }
122 }
123 
124 QVector<MessageViewer::MessageViewerCheckBeforeDeletingPlugin *> MessageViewerCheckBeforeDeletingPluginManagerPrivate::pluginsList() const
125 {
128  for (QVector<CheckBeforeDeletingPluginInfo>::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 MessageViewerCheckBeforeDeletingPluginManagerPrivate::loadPlugin(CheckBeforeDeletingPluginInfo *item)
137 {
138  if (auto plugin =
139  KPluginFactory::instantiatePlugin<MessageViewer::MessageViewerCheckBeforeDeletingPlugin>(item->data, q, QVariantList() << item->metaDataFileName)
140  .plugin) {
141  item->plugin = plugin;
142  // By default it's true
143  item->pluginData.mHasConfigureDialog = true;
144  item->plugin->setIsEnabled(item->isEnabled);
145  mPluginDataList.append(item->pluginData);
146  }
147 }
148 
149 MessageViewerCheckBeforeDeletingPlugin *MessageViewerCheckBeforeDeletingPluginManagerPrivate::pluginFromIdentifier(const QString &id)
150 {
152  for (QVector<CheckBeforeDeletingPluginInfo>::ConstIterator it = mPluginList.constBegin(); it != end; ++it) {
153  if ((*it).pluginData.mIdentifier == id) {
154  return (*it).plugin;
155  }
156  }
157  return {};
158 }
159 
160 MessageViewerCheckBeforeDeletingPluginManager *MessageViewerCheckBeforeDeletingPluginManager::self()
161 {
163  return &s_self;
164 }
165 
166 MessageViewerCheckBeforeDeletingPluginManager::MessageViewerCheckBeforeDeletingPluginManager(QObject *parent)
167  : QObject(parent)
168  , d(new MessageViewer::MessageViewerCheckBeforeDeletingPluginManagerPrivate(this))
169 {
170  d->initializePluginList();
171 }
172 
173 MessageViewerCheckBeforeDeletingPluginManager::~MessageViewerCheckBeforeDeletingPluginManager() = default;
174 
175 QVector<MessageViewer::MessageViewerCheckBeforeDeletingPlugin *> MessageViewerCheckBeforeDeletingPluginManager::pluginsList() const
176 {
177  return d->pluginsList();
178 }
179 
180 QString MessageViewerCheckBeforeDeletingPluginManager::configGroupName() const
181 {
182  return d->configGroupName();
183 }
184 
185 QString MessageViewerCheckBeforeDeletingPluginManager::configPrefixSettingKey() const
186 {
187  return d->configPrefixSettingKey();
188 }
189 
190 QVector<PimCommon::PluginUtilData> MessageViewerCheckBeforeDeletingPluginManager::pluginsDataList() const
191 {
192  return d->pluginDataList();
193 }
194 
195 MessageViewerCheckBeforeDeletingPlugin *MessageViewerCheckBeforeDeletingPluginManager::pluginFromIdentifier(const QString &id)
196 {
197  return d->pluginFromIdentifier(id);
198 }
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 Sun Dec 5 2021 23:04:54 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.