~ubuntu-branches/ubuntu/vivid/kate/vivid-updates

« back to all changes in this revision

Viewing changes to kate/src/katepluginmanager.cpp

  • Committer: Package Import Robot
  • Author(s): Jonathan Riddell
  • Date: 2014-12-04 16:49:41 UTC
  • mfrom: (1.6.6)
  • Revision ID: package-import@ubuntu.com-20141204164941-l3qbvsly83hhlw2v
Tags: 4:14.11.97-0ubuntu1
* New upstream release
* Update build-deps and use pkg-kde v3 for Qt 5 build
* kate-data now kate5-data for co-installability

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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>
 
5
 
 
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.
 
9
 
 
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.
 
14
 
 
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.
 
19
*/
 
20
 
 
21
#include "config.h"
 
22
 
 
23
#include "katepluginmanager.h"
 
24
 
 
25
#include "kateapp.h"
 
26
#include "katemainwindow.h"
 
27
#include "katedebug.h"
 
28
 
 
29
#include <KConfig>
 
30
#include <KConfigGroup>
 
31
#include <KPluginFactory>
 
32
#include <KPluginLoader>
 
33
 
 
34
#include <QFile>
 
35
#include <QFileInfo>
 
36
 
 
37
#include <ktexteditor/sessionconfiginterface.h>
 
38
 
 
39
QString KatePluginInfo::saveName() const
 
40
{
 
41
    return QFileInfo(metaData.fileName()).baseName();
 
42
}
 
43
 
 
44
KatePluginManager::KatePluginManager(QObject *parent) : QObject(parent)
 
45
{
 
46
    setupPluginList();
 
47
}
 
48
 
 
49
KatePluginManager::~KatePluginManager()
 
50
{
 
51
    // than unload the plugins
 
52
    unloadAllPlugins();
 
53
}
 
54
 
 
55
void KatePluginManager::setupPluginList()
 
56
{
 
57
    /**
 
58
     * get all KTextEditor/Plugins
 
59
     */
 
60
    const QVector<KPluginMetaData> plugins = KPluginLoader::findPlugins(QStringLiteral("ktexteditor"), [](const KPluginMetaData & md) {
 
61
            return md.serviceTypes().contains(QStringLiteral("KTextEditor/Plugin"));
 
62
        });
 
63
 
 
64
    /**
 
65
     * move them to our internal data structure
 
66
     * activate some plugins per default
 
67
     */
 
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) {
 
75
        KatePluginInfo info;
 
76
        info.metaData = metaData;
 
77
        info.defaultLoad = defaultPlugins.contains(info.saveName());
 
78
        info.load = false;
 
79
        info.plugin = nullptr;
 
80
        m_pluginList.push_back(info);
 
81
    }
 
82
 
 
83
    /**
 
84
     * construct fast lookup map
 
85
     */
 
86
    m_name2Plugin.clear();
 
87
    for (int i = 0; i < m_pluginList.size(); ++i) {
 
88
        m_name2Plugin[m_pluginList[i].saveName()] = &(m_pluginList[i]);
 
89
    }
 
90
}
 
91
 
 
92
void KatePluginManager::loadConfig(KConfig *config)
 
93
{
 
94
    // first: unload the plugins
 
95
    unloadAllPlugins();
 
96
 
 
97
    /**
 
98
     * ask config object
 
99
     */
 
100
    if (config) {
 
101
        KConfigGroup cg = KConfigGroup(config, QStringLiteral("Kate Plugins"));
 
102
 
 
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);
 
106
        }
 
107
    }
 
108
 
 
109
    /**
 
110
     * load plugins
 
111
     */
 
112
    for (KatePluginList::iterator it = m_pluginList.begin(); it != m_pluginList.end(); ++it) {
 
113
        if (it->load) {
 
114
            loadPlugin(&(*it));
 
115
 
 
116
            // restore config
 
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);
 
120
            }
 
121
        }
 
122
    }
 
123
}
 
124
 
 
125
void KatePluginManager::writeConfig(KConfig *config)
 
126
{
 
127
    Q_ASSERT(config);
 
128
 
 
129
    KConfigGroup cg = KConfigGroup(config, QStringLiteral("Kate Plugins"));
 
130
    foreach(const KatePluginInfo & plugin, m_pluginList) {
 
131
        QString saveName = plugin.saveName();
 
132
 
 
133
        cg.writeEntry(saveName, plugin.load);
 
134
 
 
135
        // save config
 
136
        if (auto interface = qobject_cast<KTextEditor::SessionConfigInterface *> (plugin.plugin)) {
 
137
            KConfigGroup group(config, QString::fromLatin1("Plugin:%1:").arg(saveName));
 
138
            interface->writeSessionConfig(group);
 
139
        }
 
140
    }
 
141
}
 
142
 
 
143
void KatePluginManager::unloadAllPlugins()
 
144
{
 
145
    for (KatePluginList::iterator it = m_pluginList.begin(); it != m_pluginList.end(); ++it) {
 
146
        if (it->plugin) {
 
147
            unloadPlugin(&(*it));
 
148
        }
 
149
    }
 
150
}
 
151
 
 
152
void KatePluginManager::enableAllPluginsGUI(KateMainWindow *win, KConfigBase *config)
 
153
{
 
154
    for (KatePluginList::iterator it = m_pluginList.begin(); it != m_pluginList.end(); ++it) {
 
155
        if (it->plugin) {
 
156
            enablePluginGUI(&(*it), win, config);
 
157
        }
 
158
    }
 
159
}
 
160
 
 
161
void KatePluginManager::disableAllPluginsGUI(KateMainWindow *win)
 
162
{
 
163
    for (KatePluginList::iterator it = m_pluginList.begin(); it != m_pluginList.end(); ++it) {
 
164
        if (it->plugin) {
 
165
            disablePluginGUI(&(*it), win);
 
166
        }
 
167
    }
 
168
}
 
169
 
 
170
void KatePluginManager::loadPlugin(KatePluginInfo *item)
 
171
{
 
172
    /**
 
173
     * try to load the plugin
 
174
     */
 
175
    item->load = (item->plugin = KPluginLoader(item->metaData.fileName()).factory()->create<KTextEditor::Plugin>(this, QVariantList() << item->saveName()));
 
176
    
 
177
    /**
 
178
     * tell the world about the success
 
179
     */
 
180
    if (item->plugin) {
 
181
        emit KateApp::self()->wrapper()->pluginCreated(item->saveName(), item->plugin);
 
182
    }
 
183
}
 
184
 
 
185
void KatePluginManager::unloadPlugin(KatePluginInfo *item)
 
186
{
 
187
    disablePluginGUI(item);
 
188
    delete item->plugin;
 
189
    KTextEditor::Plugin *plugin = item->plugin;
 
190
    item->plugin = 0L;
 
191
    item->load = false;
 
192
    emit KateApp::self()->wrapper()->pluginDeleted(item->saveName(), plugin);
 
193
}
 
194
 
 
195
void KatePluginManager::enablePluginGUI(KatePluginInfo *item, KateMainWindow *win, KConfigBase *config)
 
196
{
 
197
    // plugin around at all?
 
198
    if (!item->plugin) {
 
199
        return;
 
200
    }
 
201
 
 
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());
 
207
        if (createdView) {
 
208
            win->pluginViews().insert(item->plugin, createdView);
 
209
        }
 
210
    }
 
211
 
 
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);
 
217
        }
 
218
    }
 
219
 
 
220
    if (createdView) {
 
221
        emit win->wrapper()->pluginViewCreated(item->saveName(), createdView);
 
222
    }
 
223
}
 
224
 
 
225
void KatePluginManager::enablePluginGUI(KatePluginInfo *item)
 
226
{
 
227
    // plugin around at all?
 
228
    if (!item->plugin) {
 
229
        return;
 
230
    }
 
231
 
 
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);
 
235
    }
 
236
}
 
237
 
 
238
void KatePluginManager::disablePluginGUI(KatePluginInfo *item, KateMainWindow *win)
 
239
{
 
240
    // plugin around at all?
 
241
    if (!item->plugin) {
 
242
        return;
 
243
    }
 
244
 
 
245
    // lookup if there is a view for it..
 
246
    if (!win->pluginViews().contains(item->plugin)) {
 
247
        return;
 
248
    }
 
249
 
 
250
    // really delete the view of this plugin
 
251
    QObject *deletedView = win->pluginViews().value(item->plugin);
 
252
    delete deletedView;
 
253
    win->pluginViews().remove(item->plugin);
 
254
    emit win->wrapper()->pluginViewDeleted(item->saveName(), deletedView);
 
255
}
 
256
 
 
257
void KatePluginManager::disablePluginGUI(KatePluginInfo *item)
 
258
{
 
259
    // plugin around at all?
 
260
    if (!item->plugin) {
 
261
        return;
 
262
    }
 
263
 
 
264
    // disable the gui for all mainwindows...
 
265
    for (int i = 0; i < KateApp::self()->mainWindowsCount(); i++) {
 
266
        disablePluginGUI(item, KateApp::self()->mainWindow(i));
 
267
    }
 
268
}
 
269
 
 
270
KTextEditor::Plugin *KatePluginManager::plugin(const QString &name)
 
271
{
 
272
    /**
 
273
     * name known?
 
274
     */
 
275
    if (!m_name2Plugin.contains(name)) {
 
276
        return 0;
 
277
    }
 
278
 
 
279
    /**
 
280
     * real plugin instance, if any ;)
 
281
     */
 
282
    return m_name2Plugin.value(name)->plugin;
 
283
}
 
284
 
 
285
bool KatePluginManager::pluginAvailable(const QString &name)
 
286
{
 
287
    return m_name2Plugin.contains(name);
 
288
}
 
289
 
 
290
class KTextEditor::Plugin *KatePluginManager::loadPlugin(const QString &name, bool permanent)
 
291
{
 
292
    /**
 
293
     * name known?
 
294
     */
 
295
    if (!m_name2Plugin.contains(name)) {
 
296
        return 0;
 
297
    }
 
298
 
 
299
    /**
 
300
     * load, bail out on error
 
301
     */
 
302
    loadPlugin(m_name2Plugin.value(name));
 
303
    if (!m_name2Plugin.value(name)->plugin) {
 
304
        return 0;
 
305
    }
 
306
 
 
307
    /**
 
308
     * perhaps patch not load again back to "ok, load it once again on next loadConfig"
 
309
     */
 
310
    m_name2Plugin.value(name)->load = permanent;
 
311
    return m_name2Plugin.value(name)->plugin;
 
312
}
 
313
 
 
314
void KatePluginManager::unloadPlugin(const QString &name, bool permanent)
 
315
{
 
316
    /**
 
317
     * name known?
 
318
     */
 
319
    if (!m_name2Plugin.contains(name)) {
 
320
        return;
 
321
    }
 
322
 
 
323
    /**
 
324
     * unload
 
325
     */
 
326
    unloadPlugin(m_name2Plugin.value(name));
 
327
 
 
328
    /**
 
329
     * perhaps patch load again back to "ok, load it once again on next loadConfig"
 
330
     */
 
331
    m_name2Plugin.value(name)->load = !permanent;
 
332
}
 
333