Messagelib

messageviewercheckbeforedeletingpluginmanager.cpp
1/*
2 SPDX-FileCopyrightText: 2021-2024 Laurent Montel <montel@kde.org>
3
4 SPDX-License-Identifier: LGPL-2.0-or-later
5*/
6
7#include "messageviewercheckbeforedeletingpluginmanager.h"
8#include "messageviewer_debug.h"
9#include "messageviewercheckbeforedeletingplugin.h"
10#include <KPluginFactory>
11#include <KPluginMetaData>
12#include <QFileInfo>
13
14using namespace MessageViewer;
15
16class CheckBeforeDeletingPluginInfo
17{
18public:
19 CheckBeforeDeletingPluginInfo() = default;
20
21 KPluginMetaData data;
23 QString metaDataFileNameBaseName;
24 QString metaDataFileName;
25 bool isEnabled = true;
27};
28
29class MessageViewer::MessageViewerCheckBeforeDeletingPluginManagerPrivate
30{
31public:
32 explicit MessageViewerCheckBeforeDeletingPluginManagerPrivate(MessageViewerCheckBeforeDeletingPluginManager *qq)
33 : q(qq)
34 {
35 }
36
38 [[nodiscard]] QList<PimCommon::PluginUtilData> pluginDataList() const;
39 void initializePluginList();
40 void loadPlugin(CheckBeforeDeletingPluginInfo *item);
41 [[nodiscard]] QString configGroupName() const;
42 [[nodiscard]] QString configPrefixSettingKey() const;
43 [[nodiscard]] MessageViewerCheckBeforeDeletingPlugin *pluginFromIdentifier(const QString &id);
44
45private:
49};
50
51namespace
52{
53QString pluginVersion()
54{
55 return QStringLiteral("1.0");
56}
57}
58
59QList<PimCommon::PluginUtilData> MessageViewerCheckBeforeDeletingPluginManagerPrivate::pluginDataList() const
60{
61 return mPluginDataList;
62}
63
64QString MessageViewerCheckBeforeDeletingPluginManagerPrivate::configGroupName() const
65{
66 return QStringLiteral("MessageViewerCheckBeforeDeletingPlugins");
67}
68
69QString MessageViewerCheckBeforeDeletingPluginManagerPrivate::configPrefixSettingKey() const
70{
71 return QStringLiteral("MessageViewerCheckBeforeDeletingPlugin");
72}
73
74void MessageViewerCheckBeforeDeletingPluginManagerPrivate::initializePluginList()
75{
76 const QList<KPluginMetaData> plugins = KPluginMetaData::findPlugins(QStringLiteral("pim6/messageviewer/checkbeforedeleting"));
78 i.toBack();
79 const QPair<QStringList, QStringList> pair = PimCommon::PluginUtil::loadPluginSetting(configGroupName(), configPrefixSettingKey());
80 QList<int> listOrder;
81 while (i.hasPrevious()) {
82 CheckBeforeDeletingPluginInfo 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 const bool isPluginActivated =
90 PimCommon::PluginUtil::isPluginActivated(pair.first, pair.second, info.pluginData.mEnableByDefault, info.pluginData.mIdentifier);
91 info.isEnabled = isPluginActivated;
92 info.data = data;
93
94 info.metaDataFileNameBaseName = QFileInfo(data.fileName()).baseName();
95 info.metaDataFileName = data.fileName();
96 const QString version = data.version();
97 if (pluginVersion() == version) {
98 const QVariant p = data.rawData().value(QStringLiteral("X-KDE-MessageViewer-Configure-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<CheckBeforeDeletingPluginInfo>::iterator it = mPluginList.begin(); it != end; ++it) {
120 loadPlugin(&(*it));
121 }
122}
123
124QList<MessageViewer::MessageViewerCheckBeforeDeletingPlugin *> MessageViewerCheckBeforeDeletingPluginManagerPrivate::pluginsList() const
125{
128 for (QList<CheckBeforeDeletingPluginInfo>::ConstIterator it = mPluginList.constBegin(); it != end; ++it) {
129 if (auto plugin = (*it).plugin) {
130 lst << plugin;
131 }
132 }
133 return lst;
134}
135
136void MessageViewerCheckBeforeDeletingPluginManagerPrivate::loadPlugin(CheckBeforeDeletingPluginInfo *item)
137{
138 if (auto plugin =
139 KPluginFactory::instantiatePlugin<MessageViewer::MessageViewerCheckBeforeDeletingPlugin>(item->data, q, QVariantList() << item->metaDataFileName)
140 .plugin) {
141 item->plugin = plugin;
142 // By default it's true
143 item->pluginData.mHasConfigureDialog = true;
144 item->plugin->setIsEnabled(item->isEnabled);
145 mPluginDataList.append(item->pluginData);
146 }
147}
148
149MessageViewerCheckBeforeDeletingPlugin *MessageViewerCheckBeforeDeletingPluginManagerPrivate::pluginFromIdentifier(const QString &id)
150{
152 for (QList<CheckBeforeDeletingPluginInfo>::ConstIterator it = mPluginList.constBegin(); it != end; ++it) {
153 if ((*it).pluginData.mIdentifier == id) {
154 return (*it).plugin;
155 }
156 }
157 return {};
158}
159
160MessageViewerCheckBeforeDeletingPluginManager *MessageViewerCheckBeforeDeletingPluginManager::self()
161{
163 return &s_self;
164}
165
166MessageViewerCheckBeforeDeletingPluginManager::MessageViewerCheckBeforeDeletingPluginManager(QObject *parent)
167 : QObject(parent)
168 , d(new MessageViewer::MessageViewerCheckBeforeDeletingPluginManagerPrivate(this))
169{
170 d->initializePluginList();
171}
172
173MessageViewerCheckBeforeDeletingPluginManager::~MessageViewerCheckBeforeDeletingPluginManager() = default;
174
175QList<MessageViewer::MessageViewerCheckBeforeDeletingPlugin *> MessageViewerCheckBeforeDeletingPluginManager::pluginsList() const
176{
177 return d->pluginsList();
178}
179
180QString MessageViewerCheckBeforeDeletingPluginManager::configGroupName() const
181{
182 return d->configGroupName();
183}
184
185QString MessageViewerCheckBeforeDeletingPluginManager::configPrefixSettingKey() const
186{
187 return d->configPrefixSettingKey();
188}
189
190QList<PimCommon::PluginUtilData> MessageViewerCheckBeforeDeletingPluginManager::pluginsDataList() const
191{
192 return d->pluginDataList();
193}
194
195MessageViewerCheckBeforeDeletingPlugin *MessageViewerCheckBeforeDeletingPluginManager::pluginFromIdentifier(const QString &id)
196{
197 return d->pluginFromIdentifier(id);
198}
199
200#include "moc_messageviewercheckbeforedeletingpluginmanager.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
const QList< QKeySequence > & end()
NETWORKMANAGERQT_EXPORT QString version()
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)
T qobject_cast(QObject *object)
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 Fri Jun 14 2024 11:49:07 by doxygen 1.10.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.