Messagelib

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