Messagelib

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

KDE's Doxygen guidelines are available online.