summaryrefslogtreecommitdiffstats
path: root/knetworkmanager-0.8/src/knetworkmanager-wireless_device.cpp
blob: 554f797723b4aadadd9f1f105845cbfe64b0111c (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
/***************************************************************************
 *
 * knetworkmanager-device.cpp - A NetworkManager frontend for KDE 
 *
 * Copyright (C) 2005, 2006 Novell, Inc.
 *
 * Author: Timo Hoenig     <[email protected]>, <[email protected]>
 *         Will Stephenson <[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
 *
 **************************************************************************/

// NM includes
#include <NetworkManager.h>

// KNM includes
#include "knetworkmanager.h"
#include "knetworkmanager-wireless_device.h"
#include "dbus/accesspointproxy.h"
#include "dbus/wirelessproxy.h"
#include "knetworkmanager-accesspoint.h"

// KDE includes
#include <kdebug.h>

// TQt includes
#include <tqhostaddress.h>
#include <tqmap.h>
#include <tqvaluelist.h>

// TQt-Dbus includes
#include <tqdbusconnection.h>
#include <tqdbuserror.h>
#include <tqdbusobjectpath.h>

class WirelessDevicePrivate
{
public:
	WirelessDevicePrivate(TQString service, TQString obj_path)
		: nmWireless(service, obj_path)
	{}

	~WirelessDevicePrivate()
	{
		// cleanup the AP-list
		for (TQMap<TQT_DBusObjectPath, AccessPoint*>::Iterator it = aps.begin(); it != aps.end(); ++it)
		{
			AccessPoint* p = it.data();
			delete p;
		}
	}

	DBus::WirelessDeviceProxy nmWireless;
	TQMap<TQT_DBusObjectPath, AccessPoint *> aps;
};

WirelessDevice::WirelessDevice (const TQString & obj_path)
	: Device(obj_path)
{
	d = new WirelessDevicePrivate(NM_DBUS_SERVICE, obj_path);
	d->nmWireless.setConnection(TQT_DBusConnection::systemBus());

	// get notified when the devices properties change
	connect(&(d->nmWireless), TQT_SIGNAL(PropertiesChanged(const TQMap<TQString, TQT_DBusVariant>&)), this, TQT_SLOT(slotPropertiesChanged(const TQMap<TQString, TQT_DBusVariant>&)));
	connect(&(d->nmWireless), TQT_SIGNAL(AccessPointAdded(const TQT_DBusObjectPath&)), this, TQT_SLOT(slotAccessPointAdded(const TQT_DBusObjectPath&)));
	connect(&(d->nmWireless), TQT_SIGNAL(AccessPointRemoved(const TQT_DBusObjectPath&)), this, TQT_SLOT(slotAccessPointRemoved(const TQT_DBusObjectPath&)));
}

WirelessDevice::~WirelessDevice ()
{
	delete d;
}

void WirelessDevice::slotPropertiesChanged(const TQMap<TQString, TQT_DBusVariant>& properties)
{
	// TODO: optimization: emit different signals for each property
	emit propertiesChanged();
}

void WirelessDevice::slotAccessPointAdded(const TQT_DBusObjectPath& obj_path)
{
	AccessPoint* ap = 0;

	if ( !d->aps.contains(obj_path)) {
		// we do not know this AP yet, add it to the list
		ap = new AccessPoint(obj_path, this, "access_point_object");
		d->aps.insert(obj_path, ap);
	}
	else
		ap = d->aps[obj_path];

	// notify about the new accesspoint
	emit accessPointAdded(ap);
}

void WirelessDevice::slotAccessPointRemoved(const TQT_DBusObjectPath& obj_path)
{
	AccessPoint* ap = 0;

	if ( d->aps.contains(obj_path)) {
		ap = d->aps[obj_path];

		// notify about the AP removal
		emit accessPointRemoved(obj_path);

		// remove the appropriate AP from our list
		d->aps.remove(obj_path);
		delete ap;
	}
	else
	{
		// nothing we can do here
		kdDebug() << k_funcinfo << "NM requests removal of unknown AP " << obj_path << endl;
	}
}

TQValueList<AccessPoint*> WirelessDevice::accessPoints()
{
	// update our AP list
	if (d->aps.isEmpty()) {
		updateAPList();
	}

	return d->aps.values();
}

void WirelessDevice::updateAPList()
{
	TQT_DBusError err;
	TQValueList<TQT_DBusObjectPath> aps;

	// get the APs from NM
	if (d->nmWireless.GetAccessPoints(aps, err))
	{
        // for each AP
		for (TQValueList<TQT_DBusObjectPath>::iterator it = aps.begin(); it != aps.end(); ++it)
		{
			// create an AP-object for each objectpath
			if (d->aps.contains(*it)) {
				continue;
			}
			AccessPoint* ap = new AccessPoint(*it, this, "access_point_object");
			d->aps.insert(*it, ap);
		}
	}
	else
		kdWarning() << k_funcinfo << "Could not get a list of wireless accesspoints over DBus." << endl;

}

TQValueList<AccessPoint*> WirelessDevice::accessPointsForEssid( TQByteArray essid)
{
	TQValueList<AccessPoint*> aps;
	// the DBus proxy is shared
	for (TQMap<TQT_DBusObjectPath, AccessPoint*>::Iterator it = d->aps.begin(); it != d->aps.end(); ++it)
	{
		AccessPoint * ap = it.data();
		if (essid.isNull() || (ap && ap->getSsidByteArray() == essid))
			aps.append(ap);
	}
	return aps;
}

AccessPoint * WirelessDevice::getActiveAccessPoint()
{
	TQT_DBusError err;
	TQT_DBusObjectPath obj_path;

	AccessPoint * ap = 0;
	//fixme, listen to signal and use cached value
	obj_path = d->nmWireless.getActiveAccessPoint(err);
	if (!obj_path.isEmpty()) {
		if (d->aps.contains(obj_path)) {
			ap = d->aps[obj_path];
		} else {
			kdWarning() << k_funcinfo << "No object for active access point found!" << endl;
		}
	}
	return ap;
}

TQ_UINT32 WirelessDevice::getWirelessCapabilities() const
{
	TQT_DBusError err;
	return d->nmWireless.getWirelessCapabilities(err);
}

TQ_UINT32 WirelessDevice::getBitrate() const
{
	TQT_DBusError err;
	return d->nmWireless.getBitrate(err);
}

#include "knetworkmanager-wireless_device.moc"