summaryrefslogtreecommitdiffstats
path: root/umbrello/umbrello/pluginloader.cpp
blob: db79bca1133e1d275bd405869d8994e3949f13c4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
/***************************************************************************
    begin                : Mon Jan 13 2003
    copyright            : (C) 2003 by Andrew Sutton
    email                : ansutton@kent.edu
***************************************************************************/

/***************************************************************************
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 ***************************************************************************/

// own header
#include "pluginloader.h"

// Qt includes
#include <qstring.h>

// KDE includes
#include <kdebug.h>
#include <klibloader.h>

// u2 includes
#include "plugin.h"

using namespace Umbrello;

// static data
PluginLoader *PluginLoader::_instance = NULL;

PluginLoader::PluginLoader() :
        _plugins(),
        _categories()
{
    // preseed the categories
    _categories["metamodel"] = PluginList();
    _categories["storage"] = PluginList();
    _categories["visual"] = PluginList();
}

PluginLoader::~PluginLoader()
{
}

PluginLoader *
PluginLoader::instance()
{
    if(!_instance) _instance = new PluginLoader;
    return _instance;
}

Plugin *
PluginLoader::loadPlugin(const QString &name)
{
    KLibrary *lib = NULL;
    KLibFactory *factory = NULL;
    Plugin *plugin = NULL;
    PluginMap::iterator it;
    bool success = true;

    // if the plugin has already been loaded, increment
    // its reference and return.
    if((it = _plugins.find(name)) != _plugins.end()) {
        plugin = it.data();
        plugin->ref();
        return plugin;
    }

    // use KLibLoader to get a reference to the library
    lib = KLibLoader::self()->library(name.latin1());
    if(!lib) {
        kdError() << "failed loading plugin library " << name << endl;
        success = false;
    }

    // get the factory from the library
    if(success) {
        factory = lib->factory();
        if(!factory) {
            kdError() << "failed to find factory for " << name << endl;
            success = false;
        }
    }

    // use the factory to create the plugin
    if(success) {
        plugin = dynamic_cast<Plugin *>(factory->create((QObject*)0, name.latin1()));
        if(!plugin) {
            kdError() << "failed to create a plugin object for " << name << endl;
            success = false;
        }
        else {
            // we have to register the plugin here, otherwise, we can get
            // recursive loads
            _plugins[name] = plugin;
            _categories[plugin->category()].append(plugin);
        }
    }

    // initialize the plugin
    if(success && plugin) {
        success = plugin->init();
        if(!success) {
            // on failure, delete the plugin. this should cause the
            // library to unload.
            kdError() << "failure initializing " << name << endl;
            _categories[plugin->category()].remove(plugin);
            _plugins.remove(name);
            delete plugin;
        }
    }

    // finally, finally connect to the destroyed signal and keep a
    // reference to it
    if(success) {
        plugin->ref();
        connect(plugin, SIGNAL(destroyed(QObject *)), SLOT(slotDestroyed(QObject *)));
    }

    return plugin;
}

Plugin *
PluginLoader::findPlugin(const QString &name)
{
    Plugin *ret = NULL;
    PluginMap::iterator it = _plugins.find(name);
    if(it != _plugins.end()) {
        ret = it.data();
    }
    return ret;
}

void
PluginLoader::unloadPlugin(const QString &name)
{
    KLibLoader::self()->unloadLibrary(name.latin1());
}

const PluginLoader::PluginMap &
PluginLoader::plugins() const
{
    return _plugins;
}

const PluginLoader::CategoryMap &
PluginLoader::categories() const
{
    return _categories;
}

void
PluginLoader::slotDestroyed(QObject *obj)
{
    Plugin *plugin = static_cast<Plugin *>(obj);

    // we can't just use the name because its already been destroyed
    // at this point. we have to iterate thru and find the reference
    // by hand.

    PluginMap::iterator end(_plugins.end());
    for(PluginMap::iterator i = _plugins.begin(); i != end; ++i) {
        Plugin *p = i.data();
        if(p == plugin) {
            kdDebug() << "unloading plugin " << i.key() << endl;

            // remove it from the mapping
            _plugins.remove(i);
            break;
        }
    }
}

#include "pluginloader.moc"