Messagelib

messageviewercheckbeforedeletingpluginmanager.cpp
1 /*
2  SPDX-FileCopyrightText: 2021-2022 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"));
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  CheckBeforeDeletingPluginInfo 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  const bool isPluginActivated =
94  PimCommon::PluginUtil::isPluginActivated(pair.first, pair.second, info.pluginData.mEnableByDefault, info.pluginData.mIdentifier);
95  info.isEnabled = isPluginActivated;
96  info.data = data;
97 
98  info.metaDataFileNameBaseName = QFileInfo(data.fileName()).baseName();
99  info.metaDataFileName = data.fileName();
100  const QString version = data.version();
101  if (pluginVersion() == version) {
102  const QVariant p = data.rawData().value(QStringLiteral("X-KDE-MessageViewer-Configure-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<CheckBeforeDeletingPluginInfo>::iterator it = mPluginList.begin(); it != end; ++it) {
124  loadPlugin(&(*it));
125  }
126 }
127 
128 QVector<MessageViewer::MessageViewerCheckBeforeDeletingPlugin *> MessageViewerCheckBeforeDeletingPluginManagerPrivate::pluginsList() const
129 {
132  for (QVector<CheckBeforeDeletingPluginInfo>::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 MessageViewerCheckBeforeDeletingPluginManagerPrivate::loadPlugin(CheckBeforeDeletingPluginInfo *item)
141 {
142  if (auto plugin =
143  KPluginFactory::instantiatePlugin<MessageViewer::MessageViewerCheckBeforeDeletingPlugin>(item->data, q, QVariantList() << item->metaDataFileName)
144  .plugin) {
145  item->plugin = plugin;
146  // By default it's true
147  item->pluginData.mHasConfigureDialog = true;
148  item->plugin->setIsEnabled(item->isEnabled);
149  mPluginDataList.append(item->pluginData);
150  }
151 }
152 
153 MessageViewerCheckBeforeDeletingPlugin *MessageViewerCheckBeforeDeletingPluginManagerPrivate::pluginFromIdentifier(const QString &id)
154 {
156  for (QVector<CheckBeforeDeletingPluginInfo>::ConstIterator it = mPluginList.constBegin(); it != end; ++it) {
157  if ((*it).pluginData.mIdentifier == id) {
158  return (*it).plugin;
159  }
160  }
161  return {};
162 }
163 
164 MessageViewerCheckBeforeDeletingPluginManager *MessageViewerCheckBeforeDeletingPluginManager::self()
165 {
167  return &s_self;
168 }
169 
170 MessageViewerCheckBeforeDeletingPluginManager::MessageViewerCheckBeforeDeletingPluginManager(QObject *parent)
171  : QObject(parent)
172  , d(new MessageViewer::MessageViewerCheckBeforeDeletingPluginManagerPrivate(this))
173 {
174  d->initializePluginList();
175 }
176 
177 MessageViewerCheckBeforeDeletingPluginManager::~MessageViewerCheckBeforeDeletingPluginManager() = default;
178 
179 QVector<MessageViewer::MessageViewerCheckBeforeDeletingPlugin *> MessageViewerCheckBeforeDeletingPluginManager::pluginsList() const
180 {
181  return d->pluginsList();
182 }
183 
184 QString MessageViewerCheckBeforeDeletingPluginManager::configGroupName() const
185 {
186  return d->configGroupName();
187 }
188 
189 QString MessageViewerCheckBeforeDeletingPluginManager::configPrefixSettingKey() const
190 {
191  return d->configPrefixSettingKey();
192 }
193 
194 QVector<PimCommon::PluginUtilData> MessageViewerCheckBeforeDeletingPluginManager::pluginsDataList() const
195 {
196  return d->pluginDataList();
197 }
198 
199 MessageViewerCheckBeforeDeletingPlugin *MessageViewerCheckBeforeDeletingPluginManager::pluginFromIdentifier(const QString &id)
200 {
201  return d->pluginFromIdentifier(id);
202 }
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 Thu May 19 2022 03:53:25 by doxygen 1.8.17 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.