Pimcommon

customtoolspluginmanager.cpp
1/*
2 SPDX-FileCopyrightText: 2015-2024 Laurent Montel <montel@kde.org>
3
4 SPDX-License-Identifier: GPL-2.0-or-later
5*/
6
7#include "customtoolspluginmanager.h"
8#include "customtoolsplugin.h"
9#include "pimcommon_debug.h"
10
11#include <KPluginFactory>
12#include <KPluginMetaData>
13
14#include <QFileInfo>
15#include <QSet>
16
17using namespace PimCommon;
18
19class CustomToolsPluginInfo
20{
21public:
22 [[nodiscard]] QString saveName() const;
23
24 KPluginMetaData metaData;
25 PimCommon::CustomToolsPlugin *plugin = nullptr;
26};
27
28QString CustomToolsPluginInfo::saveName() const
29{
30 return QFileInfo(metaData.fileName()).baseName();
31}
32
33namespace
34{
35QString pluginVersion()
36{
37 return QStringLiteral("1.0");
38}
39}
40
41class PimCommon::CustomToolsPluginManagerPrivate
42{
43public:
44 CustomToolsPluginManagerPrivate(CustomToolsPluginManager *qq)
45 : q(qq)
46 {
47 }
48
49 [[nodiscard]] QList<PimCommon::CustomToolsPlugin *> pluginsList() const;
50 void initializePluginList();
51 void loadPlugin(CustomToolsPluginInfo *item);
54};
55
56void CustomToolsPluginManagerPrivate::initializePluginList()
57{
58 const QList<KPluginMetaData> plugins = KPluginMetaData::findPlugins(QStringLiteral("pim6/pimcommon/customtools"));
60 i.toBack();
61 QSet<QString> unique;
62 while (i.hasPrevious()) {
63 CustomToolsPluginInfo info;
64 info.metaData = i.previous();
65 if (info.metaData.version() == pluginVersion()) {
66 // only load plugins once, even if found multiple times!
67 if (unique.contains(info.saveName())) {
68 continue;
69 }
70 info.plugin = nullptr;
71 mPluginList.push_back(info);
72 unique.insert(info.saveName());
73 } else {
74 qCWarning(PIMCOMMON_LOG) << "Plugin " << info.metaData.name() << " doesn't have correction plugin version. It will not be loaded.";
75 }
76 }
78 for (QList<CustomToolsPluginInfo>::iterator it = mPluginList.begin(); it != end; ++it) {
79 loadPlugin(&(*it));
80 }
81}
82
83QList<PimCommon::CustomToolsPlugin *> CustomToolsPluginManagerPrivate::pluginsList() const
84{
87 for (QList<CustomToolsPluginInfo>::ConstIterator it = mPluginList.constBegin(); it != end; ++it) {
88 if (auto plugin = (*it).plugin) {
89 lst << plugin;
90 }
91 }
92 return lst;
93}
94
95void CustomToolsPluginManagerPrivate::loadPlugin(CustomToolsPluginInfo *item)
96{
97 if (auto plugin = KPluginFactory::instantiatePlugin<PimCommon::CustomToolsPlugin>(item->metaData, q, QVariantList() << item->saveName()).plugin) {
98 item->plugin = plugin;
99 }
100}
101
102CustomToolsPluginManager *CustomToolsPluginManager::self()
103{
104 static CustomToolsPluginManager s_self;
105 return &s_self;
106}
107
108CustomToolsPluginManager::CustomToolsPluginManager(QObject *parent)
109 : QObject(parent)
110 , d(new PimCommon::CustomToolsPluginManagerPrivate(this))
111{
112 d->initializePluginList();
113}
114
115CustomToolsPluginManager::~CustomToolsPluginManager() = default;
116
117QList<PimCommon::CustomToolsPlugin *> CustomToolsPluginManager::pluginsList() const
118{
119 return d->pluginsList();
120}
121
122#include "moc_customtoolspluginmanager.cpp"
QString fileName() const
static QList< KPluginMetaData > findPlugins(const QString &directory, std::function< bool(const KPluginMetaData &)> filter={}, KPluginMetaDataOptions options={})
QString name() const
QString version() const
The CustomToolsPluginManager class.
The CustomToolsPlugin class.
const QList< QKeySequence > & end()
folderdialogacltab.h
QString baseName() const const
iterator begin()
const_iterator constBegin() const const
const_iterator constEnd() const const
iterator end()
void push_back(parameter_type value)
bool contains(const QSet< T > &other) const const
iterator insert(const T &value)
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Mon Nov 18 2024 12:14:35 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.