summaryrefslogtreecommitdiffstats
path: root/wifi/kcmwifi/vendorconfig.cpp
blob: 01936667d216af2fb3551581ce84633c9517036e (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
/*
  Copyright (C) 2005 Stefan Winter <swinter@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 <tqdir.h>
#include <tqstringlist.h>
#include <tqbuttongroup.h>
#include <tqradiobutton.h>
#include <tqcombobox.h>
#include <tqcheckbox.h>

#include <kdebug.h>
#include <klineedit.h>
#include <kcombobox.h>

#include "kcmwifi.h"
#include "vendorconfig.h"
#include "ifconfigpage.h"
#include "configcrypto.h"
#include "wificonfig.h"

VendorConfig::VendorConfig ( KCMWifi * caller )
{
  m_caller = caller;
  m_totalConfigs = 0;
}

void
VendorConfig::initAll (  )
{
  initSuSE_92plus (  );
  initDebian (  );
}

void
VendorConfig::initSuSE_92plus (  )
{

  /* configurations are located in /etc/sysconfig/network/ifcfg-wlan*
     there can be more than one if there are multiple wireless interfaces */

  TQDir filelist ( "/etc/sysconfig/network", "ifcfg-wlan*" );
  TQStringList files = filelist.entryList (  );

  if ( files.count() == 0 )
  {
    return;
  }

  for ( TQStringList::Iterator it = files.begin (  ); it != files.end (  ); ++it )
    {
      if ( ( ( *it ).endsWith ( "~" ) ) == false )
	{			// ignore backup files
	  TQFile configfile ( TQString ( "/etc/sysconfig/network/%1" ).arg ( *it ) );
	  configfile.open ( IO_ReadOnly );
	  kdDebug (  ) << "Opened SuSE configuration file " << configfile.name (  ) << "\n";

	  IfConfigPage *newtab = m_caller->addConfigTab ( 1, true );
	  /* this config tab gets automagically stored in an array, the first
	     starting at position vendorBase. We need to construct its power
	     and crypto settings and therefore need the array index, which is
	     vendorBase+m_totalConfigs. It is _essential_ that m_totalConfigs
	     is incremented _after_ successful setup of each given config. */

	  // init crypto entries
	  ConfigCrypto *crypto = new ConfigCrypto ( 0, "ConfigCrypto" );
	  WifiConfig *config = WifiConfig::instance (  );
	  IfConfig & ifconfig = config->m_ifConfig[KCMWifi::vendorBase + m_totalConfigs];

	  TQString buffer;
	  while ( configfile.readLine ( buffer, 255 ) != -1 )
	    {
	      // lines look like WIRELESS_ESSID='bla', so split it into WIRELESS_ESSID and bla
	      TQString var = buffer.section ( '=', 0, 0 );
	      TQString value = buffer.section ( '=', 1, 1 );
	      value = value.mid ( 1, value.length (  ) - 3 );
	      kdDebug (  ) << "Variable " << var << " tqcontains " << value << ".\n";
	      // evaluate the meaningful lines
	      if ( var == "WIRELESS_ESSID" )
		{
		  newtab->le_networkName->setText ( value );
		}
	      else if ( var == "WIRELESS_MODE" )
		{
		  newtab->cmb_wifiMode->setCurrentItem ( IfConfig::convertToWifiModeFromString ( value ) );
		}
	      else if ( var == "WIRELESS_BITRATE" )
		{
		  newtab->cmb_speed->setCurrentItem ( IfConfig::convertToSpeedFromString ( value ) );
		}
	      else if ( var == "WIRELESS_DEFAULT_KEY" )
		{
		  crypto->cmb_activeKey->setCurrentItem ( value.toInt (  ) );
		}
	      else if ( var == "WIRELESS_AUTH_MODE" )
		{
		  if ( value == "shared" || value == "sharedkey" || value == "restricted" )
		    {
		      crypto->rb_restrictMode->setChecked ( true );
		      newtab->cb_useCrypto->setChecked ( true );
		    }
		  if ( value == "open" || value == "opensystem" || value == "" )
		    {
		      crypto->rb_openMode->setChecked ( true );
		      newtab->cb_useCrypto->setChecked ( false );
		    }
		}
	      else if ( var == "WIRELESS_KEY_0" )
		{
		  if ( value.startsWith ( "s:" ) )
		    value = value.right ( value.length (  ) - 2 );
		  crypto->le_key1->setText ( value );
		}
	      else if ( var == "WIRELESS_KEY_1" )
		{
		  if ( value.startsWith ( "s:" ) )
		    value = value.right ( value.length (  ) - 2 );
		  crypto->le_key2->setText ( value );
		}
	      else if ( var == "WIRELESS_KEY_2" )
		{
		  if ( value.startsWith ( "s:" ) )
		    value = value.right ( value.length (  ) - 2 );
		  crypto->le_key3->setText ( value );
		}
	      else if ( var == "WIRELESS_KEY_3" )
		{
		  if ( value.startsWith ( "s:" ) )
		    value = value.right ( value.length (  ) - 2 );
		  crypto->le_key4->setText ( value );
		}
	    }
	  newtab->save (  );
	  crypto->save ( ifconfig );
	  configfile.close (  );
	  m_totalConfigs++;
	}
    }

}

void
VendorConfig::initDebian (  )
{
  TQFile configfile ( TQString ( "/etc/network/interfaces" ) );
  if ( !configfile.open ( IO_ReadOnly ) )
  {
    return;
  }

  kdDebug (  ) << "Opened Debian configuration file " << configfile.name (  ) << "\n";

  IfConfigPage *newtab = m_caller->addConfigTab ( 1, true );
/* this config tab gets automagically stored in an array, the first
starting at position vendorBase. We need to construct its power
and crypto settings and therefore need the array index, which is
vendorBase+m_totalConfigs. It is _essential_ that m_totalConfigs
is incremented _after_ successful setup of each given config. */
// init crypto entries
  ConfigCrypto *crypto = new ConfigCrypto ( 0, "ConfigCrypto" );
  WifiConfig *config = WifiConfig::instance (  );
  IfConfig & ifconfig = config->m_ifConfig[KCMWifi::vendorBase + m_totalConfigs];

  TQString buffer;
  while ( configfile.readLine ( buffer, 255 ) != -1 )
    {
// lines look like wireless-defaultkey bla, so split it into wireless-defaultkey and bla
      TQString var = buffer.section ( ' ', 0, 0 );
      TQString value = buffer.section ( ' ', 1, 1 );
      kdDebug (  ) << "Variable " << var << " tqcontains " << value << ".\n";
// evaluate the meaningful lines
      if ( var.tqstartsWith ( "wireless", false ) )
	{
	  if ( var.endsWith ( "essid" ) )
	    {
	      newtab->le_networkName->setText ( value );
	    }

	  else if ( var.endsWith ( "mode" ) && !var.tqcontains ( "key" ) )

	    {
	      newtab->cmb_wifiMode->setCurrentItem ( IfConfig::convertToWifiModeFromString ( value ) );
	    }

	  else if ( var.endsWith ( "rate" ) )

	    {
	      newtab->cmb_speed->setCurrentItem ( IfConfig::convertToSpeedFromString ( value ) );
	    }

	  else if ( var.tqcontains ( "key" ) )
	    {
// Could be any of key, key1, key2, ..., or defaultkey
	      if ( var.tqcontains ( "default" ) )
		{
		  crypto->cmb_activeKey->setCurrentItem ( value.toInt (  ) );
		}

	      else if ( var.tqcontains ( "mode" ) )
		{
		  if ( value == "shared" || value == "sharedkey" || value == "restricted" )
		    {
		      crypto->rb_restrictMode->setChecked ( true );
		      newtab->cb_useCrypto->setChecked ( true );
		    }
		}

	      else if ( value == "open" || value == "opensystem" || value == "" )
		{
		  crypto->rb_openMode->setChecked ( true );
		  newtab->cb_useCrypto->setChecked ( false );
		}

	      else if ( var.right ( 1 ) == "1" )
		{
		  if ( value.startsWith ( "s:" ) )
		    value = value.right ( value.length (  ) - 2 );
		  crypto->le_key1->setText ( value );
		}

	      else if ( var.right ( 1 ) == "2" )
		{
		  if ( value.startsWith ( "s:" ) )
		    value = value.right ( value.length (  ) - 2 );
		  crypto->le_key2->setText ( value );
		}

	      else if ( var.right ( 1 ) == "3" )
		{
		  if ( value.startsWith ( "s:" ) )
		    value = value.right ( value.length (  ) - 2 );
		  crypto->le_key3->setText ( value );
		}

	      else if ( var.right ( 1 ) == "4" )
		{
		  if ( value.startsWith ( "s:" ) )
		    value = value.right ( value.length (  ) - 2 );
		  crypto->le_key4->setText ( value );
		}

	      else
		{
// does not contain a number. It's the only one.
		  if ( value.startsWith ( "s:" ) )
		    value = value.right ( value.length (  ) - 2 );
		  crypto->le_key1->setText ( value );
		}
	    }
	}
    }
  newtab->save (  );
  crypto->save ( ifconfig );
  configfile.close (  );
  m_totalConfigs++;
}