Messagelib

plugineditorcheckbeforesendmanager.cpp
1/*
2 SPDX-FileCopyrightText: 2016-2024 Laurent Montel <montel@kde.org>
3
4 SPDX-License-Identifier: LGPL-2.0-or-later
5*/
6
7#include "plugineditorcheckbeforesendmanager.h"
8#include "messagecomposer_debug.h"
9#include "plugineditorcheckbeforesend.h"
10#include <KPluginFactory>
11#include <KPluginMetaData>
12#include <QFileInfo>
13
14using namespace MessageComposer;
15
16class PluginEditorCheckBeforeSendInfo
17{
18public:
19 PluginEditorCheckBeforeSendInfo() = default;
20
21 KPluginMetaData data;
23 QString metaDataFileNameBaseName;
24 QString metaDataFileName;
25 PluginEditorCheckBeforeSend *plugin = nullptr;
26 bool isEnabled = true;
27};
28
29namespace
30{
31QString pluginVersion()
32{
33 return QStringLiteral("1.0");
34}
35}
36
37class MessageComposer::PluginEditorCheckBeforeSendManagerPrivate
38{
39public:
40 explicit PluginEditorCheckBeforeSendManagerPrivate(PluginEditorCheckBeforeSendManager *qq)
41 : q(qq)
42 {
43 initializePlugins();
44 }
45
46 void loadPlugin(PluginEditorCheckBeforeSendInfo *item);
47 [[nodiscard]] QList<PluginEditorCheckBeforeSend *> pluginsList() const;
48 void initializePlugins();
50 [[nodiscard]] QString configPrefixSettingKey() const;
51 [[nodiscard]] QString configGroupName() const;
52 [[nodiscard]] QList<PimCommon::PluginUtilData> pluginsDataList() const;
53 PluginEditorCheckBeforeSend *pluginFromIdentifier(const QString &id);
54
55private:
58};
59
60QString PluginEditorCheckBeforeSendManagerPrivate::configGroupName() const
61{
62 return QStringLiteral("KMailPluginCheckBefore");
63}
64
65QString PluginEditorCheckBeforeSendManagerPrivate::configPrefixSettingKey() const
66{
67 return QStringLiteral("PluginCheckBefore");
68}
69
70QList<PimCommon::PluginUtilData> PluginEditorCheckBeforeSendManagerPrivate::pluginsDataList() const
71{
72 return mPluginDataList;
73}
74
75void PluginEditorCheckBeforeSendManagerPrivate::initializePlugins()
76{
77 const QList<KPluginMetaData> plugins = KPluginMetaData::findPlugins(QStringLiteral("pim6/kmail/plugincheckbeforesend"));
78
79 const QPair<QStringList, QStringList> pair = PimCommon::PluginUtil::loadPluginSetting(configGroupName(), configPrefixSettingKey());
80
82 i.toBack();
83 while (i.hasPrevious()) {
84 PluginEditorCheckBeforeSendInfo 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<PluginEditorCheckBeforeSendInfo>::iterator it = mPluginList.begin(); it != end; ++it) {
105 loadPlugin(&(*it));
106 }
107}
108
109void PluginEditorCheckBeforeSendManagerPrivate::loadPlugin(PluginEditorCheckBeforeSendInfo *item)
110{
111 if (auto plugin = KPluginFactory::instantiatePlugin<PluginEditorCheckBeforeSend>(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<PluginEditorCheckBeforeSend *> PluginEditorCheckBeforeSendManagerPrivate::pluginsList() const
120{
123 for (QList<PluginEditorCheckBeforeSendInfo>::ConstIterator it = mPluginList.constBegin(); it != end; ++it) {
124 if (auto plugin = (*it).plugin) {
125 lst << plugin;
126 }
127 }
128 return lst;
129}
130
131PluginEditorCheckBeforeSend *PluginEditorCheckBeforeSendManagerPrivate::pluginFromIdentifier(const QString &id)
132{
134 for (QList<PluginEditorCheckBeforeSendInfo>::ConstIterator it = mPluginList.constBegin(); it != end; ++it) {
135 if ((*it).pluginData.mIdentifier == id) {
136 return (*it).plugin;
137 }
138 }
139 return {};
140}
141
142PluginEditorCheckBeforeSendManager::PluginEditorCheckBeforeSendManager(QObject *parent)
143 : QObject(parent)
144 , d(new MessageComposer::PluginEditorCheckBeforeSendManagerPrivate(this))
145{
146}
147
148PluginEditorCheckBeforeSendManager::~PluginEditorCheckBeforeSendManager() = default;
149
150PluginEditorCheckBeforeSendManager *PluginEditorCheckBeforeSendManager::self()
151{
153 return &s_self;
154}
155
156QList<PluginEditorCheckBeforeSend *> PluginEditorCheckBeforeSendManager::pluginsList() const
157{
158 return d->pluginsList();
159}
160
161QString PluginEditorCheckBeforeSendManager::configGroupName() const
162{
163 return d->configGroupName();
164}
165
166QString PluginEditorCheckBeforeSendManager::configPrefixSettingKey() const
167{
168 return d->configPrefixSettingKey();
169}
170
171QList<PimCommon::PluginUtilData> PluginEditorCheckBeforeSendManager::pluginsDataList() const
172{
173 return d->pluginsDataList();
174}
175
176PluginEditorCheckBeforeSend *PluginEditorCheckBeforeSendManager::pluginFromIdentifier(const QString &id)
177{
178 return d->pluginFromIdentifier(id);
179}
180
181#include "moc_plugineditorcheckbeforesendmanager.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)
T qobject_cast(QObject *object)
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Fri Jun 14 2024 11:49:06 by doxygen 1.10.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.