summaryrefslogtreecommitdiffstats
path: root/wifi/kcmwifi/wificonfig.cpp
blob: 7a1cdac3bb1bd13352b78cb0ea58101a6eb75b74 (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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
/*
  Copyright (C) 2004 Nadeem Hasan <nhasan@kde.org>

  This library 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
  Library General Public License for more details.

  You should have received a copy of the GNU General Public License
  along with this library; see the file COPYING.  If not, write to
  the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
  Boston, MA 02110-1301, USA.
*/

#include <tqfile.h>

#include <ksimpleconfig.h>
#include <kglobal.h>
#include <kdebug.h>
#include <kprocio.h>
#include <kmessagebox.h>
#include <klocale.h>

#include "wificonfig.h"

static TQStringList speedList;
static TQStringList wifiModeList;
static TQStringList cryptoModeList;
static TQStringList powerModeList;

Key::Key()
{
}

Key::Key( const TQString &key )
{
  setKey( key );
}

TQString Key::rawKey() const
{
  TQString s = m_key;

  if ( isValid( m_key ) >= STRING_64 )
    s = "s:" + s;

  return s;
}

void Key::setKey( const TQString &key )
{
    m_key = key;
}

KeyStates Key::isValid ( TQString keyCandidate )
{ 
  if (keyCandidate.isEmpty()) {
	kdDebug() << "Ignoring key: empty.\n";
	return EMPTY; 
  }

  if (keyCandidate.length() == 5)
	return STRING_64;

  if (keyCandidate.length() == 10)
	return HEX_64;

  if (keyCandidate.length() == 13)
	return STRING_128;

  if (keyCandidate.length() == 26)
	return HEX_128;

  if (keyCandidate.length() == 32)
	return STRING_256;

  if (keyCandidate.length() == 64)
	return HEX_256;
  
  return INVALID;
}

IfConfig::IfConfig()
{

  // initialise all config items...

  m_networkName = "";
  m_interface = "";
  m_wifiMode = Managed;
  m_speed = AUTO;
  m_runScript = false;
  m_connectScript = "";

  m_useCrypto = false;
  m_cryptoMode = Open;
  m_activeKey = 1;
  for (int i=0; i<4; i++)
    m_keys[ i ].setKey( "" );

  m_pmEnabled = false;
  m_pmMode = AllPackets;
  m_sleepTimeout = 1;
  m_wakeupPeriod = 1;

  speedList << "Auto" << "1M" << "2M" << "5.5M" << "6M" << "9M" << "11M" << "12M" << "18M" << "24M" << "36M" << "48M" << "54M";
  wifiModeList << "Ad-Hoc" << "Managed" << "Repeater"
               << "Master" << "Secondary";
  cryptoModeList << "Open" << "Restricted";
  powerModeList << "All" << "UnicastOnly" << "MulticastOnly";
}

void IfConfig::load( TDEConfig *config, int i )
{
  TQString entry;

  TQString group = TQString( "Configuration %1" ).arg( i+1 );
  config->setGroup( group );

  m_networkName = config->readEntry( "NetworkName" );
  m_interface = config->readEntry( "InterfaceName" );

  entry = config->readEntry( "WifiMode", "Managed" );
  wifimodeFromString( entry );
  entry =  config->readEntry( "Speed", "Auto" );
  speedFromString( entry );
  m_runScript = config->readBoolEntry( "RunScript", false );
  m_connectScript = config->readEntry( "ScriptName" );

  m_useCrypto = config->readBoolEntry( "UseCrypto", false );
  entry = config->readEntry( "CryptoMode", "Open" );
  cryptomodeFromString( entry );
  m_activeKey = config->readNumEntry( "ActiveKey", 1 );
  m_keys[ 0 ].setKey( config->readEntry( "Key1" ) );
  m_keys[ 1 ].setKey( config->readEntry( "Key2" ) );
  m_keys[ 2 ].setKey( config->readEntry( "Key3" ) );
  m_keys[ 3 ].setKey( config->readEntry( "Key4" ) );

  m_pmEnabled = config->readBoolEntry( "PMEnabled", false );
  entry = config->readEntry( "PMMode", "All" );
  powermodeFromString( entry );
  m_sleepTimeout = config->readNumEntry( "SleepTimeout", 30 );
  m_wakeupPeriod = config->readNumEntry( "WakeupPeriod", 20 );
}

void IfConfig::save( TDEConfig *config, int i )
{
  TQString group = TQString( "Configuration %1" ).arg( i+1 );
  config->setGroup( group );

  config->writeEntry( "NetworkName", m_networkName );
  config->writeEntry( "InterfaceName", m_interface );
  config->writeEntry( "WifiMode", wifimodeAsString() );
  config->writeEntry( "Speed", speedAsString() );
  config->writeEntry( "RunScript", m_runScript );
  config->writeEntry( "ScriptName", m_connectScript );

  config->writeEntry( "UseCrypto", m_useCrypto );
  config->writeEntry( "CryptoMode", cryptomodeAsString() );
  config->writeEntry( "ActiveKey", m_activeKey );
  config->writeEntry( "Key1", m_keys[ 0 ].key() );
  config->writeEntry( "Key2", m_keys[ 1 ].key() );
  config->writeEntry( "Key3", m_keys[ 2 ].key() );
  config->writeEntry( "Key4", m_keys[ 3 ].key() );

  config->writeEntry( "PMEnabled", m_pmEnabled );
  config->writeEntry( "PMMode", powermodeAsString() );
  config->writeEntry( "SleepTimeout", m_sleepTimeout );
  config->writeEntry( "WakeupPeriod", m_wakeupPeriod );
}

WifiConfig *WifiConfig::m_instance = 0;

WifiConfig *WifiConfig::instance()
{
  if ( m_instance == 0 )
    m_instance = new WifiConfig();

  return m_instance;
}

WifiConfig::WifiConfig()
{
  //m_config = TDEGlobal::config();
  m_config = new KSimpleConfig( "kcmwifirc" );

  load();
}

WifiConfig::~WifiConfig()
{
		delete m_config;
}

void WifiConfig::load()
{
  m_config->setGroup( "General" );

  m_usePreset = m_config->readBoolEntry( "UsePreset", false );
  m_presetConfig = m_config->readNumEntry( "PresetConfig", 1 )-1;
  m_numConfigs = m_config->readNumEntry( "NumberConfigs", 4 );

  for ( int i=0; i<m_numConfigs; ++i )
  {
    m_ifConfig[ i ].load( m_config, i );
  }
}

void WifiConfig::save()
{
  m_config->setGroup( "General" );

  m_config->writeEntry( "UsePreset", m_usePreset );
  m_config->writeEntry( "PresetConfig", m_presetConfig+1 );
  m_config->writeEntry( "NumberConfigs", m_numConfigs );

  for ( int i=0; i<m_numConfigs; ++i )
  {
    m_ifConfig[ i ].save( m_config, i );
  }

  m_config->sync();
}

TQString WifiConfig::autoDetectInterface()
{
  m_detectedInterface.truncate( 0 );

  TQFile procFile ( "/proc/net/dev" );

  if ( !procFile.open( IO_ReadOnly ) )
  {
//     KMessageBox::sorry( 0,
//         i18n( "Unable to autodetect wireless interface." ) );
    return m_detectedInterface;
  }

  TQStringList list;
  TQString line;

  while ( !procFile.atEnd() )
  {
    procFile.readLine( line, 9999 );
    if ( line.find( ":" ) > 0 )
    {
      line.truncate ( line.find( ":" ) );
      list.append( line.stripWhiteSpace() );
    }
  }

  procFile.close();

  if ( list.empty() )
  {
//     KMessageBox::sorry( 0,
//         i18n( "Unable to autodetect wireless interface." ) );
    return m_detectedInterface;
  }

  for ( TQStringList::Iterator it = list.begin (); it != list.end (); ++it )
  {
    if ( ((*it).contains("wifi"))==0 ) { // if the name is wifiX, ignore
      KProcIO test;
      test << "iwconfig";
      test << *it;
      connect( &test, TQT_SIGNAL( readReady( KProcIO * ) ),
               this, TQT_SLOT( slotTestInterface( KProcIO * ) ) );
      test.start ( TDEProcess::Block );
    }
  }

  if ( m_detectedInterface.isEmpty() )
  {
//     KMessageBox::sorry( 0,
//         i18n( "Unable to autodetect wireless interface." ) );
    return m_detectedInterface;
  }

  return m_detectedInterface;
}

void WifiConfig::slotTestInterface( KProcIO *proc )
{
  TQString output;
  proc->readln( output );
  if ( output.find ( "no wireless extensions" ) == -1 )
  {
    output.truncate( output.find ( " " ) );
    m_detectedInterface = output.stripWhiteSpace();
    printf("[kcontrol wificonfig] Wireless extensions not available on specified interface\n\r");
  }
}

TQString IfConfig::speedAsString()
{
  return speedList[ m_speed ];
}

TQString IfConfig::wifimodeAsString()
{
  return wifiModeList[ m_wifiMode ];
}

TQString IfConfig::cryptomodeAsString()
{
  return cryptoModeList[ m_cryptoMode ];
}

TQString IfConfig::powermodeAsString()
{
  return powerModeList[ m_pmMode ];
}

int IfConfig::activeKeyId()
{
  return m_activeKey;
}

Key IfConfig::activeKey()
{
  return m_keys[ m_activeKey-1 ];
}

IfConfig::Speed IfConfig::convertToSpeedFromString( const TQString &s )
{
  return ( IfConfig::Speed )speedList.findIndex( s );
}

void IfConfig::speedFromString( const TQString &s )
{
  m_speed = convertToSpeedFromString( s );
}

IfConfig::WifiMode IfConfig::convertToWifiModeFromString( const TQString &s )
{
  return ( IfConfig::WifiMode )wifiModeList.findIndex( s );
}

void IfConfig::wifimodeFromString( const TQString &s )
{
  m_wifiMode = convertToWifiModeFromString( s );
}

IfConfig::CryptoMode IfConfig::convertToCryptoModeFromString( const TQString &s )
{
  return ( IfConfig::CryptoMode )cryptoModeList.findIndex( s );
}

void IfConfig::cryptomodeFromString( const TQString &s )
{
  m_cryptoMode = convertToCryptoModeFromString( s );
}

IfConfig::PowerMode IfConfig::convertToPowerModeFromString( const TQString &s )
{
  return ( IfConfig::PowerMode )powerModeList.findIndex( s );
}

void IfConfig::powermodeFromString( const TQString &s )
{
  m_pmMode = convertToPowerModeFromString( s );
}

#include "wificonfig.moc"