File indexing completed on 2025-03-09 04:54:36
0001 /* 0002 SPDX-FileCopyrightText: 2021-2024 Laurent Montel <montel@kde.org> 0003 0004 SPDX-License-Identifier: LGPL-2.0-or-later 0005 */ 0006 0007 #include "messageviewercheckbeforedeletingpluginmanager.h" 0008 #include "messageviewer_debug.h" 0009 #include "messageviewercheckbeforedeletingplugin.h" 0010 #include <KPluginFactory> 0011 #include <KPluginMetaData> 0012 #include <QFileInfo> 0013 0014 using namespace MessageViewer; 0015 0016 class CheckBeforeDeletingPluginInfo 0017 { 0018 public: 0019 CheckBeforeDeletingPluginInfo() = default; 0020 0021 KPluginMetaData data; 0022 PimCommon::PluginUtilData pluginData; 0023 QString metaDataFileNameBaseName; 0024 QString metaDataFileName; 0025 bool isEnabled = true; 0026 MessageViewer::MessageViewerCheckBeforeDeletingPlugin *plugin = nullptr; 0027 }; 0028 0029 class MessageViewer::MessageViewerCheckBeforeDeletingPluginManagerPrivate 0030 { 0031 public: 0032 explicit MessageViewerCheckBeforeDeletingPluginManagerPrivate(MessageViewerCheckBeforeDeletingPluginManager *qq) 0033 : q(qq) 0034 { 0035 } 0036 0037 [[nodiscard]] QList<MessageViewer::MessageViewerCheckBeforeDeletingPlugin *> pluginsList() const; 0038 [[nodiscard]] QList<PimCommon::PluginUtilData> pluginDataList() const; 0039 void initializePluginList(); 0040 void loadPlugin(CheckBeforeDeletingPluginInfo *item); 0041 [[nodiscard]] QString configGroupName() const; 0042 [[nodiscard]] QString configPrefixSettingKey() const; 0043 [[nodiscard]] MessageViewerCheckBeforeDeletingPlugin *pluginFromIdentifier(const QString &id); 0044 0045 private: 0046 QList<PimCommon::PluginUtilData> mPluginDataList; 0047 QList<CheckBeforeDeletingPluginInfo> mPluginList; 0048 MessageViewerCheckBeforeDeletingPluginManager *const q; 0049 }; 0050 0051 namespace 0052 { 0053 QString pluginVersion() 0054 { 0055 return QStringLiteral("1.0"); 0056 } 0057 } 0058 0059 QList<PimCommon::PluginUtilData> MessageViewerCheckBeforeDeletingPluginManagerPrivate::pluginDataList() const 0060 { 0061 return mPluginDataList; 0062 } 0063 0064 QString MessageViewerCheckBeforeDeletingPluginManagerPrivate::configGroupName() const 0065 { 0066 return QStringLiteral("MessageViewerCheckBeforeDeletingPlugins"); 0067 } 0068 0069 QString MessageViewerCheckBeforeDeletingPluginManagerPrivate::configPrefixSettingKey() const 0070 { 0071 return QStringLiteral("MessageViewerCheckBeforeDeletingPlugin"); 0072 } 0073 0074 void MessageViewerCheckBeforeDeletingPluginManagerPrivate::initializePluginList() 0075 { 0076 const QList<KPluginMetaData> plugins = KPluginMetaData::findPlugins(QStringLiteral("pim6/messageviewer/checkbeforedeleting")); 0077 QListIterator<KPluginMetaData> i(plugins); 0078 i.toBack(); 0079 const QPair<QStringList, QStringList> pair = PimCommon::PluginUtil::loadPluginSetting(configGroupName(), configPrefixSettingKey()); 0080 QList<int> listOrder; 0081 while (i.hasPrevious()) { 0082 CheckBeforeDeletingPluginInfo info; 0083 0084 const KPluginMetaData data = i.previous(); 0085 0086 // 1) get plugin data => name/description etc. 0087 info.pluginData = PimCommon::PluginUtil::createPluginMetaData(data); 0088 // 2) look at if plugin is activated 0089 const bool isPluginActivated = 0090 PimCommon::PluginUtil::isPluginActivated(pair.first, pair.second, info.pluginData.mEnableByDefault, info.pluginData.mIdentifier); 0091 info.isEnabled = isPluginActivated; 0092 info.data = data; 0093 0094 info.metaDataFileNameBaseName = QFileInfo(data.fileName()).baseName(); 0095 info.metaDataFileName = data.fileName(); 0096 const QString version = data.version(); 0097 if (pluginVersion() == version) { 0098 const QVariant p = data.rawData().value(QStringLiteral("X-KDE-MessageViewer-Configure-Order")).toVariant(); 0099 int order = -1; 0100 if (p.isValid()) { 0101 order = p.toInt(); 0102 } 0103 int pos = 0; 0104 for (; pos < listOrder.count(); ++pos) { 0105 if (listOrder.at(pos) > order) { 0106 pos--; 0107 break; 0108 } 0109 } 0110 pos = qMax(0, pos); 0111 listOrder.insert(pos, order); 0112 info.plugin = nullptr; 0113 mPluginList.insert(pos, info); 0114 } else { 0115 qCWarning(MESSAGEVIEWER_LOG) << "Plugin " << data.name() << " doesn't have correction plugin version. It will not be loaded."; 0116 } 0117 } 0118 QList<CheckBeforeDeletingPluginInfo>::iterator end(mPluginList.end()); 0119 for (QList<CheckBeforeDeletingPluginInfo>::iterator it = mPluginList.begin(); it != end; ++it) { 0120 loadPlugin(&(*it)); 0121 } 0122 } 0123 0124 QList<MessageViewer::MessageViewerCheckBeforeDeletingPlugin *> MessageViewerCheckBeforeDeletingPluginManagerPrivate::pluginsList() const 0125 { 0126 QList<MessageViewer::MessageViewerCheckBeforeDeletingPlugin *> lst; 0127 QList<CheckBeforeDeletingPluginInfo>::ConstIterator end(mPluginList.constEnd()); 0128 for (QList<CheckBeforeDeletingPluginInfo>::ConstIterator it = mPluginList.constBegin(); it != end; ++it) { 0129 if (auto plugin = (*it).plugin) { 0130 lst << plugin; 0131 } 0132 } 0133 return lst; 0134 } 0135 0136 void MessageViewerCheckBeforeDeletingPluginManagerPrivate::loadPlugin(CheckBeforeDeletingPluginInfo *item) 0137 { 0138 if (auto plugin = 0139 KPluginFactory::instantiatePlugin<MessageViewer::MessageViewerCheckBeforeDeletingPlugin>(item->data, q, QVariantList() << item->metaDataFileName) 0140 .plugin) { 0141 item->plugin = plugin; 0142 // By default it's true 0143 item->pluginData.mHasConfigureDialog = true; 0144 item->plugin->setIsEnabled(item->isEnabled); 0145 mPluginDataList.append(item->pluginData); 0146 } 0147 } 0148 0149 MessageViewerCheckBeforeDeletingPlugin *MessageViewerCheckBeforeDeletingPluginManagerPrivate::pluginFromIdentifier(const QString &id) 0150 { 0151 QList<CheckBeforeDeletingPluginInfo>::ConstIterator end(mPluginList.constEnd()); 0152 for (QList<CheckBeforeDeletingPluginInfo>::ConstIterator it = mPluginList.constBegin(); it != end; ++it) { 0153 if ((*it).pluginData.mIdentifier == id) { 0154 return (*it).plugin; 0155 } 0156 } 0157 return {}; 0158 } 0159 0160 MessageViewerCheckBeforeDeletingPluginManager *MessageViewerCheckBeforeDeletingPluginManager::self() 0161 { 0162 static MessageViewerCheckBeforeDeletingPluginManager s_self; 0163 return &s_self; 0164 } 0165 0166 MessageViewerCheckBeforeDeletingPluginManager::MessageViewerCheckBeforeDeletingPluginManager(QObject *parent) 0167 : QObject(parent) 0168 , d(new MessageViewer::MessageViewerCheckBeforeDeletingPluginManagerPrivate(this)) 0169 { 0170 d->initializePluginList(); 0171 } 0172 0173 MessageViewerCheckBeforeDeletingPluginManager::~MessageViewerCheckBeforeDeletingPluginManager() = default; 0174 0175 QList<MessageViewer::MessageViewerCheckBeforeDeletingPlugin *> MessageViewerCheckBeforeDeletingPluginManager::pluginsList() const 0176 { 0177 return d->pluginsList(); 0178 } 0179 0180 QString MessageViewerCheckBeforeDeletingPluginManager::configGroupName() const 0181 { 0182 return d->configGroupName(); 0183 } 0184 0185 QString MessageViewerCheckBeforeDeletingPluginManager::configPrefixSettingKey() const 0186 { 0187 return d->configPrefixSettingKey(); 0188 } 0189 0190 QList<PimCommon::PluginUtilData> MessageViewerCheckBeforeDeletingPluginManager::pluginsDataList() const 0191 { 0192 return d->pluginDataList(); 0193 } 0194 0195 MessageViewerCheckBeforeDeletingPlugin *MessageViewerCheckBeforeDeletingPluginManager::pluginFromIdentifier(const QString &id) 0196 { 0197 return d->pluginFromIdentifier(id); 0198 } 0199 0200 #include "moc_messageviewercheckbeforedeletingpluginmanager.cpp"