Messagelib

headerstylepluginmanager.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 "headerstylepluginmanager.h"
8#include "headerstyleplugin.h"
9#include "messageviewer_debug.h"
10#include <KPluginFactory>
11#include <KPluginMetaData>
12#include <QFileInfo>
13
14using namespace MessageViewer;
15
16class HeaderStylePluginInfo
17{
18public:
19 HeaderStylePluginInfo() = default;
20
22 QString metaDataFileNameBaseName;
23 QString metaDataFileName;
24 KPluginMetaData data;
25 MessageViewer::HeaderStylePlugin *plugin = nullptr;
26 bool isEnabled = false;
27};
28
29class MessageViewer::HeaderStylePluginManagerPrivate
30{
31public:
32 HeaderStylePluginManagerPrivate(HeaderStylePluginManager *qq)
33 : q(qq)
34 {
35 }
36
37 [[nodiscard]] QList<MessageViewer::HeaderStylePlugin *> pluginsList() const;
38 [[nodiscard]] QList<PimCommon::PluginUtilData> pluginDataList() const;
39 void initializePluginList();
40 void loadPlugin(HeaderStylePluginInfo *item);
41 [[nodiscard]] QString configGroupName() const;
42 [[nodiscard]] QString configPrefixSettingKey() const;
43 MessageViewer::HeaderStylePlugin *pluginFromIdentifier(const QString &id);
44
45private:
49};
50
51namespace
52{
53QString pluginVersion()
54{
55 return QStringLiteral("1.0");
56}
57}
58
59QList<PimCommon::PluginUtilData> HeaderStylePluginManagerPrivate::pluginDataList() const
60{
61 return mPluginDataList;
62}
63
64QString HeaderStylePluginManagerPrivate::configGroupName() const
65{
66 return QStringLiteral("HeaderStylePlugins");
67}
68
69QString HeaderStylePluginManagerPrivate::configPrefixSettingKey() const
70{
71 return QStringLiteral("PluginHeaderStyle");
72}
73
74void HeaderStylePluginManagerPrivate::initializePluginList()
75{
76 const QList<KPluginMetaData> plugins = KPluginMetaData::findPlugins(QStringLiteral("pim6/messageviewer/headerstyle"));
77
79 i.toBack();
80 const QPair<QStringList, QStringList> pair = PimCommon::PluginUtil::loadPluginSetting(configGroupName(), configPrefixSettingKey());
81 QList<int> listOrder;
82 while (i.hasPrevious()) {
83 HeaderStylePluginInfo info;
84
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 const QString version = data.version();
97 if (pluginVersion() == version) {
98 const QVariant p = data.rawData().value(QStringLiteral("X-KDE-MessageViewer-Header-Order")).toVariant();
99 int order = -1;
100 if (p.isValid()) {
101 order = p.toInt();
102 }
103 int pos = 0;
104 for (; pos < listOrder.count(); ++pos) {
105 if (listOrder.at(pos) > order) {
106 pos--;
107 break;
108 }
109 }
110 pos = qMax(0, pos);
111 listOrder.insert(pos, order);
112 info.plugin = nullptr;
113 mPluginList.insert(pos, info);
114 } else {
115 qCWarning(MESSAGEVIEWER_LOG) << "Plugin " << data.name() << " doesn't have correction plugin version. It will not be loaded.";
116 }
117 }
119 for (QList<HeaderStylePluginInfo>::iterator it = mPluginList.begin(); it != end; ++it) {
120 loadPlugin(&(*it));
121 }
122}
123
124QList<MessageViewer::HeaderStylePlugin *> HeaderStylePluginManagerPrivate::pluginsList() const
125{
128 for (QList<HeaderStylePluginInfo>::ConstIterator it = mPluginList.constBegin(); it != end; ++it) {
129 if (auto plugin = (*it).plugin) {
130 lst << plugin;
131 }
132 }
133 return lst;
134}
135
136void HeaderStylePluginManagerPrivate::loadPlugin(HeaderStylePluginInfo *item)
137{
138 if (auto plugin = KPluginFactory::instantiatePlugin<MessageViewer::HeaderStylePlugin>(item->data, q, QVariantList() << item->metaDataFileName).plugin) {
139 item->plugin = plugin;
140 item->plugin->setIsEnabled(item->isEnabled);
141 item->pluginData.mHasConfigureDialog = false;
142 mPluginDataList.append(item->pluginData);
143 }
144}
145
146MessageViewer::HeaderStylePlugin *HeaderStylePluginManagerPrivate::pluginFromIdentifier(const QString &id)
147{
149 for (QList<HeaderStylePluginInfo>::ConstIterator it = mPluginList.constBegin(); it != end; ++it) {
150 if ((*it).pluginData.mIdentifier == id) {
151 return (*it).plugin;
152 }
153 }
154 return {};
155}
156
157HeaderStylePluginManager *HeaderStylePluginManager::self()
158{
159 static HeaderStylePluginManager s_self;
160 return &s_self;
161}
162
163HeaderStylePluginManager::HeaderStylePluginManager(QObject *parent)
164 : QObject(parent)
165 , d(new MessageViewer::HeaderStylePluginManagerPrivate(this))
166{
167 d->initializePluginList();
168}
169
170HeaderStylePluginManager::~HeaderStylePluginManager() = default;
171
172QList<MessageViewer::HeaderStylePlugin *> HeaderStylePluginManager::pluginsList() const
173{
174 return d->pluginsList();
175}
176
177QStringList HeaderStylePluginManager::pluginListName() const
178{
179 QStringList lst;
180 lst.reserve(d->pluginsList().count());
181 const auto pluginsList{d->pluginsList()};
182 for (MessageViewer::HeaderStylePlugin *plugin : pluginsList) {
183 lst << plugin->name();
184 }
185 return lst;
186}
187
188QString HeaderStylePluginManager::configGroupName() const
189{
190 return d->configGroupName();
191}
192
193QString HeaderStylePluginManager::configPrefixSettingKey() const
194{
195 return d->configPrefixSettingKey();
196}
197
198QList<PimCommon::PluginUtilData> HeaderStylePluginManager::pluginsDataList() const
199{
200 return d->pluginDataList();
201}
202
203MessageViewer::HeaderStylePlugin *HeaderStylePluginManager::pluginFromIdentifier(const QString &id)
204{
205 return d->pluginFromIdentifier(id);
206}
207
208#include "moc_headerstylepluginmanager.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 HeaderStylePluginManager class.
The HeaderStylePlugin class.
KDB_EXPORT KDbVersionInfo version()
QAction * end(const QObject *recvr, const char *slot, QObject *parent)
QString baseName() const const
QJsonValue value(QLatin1StringView key) const const
QVariant toVariant() const const
void append(QList< T > &&value)
const_reference at(qsizetype i) const const
iterator begin()
const_iterator constBegin() const const
const_iterator constEnd() const const
qsizetype count() const const
iterator end()
iterator insert(const_iterator before, parameter_type value)
void reserve(qsizetype size)
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 Mon Nov 4 2024 16:33:26 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.