summaryrefslogtreecommitdiffstats
path: root/knetworkmanager-0.8/src/knetworkmanager-pluginmanager.cpp
blob: 57ee8c1ff98bbb973d3d43aadb3bd727dddc4fb1 (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
178
179
180
181
182
183
184
/***************************************************************************
 *
 * knetworkmanager-pluginmanager.cpp - A NetworkManager frontend for KDE 
 *
 * Copyright (C) 2006 Novell, Inc.
 *
 * Author: Helmut Schaa <hschaa@suse.de>, <helmut.schaa@gmx.de>
 *
 * 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.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 **************************************************************************/

#include <kplugininfo.h>
#include <tdeparts/componentfactory.h>
#include <kdebug.h>
#include <tdelocale.h>

#include "knetworkmanager-pluginmanager.h"

PluginManager* PluginManager::_instance;

PluginManager* PluginManager::getInstance()
{
	if (_instance)
		return _instance;
	return new PluginManager(TQT_TQOBJECT(KNetworkManager::getInstance()), "pluginmanager");
}

PluginManager::PluginManager(TQObject* parent, const char* name)
	: TQObject(parent, name)
{
	// get list of available plugins
	this->_plugins = KPluginInfo::fromServices( TDETrader::self()->query( TQString::fromLatin1( "KNetworkManager/Plugin" )));

	// a bit debug output
	for(TQValueList<KPluginInfo*>::ConstIterator it = _plugins.begin(); it != _plugins.end(); ++it)
		kdDebug() << k_funcinfo << TQString("Found Plugin '%1'").arg((*it)->pluginName()) << endl;
}

PluginManager::~PluginManager()
{
	// delete all loaded plugins
	while(!_loadedPlugins.empty())
	{
		PluginMap::Iterator it = _loadedPlugins.begin();
		_loadedPlugins.remove(it);
	}

	// delete all available plugininfos
	while(!_plugins.empty())
	{
		TQValueList<KPluginInfo*>::Iterator it = _plugins.begin();
		delete *it;
		_plugins.remove(it);
	}
}

TQStringList PluginManager::getPluginList(const TQString& serviceType, const TQString& property, const TQString& value) const
{
	TQStringList ret;
	// find a suitable plugin
	for(TQValueList<KPluginInfo*>::ConstIterator it = _plugins.begin(); it != _plugins.end(); ++it)
	{
		if ((*it)->service()->serviceTypes().contains(serviceType) > 0)
			if ((*it)->property(property).toString().contains(value))
				ret.append( (*it)->pluginName() );
	}
	return ret;
}

Plugin* PluginManager::getPlugin(const TQString& pluginID)
{
	KPluginInfo* info = infoForPluginID(pluginID);
	if (_loadedPlugins.contains(info))
	{
		return _loadedPlugins[info];
	}
	else
	{
		return loadPlugin(pluginID);
	}
	return NULL;
}

const KPluginInfo* PluginManager::getPluginInfo(const TQString& pluginID)
{
	return infoForPluginID(pluginID);
}

const KPluginInfo* PluginManager::getPluginInfo(const Plugin* plugin)
{
	for(PluginMap::ConstIterator it = _loadedPlugins.begin(); it != _loadedPlugins.end(); ++it)
	{
		if (it.data() == plugin)
			return it.key();
	}
	return NULL;
}

void PluginManager::loadAllPlugins()
{
	// iterate over all plugins
	for(TQValueList<KPluginInfo*>::ConstIterator it = _plugins.begin(); it != _plugins.end(); ++it)
	{
		// load Plugin
		loadPlugin((*it)->pluginName());
	}
}


Plugin* PluginManager::loadPlugin(const TQString& pluginID)
{
	// try to load Plugin
	int error = 0;
	KPluginInfo* info = infoForPluginID(pluginID);
	Plugin *plugin = KParts::ComponentFactory::createInstanceFromQuery<Plugin>( TQString::fromLatin1( "KNetworkManager/Plugin" ),
	                 TQString::fromLatin1( "[X-KDE-PluginInfo-Name]=='%1'" ).arg( pluginID ), this, 0, TQStringList(), &error );

	// plugin loaded?
	if (plugin)
	{
		kdDebug() << k_funcinfo << TQString(i18n("successfully loaded plugin '%1'")).arg(info->pluginName()) << endl;
		_loadedPlugins.insert(info, plugin);
	}
	else
	{
		// error
		switch( error )
		{
		case KParts::ComponentFactory::ErrNoServiceFound:
			kdDebug( ) << k_funcinfo << "No service implementing the given mimetype "
			           << "and fullfilling the given constraint expression can be found." << endl;
			break;

		case KParts::ComponentFactory::ErrServiceProvidesNoLibrary:
			kdDebug( ) << "the specified service provides no shared library." << endl;
			break;

		case KParts::ComponentFactory::ErrNoLibrary:
			kdDebug( ) << "the specified library could not be loaded." << endl;
			break;

		case KParts::ComponentFactory::ErrNoFactory:
			kdDebug( ) << "the library does not export a factory for creating components." << endl;
			break;

		case KParts::ComponentFactory::ErrNoComponent:
			kdDebug( ) << "the factory does not support creating components of the specified type." << endl;
			break;
		}

		kdDebug() << k_funcinfo << "Loading plugin '" << pluginID << "' failed, KLibLoader reported error: '" << endl
		          << KLibLoader::self()->lastErrorMessage() << "'" << endl;

	}

	return plugin;
}

KPluginInfo * PluginManager::infoForPluginID( const TQString& pluginID ) const
{
	TQValueList<KPluginInfo *>::ConstIterator it;
	for ( it = this->_plugins.begin(); it != this->_plugins.end(); ++it )
	{
		if ( ( *it )->pluginName() == pluginID )
			return *it;
	}

	return 0L;
}

#include "knetworkmanager-pluginmanager.moc"