Messagelib

viewerpluginmanager.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 "viewerpluginmanager.h"
8#include "messageviewer_debug.h"
9#include "viewerplugin.h"
10
11#include <KPluginFactory>
12#include <KPluginMetaData>
13#include <KSharedConfig>
14#include <QFileInfo>
15
16using namespace MessageViewer;
17
18class ViewerPluginInfo
19{
20public:
21 ViewerPluginInfo() = default;
22
23 KPluginMetaData data;
24 QString metaDataFileNameBaseName;
25 QString metaDataFileName;
27 MessageViewer::ViewerPlugin *plugin = nullptr;
28 bool isEnabled = false;
29};
30
31class MessageViewer::ViewerPluginManagerPrivate
32{
33public:
34 explicit ViewerPluginManagerPrivate(ViewerPluginManager *qq)
35 : q(qq)
36 {
37 }
38
39 bool initializePluginList();
40 void loadPlugin(ViewerPluginInfo *item);
41 [[nodiscard]] QList<MessageViewer::ViewerPlugin *> pluginsList() const;
42 [[nodiscard]] QList<PimCommon::PluginUtilData> pluginDataList() const;
43
44 QString pluginDirectory;
45 QString pluginName;
46
47 [[nodiscard]] QString configGroupName() const;
48 [[nodiscard]] QString configPrefixSettingKey() const;
49 ViewerPlugin *pluginFromIdentifier(const QString &id);
50
51private:
52 QList<ViewerPluginInfo> mPluginList;
54 ViewerPluginManager *const q;
55};
56
57namespace
58{
59QString pluginVersion()
60{
61 return QStringLiteral("2.0");
62}
63}
64
65QString ViewerPluginManagerPrivate::configGroupName() const
66{
67 return QStringLiteral("PluginMessageViewer%1").arg(pluginName);
68}
69
70QString ViewerPluginManagerPrivate::configPrefixSettingKey() const
71{
72 return QStringLiteral("MessageViewerPlugins");
73}
74
75bool ViewerPluginManagerPrivate::initializePluginList()
76{
77 if (!mPluginList.isEmpty()) {
78 return true;
79 }
80 if (pluginDirectory.isEmpty()) {
81 return false;
82 }
83
85
86 // We need common plugin to avoid to duplicate code between akregator/kmail
87 plugins += KPluginMetaData::findPlugins(QStringLiteral("pim6/messageviewer/viewercommonplugin"));
88
89 const QPair<QStringList, QStringList> pair = PimCommon::PluginUtil::loadPluginSetting(configGroupName(), configPrefixSettingKey());
91 i.toBack();
92 while (i.hasPrevious()) {
93 ViewerPluginInfo info;
94
95 const KPluginMetaData data = i.previous();
96
97 // 1) get plugin data => name/description etc.
98 info.pluginData = PimCommon::PluginUtil::createPluginMetaData(data);
99 // 2) look at if plugin is activated
100 const bool isPluginActivated =
101 PimCommon::PluginUtil::isPluginActivated(pair.first, pair.second, info.pluginData.mEnableByDefault, info.pluginData.mIdentifier);
102 info.isEnabled = isPluginActivated;
103 info.metaDataFileNameBaseName = QFileInfo(data.fileName()).baseName();
104 info.metaDataFileName = data.fileName();
105 info.data = data;
106
107 if (pluginVersion() == data.version()) {
108 info.plugin = nullptr;
109 mPluginList.push_back(info);
110 } else {
111 qCWarning(MESSAGEVIEWER_LOG) << "Plugin name :" << data.name() << " doesn't have correct plugin version. Please update it";
112 }
113 }
115 for (QList<ViewerPluginInfo>::iterator it = mPluginList.begin(); it != end; ++it) {
116 loadPlugin(&(*it));
117 }
118 return true;
119}
120
121void ViewerPluginManagerPrivate::loadPlugin(ViewerPluginInfo *item)
122{
123 if (auto plugin = KPluginFactory::instantiatePlugin<MessageViewer::ViewerPlugin>(item->data, q, QVariantList() << item->metaDataFileName).plugin) {
124 item->plugin = plugin;
125 item->plugin->setIsEnabled(item->isEnabled);
126 item->pluginData.mHasConfigureDialog = item->plugin->hasConfigureDialog();
127 mPluginDataList.append(item->pluginData);
128 }
129}
130
131QList<ViewerPlugin *> ViewerPluginManagerPrivate::pluginsList() const
132{
135 for (QList<ViewerPluginInfo>::ConstIterator it = mPluginList.constBegin(); it != end; ++it) {
136 if (auto plugin = (*it).plugin) {
137 lst << plugin;
138 }
139 }
140 return lst;
141}
142
143QList<PimCommon::PluginUtilData> ViewerPluginManagerPrivate::pluginDataList() const
144{
145 return mPluginDataList;
146}
147
148ViewerPlugin *ViewerPluginManagerPrivate::pluginFromIdentifier(const QString &id)
149{
151 for (QList<ViewerPluginInfo>::ConstIterator it = mPluginList.constBegin(); it != end; ++it) {
152 if ((*it).pluginData.mIdentifier == id) {
153 return (*it).plugin;
154 }
155 }
156 return {};
157}
158
159ViewerPluginManager::ViewerPluginManager(QObject *parent)
160 : QObject(parent)
161 , d(new MessageViewer::ViewerPluginManagerPrivate(this))
162{
163}
164
165MessageViewer::ViewerPluginManager::~ViewerPluginManager() = default;
166
167bool ViewerPluginManager::initializePluginList()
168{
169 return d->initializePluginList();
170}
171
172ViewerPluginManager *ViewerPluginManager::self()
173{
174 static ViewerPluginManager s_self;
175 return &s_self;
176}
177
178QList<MessageViewer::ViewerPlugin *> ViewerPluginManager::pluginsList() const
179{
180 return d->pluginsList();
181}
182
183void ViewerPluginManager::setPluginDirectory(const QString &directory)
184{
185 d->pluginDirectory = directory;
186}
187
188QString ViewerPluginManager::pluginDirectory() const
189{
190 return d->pluginDirectory;
191}
192
193QList<PimCommon::PluginUtilData> ViewerPluginManager::pluginsDataList() const
194{
195 return d->pluginDataList();
196}
197
198QString ViewerPluginManager::configGroupName() const
199{
200 return d->configGroupName();
201}
202
203QString ViewerPluginManager::configPrefixSettingKey() const
204{
205 return d->configPrefixSettingKey();
206}
207
208MessageViewer::ViewerPlugin *ViewerPluginManager::pluginFromIdentifier(const QString &id)
209{
210 return d->pluginFromIdentifier(id);
211}
212
213void ViewerPluginManager::setPluginName(const QString &pluginName)
214{
215 d->pluginName = pluginName;
216}
217
218QString ViewerPluginManager::pluginName() const
219{
220 return d->pluginName;
221}
222
223#include "moc_viewerpluginmanager.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 ViewerPluginManager class.
The ViewerPlugin class.
const QList< QKeySequence > & end()
QString baseName() const const
void append(QList< T > &&value)
iterator begin()
const_iterator constBegin() const const
const_iterator constEnd() const const
iterator end()
bool isEmpty() const const
void push_back(parameter_type value)
QString arg(Args &&... args) const const
bool isEmpty() const const
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.