File indexing completed on 2024-10-27 04:50:59

0001 /*
0002    SPDX-FileCopyrightText: 2015-2024 Laurent Montel <montel@kde.org>
0003 
0004    SPDX-License-Identifier: GPL-2.0-or-later
0005 */
0006 
0007 #include "kmailplugineditormanagerinterface.h"
0008 #include "kmail_debug.h"
0009 #include <MessageComposer/PluginComposerInterface>
0010 #include <MessageComposer/PluginEditor>
0011 #include <MessageComposer/PluginEditorManager>
0012 
0013 #include <QAction>
0014 #include <QList>
0015 #include <QWidget>
0016 
0017 KMailPluginEditorManagerInterface::KMailPluginEditorManagerInterface(QObject *parent)
0018     : QObject(parent)
0019 {
0020 }
0021 
0022 KMailPluginEditorManagerInterface::~KMailPluginEditorManagerInterface() = default;
0023 
0024 TextCustomEditor::RichTextEditor *KMailPluginEditorManagerInterface::richTextEditor() const
0025 {
0026     return mRichTextEditor;
0027 }
0028 
0029 void KMailPluginEditorManagerInterface::setRichTextEditor(TextCustomEditor::RichTextEditor *richTextEditor)
0030 {
0031     mRichTextEditor = richTextEditor;
0032 }
0033 
0034 QWidget *KMailPluginEditorManagerInterface::parentWidget() const
0035 {
0036     return mParentWidget;
0037 }
0038 
0039 void KMailPluginEditorManagerInterface::setParentWidget(QWidget *parentWidget)
0040 {
0041     mParentWidget = parentWidget;
0042 }
0043 
0044 void KMailPluginEditorManagerInterface::initializePlugins()
0045 {
0046     if (!mListPluginInterface.isEmpty()) {
0047         qCWarning(KMAIL_LOG) << "Plugin was already initialized. This is a bug";
0048         return;
0049     }
0050     if (!mRichTextEditor) {
0051         qCWarning(KMAIL_LOG) << "KMailPluginEditorManagerInterface: Missing richtexteditor";
0052         return;
0053     }
0054     if (!mParentWidget) {
0055         qCWarning(KMAIL_LOG) << "KMailPluginEditorManagerInterface : Parent is null. This is a bug";
0056     }
0057 
0058     const QList<MessageComposer::PluginEditor *> lstPlugin = MessageComposer::PluginEditorManager::self()->pluginsList();
0059     for (MessageComposer::PluginEditor *plugin : lstPlugin) {
0060         if (plugin->isEnabled()) {
0061             auto interface = static_cast<MessageComposer::PluginEditorInterface *>(plugin->createInterface(this));
0062             auto composerInterface = new MessageComposer::PluginComposerInterface;
0063             composerInterface->setComposerViewBase(mComposerInterface);
0064             interface->setComposerInterface(composerInterface);
0065             interface->setRichTextEditor(mRichTextEditor);
0066             interface->setParentWidget(mParentWidget);
0067             interface->createAction(mActionCollection);
0068             interface->setPlugin(plugin);
0069             connect(interface, &MessageComposer::PluginEditorInterface::emitPluginActivated, this, &KMailPluginEditorManagerInterface::slotPluginActivated);
0070             connect(interface, &MessageComposer::PluginEditorInterface::message, this, &KMailPluginEditorManagerInterface::message);
0071             connect(interface, &MessageComposer::PluginEditorInterface::errorMessage, this, &KMailPluginEditorManagerInterface::errorMessage);
0072             connect(interface, &MessageComposer::PluginEditorInterface::successMessage, this, &KMailPluginEditorManagerInterface::successMessage);
0073             mListPluginInterface.append(interface);
0074         }
0075     }
0076 }
0077 
0078 void KMailPluginEditorManagerInterface::slotPluginActivated(MessageComposer::PluginEditorInterface *interface)
0079 {
0080     interface->exec();
0081 }
0082 
0083 MessageComposer::ComposerViewBase *KMailPluginEditorManagerInterface::composerInterface() const
0084 {
0085     return mComposerInterface;
0086 }
0087 
0088 void KMailPluginEditorManagerInterface::setComposerInterface(MessageComposer::ComposerViewBase *composerInterface)
0089 {
0090     mComposerInterface = composerInterface;
0091 }
0092 
0093 bool KMailPluginEditorManagerInterface::processProcessKeyEvent(QKeyEvent *event)
0094 {
0095     if (!mListPluginInterface.isEmpty()) {
0096         for (MessageComposer::PluginEditorInterface *interface : std::as_const(mListPluginInterface)) {
0097             if (static_cast<MessageComposer::PluginEditor *>(interface->plugin())->canProcessKeyEvent()) {
0098                 if (interface->processProcessKeyEvent(event)) {
0099                     return true;
0100                 }
0101             }
0102         }
0103     }
0104     return false;
0105 }
0106 
0107 KActionCollection *KMailPluginEditorManagerInterface::actionCollection() const
0108 {
0109     return mActionCollection;
0110 }
0111 
0112 void KMailPluginEditorManagerInterface::setActionCollection(KActionCollection *actionCollection)
0113 {
0114     mActionCollection = actionCollection;
0115 }
0116 
0117 QList<QAction *> KMailPluginEditorManagerInterface::actionsType(MessageComposer::PluginActionType::Type type)
0118 {
0119     return mActionHash.value(type);
0120 }
0121 
0122 void KMailPluginEditorManagerInterface::setStatusBarWidgetEnabled(MessageComposer::PluginEditorInterface::ApplyOnFieldType type)
0123 {
0124     if (!mStatusBarWidget.isEmpty()) {
0125         for (MessageComposer::PluginEditorInterface *interface : std::as_const(mListPluginInterface)) {
0126             if (auto w = interface->statusBarWidget()) {
0127                 w->setEnabled((interface->applyOnFieldTypes() & type));
0128             }
0129         }
0130     }
0131 }
0132 
0133 QList<QWidget *> KMailPluginEditorManagerInterface::statusBarWidgetList()
0134 {
0135     if (mStatusBarWidget.isEmpty() && !mListPluginInterface.isEmpty()) {
0136         for (MessageComposer::PluginEditorInterface *interface : std::as_const(mListPluginInterface)) {
0137             if (interface->plugin()->hasStatusBarSupport()) {
0138                 mStatusBarWidget.append(interface->statusBarWidget());
0139             }
0140         }
0141     }
0142     return mStatusBarWidget;
0143 }
0144 
0145 QHash<MessageComposer::PluginActionType::Type, QList<QAction *>> KMailPluginEditorManagerInterface::actionsType()
0146 {
0147     if (mActionHash.isEmpty() && !mListPluginInterface.isEmpty()) {
0148         for (MessageComposer::PluginEditorInterface *interface : std::as_const(mListPluginInterface)) {
0149             const MessageComposer::PluginActionType actionType = interface->actionType();
0150             MessageComposer::PluginActionType::Type type = actionType.type();
0151             const bool needSelectedText = interface->needSelectedText();
0152             if (needSelectedText) {
0153                 // Disable by default as we don't have selection by default.
0154                 actionType.action()->setEnabled(false);
0155                 connect(this, &KMailPluginEditorManagerInterface::textSelectionChanged, actionType.action(), &QAction::setEnabled);
0156             }
0157             QList<QAction *> lst = mActionHash.value(type);
0158             if (!lst.isEmpty()) {
0159                 auto act = new QAction(this);
0160                 act->setSeparator(true);
0161                 lst << act << actionType.action();
0162                 mActionHash.insert(type, lst);
0163             } else {
0164                 mActionHash.insert(type, QList<QAction *>() << actionType.action());
0165             }
0166             if (interface->plugin()->hasPopupMenuSupport()) {
0167                 type = MessageComposer::PluginActionType::PopupMenu;
0168                 lst = mActionHash.value(type);
0169                 if (!lst.isEmpty()) {
0170                     auto act = new QAction(this);
0171                     act->setSeparator(true);
0172                     lst << act << actionType.action();
0173                     mActionHash.insert(type, lst);
0174                 } else {
0175                     mActionHash.insert(type, QList<QAction *>() << actionType.action());
0176                 }
0177             }
0178             if (interface->plugin()->hasToolBarSupport()) {
0179                 type = MessageComposer::PluginActionType::ToolBar;
0180                 lst = mActionHash.value(type);
0181                 if (!lst.isEmpty()) {
0182                     auto act = new QAction(this);
0183                     act->setSeparator(true);
0184                     lst << act << actionType.action();
0185                     mActionHash.insert(type, lst);
0186                 } else {
0187                     mActionHash.insert(type, QList<QAction *>() << actionType.action());
0188                 }
0189             }
0190         }
0191     }
0192     return mActionHash;
0193 }
0194 
0195 #include "moc_kmailplugineditormanagerinterface.cpp"