Messagelib

messageviewerconfiguresettingspluginmanager.cpp
1/*
2 SPDX-FileCopyrightText: 2018-2024 Laurent Montel <montel@kde.org>
3
4 SPDX-License-Identifier: LGPL-2.0-or-later
5*/
6
7#include "messageviewerconfiguresettingspluginmanager.h"
8#include "messageviewer_debug.h"
9#include "messageviewerconfiguresettingsplugin.h"
10#include <KPluginFactory>
11#include <KPluginMetaData>
12#include <QFileInfo>
13
14using namespace MessageViewer;
15
16class ConfigureSettingsPluginInfo
17{
18public:
19 ConfigureSettingsPluginInfo() = default;
20
21 KPluginMetaData data;
23 QString metaDataFileNameBaseName;
24 QString metaDataFileName;
26};
27
28class MessageViewer::MessageViewerConfigureSettingsPluginManagerPrivate
29{
30public:
31 explicit MessageViewerConfigureSettingsPluginManagerPrivate(MessageViewerConfigureSettingsPluginManager *qq)
32 : q(qq)
33 {
34 }
35
37 [[nodiscard]] QList<PimCommon::PluginUtilData> pluginDataList() const;
38 void initializePluginList();
39 void loadPlugin(ConfigureSettingsPluginInfo *item);
40 [[nodiscard]] QString configGroupName() const;
41 [[nodiscard]] QString configPrefixSettingKey() const;
42 MessageViewerConfigureSettingsPlugin *pluginFromIdentifier(const QString &id);
43
44private:
48};
49
50namespace
51{
52QString configurePluginVersion()
53{
54 return QStringLiteral("1.0");
55}
56}
57
58QList<PimCommon::PluginUtilData> MessageViewerConfigureSettingsPluginManagerPrivate::pluginDataList() const
59{
60 return mPluginDataList;
61}
62
63QString MessageViewerConfigureSettingsPluginManagerPrivate::configGroupName() const
64{
65 return QStringLiteral("MessageViewerConfigureSettingsPlugins");
66}
67
68QString MessageViewerConfigureSettingsPluginManagerPrivate::configPrefixSettingKey() const
69{
70 return QStringLiteral("MessageViewerConfigureSettingsPlugin");
71}
72
73void MessageViewerConfigureSettingsPluginManagerPrivate::initializePluginList()
74{
75 const QList<KPluginMetaData> plugins = KPluginMetaData::findPlugins(QStringLiteral("pim6/messageviewer/configuresettings"));
76
78 i.toBack();
79 const QPair<QStringList, QStringList> pair = PimCommon::PluginUtil::loadPluginSetting(configGroupName(), configPrefixSettingKey());
80 QList<int> listOrder;
81 while (i.hasPrevious()) {
82 ConfigureSettingsPluginInfo info;
83
84 const KPluginMetaData data = i.previous();
85
86 // 1) get plugin data => name/description etc.
87 info.pluginData = PimCommon::PluginUtil::createPluginMetaData(data);
88 // 2) look at if plugin is activated
89 info.metaDataFileNameBaseName = QFileInfo(data.fileName()).baseName();
90 info.metaDataFileName = data.fileName();
91 info.data = data;
92 const QString version = data.version();
93 if (configurePluginVersion() == version) {
94 const QVariant p = data.rawData().value(QStringLiteral("X-KDE-MessageViewer-Configure-Order")).toVariant();
95 int order = -1;
96 if (p.isValid()) {
97 order = p.toInt();
98 }
99 int pos = 0;
100 for (; pos < listOrder.count(); ++pos) {
101 if (listOrder.at(pos) > order) {
102 pos--;
103 break;
104 }
105 }
106 pos = qMax(0, pos);
107 listOrder.insert(pos, order);
108 info.plugin = nullptr;
109 mPluginList.insert(pos, info);
110 } else {
111 qCWarning(MESSAGEVIEWER_LOG) << "Plugin " << data.name() << " doesn't have correction plugin version. It will not be loaded.";
112 }
113 }
115 for (QList<ConfigureSettingsPluginInfo>::iterator it = mPluginList.begin(); it != end; ++it) {
116 loadPlugin(&(*it));
117 }
118}
119
120QList<MessageViewer::MessageViewerConfigureSettingsPlugin *> MessageViewerConfigureSettingsPluginManagerPrivate::pluginsList() const
121{
124 for (QList<ConfigureSettingsPluginInfo>::ConstIterator it = mPluginList.constBegin(); it != end; ++it) {
125 if (auto plugin = (*it).plugin) {
126 lst << plugin;
127 }
128 }
129 return lst;
130}
131
132void MessageViewerConfigureSettingsPluginManagerPrivate::loadPlugin(ConfigureSettingsPluginInfo *item)
133{
134 if (auto plugin =
135 KPluginFactory::instantiatePlugin<MessageViewer::MessageViewerConfigureSettingsPlugin>(item->data, q, QVariantList() << item->metaDataFileName)
136 .plugin) {
137 item->plugin = plugin;
138 // By default it's true
139 item->pluginData.mHasConfigureDialog = true;
140 mPluginDataList.append(item->pluginData);
141 }
142}
143
144MessageViewerConfigureSettingsPlugin *MessageViewerConfigureSettingsPluginManagerPrivate::pluginFromIdentifier(const QString &id)
145{
147 for (QList<ConfigureSettingsPluginInfo>::ConstIterator it = mPluginList.constBegin(); it != end; ++it) {
148 if ((*it).pluginData.mIdentifier == id) {
149 return (*it).plugin;
150 }
151 }
152 return {};
153}
154
155MessageViewerConfigureSettingsPluginManager *MessageViewerConfigureSettingsPluginManager::self()
156{
158 return &s_self;
159}
160
161MessageViewerConfigureSettingsPluginManager::MessageViewerConfigureSettingsPluginManager(QObject *parent)
162 : QObject(parent)
163 , d(new MessageViewer::MessageViewerConfigureSettingsPluginManagerPrivate(this))
164{
165 d->initializePluginList();
166}
167
168MessageViewerConfigureSettingsPluginManager::~MessageViewerConfigureSettingsPluginManager() = default;
169
170QList<MessageViewer::MessageViewerConfigureSettingsPlugin *> MessageViewerConfigureSettingsPluginManager::pluginsList() const
171{
172 return d->pluginsList();
173}
174
175QString MessageViewerConfigureSettingsPluginManager::configGroupName() const
176{
177 return d->configGroupName();
178}
179
180QString MessageViewerConfigureSettingsPluginManager::configPrefixSettingKey() const
181{
182 return d->configPrefixSettingKey();
183}
184
185QList<PimCommon::PluginUtilData> MessageViewerConfigureSettingsPluginManager::pluginsDataList() const
186{
187 return d->pluginDataList();
188}
189
190MessageViewerConfigureSettingsPlugin *MessageViewerConfigureSettingsPluginManager::pluginFromIdentifier(const QString &id)
191{
192 return d->pluginFromIdentifier(id);
193}
194
195#include "moc_messageviewerconfiguresettingspluginmanager.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
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)
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.