~ubuntu-branches/ubuntu/trusty/knetworkmanager/trusty

« back to all changes in this revision

Viewing changes to knetworkmanager-0.7/src/knetworkmanager-connection_setting_wireless.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Jonathan Riddell
  • Date: 2008-09-26 12:40:26 UTC
  • mfrom: (1.2.7 upstream)
  • Revision ID: james.westby@ubuntu.com-20080926124026-t5fr920l4lf2l6hz
Tags: 1:0.7svn864988-0ubuntu1
New upstream snapshot, now works with current NM API, 
closes LP: #259278

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/***************************************************************************
 
2
 *
 
3
 * knetworkmanager-devicestore_dbus.cpp - A NetworkManager frontend for KDE
 
4
 *
 
5
 * Copyright (C) 2005, 2006 Novell, Inc.
 
6
 *
 
7
 * Author: Helmut Schaa <hschaa@suse.de>, <helmut.schaa@gmx.de>
 
8
 *
 
9
 * This program is free software; you can redistribute it and/or modify
 
10
 * it under the terms of the GNU General Public License as published by
 
11
 * the Free Software Foundation; either version 2 of the License, or
 
12
 * (at your option) any later version.
 
13
 *
 
14
 * This program is distributed in the hope that it will be useful,
 
15
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
16
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
17
 * GNU General Public License for more details.
 
18
 * 
 
19
 * You should have received a copy of the GNU General Public License
 
20
 * along with this program; if not, write to the Free Software
 
21
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 
22
 *
 
23
 **************************************************************************/
 
24
 
 
25
/* qt headers */
 
26
#include <qhostaddress.h>
 
27
#include <qvariant.h>
 
28
#include <qregexp.h>
 
29
 
 
30
/* kde headers */
 
31
#include <kdebug.h>
 
32
#include <klocale.h>
 
33
 
 
34
/* QDBus headers*/
 
35
#include <dbus/qdbusdata.h>
 
36
#include <dbus/qdbusdatamap.h>
 
37
 
 
38
/* knetworkmanager headers */
 
39
#include "knetworkmanager.h"
 
40
#include "knetworkmanager-connection_setting_wireless.h"
 
41
#include "knetworkmanager-connection_setting_wireless_security.h"
 
42
#include "knetworkmanager-accesspoint.h"
 
43
 
 
44
// the bssid should look like XX:XX:XX:XX:XX:XX where X is a hexadecimal digit
 
45
#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}"
 
46
 
 
47
using namespace ConnectionSettings;
 
48
 
 
49
/*
 
50
        class Wireless
 
51
*/
 
52
Wireless::Wireless(Connection* conn, ::AccessPoint* ap, WirelessSecurity* security)
 
53
        : ConnectionSetting(conn, NM_SETTING_WIRELESS_SETTING_NAME)
 
54
{
 
55
        _security = QString::null;
 
56
 
 
57
        if (ap)
 
58
        {
 
59
                _essid = ap->getSsidByteArray();
 
60
                _mode = ap->getMode() == 0 ? MODE_ADHOC : MODE_INFRASTRUCTURE;
 
61
        }
 
62
        else
 
63
        {
 
64
                _essid = QByteArray();
 
65
                _mode = MODE_INFRASTRUCTURE;
 
66
        }
 
67
 
 
68
        _modeMap[MODE_INFRASTRUCTURE] = "infrastructure";
 
69
        _modeMap[MODE_ADHOC] = "adhoc";
 
70
}
 
71
 
 
72
SettingsMap
 
73
Wireless::toMap() const
 
74
{
 
75
        SettingsMap map;
 
76
 
 
77
        map.insert(NM_SETTING_WIRELESS_MODE, QDBusData::fromString(_modeMap[_mode]));
 
78
 
 
79
        QValueList<QDBusData> essid;
 
80
        for (QByteArray::ConstIterator it = _essid.begin(); it != _essid.end(); ++it)
 
81
                essid.append(QDBusData::fromByte(*it));
 
82
 
 
83
        if (essid.size() > 0)
 
84
                map.insert(NM_SETTING_WIRELESS_SSID, QDBusData::fromQValueList(essid));
 
85
        else
 
86
                kdWarning() << k_funcinfo << " SSID undefined" << endl;
 
87
 
 
88
        if (!_security.isEmpty())
 
89
                map.insert(NM_SETTING_WIRELESS_SEC, QDBusData::fromString(_security));
 
90
 
 
91
        if (!_seenBssids.empty())
 
92
        {
 
93
                QValueList<QDBusData> bssids;
 
94
                for (QValueList<QString>::ConstIterator it = _seenBssids.begin(); it != _seenBssids.end(); ++it)
 
95
                        bssids.append(QDBusData::fromString(*it));
 
96
 
 
97
                map.insert(NM_SETTING_WIRELESS_SEEN_BSSIDS, QDBusData::fromQValueList(bssids));
 
98
        }
 
99
 
 
100
        return map;
 
101
}
 
102
 
 
103
void
 
104
Wireless::fromMap(const SettingsMap& map)
 
105
{
 
106
        SettingsMap::ConstIterator it;
 
107
 
 
108
        // Mode
 
109
        if ((it = map.find(NM_SETTING_WIRELESS_MODE)) != map.end())
 
110
        {
 
111
                QBiDirectionalMap<MODES, QString>::Iterator mode_it = _modeMap.findData(it.data().toString());
 
112
                if (mode_it != _modeMap.end())
 
113
                        _mode = mode_it.key();
 
114
                else
 
115
                        _mode = MODE_INFRASTRUCTURE;
 
116
        }
 
117
 
 
118
        // Essid
 
119
        if ((it = map.find(NM_SETTING_WIRELESS_SSID)) != map.end())
 
120
        {
 
121
                QValueList<QDBusData> dbus_essid = (*it).toQValueList();
 
122
                QByteArray essid(dbus_essid.size());
 
123
 
 
124
                int index = 0;
 
125
                for (QValueList<QDBusData>::ConstIterator byte_it = dbus_essid.begin(); byte_it != dbus_essid.end(); ++byte_it)
 
126
                {
 
127
                        essid[index] = (*byte_it).toByte();
 
128
                        index++;
 
129
                }
 
130
                _essid = essid;
 
131
        }
 
132
 
 
133
        if ((it = map.find(NM_SETTING_WIRELESS_SEC)) != map.end())
 
134
        {
 
135
                _security = (*it).toString();
 
136
        }
 
137
 
 
138
        // Seen BSSIDS
 
139
        if ((it = map.find(NM_SETTING_WIRELESS_SEEN_BSSIDS)) != map.end())
 
140
        {
 
141
                QRegExp exp(MAC_ADDRESS_PATTERN);
 
142
                QValueList<QDBusData> bssids = (*it).toQValueList();
 
143
                _seenBssids.clear();
 
144
 
 
145
                for(QValueList<QDBusData>::Iterator it = bssids.begin(); it != bssids.end(); ++it)
 
146
                {
 
147
                        QString bssid = (*it).toString();
 
148
                        if (exp.exactMatch(bssid))
 
149
                                _seenBssids.append(bssid);
 
150
                }
 
151
        }
 
152
}
 
153
 
 
154
void
 
155
Wireless::setEssid(const QByteArray& essid)
 
156
{
 
157
        _essid = essid;
 
158
        emitValidityChanged();
 
159
}
 
160
 
 
161
QByteArray
 
162
Wireless::getEssid(void) const
 
163
{
 
164
        return _essid;
 
165
}
 
166
 
 
167
void
 
168
Wireless::setMode(MODES mode)
 
169
{
 
170
        _mode = mode;
 
171
        emitValidityChanged();
 
172
}
 
173
 
 
174
Wireless::MODES
 
175
Wireless::getMode(void) const
 
176
{
 
177
        return _mode;
 
178
}
 
179
 
 
180
void
 
181
Wireless::setSecurity(const QString& security)
 
182
{
 
183
        _security = security;
 
184
        emitValidityChanged();
 
185
}
 
186
 
 
187
QString
 
188
Wireless::getSecurity(void) const
 
189
{
 
190
        return _security;
 
191
}
 
192
 
 
193
bool
 
194
Wireless::isValid() const
 
195
{
 
196
        // ESSID is essential
 
197
        if (_essid.isEmpty())
 
198
                return false;
 
199
 
 
200
        return true;
 
201
}
 
202
 
 
203
void
 
204
Wireless::addSeenBssid(const QString& bssid)
 
205
{
 
206
        QRegExp exp(MAC_ADDRESS_PATTERN);
 
207
        if (!exp.exactMatch(bssid))
 
208
                return;
 
209
 
 
210
        // no duplicates please
 
211
        for(QValueList<QString>::Iterator it = _seenBssids.begin(); it != _seenBssids.end(); ++it)
 
212
        {
 
213
                if ((*it) == bssid)
 
214
                        return;
 
215
        }
 
216
        
 
217
        // insert this bssid
 
218
        _seenBssids.append(bssid);
 
219
        emitValidityChanged();
 
220
}