Pimcommon

genericpluginmanager.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 "genericpluginmanager.h"
8#include "genericplugin.h"
9#include "pimcommon_debug.h"
10
11#include <KPluginFactory>
12
13#include <QFileInfo>
14
15using namespace PimCommon;
16
17class GenericPluginInfo
18{
19public:
20 GenericPluginInfo() = default;
21
22 KPluginMetaData data;
23 QString metaDataFileNameBaseName;
24 QString metaDataFileName;
26 PimCommon::GenericPlugin *plugin = nullptr;
27 bool isEnabled = true;
28};
29
30namespace
31{
32QString pluginVersion()
33{
34 return QStringLiteral("1.0");
35}
36}
37
38class PimCommon::GenericPluginManagerPrivate
39{
40public:
41 explicit GenericPluginManagerPrivate(GenericPluginManager *qq)
42 : q(qq)
43 {
44 }
45
46 void loadPlugin(GenericPluginInfo *item);
47 [[nodiscard]] QList<GenericPlugin *> pluginsList() const;
48 bool initializePlugins();
49 QString pluginDirectory;
50 QString pluginName;
51 QList<GenericPluginInfo> mPluginList;
52
53 [[nodiscard]] QList<PluginUtilData> pluginsDataList() const;
54 [[nodiscard]] QString configGroupName() const;
55 [[nodiscard]] QString configPrefixSettingKey() const;
56 GenericPlugin *pluginFromIdentifier(const QString &id);
57
58private:
59 QList<PluginUtilData> mPluginDataList;
60 GenericPluginManager *const q;
61};
62
63QString GenericPluginManagerPrivate::configGroupName() const
64{
65 return QStringLiteral("GenericPlugin-%1").arg(pluginName);
66}
67
68QString GenericPluginManagerPrivate::configPrefixSettingKey() const
69{
70 return QStringLiteral("%1Plugin").arg(pluginName);
71}
72
73bool GenericPluginManagerPrivate::initializePlugins()
74{
75 if (!mPluginList.isEmpty()) {
76 return true;
77 }
78
79 if (pluginDirectory.isEmpty() || pluginName.isEmpty()) {
80 return false;
81 }
82 const QList<KPluginMetaData> plugins = KPluginMetaData::findPlugins(pluginDirectory);
83
84 const QPair<QStringList, QStringList> pair = PimCommon::PluginUtil::loadPluginSetting(configGroupName(), configPrefixSettingKey());
86 i.toBack();
87 while (i.hasPrevious()) {
88 GenericPluginInfo info;
89 const KPluginMetaData data = i.previous();
90
91 // 1) get plugin data => name/description etc.
92 info.pluginData = PimCommon::PluginUtil::createPluginMetaData(data);
93 // 2) look at if plugin is activated
94 const bool isPluginActivated =
95 PimCommon::PluginUtil::isPluginActivated(pair.first, pair.second, info.pluginData.mEnableByDefault, info.pluginData.mIdentifier);
96 info.isEnabled = isPluginActivated;
97 info.metaDataFileNameBaseName = QFileInfo(data.fileName()).baseName();
98 info.metaDataFileName = data.fileName();
99 info.data = data;
100
101 if (pluginVersion() == data.version()) {
102 info.plugin = nullptr;
103 mPluginList.push_back(info);
104 } else {
105 qCWarning(PIMCOMMON_LOG) << "Plugin " << data.name() << " doesn't have correction plugin version. It will not be loaded.";
106 }
107 }
109 for (QList<GenericPluginInfo>::iterator it = mPluginList.begin(); it != end; ++it) {
110 loadPlugin(&(*it));
111 }
112 return true;
113}
114
115QList<PluginUtilData> GenericPluginManagerPrivate::pluginsDataList() const
116{
117 return mPluginDataList;
118}
119
120QList<GenericPlugin *> GenericPluginManagerPrivate::pluginsList() const
121{
124 for (QList<GenericPluginInfo>::ConstIterator it = mPluginList.constBegin(); it != end; ++it) {
125 if (auto plugin = (*it).plugin) {
126 lst << plugin;
127 }
128 }
129 return lst;
130}
131
132void GenericPluginManagerPrivate::loadPlugin(GenericPluginInfo *item)
133{
134 if (auto plugin = KPluginFactory::instantiatePlugin<PimCommon::GenericPlugin>(item->data, q, QVariantList() << item->metaDataFileNameBaseName).plugin) {
135 item->plugin = plugin;
136 item->plugin->setIsEnabled(item->isEnabled);
137 item->pluginData.mHasConfigureDialog = item->plugin->hasConfigureDialog();
138 mPluginDataList.append(item->pluginData);
139 }
140}
141
142GenericPlugin *GenericPluginManagerPrivate::pluginFromIdentifier(const QString &id)
143{
145 for (QList<GenericPluginInfo>::ConstIterator it = mPluginList.constBegin(); it != end; ++it) {
146 if ((*it).pluginData.mIdentifier == id) {
147 return (*it).plugin;
148 }
149 }
150 return {};
151}
152
153GenericPluginManager::GenericPluginManager(QObject *parent)
154 : QObject(parent)
155 , d(new GenericPluginManagerPrivate(this))
156{
157}
158
159GenericPluginManager::~GenericPluginManager() = default;
160
161bool GenericPluginManager::initializePlugins()
162{
163 return d->initializePlugins();
164}
165
166void GenericPluginManager::setPluginDirectory(const QString &directory)
167{
168 d->pluginDirectory = directory;
169}
170
171QString GenericPluginManager::pluginDirectory() const
172{
173 return d->pluginDirectory;
174}
175
176void GenericPluginManager::setPluginName(const QString &pluginName)
177{
178 d->pluginName = pluginName;
179}
180
181QString GenericPluginManager::pluginName() const
182{
183 return d->pluginName;
184}
185
186QList<GenericPlugin *> GenericPluginManager::pluginsList() const
187{
188 return d->pluginsList();
189}
190
191QList<PluginUtilData> GenericPluginManager::pluginsDataList() const
192{
193 return d->pluginsDataList();
194}
195
196QString GenericPluginManager::configGroupName() const
197{
198 return d->configGroupName();
199}
200
201QString GenericPluginManager::configPrefixSettingKey() const
202{
203 return d->configPrefixSettingKey();
204}
205
206GenericPlugin *GenericPluginManager::pluginFromIdentifier(const QString &id)
207{
208 return d->pluginFromIdentifier(id);
209}
210
211#include "moc_genericpluginmanager.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 GenericPluginManager class.
The GenericPlugin class.
The PluginUtilData class.
Definition pluginutil.h:23
const QList< QKeySequence > & end()
folderdialogacltab.h
QString baseName() const const
iterator begin()
const_iterator constBegin() const const
const_iterator constEnd() const const
iterator end()
bool isEmpty() const const
void push_back(parameter_type value)
QString arg(Args &&... args) const const
bool isEmpty() const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Fri Oct 11 2024 12:08:32 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.