~ubuntu-branches/debian/sid/knetworkmanager/sid

« back to all changes in this revision

Viewing changes to src/knetworkmanager-network.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Anthony Mercatante
  • Date: 2006-09-28 11:47:00 UTC
  • Revision ID: james.westby@ubuntu.com-20060928114700-fbiktmk2woj1jww4
Tags: upstream-0.1
ImportĀ upstreamĀ versionĀ 0.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/***************************************************************************
2
 
 *
3
 
 * knetworkmanager-network.cpp - A NetworkManager frontend for KDE
4
 
 *
5
 
 * Copyright (C) 2005, 2006 Novell, Inc.
6
 
 *
7
 
 * Author: Timo Hoenig        <thoenig@suse.de>, <thoenig@nouse.net>
8
 
 *         Will Stephenson    <wstephenson@suse.de>, <wstephenson@kde.org>
9
 
 *         Valentine Sinitsyn <e_val@inbox.ru>
10
 
 *
11
 
 * This program is free software; you can redistribute it and/or modify
12
 
 * it under the terms of the GNU General Public License as published by
13
 
 * the Free Software Foundation; either version 2 of the License, or
14
 
 * (at your option) any later version.
15
 
 *
16
 
 * This program is distributed in the hope that it will be useful,
17
 
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18
 
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19
 
 * GNU General Public License for more details.
20
 
 * 
21
 
 * You should have received a copy of the GNU General Public License
22
 
 * along with this program; if not, write to the Free Software
23
 
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
24
 
 *
25
 
 **************************************************************************/
26
 
 
27
 
#include "knetworkmanager-network.h"
28
 
#include "knetworkmanager-encryption.h"
29
 
 
30
 
#include <kconfigbase.h>
31
 
#include <kdebug.h>
32
 
 
33
 
void
34
 
Network::setObjectPath (const QString & obj_path)
35
 
{
36
 
        _obj_path = obj_path;
37
 
}
38
 
 
39
 
QString
40
 
Network::getObjectPath () const
41
 
{
42
 
        return _obj_path;
43
 
}
44
 
 
45
 
void
46
 
Network::setEssid (const QString & essid)
47
 
{
48
 
        _dirty |= (_essid != essid);
49
 
        _essid = essid;
50
 
}
51
 
 
52
 
QString
53
 
Network::getEssid () const
54
 
{
55
 
        return _essid;
56
 
}
57
 
 
58
 
void
59
 
Network::insertHardwareAddress (const QString & hw_address)
60
 
{
61
 
        if ( ( hw_address != "00:00:00:00:00:00" )  && ( _hw_addresses.find( hw_address ) == _hw_addresses.end() ) ) {
62
 
                _hw_addresses.append( hw_address );
63
 
                _dirty = true;
64
 
        }
65
 
}
66
 
 
67
 
void
68
 
Network::removeHardwareAddress (const QString & hw_address)
69
 
{
70
 
        QStringList::Iterator it = _hw_addresses.find( hw_address );
71
 
        if ( it != _hw_addresses.end() ) {
72
 
                _hw_addresses.remove( it );
73
 
                _dirty = true;
74
 
        }
75
 
}
76
 
 
77
 
QStringList
78
 
Network::getHardwareAddresses () const
79
 
{
80
 
        return _hw_addresses;
81
 
}
82
 
 
83
 
void
84
 
Network::setHardwareAddresses( const QStringList & addresses )
85
 
{
86
 
        _dirty |= (_hw_addresses != addresses);
87
 
        _hw_addresses = addresses;
88
 
}
89
 
 
90
 
void
91
 
Network::setStrength (int strength)
92
 
{
93
 
        _strength = strength;
94
 
}
95
 
 
96
 
int
97
 
Network::getStrength () const
98
 
{
99
 
        return _strength;
100
 
}
101
 
 
102
 
void
103
 
Network::setFrequency (double frequency)
104
 
{
105
 
        _frequency = frequency;
106
 
}
107
 
 
108
 
double
109
 
Network::getFrequency () const
110
 
{
111
 
        return _frequency;
112
 
}
113
 
 
114
 
void
115
 
Network::setRate (int rate)
116
 
{
117
 
        _rate = rate;
118
 
}
119
 
 
120
 
int
121
 
Network::getRate () const
122
 
{
123
 
        return _rate;
124
 
}
125
 
 
126
 
void
127
 
Network::setMode (int mode)
128
 
{
129
 
        _mode = mode;
130
 
}
131
 
 
132
 
int
133
 
Network::getMode () const
134
 
{
135
 
        return _mode;
136
 
}
137
 
 
138
 
void
139
 
Network::setCapabilities (int capabilties)
140
 
{
141
 
        _capabilities = capabilties;
142
 
}
143
 
 
144
 
int
145
 
Network::getCapabilities () const
146
 
{
147
 
        return _capabilities;
148
 
}
149
 
 
150
 
void
151
 
Network::setActive (bool active)
152
 
{
153
 
        _active = active;
154
 
}
155
 
 
156
 
bool
157
 
Network::isActive () const
158
 
{
159
 
        return _active;
160
 
}
161
 
 
162
 
Encryption*
163
 
Network::getEncryption (void) const
164
 
{
165
 
        return _encryption;
166
 
}
167
 
 
168
 
void
169
 
Network::setEncryption (Encryption* encryption)
170
 
{
171
 
        _encryption = encryption;
172
 
        _dirty = true;
173
 
        if ( _encryption )
174
 
                _encryption->setNetwork(this);
175
 
}
176
 
 
177
 
QStringList
178
 
Network::getEncryptionProtocol () const
179
 
{
180
 
        QStringList encryptionProtocol;
181
 
 
182
 
        /* string freeze
183
 
        if (_capabilities & NM_802_11_CAP_PROTO_NONE)
184
 
                encryptionProtocol.append (i18n ("None"));
185
 
        */
186
 
        if (_capabilities & NM_802_11_CAP_PROTO_WEP)
187
 
                encryptionProtocol.append ("WEP");
188
 
        if (_capabilities & NM_802_11_CAP_PROTO_WPA)
189
 
                encryptionProtocol.append ("WPA");
190
 
        if (_capabilities & NM_802_11_CAP_PROTO_WPA2)
191
 
                encryptionProtocol.append ("WPA2");
192
 
 
193
 
        return encryptionProtocol;
194
 
}
195
 
 
196
 
bool
197
 
Network::isEncrypted () const
198
 
{
199
 
        if (_capabilities & NM_802_11_CAP_PROTO_NONE) {
200
 
                return false;
201
 
        } else {
202
 
                return true;
203
 
        }
204
 
}
205
 
 
206
 
bool Network::isTrusted() const
207
 
{
208
 
        return _trusted;
209
 
}
210
 
 
211
 
void Network::setTrusted( bool trusted )
212
 
{
213
 
        _dirty |= (_trusted != trusted);
214
 
        _trusted = trusted;
215
 
}
216
 
 
217
 
bool Network::isHidden () const
218
 
{
219
 
        return _hidden;
220
 
}
221
 
 
222
 
void Network::setHidden (bool hidden)
223
 
{
224
 
        _dirty |= (_hidden != hidden);
225
 
        _hidden = hidden;
226
 
}
227
 
 
228
 
bool
229
 
Network::operator == (Network net)
230
 
{
231
 
        return (this->getObjectPath () == net.getObjectPath ());
232
 
}
233
 
 
234
 
bool
235
 
Network::isModified (void) const
236
 
{
237
 
        /* 
238
 
         * Since Encryption objects are persisted on par with Network, we need
239
 
         * to mark Network dirty if it was modified itself or it contains modified Encryption
240
 
         */
241
 
        return ( _dirty || _encryption->isModified() );
242
 
}
243
 
 
244
 
Network::Network (const QString & obj_path): _encryption (0), _obj_path (obj_path), _essid (""), _hw_addresses (),
245
 
                  _strength (0), _frequency (0), _rate (0), _passphrase (""), _active( false ), _mode (0),
246
 
                  _capabilities( NM_DEVICE_CAP_NONE ), _trusted( true )
247
 
{
248
 
        _encryption = new EncryptionNone ();
249
 
        _encryption->setNetwork(this);
250
 
        _dirty = true;
251
 
}
252
 
 
253
 
Network::Network () : _encryption (0), _obj_path (""), _essid (""), _hw_addresses (), _strength (0), _frequency (0),
254
 
                      _rate (0), _passphrase (""), _active( false ), _mode (0), _capabilities( NM_DEVICE_CAP_NONE ),
255
 
                      _trusted( true )
256
 
{
257
 
        _encryption = new EncryptionNone ();
258
 
        _encryption->setNetwork(this);
259
 
        _dirty = true;
260
 
}
261
 
 
262
 
Network::~Network ()
263
 
{
264
 
        delete _encryption;
265
 
}
266
 
 
267
 
QDateTime Network::getTimestamp( void ) const
268
 
{
269
 
        return _timeStamp;
270
 
}
271
 
 
272
 
void Network::setTimestamp( const QDateTime & dt )
273
 
{
274
 
        _timeStamp = dt;
275
 
}
276
 
 
277
 
void Network::persist( KConfigBase * cfg, bool updateTimestamp, bool withKey ) const
278
 
{
279
 
        // write network settings
280
 
        // ESSID
281
 
        cfg->writeEntry( "ESSID", getEssid() );
282
 
        // trusted
283
 
        cfg->writeEntry( "Trusted", _trusted );
284
 
        // hardware addresses
285
 
        //if ( !_hw_addresses.isEmpty() )
286
 
        cfg->writeEntry( "HardwareAddresses", _hw_addresses );
287
 
 
288
 
        if ( updateTimestamp )
289
 
                persistTimestamp( cfg );
290
 
 
291
 
        if ( _encryption && _encryption->isModified( ) )
292
 
                _encryption->persist( cfg, withKey );
293
 
 
294
 
        _dirty = false; 
295
 
}
296
 
 
297
 
void Network::restore( const KConfigBase * cfg, bool withKey )
298
 
{
299
 
        setEssid( cfg->readEntry( "ESSID", QString::null ) );
300
 
        //setEncryption( 0 ); /* Memory leak here */
301
 
        QDateTime defaultTime;
302
 
        defaultTime.setTime_t( 1 ); // invalid time
303
 
        setTimestamp( cfg->readDateTimeEntry( "Timestamp", &defaultTime) );
304
 
        kdDebug() << "restore read timestamp " << _timeStamp << endl;
305
 
        _hw_addresses = cfg->readListEntry( "HardwareAddresses" );
306
 
        QString encryption = cfg->readEntry( "Encryption", "none" );
307
 
        _trusted = cfg->readBoolEntry( "Trusted", false );
308
 
        if ( _encryption ) {
309
 
                delete _encryption;
310
 
                _encryption = 0;
311
 
        }
312
 
        if ( "WPA" == encryption )
313
 
                _encryption = new EncryptionWPAPersonal ( );
314
 
        else if ( "WPA-EAP" == encryption )
315
 
                _encryption = new EncryptionWPAEnterprise ( );
316
 
        else if ( "WEP" == encryption )
317
 
                _encryption = new EncryptionWEP( WEP_ASCII ); // actual type is not important, restore overwrites it
318
 
        else if ( "none" == encryption )
319
 
                _encryption = new EncryptionNone();
320
 
        _encryption->setNetwork(this);
321
 
        _encryption->restore( cfg, withKey );
322
 
        _dirty = false;
323
 
}
324
 
 
325
 
void Network::persistTimestamp( KConfigBase *cfg ) const
326
 
{
327
 
        QDateTime current = QDateTime::currentDateTime( );
328
 
        cfg->writeEntry( "Timestamp", current );
329
 
}