summaryrefslogtreecommitdiffstats
path: root/knetworkmanager-0.8/src/knetworkmanager-devicestore.cpp
blob: c9bc54c32bbaf8d604911dc096d387af6d62ec3b (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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
/***************************************************************************
 *
 * knetworkmanager-devicestore.cpp - A NetworkManager frontend for KDE 
 *
 * Copyright (C) 2005, 2006 Novell, Inc.
 *
 * Author: Helmut Schaa <[email protected]>, <[email protected]>
 *
 * 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 <NetworkManager.h>

#include "knetworkmanager.h"
#include "knetworkmanager-devicestore.h"
#include "knetworkmanager-wired_device.h"
#include "knetworkmanager-wireless_device.h"
#include "knetworkmanager-gsm_device.h"
#include "knetworkmanager-cdma_device.h"
#include "knetworkmanager-device.h"
#include "knetworkmanager-nm_proxy.h"
#include "dbus/deviceproxy.h"

#include <tqdbuserror.h>
#include <tqdbusobjectpath.h>
#include <tqdbusconnection.h>

#include <kdebug.h>

#if !defined(NM_CHECK_VERSION)
#define NM_CHECK_VERSION(x,y,z) 0
#endif

class DeviceStorePrivate
{
	public:
		DeviceStorePrivate() {}
		~DeviceStorePrivate() {}

	TQMap<TQString, Device*> devices;
	static DeviceStore* store;
};

DeviceStore* DeviceStorePrivate::store = NULL;

DeviceStore* DeviceStore::getInstance()
{
	if (DeviceStorePrivate::store)
		return DeviceStorePrivate::store;
	return (DeviceStorePrivate::store = new DeviceStore());
}

void DeviceStore::slotDeviceRemoved(const TQT_DBusObjectPath & obj_path)
{
	kdDebug() << "DeviceStore::slotDeviceRemoved" << endl;
	TQMap<TQString, Device*>::Iterator it = d->devices.find(TQString(obj_path));
	if (it != d->devices.end())
	{
		// remove this device
		Device* dev = it.data();

		emit DeviceRemoved(dev);

		d->devices.remove(it);
		delete dev;
		dev = NULL;
	}
}

void DeviceStore::slotDeviceAdded(const TQT_DBusObjectPath & obj_path)
{
	// just start an update
	Device* dev = createDevice(obj_path);
	if (dev)
		emit DeviceAdded(dev);
}

Device* DeviceStore::createDevice(const TQT_DBusObjectPath &obj_path)
{
	TQT_DBusError err;
	// if we have this device already in our list goto the next one
	TQMap<TQString, Device*>::Iterator it = d->devices.find(obj_path);
	if ( it != d->devices.end())
		return it.data();

	// FIXME: ugly stuff is going on here, better pass the DeviceProxy to the Device's constructor instead of the object_path
	DBus::DeviceProxy* dev = new DBus::DeviceProxy(NM_DBUS_SERVICE, obj_path);
	Device* new_dev = NULL;

	if (dev)
	{
		dev->setConnection(TQT_DBusConnection::systemBus());
		TQ_UINT32 type = dev->getDeviceType(err);

		//printf("Device obj_path: %s\n", obj_path->data());

		// FIXME: This should not be hardcoded, it would be better if wireless, wired etc. modules register their device type
		// select the right device type and create the appropriate objects
		switch(type)
		{
#if NM_CHECK_VERSION(0,8,992)
			case NM_DEVICE_TYPE_WIFI:
#else
			case DEVICE_TYPE_802_11_WIRELESS:
#endif
				new_dev = new WirelessDevice(obj_path);
				break;
#if NM_CHECK_VERSION(0,8,992)
			case NM_DEVICE_TYPE_ETHERNET:
#else
			case DEVICE_TYPE_802_3_ETHERNET:
#endif
				new_dev = new WiredDevice(obj_path);
				break;
#if NM_CHECK_VERSION(0,8,992)
			case NM_DEVICE_TYPE_MODEM:
#else
			case DEVICE_TYPE_GSM:
#endif
				new_dev = new GSMDevice(obj_path);
				break;
#if NM_CHECK_VERSION(0,8,992)
#else
			case DEVICE_TYPE_CDMA:
#endif
				new_dev = new CDMADevice(obj_path);
				break;
			default:
				kdWarning() << k_funcinfo << "Unknown devicetype" << endl;
				new_dev = new Device(obj_path);
				break;
		}

		// insert the new device into our list	
		if (new_dev)
			d->devices.insert(obj_path, new_dev);

		delete dev;
	}
	else
		kdWarning() << k_funcinfo << "Dev is Null" << endl;

	return new_dev;
}

void DeviceStore::updateDevices()
{
	NMProxy* nm = NMProxy::getInstance();
	TQValueList<TQT_DBusObjectPath> obj_paths;
	TQT_DBusError err;

	// get a list of NM devices
	nm->GetDevices(obj_paths, err);

	// create a list of KNM devices
	for (TQValueList<TQT_DBusObjectPath>::Iterator it = obj_paths.begin(); it != obj_paths.end(); ++it)
	{
		createDevice((*it));
	}
}

TQValueList<Device*> DeviceStore::getDevices(TQ_UINT32 type)
{
	updateDevices();

	if (type == 0)
		return d->devices.values();
	else
	{
		// only return devices of a special type
		TQValueList<Device*> devs;
		for (TQMap<TQString, Device*>::Iterator it = d->devices.begin(); it != d->devices.end(); ++it)
		{
			if (it.data()->getDeviceType() == type)
				devs.append(it.data());
		}
		return devs;
	}
}

Device* DeviceStore::getDevice(TQT_DBusObjectPath objpath)
{
	return d->devices[objpath];
}

DeviceStore::DeviceStore ( TQObject * parent, const char * name ) : TQObject( parent, name )
{
	d = new DeviceStorePrivate();

	// get notified from NM when devices are added or removed
	NMProxy* nm = NMProxy::getInstance();
	connect(nm, TQT_SIGNAL(DeviceAdded(const TQT_DBusObjectPath& )), this, TQT_SLOT(slotDeviceAdded(const TQT_DBusObjectPath&)));
	connect(nm, TQT_SIGNAL(DeviceRemoved(const TQT_DBusObjectPath& )), this, TQT_SLOT(slotDeviceRemoved(const TQT_DBusObjectPath&)));
}

DeviceStore::~DeviceStore ()
{
	// delete all devicepointers
	while (d->devices.begin() != d->devices.end())
		delete d->devices.begin().data();

	// delete private data
	delete d;
}


#include "knetworkmanager-devicestore.moc"