File indexing completed on 2024-12-22 05:01:00

0001 /*
0002   SPDX-FileCopyrightText: 2016-2024 Laurent Montel <montel@kde.org>
0003 
0004   SPDX-License-Identifier: GPL-2.0-only
0005 */
0006 
0007 #include "configurepluginslistwidget.h"
0008 
0009 #include "../../plugininterface/kmailplugininterface.h"
0010 #include "kmail_debug.h"
0011 #include <Akonadi/ServerManager>
0012 #include <KLocalizedString>
0013 #include <KSharedConfig>
0014 #include <MessageComposer/PluginEditorCheckBeforeSendManager>
0015 #include <MessageComposer/PluginEditorConvertText>
0016 #include <MessageComposer/PluginEditorConvertTextManager>
0017 #include <MessageComposer/PluginEditorGrammarManager>
0018 #include <MessageComposer/PluginEditorInitManager>
0019 #include <MessageComposer/PluginEditorManager>
0020 #include <MessageViewer/HeaderStylePluginManager>
0021 #include <MessageViewer/MessageViewerCheckBeforeDeletingPlugin>
0022 #include <MessageViewer/MessageViewerCheckBeforeDeletingPluginManager>
0023 #include <MessageViewer/ViewerPluginManager>
0024 #include <PimCommon/CustomToolsPlugin>
0025 #include <PimCommon/GenericPluginManager>
0026 #include <WebEngineViewer/NetworkUrlInterceptorPluginManager>
0027 
0028 #include <Akonadi/AgentConfigurationDialog>
0029 #include <Akonadi/AgentInstance>
0030 #include <Akonadi/AgentManager>
0031 #include <MessageComposer/PluginEditor>
0032 #include <MessageComposer/PluginEditorCheckBeforeSend>
0033 #include <MessageComposer/PluginEditorInit>
0034 #include <MessageViewer/HeaderStylePlugin>
0035 #include <MessageViewer/MessageViewerConfigureSettingsPlugin>
0036 #include <MessageViewer/MessageViewerConfigureSettingsPluginManager>
0037 #include <MessageViewer/ViewerPlugin>
0038 #include <PimCommon/GenericPlugin>
0039 #include <QDBusInterface>
0040 #include <QDBusReply>
0041 #include <QPointer>
0042 #include <WebEngineViewer/NetworkPluginUrlInterceptor>
0043 
0044 namespace
0045 {
0046 QString pluginEditorCheckBeforeDeletingGroupName()
0047 {
0048     return QStringLiteral("plugincheckbeforedeletinggroupname");
0049 }
0050 
0051 QString pluginEditorGroupName()
0052 {
0053     return QStringLiteral("plugineditorgroupname");
0054 }
0055 
0056 QString pluginEditorGrammarGroupName()
0057 {
0058     return QStringLiteral("plugineditorgrammargroupname");
0059 }
0060 
0061 QString viewerPluginGroupName()
0062 {
0063     return QStringLiteral("viewerplugingroupname");
0064 }
0065 
0066 QString pluginEditorCheckBeforeGroupName()
0067 {
0068     return QStringLiteral("plugineditorcheckbeforegroupname");
0069 }
0070 
0071 QString pluginEditorInitGroupName()
0072 {
0073     return QStringLiteral("plugineditorinitgroupname");
0074 }
0075 
0076 QString pluginEditorConvertTextGroupName()
0077 {
0078     return QStringLiteral("plugineditorconvertTextgroupname");
0079 }
0080 
0081 QString kmailPluginToolsGroupName()
0082 {
0083     return QStringLiteral("kmailplugintoolsgroupname");
0084 }
0085 
0086 QString networkUrlInterceptorGroupName()
0087 {
0088     return QStringLiteral("networkurlinterceptorgroupname");
0089 }
0090 
0091 QString headerStyleGroupName()
0092 {
0093     return QStringLiteral("headerstylegroupname");
0094 }
0095 
0096 QString agentAkonadiGroupName()
0097 {
0098     return QStringLiteral("agentakonadigroupname");
0099 }
0100 
0101 QString configurePluginGroupName()
0102 {
0103     return QStringLiteral("configuregroupname");
0104 }
0105 }
0106 
0107 ConfigurePluginsListWidget::ConfigurePluginsListWidget(QWidget *parent)
0108     : PimCommon::ConfigurePluginsListWidget(parent)
0109 {
0110     connect(this, &ConfigurePluginsListWidget::configureClicked, this, &ConfigurePluginsListWidget::slotConfigureClicked);
0111 }
0112 
0113 ConfigurePluginsListWidget::~ConfigurePluginsListWidget() = default;
0114 
0115 void ConfigurePluginsListWidget::save()
0116 {
0117     PimCommon::ConfigurePluginsListWidget::savePlugins(MessageComposer::PluginEditorManager::self()->configGroupName(),
0118                                                        MessageComposer::PluginEditorManager::self()->configPrefixSettingKey(),
0119                                                        mPluginEditorItems);
0120     PimCommon::ConfigurePluginsListWidget::savePlugins(MessageViewer::ViewerPluginManager::self()->configGroupName(),
0121                                                        MessageViewer::ViewerPluginManager::self()->configPrefixSettingKey(),
0122                                                        mPluginMessageViewerItems);
0123     PimCommon::ConfigurePluginsListWidget::savePlugins(MessageComposer::PluginEditorInitManager::self()->configGroupName(),
0124                                                        MessageComposer::PluginEditorInitManager::self()->configPrefixSettingKey(),
0125                                                        mPluginEditorInitItems);
0126     PimCommon::ConfigurePluginsListWidget::savePlugins(MessageComposer::PluginEditorCheckBeforeSendManager::self()->configGroupName(),
0127                                                        MessageComposer::PluginEditorCheckBeforeSendManager::self()->configPrefixSettingKey(),
0128                                                        mPluginCheckBeforeSendItems);
0129     PimCommon::ConfigurePluginsListWidget::savePlugins(MessageComposer::PluginEditorGrammarManager::self()->configGroupName(),
0130                                                        MessageComposer::PluginEditorGrammarManager::self()->configPrefixSettingKey(),
0131                                                        mPluginEditorGrammarItems);
0132     PimCommon::ConfigurePluginsListWidget::savePlugins(KMailPluginInterface::self()->configGroupName(),
0133                                                        KMailPluginInterface::self()->configPrefixSettingKey(),
0134                                                        mPluginGenericItems);
0135     PimCommon::ConfigurePluginsListWidget::savePlugins(WebEngineViewer::NetworkUrlInterceptorPluginManager::self()->configGroupName(),
0136                                                        WebEngineViewer::NetworkUrlInterceptorPluginManager::self()->configPrefixSettingKey(),
0137                                                        mPluginWebEngineItems);
0138     PimCommon::ConfigurePluginsListWidget::savePlugins(MessageViewer::HeaderStylePluginManager::self()->configGroupName(),
0139                                                        MessageViewer::HeaderStylePluginManager::self()->configPrefixSettingKey(),
0140                                                        mPluginHeaderStyleItems);
0141     PimCommon::ConfigurePluginsListWidget::savePlugins(MessageComposer::PluginEditorConvertTextManager::self()->configGroupName(),
0142                                                        MessageComposer::PluginEditorConvertTextManager::self()->configPrefixSettingKey(),
0143                                                        mPluginConvertTextItems);
0144     PimCommon::ConfigurePluginsListWidget::savePlugins(MessageViewer::MessageViewerCheckBeforeDeletingPluginManager::self()->configGroupName(),
0145                                                        MessageViewer::MessageViewerCheckBeforeDeletingPluginManager::self()->configPrefixSettingKey(),
0146                                                        mPluginCheckBeforeDeletingItems);
0147     saveAkonadiAgent();
0148 }
0149 
0150 void ConfigurePluginsListWidget::saveAkonadiAgent()
0151 {
0152     for (PluginItem *item : std::as_const(mAgentPluginsItems)) {
0153         for (const PimCommon::PluginUtilData &data : std::as_const(mPluginUtilDataList)) {
0154             if (item->mIdentifier == data.mIdentifier) {
0155                 changeAgentActiveState(data.mExtraInfo.at(0), data.mExtraInfo.at(1), item->checkState(0) == Qt::Checked);
0156                 break;
0157             }
0158         }
0159     }
0160 }
0161 
0162 void ConfigurePluginsListWidget::doLoadFromGlobalSettings()
0163 {
0164     initialize();
0165     initializeDone();
0166 }
0167 
0168 void ConfigurePluginsListWidget::doResetToDefaultsOther()
0169 {
0170     changeState(mPluginEditorItems);
0171     changeState(mPluginMessageViewerItems);
0172     changeState(mPluginCheckBeforeSendItems);
0173     changeState(mPluginGenericItems);
0174     changeState(mPluginWebEngineItems);
0175     changeState(mPluginHeaderStyleItems);
0176     changeState(mAgentPluginsItems);
0177     changeState(mPluginEditorInitItems);
0178     changeState(mPluginConvertTextItems);
0179     changeState(mPluginEditorGrammarItems);
0180     changeState(mPluginCheckBeforeDeletingItems);
0181 }
0182 
0183 void ConfigurePluginsListWidget::initialize()
0184 {
0185     mListWidget->clear();
0186 
0187     // Load CheckBeforeSend
0188     PimCommon::ConfigurePluginsListWidget::fillTopItems(MessageComposer::PluginEditorCheckBeforeSendManager::self()->pluginsDataList(),
0189                                                         i18n("Check Before Send Plugins"),
0190                                                         MessageComposer::PluginEditorCheckBeforeSendManager::self()->configGroupName(),
0191                                                         MessageComposer::PluginEditorCheckBeforeSendManager::self()->configPrefixSettingKey(),
0192                                                         mPluginCheckBeforeSendItems,
0193                                                         pluginEditorCheckBeforeGroupName());
0194 
0195     PimCommon::ConfigurePluginsListWidget::fillTopItems(MessageComposer::PluginEditorInitManager::self()->pluginsDataList(),
0196                                                         i18n("Composer Plugins"),
0197                                                         MessageComposer::PluginEditorInitManager::self()->configGroupName(),
0198                                                         MessageComposer::PluginEditorInitManager::self()->configPrefixSettingKey(),
0199                                                         mPluginEditorInitItems,
0200                                                         pluginEditorInitGroupName());
0201     PimCommon::ConfigurePluginsListWidget::fillTopItems(MessageComposer::PluginEditorGrammarManager::self()->pluginsDataList(),
0202                                                         i18n("Grammar Checker Plugins"),
0203                                                         MessageComposer::PluginEditorGrammarManager::self()->configGroupName(),
0204                                                         MessageComposer::PluginEditorGrammarManager::self()->configPrefixSettingKey(),
0205                                                         mPluginEditorGrammarItems,
0206                                                         pluginEditorGrammarGroupName());
0207 
0208     // Load generic plugins
0209     // Necessary to initialize plugin when we load it outside kmail
0210     KMailPluginInterface::self()->initializePlugins();
0211     PimCommon::ConfigurePluginsListWidget::fillTopItems(KMailPluginInterface::self()->pluginsDataList(),
0212                                                         i18n("Tools Plugins"),
0213                                                         KMailPluginInterface::self()->configGroupName(),
0214                                                         KMailPluginInterface::self()->configPrefixSettingKey(),
0215                                                         mPluginGenericItems,
0216                                                         kmailPluginToolsGroupName());
0217     // Load plugin editor
0218     PimCommon::ConfigurePluginsListWidget::fillTopItems(MessageComposer::PluginEditorManager::self()->pluginsDataList(),
0219                                                         i18n("Editor Plugins"),
0220                                                         MessageComposer::PluginEditorManager::self()->configGroupName(),
0221                                                         MessageComposer::PluginEditorManager::self()->configPrefixSettingKey(),
0222                                                         mPluginEditorItems,
0223                                                         pluginEditorGroupName());
0224 
0225     // Load messageviewer plugin
0226     PimCommon::ConfigurePluginsListWidget::fillTopItems(MessageViewer::ViewerPluginManager::self()->pluginsDataList(),
0227                                                         i18n("Message Viewer"),
0228                                                         MessageViewer::ViewerPluginManager::self()->configGroupName(),
0229                                                         MessageViewer::ViewerPluginManager::self()->configPrefixSettingKey(),
0230                                                         mPluginMessageViewerItems,
0231                                                         viewerPluginGroupName());
0232 
0233     // Load webengineplugin
0234     PimCommon::ConfigurePluginsListWidget::fillTopItems(WebEngineViewer::NetworkUrlInterceptorPluginManager::self()->pluginsDataList(),
0235                                                         i18n("Webengine Plugins"),
0236                                                         WebEngineViewer::NetworkUrlInterceptorPluginManager::self()->configGroupName(),
0237                                                         WebEngineViewer::NetworkUrlInterceptorPluginManager::self()->configPrefixSettingKey(),
0238                                                         mPluginWebEngineItems,
0239                                                         networkUrlInterceptorGroupName());
0240 
0241     // Load headerstyle
0242     PimCommon::ConfigurePluginsListWidget::fillTopItems(MessageViewer::HeaderStylePluginManager::self()->pluginsDataList(),
0243                                                         i18n("Header Style Plugins"),
0244                                                         MessageViewer::HeaderStylePluginManager::self()->configGroupName(),
0245                                                         MessageViewer::HeaderStylePluginManager::self()->configPrefixSettingKey(),
0246                                                         mPluginHeaderStyleItems,
0247                                                         headerStyleGroupName());
0248     // Load Converter plugin
0249     PimCommon::ConfigurePluginsListWidget::fillTopItems(MessageComposer::PluginEditorConvertTextManager::self()->pluginsDataList(),
0250                                                         i18n("Text Conversion Plugins"),
0251                                                         MessageComposer::PluginEditorConvertTextManager::self()->configGroupName(),
0252                                                         MessageComposer::PluginEditorConvertTextManager::self()->configPrefixSettingKey(),
0253                                                         mPluginConvertTextItems,
0254                                                         pluginEditorConvertTextGroupName());
0255 
0256     PimCommon::ConfigurePluginsListWidget::fillTopItems(MessageViewer::MessageViewerConfigureSettingsPluginManager::self()->pluginsDataList(),
0257                                                         i18n("Misc"),
0258                                                         MessageViewer::MessageViewerConfigureSettingsPluginManager::self()->configGroupName(),
0259                                                         MessageViewer::MessageViewerConfigureSettingsPluginManager::self()->configPrefixSettingKey(),
0260                                                         mPluginConfigureItems,
0261                                                         configurePluginGroupName(),
0262                                                         false);
0263 
0264     PimCommon::ConfigurePluginsListWidget::fillTopItems(MessageViewer::MessageViewerCheckBeforeDeletingPluginManager::self()->pluginsDataList(),
0265                                                         i18n("Confirm Deleting Emails Plugins"),
0266                                                         MessageViewer::MessageViewerCheckBeforeDeletingPluginManager::self()->configGroupName(),
0267                                                         MessageViewer::MessageViewerCheckBeforeDeletingPluginManager::self()->configPrefixSettingKey(),
0268                                                         mPluginCheckBeforeDeletingItems,
0269                                                         pluginEditorCheckBeforeDeletingGroupName());
0270 
0271     // Load Agent Plugin
0272     initializeAgentPlugins();
0273     mListWidget->expandAll();
0274 }
0275 
0276 void ConfigurePluginsListWidget::initializeAgentPlugins()
0277 {
0278     mPluginUtilDataList.clear();
0279     mPluginUtilDataList.reserve(5);
0280     mPluginUtilDataList << createAgentPluginData(QStringLiteral("akonadi_sendlater_agent"), QStringLiteral("/SendLaterAgent"));
0281     mPluginUtilDataList << createAgentPluginData(QStringLiteral("akonadi_archivemail_agent"), QStringLiteral("/ArchiveMailAgent"));
0282     mPluginUtilDataList << createAgentPluginData(QStringLiteral("akonadi_newmailnotifier_agent"), QStringLiteral("/NewMailNotifierAgent"));
0283     mPluginUtilDataList << createAgentPluginData(QStringLiteral("akonadi_followupreminder_agent"), QStringLiteral("/FollowUpReminder"));
0284     mPluginUtilDataList << createAgentPluginData(QStringLiteral("akonadi_unifiedmailbox_agent"), QStringLiteral("/UnifiedMailboxAgent"));
0285 
0286     PimCommon::ConfigurePluginsListWidget::fillTopItems(mPluginUtilDataList,
0287                                                         i18n("Akonadi Agents"),
0288                                                         QString(),
0289                                                         QString(),
0290                                                         mAgentPluginsItems,
0291                                                         agentAkonadiGroupName());
0292 }
0293 
0294 PimCommon::PluginUtilData ConfigurePluginsListWidget::createAgentPluginData(const QString &agentIdentifier, const QString &path)
0295 {
0296     PimCommon::PluginUtilData data;
0297     data.mEnableByDefault = true;
0298     data.mHasConfigureDialog = true;
0299     const Akonadi::AgentType::List lstAgent = Akonadi::AgentManager::self()->types();
0300     for (const Akonadi::AgentType &type : lstAgent) {
0301         if (type.identifier() == agentIdentifier) {
0302             data.mExtraInfo << agentIdentifier;
0303             data.mExtraInfo << path;
0304             const bool enabled = agentActivateState(agentIdentifier, path);
0305             data.mEnableByDefault = enabled;
0306             data.mName = type.name();
0307             data.mDescription = type.description();
0308             data.mIdentifier = type.identifier();
0309             break;
0310         }
0311     }
0312     return data;
0313 }
0314 
0315 bool ConfigurePluginsListWidget::agentActivateState(const QString &agentIdentifier, const QString &pathName)
0316 {
0317     const QString service = Akonadi::ServerManager::agentServiceName(Akonadi::ServerManager::Agent, agentIdentifier);
0318     QDBusInterface interface(service, pathName);
0319     if (interface.isValid()) {
0320         QDBusReply<bool> enabled = interface.call(QStringLiteral("enabledAgent"));
0321         if (enabled.isValid()) {
0322             return enabled;
0323         } else {
0324             qCDebug(KMAIL_LOG) << agentIdentifier << "doesn't have enabledAgent function";
0325             return false;
0326         }
0327     } else {
0328         qCDebug(KMAIL_LOG) << agentIdentifier << "does not exist when trying to activate the agent state";
0329     }
0330     return false;
0331 }
0332 
0333 void ConfigurePluginsListWidget::changeAgentActiveState(const QString &agentIdentifier, const QString &path, bool enable)
0334 {
0335     if (!agentIdentifier.isEmpty() && !path.isEmpty()) {
0336         const QString service = Akonadi::ServerManager::agentServiceName(Akonadi::ServerManager::Agent, agentIdentifier);
0337         QDBusInterface interface(service, path);
0338         if (interface.isValid()) {
0339             interface.call(QStringLiteral("setEnableAgent"), enable);
0340         } else {
0341             qCDebug(KMAIL_LOG) << agentIdentifier << "does not exist when trying to change the agent active state";
0342         }
0343     }
0344 }
0345 
0346 void ConfigurePluginsListWidget::slotConfigureClicked(const QString &groupName, const QString &identifier)
0347 {
0348     if (!groupName.isEmpty() && !identifier.isEmpty()) {
0349         if (groupName == headerStyleGroupName()) {
0350             MessageViewer::HeaderStylePlugin *plugin = MessageViewer::HeaderStylePluginManager::self()->pluginFromIdentifier(identifier);
0351             plugin->showConfigureDialog(this);
0352         } else if (groupName == networkUrlInterceptorGroupName()) {
0353             WebEngineViewer::NetworkPluginUrlInterceptor *plugin =
0354                 WebEngineViewer::NetworkUrlInterceptorPluginManager::self()->pluginFromIdentifier(identifier);
0355             plugin->showConfigureDialog(this);
0356         } else if (groupName == viewerPluginGroupName()) {
0357             MessageViewer::ViewerPlugin *plugin = MessageViewer::ViewerPluginManager::self()->pluginFromIdentifier(identifier);
0358             plugin->showConfigureDialog(this);
0359         } else if (groupName == pluginEditorGroupName()) {
0360             MessageComposer::PluginEditor *plugin = MessageComposer::PluginEditorManager::self()->pluginFromIdentifier(identifier);
0361             plugin->showConfigureDialog(this);
0362         } else if (groupName == kmailPluginToolsGroupName()) {
0363             PimCommon::GenericPlugin *plugin = KMailPluginInterface::self()->pluginFromIdentifier(identifier);
0364             plugin->showConfigureDialog(this);
0365         } else if (groupName == pluginEditorInitGroupName()) {
0366             MessageComposer::PluginEditorInit *plugin = MessageComposer::PluginEditorInitManager::self()->pluginFromIdentifier(identifier);
0367             plugin->showConfigureDialog(this);
0368         } else if (groupName == pluginEditorCheckBeforeGroupName()) {
0369             MessageComposer::PluginEditorCheckBeforeSend *plugin =
0370                 MessageComposer::PluginEditorCheckBeforeSendManager::self()->pluginFromIdentifier(identifier);
0371             plugin->showConfigureDialog(this);
0372         } else if (groupName == pluginEditorCheckBeforeDeletingGroupName()) {
0373             MessageViewer::MessageViewerCheckBeforeDeletingPlugin *plugin =
0374                 MessageViewer::MessageViewerCheckBeforeDeletingPluginManager::self()->pluginFromIdentifier(identifier);
0375             plugin->showConfigureDialog(this);
0376         } else if (groupName == pluginEditorConvertTextGroupName()) {
0377             MessageComposer::PluginEditorConvertText *plugin = MessageComposer::PluginEditorConvertTextManager::self()->pluginFromIdentifier(identifier);
0378             plugin->showConfigureDialog(this);
0379         } else if (groupName == pluginEditorGrammarGroupName()) {
0380             PimCommon::CustomToolsPlugin *plugin = MessageComposer::PluginEditorGrammarManager::self()->pluginFromIdentifier(identifier);
0381             plugin->showConfigureDialog(this);
0382         } else if (groupName == configurePluginGroupName()) {
0383             MessageViewer::MessageViewerConfigureSettingsPlugin *plugin =
0384                 MessageViewer::MessageViewerConfigureSettingsPluginManager::self()->pluginFromIdentifier(identifier);
0385             plugin->showConfigureDialog(this);
0386         } else if (groupName == agentAkonadiGroupName()) {
0387             for (const PimCommon::PluginUtilData &data : std::as_const(mPluginUtilDataList)) {
0388                 if (data.mIdentifier == identifier) {
0389                     auto instance = Akonadi::AgentManager::self()->instance(identifier);
0390                     if (instance.isValid()) {
0391                         QPointer<Akonadi::AgentConfigurationDialog> dlg = new Akonadi::AgentConfigurationDialog(instance, this);
0392                         dlg->exec();
0393                         delete dlg;
0394                     }
0395                     break;
0396                 }
0397             }
0398         } else {
0399             qCWarning(KMAIL_LOG) << "Unknown configureGroupName" << groupName;
0400         }
0401     }
0402 }
0403 
0404 void ConfigurePluginsListWidget::defaults()
0405 {
0406     doResetToDefaultsOther();
0407 }
0408 
0409 #include "moc_configurepluginslistwidget.cpp"