1
/* This file is part of the KDE project
2
Copyright (C) 2001 Christoph Cullmann <cullmann@kde.org>
3
Copyright (C) 2001 Joseph Wenninger <jowenn@kde.org>
4
Copyright (C) 2001 Anders Lund <anders.lund@lund.tdcadsl.dk>
6
This library is free software; you can redistribute it and/or
7
modify it under the terms of the GNU Library General Public
8
License version 2 as published by the Free Software Foundation.
10
This library is distributed in the hope that it will be useful,
11
but WITHOUT ANY WARRANTY; without even the implied warranty of
12
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13
Library General Public License for more details.
15
You should have received a copy of the GNU Library General Public License
16
along with this library; see the file COPYING.LIB. If not, write to
17
the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18
Boston, MA 02110-1301, USA.
23
#include "katepluginmanager.h"
26
#include "katemainwindow.h"
27
#include "katedebug.h"
30
#include <KConfigGroup>
31
#include <KPluginFactory>
32
#include <KPluginLoader>
37
#include <ktexteditor/sessionconfiginterface.h>
39
QString KatePluginInfo::saveName() const
41
return QFileInfo(metaData.fileName()).baseName();
44
KatePluginManager::KatePluginManager(QObject *parent) : QObject(parent)
49
KatePluginManager::~KatePluginManager()
51
// than unload the plugins
55
void KatePluginManager::setupPluginList()
58
* get all KTextEditor/Plugins
60
const QVector<KPluginMetaData> plugins = KPluginLoader::findPlugins(QStringLiteral("ktexteditor"), [](const KPluginMetaData & md) {
61
return md.serviceTypes().contains(QStringLiteral("KTextEditor/Plugin"));
65
* move them to our internal data structure
66
* activate some plugins per default
68
QSet<QString> defaultPlugins;
69
defaultPlugins.insert (QLatin1String("katefiletreeplugin"));
70
defaultPlugins.insert (QLatin1String("tabswitcherplugin"));
71
defaultPlugins.insert (QLatin1String("kateprojectplugin"));
72
defaultPlugins.insert (QLatin1String("katesearchplugin"));
73
m_pluginList.clear ();
74
Q_FOREACH(const KPluginMetaData &metaData, plugins) {
76
info.metaData = metaData;
77
info.defaultLoad = defaultPlugins.contains(info.saveName());
79
info.plugin = nullptr;
80
m_pluginList.push_back(info);
84
* construct fast lookup map
86
m_name2Plugin.clear();
87
for (int i = 0; i < m_pluginList.size(); ++i) {
88
m_name2Plugin[m_pluginList[i].saveName()] = &(m_pluginList[i]);
92
void KatePluginManager::loadConfig(KConfig *config)
94
// first: unload the plugins
101
KConfigGroup cg = KConfigGroup(config, QStringLiteral("Kate Plugins"));
103
// disable all plugin if no config, beside the ones marked as default load
104
for (int i = 0; i < m_pluginList.size(); ++i) {
105
m_pluginList[i].load = cg.readEntry(m_pluginList[i].saveName(), m_pluginList[i].defaultLoad);
112
for (KatePluginList::iterator it = m_pluginList.begin(); it != m_pluginList.end(); ++it) {
117
if (auto interface = qobject_cast<KTextEditor::SessionConfigInterface *> (it->plugin)) {
118
KConfigGroup group(config, QString::fromLatin1("Plugin:%1:").arg(it->saveName()));
119
interface->readSessionConfig(group);
125
void KatePluginManager::writeConfig(KConfig *config)
129
KConfigGroup cg = KConfigGroup(config, QStringLiteral("Kate Plugins"));
130
foreach(const KatePluginInfo & plugin, m_pluginList) {
131
QString saveName = plugin.saveName();
133
cg.writeEntry(saveName, plugin.load);
136
if (auto interface = qobject_cast<KTextEditor::SessionConfigInterface *> (plugin.plugin)) {
137
KConfigGroup group(config, QString::fromLatin1("Plugin:%1:").arg(saveName));
138
interface->writeSessionConfig(group);
143
void KatePluginManager::unloadAllPlugins()
145
for (KatePluginList::iterator it = m_pluginList.begin(); it != m_pluginList.end(); ++it) {
147
unloadPlugin(&(*it));
152
void KatePluginManager::enableAllPluginsGUI(KateMainWindow *win, KConfigBase *config)
154
for (KatePluginList::iterator it = m_pluginList.begin(); it != m_pluginList.end(); ++it) {
156
enablePluginGUI(&(*it), win, config);
161
void KatePluginManager::disableAllPluginsGUI(KateMainWindow *win)
163
for (KatePluginList::iterator it = m_pluginList.begin(); it != m_pluginList.end(); ++it) {
165
disablePluginGUI(&(*it), win);
170
void KatePluginManager::loadPlugin(KatePluginInfo *item)
173
* try to load the plugin
175
item->load = (item->plugin = KPluginLoader(item->metaData.fileName()).factory()->create<KTextEditor::Plugin>(this, QVariantList() << item->saveName()));
178
* tell the world about the success
181
emit KateApp::self()->wrapper()->pluginCreated(item->saveName(), item->plugin);
185
void KatePluginManager::unloadPlugin(KatePluginInfo *item)
187
disablePluginGUI(item);
189
KTextEditor::Plugin *plugin = item->plugin;
192
emit KateApp::self()->wrapper()->pluginDeleted(item->saveName(), plugin);
195
void KatePluginManager::enablePluginGUI(KatePluginInfo *item, KateMainWindow *win, KConfigBase *config)
197
// plugin around at all?
202
// lookup if there is already a view for it..
203
QObject *createdView = nullptr;
204
if (!win->pluginViews().contains(item->plugin)) {
205
// create the view + try to correctly load shortcuts, if it's a GUI Client
206
createdView = item->plugin->createView(win->wrapper());
208
win->pluginViews().insert(item->plugin, createdView);
212
// load session config if needed
213
if (config && win->pluginViews().contains(item->plugin)) {
214
if (auto interface = qobject_cast<KTextEditor::SessionConfigInterface *> (win->pluginViews().value(item->plugin))) {
215
KConfigGroup group(config, QString::fromLatin1("Plugin:%1:MainWindow:0").arg(item->saveName()));
216
interface->readSessionConfig(group);
221
emit win->wrapper()->pluginViewCreated(item->saveName(), createdView);
225
void KatePluginManager::enablePluginGUI(KatePluginInfo *item)
227
// plugin around at all?
232
// enable the gui for all mainwindows...
233
for (int i = 0; i < KateApp::self()->mainWindowsCount(); i++) {
234
enablePluginGUI(item, KateApp::self()->mainWindow(i), 0);
238
void KatePluginManager::disablePluginGUI(KatePluginInfo *item, KateMainWindow *win)
240
// plugin around at all?
245
// lookup if there is a view for it..
246
if (!win->pluginViews().contains(item->plugin)) {
250
// really delete the view of this plugin
251
QObject *deletedView = win->pluginViews().value(item->plugin);
253
win->pluginViews().remove(item->plugin);
254
emit win->wrapper()->pluginViewDeleted(item->saveName(), deletedView);
257
void KatePluginManager::disablePluginGUI(KatePluginInfo *item)
259
// plugin around at all?
264
// disable the gui for all mainwindows...
265
for (int i = 0; i < KateApp::self()->mainWindowsCount(); i++) {
266
disablePluginGUI(item, KateApp::self()->mainWindow(i));
270
KTextEditor::Plugin *KatePluginManager::plugin(const QString &name)
275
if (!m_name2Plugin.contains(name)) {
280
* real plugin instance, if any ;)
282
return m_name2Plugin.value(name)->plugin;
285
bool KatePluginManager::pluginAvailable(const QString &name)
287
return m_name2Plugin.contains(name);
290
class KTextEditor::Plugin *KatePluginManager::loadPlugin(const QString &name, bool permanent)
295
if (!m_name2Plugin.contains(name)) {
300
* load, bail out on error
302
loadPlugin(m_name2Plugin.value(name));
303
if (!m_name2Plugin.value(name)->plugin) {
308
* perhaps patch not load again back to "ok, load it once again on next loadConfig"
310
m_name2Plugin.value(name)->load = permanent;
311
return m_name2Plugin.value(name)->plugin;
314
void KatePluginManager::unloadPlugin(const QString &name, bool permanent)
319
if (!m_name2Plugin.contains(name)) {
326
unloadPlugin(m_name2Plugin.value(name));
329
* perhaps patch load again back to "ok, load it once again on next loadConfig"
331
m_name2Plugin.value(name)->load = !permanent;