Pimcommon

addressesslineeditpluginmanager.cpp
1/*
2 SPDX-FileCopyrightText: 2017-2024 Laurent Montel <montel@kde.org>
3
4 SPDX-License-Identifier: LGPL-2.0-or-later
5*/
6
7#include "addressesslineeditpluginmanager.h"
8#include "addressesslineeditabstractplugin.h"
9#include "pimcommonakonadi_debug.h"
10#include <KPluginFactory>
11#include <KPluginMetaData>
12
13#include <QFileInfo>
14
15using namespace PimCommon;
16
17class AddressessLineEditPluginInfo
18{
19public:
20 AddressessLineEditPluginInfo() = default;
21
22 KPluginMetaData data;
23 QString metaDataFileNameBaseName;
24 QString metaDataFileName;
26};
27
28namespace
29{
30QString pluginVersion()
31{
32 return QStringLiteral("1.0");
33}
34}
35
36class PimCommon::AddressessLineEditPluginManagerPrivate
37{
38public:
39 explicit AddressessLineEditPluginManagerPrivate(AddressessLineEditPluginManager *qq)
40 : q(qq)
41 {
42 initializePlugins();
43 }
44
45 void loadPlugin(AddressessLineEditPluginInfo *item);
46 [[nodiscard]] QList<PimCommon::AddressessLineEditAbstractPlugin *> pluginsList() const;
48 bool initializePlugins();
49
50private:
52};
53
54bool AddressessLineEditPluginManagerPrivate::initializePlugins()
55{
56 if (!mPluginList.isEmpty()) {
57 return true;
58 }
59 const QList<KPluginMetaData> plugins = KPluginMetaData::findPlugins(QStringLiteral("addressline"));
61 i.toBack();
62 while (i.hasPrevious()) {
63 AddressessLineEditPluginInfo info;
64 const KPluginMetaData data = i.previous();
65
66 info.metaDataFileNameBaseName = QFileInfo(data.fileName()).baseName();
67 info.metaDataFileName = data.fileName();
68 info.data = data;
69 if (pluginVersion() == data.version()) {
70 info.plugin = nullptr;
71 mPluginList.push_back(info);
72 } else {
73 qCWarning(PIMCOMMONAKONADI_LOG) << "Plugin " << data.name() << " doesn't have correction plugin version. It will not be loaded.";
74 }
75 }
77 for (QList<AddressessLineEditPluginInfo>::iterator it = mPluginList.begin(); it != end; ++it) {
78 loadPlugin(&(*it));
79 }
80 return true;
81}
82
83void AddressessLineEditPluginManagerPrivate::loadPlugin(AddressessLineEditPluginInfo *item)
84{
85 if (auto plugin =
86 KPluginFactory::instantiatePlugin<PimCommon::AddressessLineEditAbstractPlugin>(item->data, q, QVariantList() << item->metaDataFileNameBaseName)
87 .plugin) {
88 item->plugin = plugin;
89 }
90}
91
92QList<PimCommon::AddressessLineEditAbstractPlugin *> AddressessLineEditPluginManagerPrivate::pluginsList() const
93{
96 for (QList<AddressessLineEditPluginInfo>::ConstIterator it = mPluginList.constBegin(); it != end; ++it) {
97 if (auto plugin = (*it).plugin) {
98 lst << plugin;
99 }
100 }
101 return lst;
102}
103
104AddressessLineEditPluginManager::AddressessLineEditPluginManager(QObject *parent)
105 : QObject(parent)
106 , d(new AddressessLineEditPluginManagerPrivate(this))
107{
108}
109
110AddressessLineEditPluginManager::~AddressessLineEditPluginManager() = default;
111
112AddressessLineEditPluginManager *AddressessLineEditPluginManager::self()
113{
115 return &s_self;
116}
117
118QList<PimCommon::AddressessLineEditAbstractPlugin *> AddressessLineEditPluginManager::pluginsList() const
119{
120 return d->pluginsList();
121}
122
123PimCommon::AddressessLineEditAbstractPlugin *AddressessLineEditPluginManager::plugin(const QString &identifier)
124{
125 const QList<PimCommon::AddressessLineEditAbstractPlugin *> lstPlugins = pluginsList();
126 for (PimCommon::AddressessLineEditAbstractPlugin *p : lstPlugins) {
127 const QList<AddressessLineEditAbstractPluginInfo> lstPluginsInfo = p->names();
128 for (const AddressessLineEditAbstractPluginInfo &info : lstPluginsInfo) {
129 if (info.identifier == identifier) {
130 return p;
131 }
132 }
133 }
134 return {};
135}
136
137#include "moc_addressesslineeditpluginmanager.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 AddressessLineEditAbstractPlugin class.
The AddressessLineEditPluginManager class.
QAction * end(const QObject *recvr, const char *slot, QObject *parent)
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)
The AddressessLineEditAbstractPluginInfo struct.
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Fri Nov 22 2024 12:12:05 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.