Pimcommon

plugininterface.cpp
1/*
2 SPDX-FileCopyrightText: 2015-2024 Laurent Montel <montel@kde.org>
3
4 SPDX-License-Identifier: GPL-2.0-or-later
5*/
6
7#include "plugininterface.h"
8
9#include "pimcommonakonadi_debug.h"
10#include <PimCommon/GenericPluginManager>
11
12#include <KActionCollection>
13#include <KXMLGUIClient>
14#include <QAction>
15
16#include <PimCommon/GenericPlugin>
17
18using namespace PimCommon;
19
20class PimCommon::PluginInterfacePrivate
21{
22public:
23 PluginInterfacePrivate() = default;
24
25 QString mPluginName;
26 QString mPluginDirectory;
27 QWidget *mParentWidget = nullptr;
28 KActionCollection *mActionCollection = nullptr;
30 GenericPluginManager *mGenericPluginManager = nullptr;
31};
32
33PluginInterface::PluginInterface(QObject *parent)
34 : QObject(parent)
35 , d(new PimCommon::PluginInterfacePrivate)
36{
37 d->mGenericPluginManager = new GenericPluginManager(this);
38}
39
40PluginInterface::~PluginInterface() = default;
41
42void PimCommon::PluginInterface::setActionCollection(KActionCollection *ac)
43{
44 d->mActionCollection = ac;
45}
46
47void PluginInterface::initializePlugins()
48{
49 d->mGenericPluginManager->setPluginName(d->mPluginName);
50 d->mGenericPluginManager->setPluginDirectory(d->mPluginDirectory);
51 if (!d->mGenericPluginManager->initializePlugins()) {
52 qCDebug(PIMCOMMONAKONADI_LOG) << " Impossible to initialize plugins";
53 }
54}
55
56void PluginInterface::setPluginName(const QString &name)
57{
58 d->mPluginName = name;
59}
60
61void PluginInterface::setPluginDirectory(const QString &name)
62{
63 d->mPluginDirectory = name;
64}
65
66void PluginInterface::createPluginInterface()
67{
68 if (!d->mActionCollection) {
69 qCWarning(PIMCOMMONAKONADI_LOG) << "Missing action collection";
70 return;
71 }
72 const auto pluginsList = d->mGenericPluginManager->pluginsList();
73 for (PimCommon::GenericPlugin *plugin : pluginsList) {
74 if (plugin->isEnabled()) {
75 auto interface = static_cast<PimCommon::GenericPluginInterface *>(plugin->createInterface(this));
76 interface->setParentWidget(d->mParentWidget);
77 // Add parent before creating action
78 interface->createAction(d->mActionCollection);
79 interface->setPlugin(plugin);
80 connect(interface, &PimCommon::GenericPluginInterface::emitPluginActivated, this, &PluginInterface::slotPluginActivated);
81 d->mListGenericInterface.append(interface);
82 }
83 }
84}
85
86bool PluginInterface::initializeInterfaceRequires(PimCommon::AbstractGenericPluginInterface *interface)
87{
88 Q_UNUSED(interface)
89 return true;
90}
91
92QString PluginInterface::actionXmlExtension(ActionType::Type type)
93{
94 switch (type) {
95 case PimCommon::ActionType::Tools:
96 return QStringLiteral("_plugins_tools");
97 case PimCommon::ActionType::Edit:
98 return QStringLiteral("_plugins_edit");
99 case PimCommon::ActionType::File:
100 return QStringLiteral("_plugins_file");
101 case PimCommon::ActionType::Action:
102 return QStringLiteral("_plugins_actions");
103 case PimCommon::ActionType::PopupMenu:
104 return QStringLiteral("_popupmenu_actions");
105 case PimCommon::ActionType::ToolBar:
106 return QStringLiteral("_toolbar_actions");
107 case PimCommon::ActionType::Message:
108 return QStringLiteral("_plugins_message");
109 case PimCommon::ActionType::Folder:
110 return QStringLiteral("_plugins_folder");
111 }
112 return {};
113}
114
115void PluginInterface::slotPluginActivated(PimCommon::AbstractGenericPluginInterface *interface)
116{
117 if (interface) {
118 if (initializeInterfaceRequires(interface)) {
119 interface->exec();
120 }
121 }
122}
123
124void PluginInterface::setParentWidget(QWidget *widget)
125{
126 d->mParentWidget = widget;
127}
128
129void PluginInterface::clearPluginActions(const QString &prefix, KXMLGUIClient *guiClient)
130{
131 if (guiClient->factory()) {
133 while (localActionsType.hasNext()) {
134 localActionsType.next();
135 QList<QAction *> lst = localActionsType.value();
136 if (!lst.isEmpty()) {
137 const QString actionlistname = prefix + PimCommon::PluginInterface::actionXmlExtension(localActionsType.key());
138 guiClient->unplugActionList(actionlistname);
139 }
140 }
141 }
142}
143
144void PluginInterface::initializePluginActions(const QString &prefix, KXMLGUIClient *guiClient)
145{
146 if (guiClient->factory()) {
148 while (localActionsType.hasNext()) {
149 localActionsType.next();
150 QList<QAction *> lst = localActionsType.value();
151 if (!lst.isEmpty()) {
152 const QString actionlistname = prefix + PimCommon::PluginInterface::actionXmlExtension(localActionsType.key());
153 guiClient->unplugActionList(actionlistname);
154 guiClient->plugActionList(actionlistname, lst);
155 }
156 }
157 }
158}
159
160void PluginInterface::updateActions(int numberOfSelectedItems, int numberOfSelectedCollections)
161{
162 for (PimCommon::GenericPluginInterface *interface : std::as_const(d->mListGenericInterface)) {
163 interface->updateActions(numberOfSelectedItems, numberOfSelectedCollections);
164 }
165}
166
167QHash<PimCommon::ActionType::Type, QList<QAction *>> PluginInterface::actionsType()
168{
170 for (PimCommon::GenericPluginInterface *interface : std::as_const(d->mListGenericInterface)) {
171 const auto actionTypes = interface->actionTypes();
172 for (const PimCommon::ActionType &actionType : actionTypes) {
173 PimCommon::ActionType::Type type = actionType.type();
174 QList<QAction *> lst = listType.value(type);
175 if (!lst.isEmpty()) {
176 auto act = new QAction(this);
177 act->setSeparator(true);
178 lst << act << actionType.action();
179 listType.insert(type, lst);
180 } else {
181 listType.insert(type, QList<QAction *>() << actionType.action());
182 }
183 if (interface->plugin()->hasPopupMenuSupport()) {
184 type = PimCommon::ActionType::PopupMenu;
185 lst = listType.value(type);
186 if (!lst.isEmpty()) {
187 auto act = new QAction(this);
188 act->setSeparator(true);
189 lst << act << actionType.action();
190 listType.insert(type, lst);
191 } else {
192 listType.insert(type, QList<QAction *>() << actionType.action());
193 }
194 }
195 if (interface->plugin()->hasToolBarSupport()) {
196 type = PimCommon::ActionType::ToolBar;
197 lst = listType.value(type);
198 if (!lst.isEmpty()) {
199 auto act = new QAction(this);
200 act->setSeparator(true);
201 lst << act << actionType.action();
202 listType.insert(type, lst);
203 } else {
204 listType.insert(type, QList<QAction *>() << actionType.action());
205 }
206 }
207 }
208 }
209
210 return listType;
211}
212
213QList<PimCommon::PluginUtilData> PluginInterface::pluginsDataList() const
214{
215 return d->mGenericPluginManager->pluginsDataList();
216}
217
218QString PluginInterface::configGroupName() const
219{
220 return d->mGenericPluginManager->configGroupName();
221}
222
223QString PluginInterface::configPrefixSettingKey() const
224{
225 return d->mGenericPluginManager->configPrefixSettingKey();
226}
227
228PimCommon::GenericPlugin *PluginInterface::pluginFromIdentifier(const QString &id)
229{
230 return d->mGenericPluginManager->pluginFromIdentifier(id);
231}
232
233#include "moc_plugininterface.cpp"
void unplugActionList(const QString &name)
void plugActionList(const QString &name, const QList< QAction * > &actionList)
KXMLGUIFactory * factory() const
The GenericPluginInterface class.
The GenericPluginManager class.
The GenericPlugin class.
Type type(const QSqlDatabase &db)
QString name(StandardAction id)
folderdialogacltab.h
iterator insert(const Key &key, const T &value)
T value(const Key &key) const const
bool isEmpty() const const
T value(qsizetype i) const const
QMetaObject::Connection connect(const QObject *sender, PointerToMemberFunction signal, Functor functor)
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Mon Nov 18 2024 12:14:35 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.