Messagelib

plugineditorinitmanager.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 "plugineditorinitmanager.h"
8#include "messagecomposer_debug.h"
9#include "plugineditorinit.h"
10#include <KPluginFactory>
11#include <KPluginMetaData>
12#include <QFileInfo>
13
14using namespace MessageComposer;
15
16class PluginEditorInitInfo
17{
18public:
19 PluginEditorInitInfo() = default;
20
22 QString metaDataFileNameBaseName;
23 QString metaDataFileName;
24 KPluginMetaData data;
25 PluginEditorInit *plugin = nullptr;
26 bool isEnabled = true;
27};
28
29namespace
30{
31QString pluginEditorInitVersion()
32{
33 return QStringLiteral("1.0");
34}
35}
36
37class MessageComposer::PluginEditorInitManagerPrivate
38{
39public:
40 PluginEditorInitManagerPrivate(PluginEditorInitManager *qq)
41 : q(qq)
42 {
43 initializePlugins();
44 }
45
46 void loadPlugin(PluginEditorInitInfo *item);
47 [[nodiscard]] QList<PluginEditorInit *> pluginsList() const;
48 void initializePlugins();
49 [[nodiscard]] QString configPrefixSettingKey() const;
50 [[nodiscard]] QString configGroupName() const;
51 [[nodiscard]] QList<PimCommon::PluginUtilData> pluginsDataList() const;
52 [[nodiscard]] PluginEditorInit *pluginFromIdentifier(const QString &id);
53
55
56private:
59};
60
61QString PluginEditorInitManagerPrivate::configGroupName() const
62{
63 return QStringLiteral("KMailPluginEditorInit");
64}
65
66QString PluginEditorInitManagerPrivate::configPrefixSettingKey() const
67{
68 return QStringLiteral("PluginEditorInit");
69}
70
71QList<PimCommon::PluginUtilData> PluginEditorInitManagerPrivate::pluginsDataList() const
72{
73 return mPluginDataList;
74}
75
76void PluginEditorInitManagerPrivate::initializePlugins()
77{
78 const QList<KPluginMetaData> plugins = KPluginMetaData::findPlugins(QStringLiteral("pim6/kmail/plugineditorinit"));
79
80 const QPair<QStringList, QStringList> pair = PimCommon::PluginUtil::loadPluginSetting(configGroupName(), configPrefixSettingKey());
81
83 i.toBack();
84 while (i.hasPrevious()) {
85 PluginEditorInitInfo info;
86 const KPluginMetaData data = i.previous();
87
88 // 1) get plugin data => name/description etc.
89 info.pluginData = PimCommon::PluginUtil::createPluginMetaData(data);
90 // 2) look at if plugin is activated
91 const bool isPluginActivated =
92 PimCommon::PluginUtil::isPluginActivated(pair.first, pair.second, info.pluginData.mEnableByDefault, info.pluginData.mIdentifier);
93 info.isEnabled = isPluginActivated;
94 info.metaDataFileNameBaseName = QFileInfo(data.fileName()).baseName();
95 info.metaDataFileName = data.fileName();
96 info.data = data;
97 if (pluginEditorInitVersion() == data.version()) {
98 info.plugin = nullptr;
99 mPluginList.push_back(info);
100 } else {
101 qCWarning(MESSAGECOMPOSER_LOG) << "Plugin " << data.name() << " doesn't have correction plugin version. It will not be loaded.";
102 }
103 }
105 for (QList<PluginEditorInitInfo>::iterator it = mPluginList.begin(); it != end; ++it) {
106 loadPlugin(&(*it));
107 }
108}
109
110void PluginEditorInitManagerPrivate::loadPlugin(PluginEditorInitInfo *item)
111{
112 if (auto plugin = KPluginFactory::instantiatePlugin<PluginEditorInit>(item->data, q, QVariantList() << item->metaDataFileName).plugin) {
113 item->plugin = plugin;
114 item->plugin->setIsEnabled(item->isEnabled);
115 item->pluginData.mHasConfigureDialog = item->plugin->hasConfigureDialog();
116 mPluginDataList.append(item->pluginData);
117 }
118}
119
120QList<PluginEditorInit *> PluginEditorInitManagerPrivate::pluginsList() const
121{
124 for (QList<PluginEditorInitInfo>::ConstIterator it = mPluginList.constBegin(); it != end; ++it) {
125 if (auto plugin = (*it).plugin) {
126 lst << plugin;
127 }
128 }
129 return lst;
130}
131
132PluginEditorInit *PluginEditorInitManagerPrivate::pluginFromIdentifier(const QString &id)
133{
135 for (QList<PluginEditorInitInfo>::ConstIterator it = mPluginList.constBegin(); it != end; ++it) {
136 if ((*it).pluginData.mIdentifier == id) {
137 return (*it).plugin;
138 }
139 }
140 return {};
141}
142
143PluginEditorInitManager::PluginEditorInitManager(QObject *parent)
144 : QObject(parent)
145 , d(new MessageComposer::PluginEditorInitManagerPrivate(this))
146{
147}
148
149PluginEditorInitManager::~PluginEditorInitManager() = default;
150
151PluginEditorInitManager *PluginEditorInitManager::self()
152{
153 static PluginEditorInitManager s_self;
154 return &s_self;
155}
156
157QList<PluginEditorInit *> PluginEditorInitManager::pluginsList() const
158{
159 return d->pluginsList();
160}
161
162QString PluginEditorInitManager::configGroupName() const
163{
164 return d->configGroupName();
165}
166
167QString PluginEditorInitManager::configPrefixSettingKey() const
168{
169 return d->configPrefixSettingKey();
170}
171
172QList<PimCommon::PluginUtilData> PluginEditorInitManager::pluginsDataList() const
173{
174 return d->pluginsDataList();
175}
176
177PluginEditorInit *PluginEditorInitManager::pluginFromIdentifier(const QString &id)
178{
179 return d->pluginFromIdentifier(id);
180}
181
182#include "moc_plugineditorinitmanager.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 PluginEditorInit class.
const QList< QKeySequence > & end()
Simple interface that both EncryptJob and SignEncryptJob implement so the composer can extract some e...
QString baseName() const const
void append(QList< T > &&value)
iterator begin()
const_iterator constBegin() const const
const_iterator constEnd() const const
iterator end()
void push_back(parameter_type value)
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Mon Nov 18 2024 12:07:25 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.