1 /*
2    SPDX-FileCopyrightText: 2015-2021 Laurent Montel <montel@kde.org>
3 
4    SPDX-License-Identifier: GPL-2.0-or-later
5 */
6 
7 #include "kmailplugineditormanagerinterface.h"
8 #include "kmail_debug.h"
9 #include <MessageComposer/PluginComposerInterface>
10 #include <MessageComposer/PluginEditor>
11 #include <MessageComposer/PluginEditorManager>
12 
13 #include <QAction>
14 #include <QVector>
15 
KMailPluginEditorManagerInterface(QObject * parent)16 KMailPluginEditorManagerInterface::KMailPluginEditorManagerInterface(QObject *parent)
17     : QObject(parent)
18 {
19 }
20 
21 KMailPluginEditorManagerInterface::~KMailPluginEditorManagerInterface() = default;
22 
richTextEditor() const23 KPIMTextEdit::RichTextEditor *KMailPluginEditorManagerInterface::richTextEditor() const
24 {
25     return mRichTextEditor;
26 }
27 
setRichTextEditor(KPIMTextEdit::RichTextEditor * richTextEditor)28 void KMailPluginEditorManagerInterface::setRichTextEditor(KPIMTextEdit::RichTextEditor *richTextEditor)
29 {
30     mRichTextEditor = richTextEditor;
31 }
32 
parentWidget() const33 QWidget *KMailPluginEditorManagerInterface::parentWidget() const
34 {
35     return mParentWidget;
36 }
37 
setParentWidget(QWidget * parentWidget)38 void KMailPluginEditorManagerInterface::setParentWidget(QWidget *parentWidget)
39 {
40     mParentWidget = parentWidget;
41 }
42 
initializePlugins()43 void KMailPluginEditorManagerInterface::initializePlugins()
44 {
45     if (!mListPluginInterface.isEmpty()) {
46         qCWarning(KMAIL_LOG) << "Plugin was already initialized. This is a bug";
47         return;
48     }
49     if (!mRichTextEditor) {
50         qCWarning(KMAIL_LOG) << "KMailPluginEditorManagerInterface: Missing richtexteditor";
51         return;
52     }
53     if (!mParentWidget) {
54         qCWarning(KMAIL_LOG) << "KMailPluginEditorManagerInterface : Parent is null. This is a bug";
55     }
56 
57     const QVector<MessageComposer::PluginEditor *> lstPlugin = MessageComposer::PluginEditorManager::self()->pluginsList();
58     for (MessageComposer::PluginEditor *plugin : lstPlugin) {
59         if (plugin->isEnabled()) {
60             auto interface = static_cast<MessageComposer::PluginEditorInterface *>(plugin->createInterface(this));
61             auto composerInterface = new MessageComposer::PluginComposerInterface;
62             composerInterface->setComposerViewBase(mComposerInterface);
63             interface->setComposerInterface(composerInterface);
64             interface->setRichTextEditor(mRichTextEditor);
65             interface->setParentWidget(mParentWidget);
66             interface->createAction(mActionCollection);
67             interface->setPlugin(plugin);
68             connect(interface, &MessageComposer::PluginEditorInterface::emitPluginActivated, this, &KMailPluginEditorManagerInterface::slotPluginActivated);
69             connect(interface, &MessageComposer::PluginEditorInterface::message, this, &KMailPluginEditorManagerInterface::message);
70             connect(interface, &MessageComposer::PluginEditorInterface::insertText, this, &KMailPluginEditorManagerInterface::insertText);
71             mListPluginInterface.append(interface);
72         }
73     }
74 }
75 
slotPluginActivated(MessageComposer::PluginEditorInterface * interface)76 void KMailPluginEditorManagerInterface::slotPluginActivated(MessageComposer::PluginEditorInterface *interface)
77 {
78     interface->exec();
79 }
80 
composerInterface() const81 MessageComposer::ComposerViewBase *KMailPluginEditorManagerInterface::composerInterface() const
82 {
83     return mComposerInterface;
84 }
85 
setComposerInterface(MessageComposer::ComposerViewBase * composerInterface)86 void KMailPluginEditorManagerInterface::setComposerInterface(MessageComposer::ComposerViewBase *composerInterface)
87 {
88     mComposerInterface = composerInterface;
89 }
90 
processProcessKeyEvent(QKeyEvent * event)91 bool KMailPluginEditorManagerInterface::processProcessKeyEvent(QKeyEvent *event)
92 {
93     if (!mListPluginInterface.isEmpty()) {
94         for (MessageComposer::PluginEditorInterface *interface : std::as_const(mListPluginInterface)) {
95             if (static_cast<MessageComposer::PluginEditor *>(interface->plugin())->canProcessKeyEvent()) {
96                 if (interface->processProcessKeyEvent(event)) {
97                     return true;
98                 }
99             }
100         }
101     }
102     return false;
103 }
104 
actionCollection() const105 KActionCollection *KMailPluginEditorManagerInterface::actionCollection() const
106 {
107     return mActionCollection;
108 }
109 
setActionCollection(KActionCollection * actionCollection)110 void KMailPluginEditorManagerInterface::setActionCollection(KActionCollection *actionCollection)
111 {
112     mActionCollection = actionCollection;
113 }
114 
actionsType(MessageComposer::PluginActionType::Type type)115 QList<QAction *> KMailPluginEditorManagerInterface::actionsType(MessageComposer::PluginActionType::Type type)
116 {
117     return mActionHash.value(type);
118 }
119 
setStatusBarWidgetEnabled(MessageComposer::PluginEditorInterface::ApplyOnFieldType type)120 void KMailPluginEditorManagerInterface::setStatusBarWidgetEnabled(MessageComposer::PluginEditorInterface::ApplyOnFieldType type)
121 {
122     if (!mStatusBarWidget.isEmpty()) {
123         for (MessageComposer::PluginEditorInterface *interface : std::as_const(mListPluginInterface)) {
124             if (auto w = interface->statusBarWidget()) {
125                 w->setEnabled((interface->applyOnFieldTypes() & type));
126             }
127         }
128     }
129 }
130 
statusBarWidgetList()131 QList<QWidget *> KMailPluginEditorManagerInterface::statusBarWidgetList()
132 {
133     if (mStatusBarWidget.isEmpty() && !mListPluginInterface.isEmpty()) {
134         for (MessageComposer::PluginEditorInterface *interface : std::as_const(mListPluginInterface)) {
135             if (interface->plugin()->hasStatusBarSupport()) {
136                 mStatusBarWidget.append(interface->statusBarWidget());
137             }
138         }
139     }
140     return mStatusBarWidget;
141 }
142 
actionsType()143 QHash<MessageComposer::PluginActionType::Type, QList<QAction *>> KMailPluginEditorManagerInterface::actionsType()
144 {
145     if (mActionHash.isEmpty() && !mListPluginInterface.isEmpty()) {
146         for (MessageComposer::PluginEditorInterface *interface : std::as_const(mListPluginInterface)) {
147             const MessageComposer::PluginActionType actionType = interface->actionType();
148             MessageComposer::PluginActionType::Type type = actionType.type();
149             const bool needSelectedText = interface->needSelectedText();
150             if (needSelectedText) {
151                 // Disable by default as we don't have selection by default.
152                 actionType.action()->setEnabled(false);
153                 connect(this, &KMailPluginEditorManagerInterface::textSelectionChanged, actionType.action(), &QAction::setEnabled);
154             }
155             QList<QAction *> lst = mActionHash.value(type);
156             if (!lst.isEmpty()) {
157                 auto act = new QAction(this);
158                 act->setSeparator(true);
159                 lst << act << actionType.action();
160                 mActionHash.insert(type, lst);
161             } else {
162                 mActionHash.insert(type, QList<QAction *>() << actionType.action());
163             }
164             if (interface->plugin()->hasPopupMenuSupport()) {
165                 type = MessageComposer::PluginActionType::PopupMenu;
166                 lst = mActionHash.value(type);
167                 if (!lst.isEmpty()) {
168                     auto act = new QAction(this);
169                     act->setSeparator(true);
170                     lst << act << actionType.action();
171                     mActionHash.insert(type, lst);
172                 } else {
173                     mActionHash.insert(type, QList<QAction *>() << actionType.action());
174                 }
175             }
176             if (interface->plugin()->hasToolBarSupport()) {
177                 type = MessageComposer::PluginActionType::ToolBar;
178                 lst = mActionHash.value(type);
179                 if (!lst.isEmpty()) {
180                     auto act = new QAction(this);
181                     act->setSeparator(true);
182                     lst << act << actionType.action();
183                     mActionHash.insert(type, lst);
184                 } else {
185                     mActionHash.insert(type, QList<QAction *>() << actionType.action());
186                 }
187             }
188         }
189     }
190     return mActionHash;
191 }
192