summaryrefslogtreecommitdiffstats
path: root/knetworkmanager-0.8/src/knetworkmanager-connection_setting_wireless.cpp
blob: a56ed12657055744fa89e0358373c6880782a30a (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_dbus.cpp - A NetworkManager frontend for KDE
 *
 * Copyright (C) 2005, 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
 *
 **************************************************************************/

/* qt headers */
#include <tqhostaddress.h>
#include <tqvariant.h>
#include <tqregexp.h>

/* kde headers */
#include <kdebug.h>
#include <tdelocale.h>

/* TQT_DBus headers*/
#include <tqdbusdata.h>
#include <tqdbusdatamap.h>

/* knetworkmanager headers */
#include "knetworkmanager.h"
#include "knetworkmanager-connection_setting_wireless.h"
#include "knetworkmanager-connection_setting_wireless_security.h"
#include "knetworkmanager-accesspoint.h"

// the bssid should look like XX:XX:XX:XX:XX:XX where X is a hexadecimal digit
#define MAC_ADDRESS_PATTERN "[0-9A-Fa-f]{2}:[0-9A-F]{2}:[0-9A-F]{2}:[0-9A-F]{2}:[0-9A-F]{2}:[0-9A-F]{2}"

using namespace ConnectionSettings;

/*
	class Wireless
*/
Wireless::Wireless(Connection* conn, ::AccessPoint* ap, WirelessSecurity* security)
	: ConnectionSetting(conn, NM_SETTING_WIRELESS_SETTING_NAME)
{
	_security = TQString();

	if (ap)
	{
		_essid = ap->getSsidByteArray();
		_mode = ap->getMode() == 0 ? MODE_ADHOC : MODE_INFRASTRUCTURE;
	}
	else
	{
		_essid = TQByteArray();
		_mode = MODE_INFRASTRUCTURE;
	}

	_modeMap[MODE_INFRASTRUCTURE] = "infrastructure";
	_modeMap[MODE_ADHOC] = "adhoc";
}

SettingsMap
Wireless::toMap() const
{
	SettingsMap map;

	map.insert(NM_SETTING_WIRELESS_MODE, TQT_DBusData::fromString(_modeMap[_mode]));

	TQValueList<TQT_DBusData> essid;
	for (TQByteArray::ConstIterator it = _essid.begin(); it != _essid.end(); ++it)
		essid.append(TQT_DBusData::fromByte(*it));

	if (essid.size() > 0)
		map.insert(NM_SETTING_WIRELESS_SSID, TQT_DBusData::fromTQValueList(essid));
	else
		kdWarning() << k_funcinfo << " SSID undefined" << endl;

	if (!_security.isEmpty())
		map.insert(NM_SETTING_WIRELESS_SEC, TQT_DBusData::fromString(_security));

	if (!_seenBssids.empty())
	{
		TQValueList<TQT_DBusData> bssids;
		for (TQValueList<TQString>::ConstIterator it = _seenBssids.begin(); it != _seenBssids.end(); ++it)
			bssids.append(TQT_DBusData::fromString(*it));

		map.insert(NM_SETTING_WIRELESS_SEEN_BSSIDS, TQT_DBusData::fromTQValueList(bssids));
	}

	return map;
}

void
Wireless::fromMap(const SettingsMap& map)
{
	SettingsMap::ConstIterator it;

	// Mode
	if ((it = map.find(NM_SETTING_WIRELESS_MODE)) != map.end())
	{
		TQBiDirectionalMap<MODES, TQString>::Iterator mode_it = _modeMap.findData(it.data().toString());
		if (mode_it != _modeMap.end())
			_mode = mode_it.key();
		else
			_mode = MODE_INFRASTRUCTURE;
	}

	// Essid
	if ((it = map.find(NM_SETTING_WIRELESS_SSID)) != map.end())
	{
		TQValueList<TQT_DBusData> dbus_essid = (*it).toTQValueList();
		TQByteArray essid(dbus_essid.size());

		int index = 0;
		for (TQValueList<TQT_DBusData>::ConstIterator byte_it = dbus_essid.begin(); byte_it != dbus_essid.end(); ++byte_it)
		{
			essid[index] = (*byte_it).toByte();
			index++;
		}
		_essid = essid;
	}

	if ((it = map.find(NM_SETTING_WIRELESS_SEC)) != map.end())
	{
		_security = (*it).toString();
	}

	// Seen BSSIDS
	if ((it = map.find(NM_SETTING_WIRELESS_SEEN_BSSIDS)) != map.end())
	{
		TQRegExp exp(MAC_ADDRESS_PATTERN);
		TQValueList<TQT_DBusData> bssids = (*it).toTQValueList();
		_seenBssids.clear();

		for(TQValueList<TQT_DBusData>::Iterator it = bssids.begin(); it != bssids.end(); ++it)
		{
			TQString bssid = (*it).toString();
			if (exp.exactMatch(bssid))
				_seenBssids.append(bssid);
		}
	}
}

void
Wireless::setEssid(const TQByteArray& essid)
{
	_essid = essid;
	emitValidityChanged();
}

TQByteArray
Wireless::getEssid(void) const
{
	return _essid;
}

void
Wireless::setMode(MODES mode)
{
	_mode = mode;
	emitValidityChanged();
}

Wireless::MODES
Wireless::getMode(void) const
{
	return _mode;
}

void
Wireless::setSecurity(const TQString& security)
{
	_security = security;
	emitValidityChanged();
}

TQString
Wireless::getSecurity(void) const
{
	return _security;
}

bool
Wireless::isValid() const
{
	// ESSID is essential
	if (_essid.isEmpty())
		return false;

	return true;
}

void
Wireless::addSeenBssid(const TQString& bssid)
{
	TQRegExp exp(MAC_ADDRESS_PATTERN);
	if (!exp.exactMatch(bssid))
		return;

	// no duplicates please
	for(TQValueList<TQString>::Iterator it = _seenBssids.begin(); it != _seenBssids.end(); ++it)
	{
		if ((*it) == bssid)
			return;
	}
	
	// insert this bssid
	_seenBssids.append(bssid);
	emitValidityChanged();
}