Pimcommon

customtoolspluginmanager.cpp
1 /*
2  SPDX-FileCopyrightText: 2015-2022 Laurent Montel <[email protected]>
3 
4  SPDX-License-Identifier: GPL-2.0-or-later
5 */
6 
7 #include "customtoolspluginmanager.h"
8 #include "customtoolsplugin.h"
9 
10 #include <KPluginFactory>
11 #include <KPluginMetaData>
12 
13 #include <QDebug>
14 #include <QFileInfo>
15 #include <QSet>
16 
17 using namespace PimCommon;
18 
19 class CustomToolsPluginInfo
20 {
21 public:
22  Q_REQUIRED_RESULT QString saveName() const;
23 
24  KPluginMetaData metaData;
25  PimCommon::CustomToolsPlugin *plugin = nullptr;
26 };
27 
28 QString CustomToolsPluginInfo::saveName() const
29 {
30  return QFileInfo(metaData.fileName()).baseName();
31 }
32 
33 namespace
34 {
35 QString pluginVersion()
36 {
37  return QStringLiteral("1.0");
38 }
39 }
40 
41 class PimCommon::CustomToolsPluginManagerPrivate
42 {
43 public:
44  CustomToolsPluginManagerPrivate(CustomToolsPluginManager *qq)
45  : q(qq)
46  {
47  }
48 
49  Q_REQUIRED_RESULT QVector<PimCommon::CustomToolsPlugin *> pluginsList() const;
50  void initializePluginList();
51  void loadPlugin(CustomToolsPluginInfo *item);
53  CustomToolsPluginManager *const q;
54 };
55 
56 void CustomToolsPluginManagerPrivate::initializePluginList()
57 {
58  const QVector<KPluginMetaData> plugins =
59  KPluginMetaData::findPlugins(QStringLiteral("pim" QT_STRINGIFY(QT_VERSION_MAJOR)) + QStringLiteral("/pimcommon/customtools"));
60 #if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
62 #else
64 #endif
65  i.toBack();
66  QSet<QString> unique;
67  while (i.hasPrevious()) {
68  CustomToolsPluginInfo info;
69  info.metaData = i.previous();
70  if (info.metaData.version() == pluginVersion()) {
71  // only load plugins once, even if found multiple times!
72  if (unique.contains(info.saveName())) {
73  continue;
74  }
75  info.plugin = nullptr;
76  mPluginList.push_back(info);
77  unique.insert(info.saveName());
78  } else {
79  qWarning() << "Plugin " << info.metaData.name() << " doesn't have correction plugin version. It will not be loaded.";
80  }
81  }
83  for (QVector<CustomToolsPluginInfo>::iterator it = mPluginList.begin(); it != end; ++it) {
84  loadPlugin(&(*it));
85  }
86 }
87 
88 QVector<PimCommon::CustomToolsPlugin *> CustomToolsPluginManagerPrivate::pluginsList() const
89 {
91  QVector<CustomToolsPluginInfo>::ConstIterator end(mPluginList.constEnd());
92  for (QVector<CustomToolsPluginInfo>::ConstIterator it = mPluginList.constBegin(); it != end; ++it) {
93  if (auto plugin = (*it).plugin) {
94  lst << plugin;
95  }
96  }
97  return lst;
98 }
99 
100 void CustomToolsPluginManagerPrivate::loadPlugin(CustomToolsPluginInfo *item)
101 {
102  if (auto plugin = KPluginFactory::instantiatePlugin<PimCommon::CustomToolsPlugin>(item->metaData, q, QVariantList() << item->saveName()).plugin) {
103  item->plugin = plugin;
104  }
105 }
106 
107 CustomToolsPluginManager *CustomToolsPluginManager::self()
108 {
109  static CustomToolsPluginManager s_self;
110  return &s_self;
111 }
112 
113 CustomToolsPluginManager::CustomToolsPluginManager(QObject *parent)
114  : QObject(parent)
115  , d(new PimCommon::CustomToolsPluginManagerPrivate(this))
116 {
117  d->initializePluginList();
118 }
119 
120 CustomToolsPluginManager::~CustomToolsPluginManager() = default;
121 
122 QVector<PimCommon::CustomToolsPlugin *> CustomToolsPluginManager::pluginsList() const
123 {
124  return d->pluginsList();
125 }
folderdialogacltab.h
QVector::iterator begin()
The CustomToolsPluginManager class.
The CustomToolsPlugin class.
QString baseName() const const
bool contains(const T &value) const const
static QVector< KPluginMetaData > findPlugins(const QString &directory, std::function< bool(const KPluginMetaData &)> filter, KPluginMetaDataOption option)
QSet::iterator insert(const T &value)
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 Aug 9 2022 03:56:05 by doxygen 1.8.17 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.