Messagelib

plugineditormanager.cpp
1/*
2 SPDX-FileCopyrightText: 2015-2024 Laurent Montel <montel@kde.org>
3
4 SPDX-License-Identifier: LGPL-2.0-or-later
5*/
6
7#include "plugineditormanager.h"
8#include "messagecomposer_debug.h"
9#include "plugineditor.h"
10#include <KPluginFactory>
11#include <KPluginMetaData>
12#include <QFileInfo>
13using namespace MessageComposer;
14
15class PluginEditorInfo
16{
17public:
18 PluginEditorInfo() = default;
19
20 KPluginMetaData data;
22 QString metaDataFileNameBaseName;
23 QString metaDataFileName;
24 PluginEditor *plugin = nullptr;
25 int order = 0;
26 bool isEnabled = true;
27};
28
29namespace
30{
31QString pluginEditorVersion()
32{
33 return QStringLiteral("1.0");
34}
35}
36
37class MessageComposer::PluginEditorManagerPrivate
38{
39public:
40 PluginEditorManagerPrivate(PluginEditorManager *qq)
41 : q(qq)
42 {
43 initializePlugins();
44 }
45
46 void loadPlugin(PluginEditorInfo *item);
47 [[nodiscard]] QList<PluginEditor *> pluginsList() const;
48 [[nodiscard]] QList<PimCommon::PluginUtilData> pluginDataList() const;
49 void initializePlugins();
50
51 [[nodiscard]] QString configGroupName() const;
52 [[nodiscard]] QString configPrefixSettingKey() const;
53 PluginEditor *pluginFromIdentifier(const QString &id);
54
55private:
56 QList<PluginEditorInfo> mPluginList;
58 PluginEditorManager *const q;
59};
60
61void PluginEditorManagerPrivate::initializePlugins()
62{
63 const QList<KPluginMetaData> plugins = KPluginMetaData::findPlugins(QStringLiteral("pim6/kmail/plugineditor"));
64
65 const QPair<QStringList, QStringList> pair = PimCommon::PluginUtil::loadPluginSetting(configGroupName(), configPrefixSettingKey());
66
68 i.toBack();
69 while (i.hasPrevious()) {
70 PluginEditorInfo info;
71 const KPluginMetaData data = i.previous();
72
73 // 1) get plugin data => name/description etc.
74 info.pluginData = PimCommon::PluginUtil::createPluginMetaData(data);
75 // 2) look at if plugin is activated
76 const bool isPluginActivated =
77 PimCommon::PluginUtil::isPluginActivated(pair.first, pair.second, info.pluginData.mEnableByDefault, info.pluginData.mIdentifier);
78 info.isEnabled = isPluginActivated;
79 info.metaDataFileNameBaseName = QFileInfo(data.fileName()).baseName();
80 info.metaDataFileName = data.fileName();
81 info.data = data;
82 const QVariant p = data.rawData().value(QStringLiteral("X-KDE-KMailEditor-Order")).toVariant();
83 int order = -1;
84 if (p.isValid()) {
85 order = p.toInt();
86 }
87 info.order = order;
88 if (pluginEditorVersion() == data.version()) {
89 info.plugin = nullptr;
90 mPluginList.push_back(info);
91 } else {
92 qCWarning(MESSAGECOMPOSER_LOG) << "Plugin " << data.name() << " doesn't have correction plugin version. It will not be loaded.";
93 }
94 }
96 for (QList<PluginEditorInfo>::iterator it = mPluginList.begin(); it != end; ++it) {
97 loadPlugin(&(*it));
98 }
99}
100
101void PluginEditorManagerPrivate::loadPlugin(PluginEditorInfo *item)
102{
103 if (auto plugin = KPluginFactory::instantiatePlugin<PluginEditor>(item->data, q, QVariantList() << item->metaDataFileName).plugin) {
104 item->plugin = plugin;
105 item->plugin->setIsEnabled(item->isEnabled);
106 item->pluginData.mHasConfigureDialog = item->plugin->hasConfigureDialog();
107 item->plugin->setOrder(item->order);
108 mPluginDataList.append(item->pluginData);
109 }
110}
111
112QList<PluginEditor *> PluginEditorManagerPrivate::pluginsList() const
113{
116 for (QList<PluginEditorInfo>::ConstIterator it = mPluginList.constBegin(); it != end; ++it) {
117 if (auto plugin = (*it).plugin) {
118 lst << plugin;
119 }
120 }
121 return lst;
122}
123
124QList<PimCommon::PluginUtilData> PluginEditorManagerPrivate::pluginDataList() const
125{
126 return mPluginDataList;
127}
128
129QString PluginEditorManagerPrivate::configGroupName() const
130{
131 return QStringLiteral("KMailPluginEditor");
132}
133
134QString PluginEditorManagerPrivate::configPrefixSettingKey() const
135{
136 return QStringLiteral("KMailEditorPlugin");
137}
138
139PluginEditor *PluginEditorManagerPrivate::pluginFromIdentifier(const QString &id)
140{
142 for (QList<PluginEditorInfo>::ConstIterator it = mPluginList.constBegin(); it != end; ++it) {
143 if ((*it).pluginData.mIdentifier == id) {
144 return (*it).plugin;
145 }
146 }
147 return {};
148}
149
150PluginEditorManager::PluginEditorManager(QObject *parent)
151 : QObject(parent)
152 , d(new MessageComposer::PluginEditorManagerPrivate(this))
153{
154}
155
156PluginEditorManager::~PluginEditorManager() = default;
157
158PluginEditorManager *PluginEditorManager::self()
159{
161 return &s_self;
162}
163
164QList<PluginEditor *> PluginEditorManager::pluginsList() const
165{
166 return d->pluginsList();
167}
168
169QList<PimCommon::PluginUtilData> PluginEditorManager::pluginsDataList() const
170{
171 return d->pluginDataList();
172}
173
174QString PluginEditorManager::configGroupName() const
175{
176 return d->configGroupName();
177}
178
179QString PluginEditorManager::configPrefixSettingKey() const
180{
181 return d->configPrefixSettingKey();
182}
183
184PluginEditor *PluginEditorManager::pluginFromIdentifier(const QString &id)
185{
186 return d->pluginFromIdentifier(id);
187}
188
189#include "moc_plugineditormanager.cpp"
QJsonObject rawData() const
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 PluginEditorManager class.
The PluginEditor class.
QAction * end(const QObject *recvr, const char *slot, QObject *parent)
Simple interface that both EncryptJob and SignEncryptJob implement so the composer can extract some e...
QString baseName() const const
QJsonValue value(QLatin1StringView key) const const
QVariant toVariant() 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)
T qobject_cast(QObject *object)
bool isValid() const const
int toInt(bool *ok) const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Fri Jun 7 2024 11:58:34 by doxygen 1.10.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.