pluginloader.cpp
00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017 #include "pluginloader.h"
00018
00019
00020 #include <qstring.h>
00021
00022
00023 #include <kdebug.h>
00024 #include <klibloader.h>
00025
00026
00027 #include "plugin.h"
00028
00029 using namespace Umbrello;
00030
00031
00032 PluginLoader *PluginLoader::_instance = NULL;
00033
00034 PluginLoader::PluginLoader() :
00035 _plugins(),
00036 _categories()
00037 {
00038
00039 _categories["metamodel"] = PluginList();
00040 _categories["storage"] = PluginList();
00041 _categories["visual"] = PluginList();
00042 }
00043
00044 PluginLoader::~PluginLoader()
00045 {
00046 }
00047
00048 PluginLoader *
00049 PluginLoader::instance()
00050 {
00051 if(!_instance) _instance = new PluginLoader;
00052 return _instance;
00053 }
00054
00055 Plugin *
00056 PluginLoader::loadPlugin(const QString &name)
00057 {
00058 KLibrary *lib = NULL;
00059 KLibFactory *factory = NULL;
00060 Plugin *plugin = NULL;
00061 PluginMap::iterator it;
00062 bool success = true;
00063
00064
00065
00066 if((it = _plugins.find(name)) != _plugins.end()) {
00067 plugin = it.data();
00068 plugin->ref();
00069 return plugin;
00070 }
00071
00072
00073 lib = KLibLoader::self()->library(name.latin1());
00074 if(!lib) {
00075 kdError() << "failed loading plugin library " << name << endl;
00076 success = false;
00077 }
00078
00079
00080 if(success) {
00081 factory = lib->factory();
00082 if(!factory) {
00083 kdError() << "failed to find factory for " << name << endl;
00084 success = false;
00085 }
00086 }
00087
00088
00089 if(success) {
00090 plugin = dynamic_cast<Plugin *>(factory->create((QObject*)0, name.latin1()));
00091 if(!plugin) {
00092 kdError() << "failed to create a plugin object for " << name << endl;
00093 success = false;
00094 }
00095 else {
00096
00097
00098 _plugins[name] = plugin;
00099 _categories[plugin->category()].append(plugin);
00100 }
00101 }
00102
00103
00104 if(success && plugin) {
00105 success = plugin->init();
00106 if(!success) {
00107
00108
00109 kdError() << "failure initializing " << name << endl;
00110 _categories[plugin->category()].remove(plugin);
00111 _plugins.remove(name);
00112 delete plugin;
00113 }
00114 }
00115
00116
00117
00118 if(success) {
00119 plugin->ref();
00120 connect(plugin, SIGNAL(destroyed(QObject *)), SLOT(slotDestroyed(QObject *)));
00121 }
00122
00123 return plugin;
00124 }
00125
00126 Plugin *
00127 PluginLoader::findPlugin(const QString &name)
00128 {
00129 Plugin *ret = NULL;
00130 PluginMap::iterator it = _plugins.find(name);
00131 if(it != _plugins.end()) {
00132 ret = it.data();
00133 }
00134 return ret;
00135 }
00136
00137 void
00138 PluginLoader::unloadPlugin(const QString &name)
00139 {
00140 KLibLoader::self()->unloadLibrary(name.latin1());
00141 }
00142
00143 const PluginLoader::PluginMap &
00144 PluginLoader::plugins() const
00145 {
00146 return _plugins;
00147 }
00148
00149 const PluginLoader::CategoryMap &
00150 PluginLoader::categories() const
00151 {
00152 return _categories;
00153 }
00154
00155 void
00156 PluginLoader::slotDestroyed(QObject *obj)
00157 {
00158 Plugin *plugin = static_cast<Plugin *>(obj);
00159
00160
00161
00162
00163
00164 PluginMap::iterator end(_plugins.end());
00165 for(PluginMap::iterator i = _plugins.begin(); i != end; ++i) {
00166 Plugin *p = i.data();
00167 if(p == plugin) {
00168 kdDebug() << "unloading plugin " << i.key() << endl;
00169
00170
00171 _plugins.remove(i);
00172 break;
00173 }
00174 }
00175 }
00176
00177 #include "pluginloader.moc"
This file is part of the documentation for umbrello Version 3.1.0.