MailTransport

transportpluginmanager.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 "transportpluginmanager.h"
8#include "mailtransport_debug.h"
9#include <KPluginFactory>
10#include <KPluginMetaData>
11
12#include <MailTransport/TransportAbstractPlugin>
13
14#include <QFileInfo>
15
16using namespace MailTransport;
17
18class MailTransportPluginInfo
19{
20public:
21 QString metaDataFileNameBaseName;
22 QString metaDataFileName;
23 KPluginMetaData data;
25};
26
27namespace
28{
29QString pluginVersion()
30{
31 return QStringLiteral("1.0");
32}
33}
34
35class MailTransport::TransportPluginManagerPrivate
36{
37public:
38 TransportPluginManagerPrivate(TransportPluginManager *qq)
39 : q(qq)
40 {
41 initializePlugins();
42 }
43
44 void loadPlugin(MailTransportPluginInfo *item);
45 [[nodiscard]] QList<MailTransport::TransportAbstractPlugin *> pluginsList() const;
47 void initializePlugins();
48
49private:
50 TransportPluginManager *const q;
51};
52
53void TransportPluginManagerPrivate::initializePlugins()
54{
55 if (!mPluginList.isEmpty()) {
56 return;
57 }
58 const QList<KPluginMetaData> plugins = KPluginMetaData::findPlugins(QStringLiteral("pim6/mailtransport"));
59
61 i.toBack();
62 while (i.hasPrevious()) {
63 MailTransportPluginInfo 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(MAILTRANSPORT_LOG) << "Plugin " << data.name() << " doesn't have correction plugin version. It will not be loaded.";
74 }
75 }
77 for (QList<MailTransportPluginInfo>::iterator it = mPluginList.begin(); it != end; ++it) {
78 loadPlugin(&(*it));
79 }
80}
81
82void TransportPluginManagerPrivate::loadPlugin(MailTransportPluginInfo *item)
83{
84 if (auto plugin =
85 KPluginFactory::instantiatePlugin<MailTransport::TransportAbstractPlugin>(item->data, q, QVariantList() << item->metaDataFileNameBaseName).plugin) {
86 item->plugin = plugin;
87 QObject::connect(plugin, &TransportAbstractPlugin::updatePluginList, q, &TransportPluginManager::updatePluginList);
88 }
89}
90
91QList<MailTransport::TransportAbstractPlugin *> TransportPluginManagerPrivate::pluginsList() const
92{
95 for (QList<MailTransportPluginInfo>::ConstIterator it = mPluginList.constBegin(); it != end; ++it) {
96 if (auto plugin = (*it).plugin) {
97 lst << plugin;
98 }
99 }
100 return lst;
101}
102
103TransportPluginManager::TransportPluginManager(QObject *parent)
104 : QObject(parent)
105 , d(new TransportPluginManagerPrivate(this))
106{
107}
108
109TransportPluginManager::~TransportPluginManager() = default;
110
111TransportPluginManager *TransportPluginManager::self()
112{
113 static TransportPluginManager s_self;
114 return &s_self;
115}
116
117QList<MailTransport::TransportAbstractPlugin *> TransportPluginManager::pluginsList() const
118{
119 return d->pluginsList();
120}
121
122MailTransport::TransportAbstractPlugin *TransportPluginManager::plugin(const QString &identifier)
123{
124 const QList<MailTransport::TransportAbstractPlugin *> lstPlugins = pluginsList();
125 for (MailTransport::TransportAbstractPlugin *p : lstPlugins) {
126 const QList<TransportAbstractPluginInfo> lstPluginInfo = p->names();
127 for (const MailTransport::TransportAbstractPluginInfo &info : lstPluginInfo) {
128 if (info.identifier == identifier) {
129 return p;
130 }
131 }
132 }
133 return {};
134}
135
136#include "moc_transportpluginmanager.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 TransportAbstractPlugin class.
QAction * end(const QObject *recvr, const char *slot, QObject *parent)
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)
QMetaObject::Connection connect(const QObject *sender, PointerToMemberFunction signal, Functor functor)
The TransportAbstractPluginInfo struct.
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Mon Nov 4 2024 16:35:38 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.