summaryrefslogtreecommitdiffstats
path: root/wifi/kcmwifi/kcmwifi.cpp
blob: 55920dad27b137d754edc2ca09fb85fb9d30df3b (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
371
372
373
374
375
376
377
378
379
380
381
382
/*
  Copyright (C) 2004 Nadeem Hasan <nhasan@kde.org>
            (C) 2001-2004 by Stefan Winter <mail@stefan-winter.de>

  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., 59 Temple Place - Suite 330,
  Boston, MA 02111-1307, USA.
*/

#include <qlayout.h>
#include <qtabwidget.h>
#include <qcombobox.h>
#include <qcheckbox.h>

#include <kaboutdata.h>
#include <kdebug.h>
#include <kdialog.h>
#include <kgenericfactory.h>
#include <klocale.h>
#include <kmessagebox.h>
#include <kprocio.h>
#include <klineedit.h>

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

typedef KGenericFactory < KCMWifi, QWidget > KWiFiFactory;
K_EXPORT_COMPONENT_FACTORY (kcm_wifi, KWiFiFactory("kcmwifi") )

KCMWifi::KCMWifi(QWidget * parent, const char *name, const QStringList &)
    : KCModule (parent, name)
{
  tabs = new QTabWidget (this, "tabs");

  m_mainConfig = new MainConfig(this, "m_mainConfig");

  WifiConfig *config = WifiConfig::instance();

  for ( int i=0; i<config->m_numConfigs; ++i )
  {
    IfConfigPage *ifConfigPage = new IfConfigPage( i, tabs, "m_configPage" );
    tabs->addTab(ifConfigPage, i18n ("Config &%1").arg( i+1 ));
    connect(ifConfigPage, SIGNAL(changed()), SLOT( slotChanged() ) );

    m_ifConfigPage[ i ] = ifConfigPage;
  }

  QVBoxLayout *top = new QVBoxLayout (this, 0, KDialog::spacingHint());
  top->addWidget( tabs );
  top->addWidget( m_mainConfig );
  top->addStretch();

  connect(m_mainConfig, SIGNAL(changed()), SLOT( slotChanged() ) );
  connect(m_mainConfig, SIGNAL(activateClicked()), SLOT(slotActivate()));

  if ( geteuid() != 0 )
  {
    tabs->setEnabled( false );
    m_mainConfig->setEnabled( false );
  }
  else
  {
    KProcess iwconfigtest;
    iwconfigtest << "iwconfig";
    if (!iwconfigtest.start(KProcess::DontCare))
    {
      KMessageBox::sorry(0, i18n("Error executing iwconfig. WLAN "
          "configurations can only be altered if the wireless tools are "
          "properly installed."), i18n ("No Wireless Tools"));

      tabs->setEnabled( false );
      m_mainConfig->setEnabled( false );
    }
  }

  load();

  m_activeVendorCount = 0;
  VendorConfig vendors( this );
  vendors.initAll();
}


KCMWifi::~KCMWifi()
{
}

void KCMWifi::load()
{
  WifiConfig *config = WifiConfig::instance();

  for ( int i=0; i<config->m_numConfigs; ++i )
    m_ifConfigPage[ i ]->load();

  m_mainConfig->load();
}

void KCMWifi::save()
{
  WifiConfig *config = WifiConfig::instance();
  for ( int i=0; i<config->m_numConfigs; ++i )
    m_ifConfigPage[ i ]->save();

  m_mainConfig->save();

  config->save();
}

void KCMWifi::slotActivate()
{
  save();
  activate();
}

void KCMWifi::defaults()
{
}

void KCMWifi::slotChanged()
{
  emit changed( true );
}

IfConfigPage* KCMWifi::addConfigTab( int count, bool vendor )
{

  WifiConfig *config = WifiConfig::instance();

  IfConfigPage* retval = NULL;
 
  if (vendor) 
  {
    IfConfigPage *ifConfigPage = new IfConfigPage( vendorBase + m_activeVendorCount, tabs, "m_configPage" );
    tabs->addTab(ifConfigPage, i18n ("Vendor %1").arg( m_activeVendorCount+1 ) );
    connect(ifConfigPage, SIGNAL(changed()), SLOT( slotChanged() ) );
    m_ifConfigPage[ vendorBase+m_activeVendorCount ] = ifConfigPage;
    m_mainConfig->registerConfig( vendorBase+m_activeVendorCount );
    m_activeVendorCount++;
    retval = ifConfigPage;

    // these configs are read-only, so disable all widgets
    // the content shall only be modified by VendorConfig code
    retval->cb_Autodetect->setEnabled( false );
    retval->le_networkName->setReadOnly( true );
    retval->cmb_wifiMode->setEnabled( false );
    retval->cmb_speed->setEnabled( false );
    retval->cb_runScript->setEnabled( false );
    retval->cb_useCrypto->setEnabled( false );
    retval->cb_pmEnabled->setEnabled( false );

  }
  else
  {
    for ( int i=config->m_numConfigs; i<config->m_numConfigs+count; i++ )
      {
        IfConfigPage *ifConfigPage = new IfConfigPage( i, tabs, "m_configPage" );
        tabs->insertTab(ifConfigPage, i18n ("Config &%1").arg( i+1 ), i );
        connect(ifConfigPage, SIGNAL(changed()), SLOT( slotChanged() ) );
        m_ifConfigPage[ i ] = ifConfigPage; 
        m_mainConfig->registerConfig( i+1 );
        retval = ifConfigPage;
      }
    config->m_numConfigs += count;
  }
 
  // if more than one was constructed, only the last is returned
  return retval;
}

void KCMWifi::delConfigTab( int count )
{
  WifiConfig *config = WifiConfig::instance();

  for ( int i=config->m_numConfigs-1; i>(config->m_numConfigs)-count-1; i-- )
  {
    tabs->setTabEnabled(m_ifConfigPage[ i ], false);
    m_ifConfigPage[ i ]->deleteLater();

    m_mainConfig->unregisterConfig( i+1 );

  }

  config->m_numConfigs -= count;

}

void KCMWifi::activate()
{
  KProcess proc;

  QStringList failedParts;

  WifiConfig *config = WifiConfig::instance();
  int index;
  if ( config->m_presetConfig >= config->m_numConfigs ) 
    {
      index = vendorBase+config->m_presetConfig-config->m_numConfigs; 
    } else
    {
      index = config->m_presetConfig;
    }

  kdDebug() << "I was told to use array slot number " << index << ".\n";

  IfConfig ifconfig = config->m_ifConfig[ index ];

  QString tempInterface;

  if ( ifconfig.m_interface.isEmpty() ) {
      tempInterface = config->autoDetectInterface();
      kdDebug() << "Autodetecting interface...\n";
    } else
    {
      tempInterface = ifconfig.m_interface;
    }

  proc << "ifconfig"
       << tempInterface
       << "down";
  kdDebug() << "Command: " << proc.args() << endl;
  proc.start (KProcess::Block);

  if ( (!proc.normalExit()) || (proc.exitStatus() != 0) ) 
	failedParts << i18n("Interface could not be shut down.  It is likely that your settings have not been applied.");

  proc.clearArguments();

  proc << "iwconfig"
       << tempInterface;
  proc << "essid"
       << ifconfig.m_networkName;

  kdDebug() << "Command: " << proc.args() << endl;
  proc.start (KProcess::Block);

  if ( (!proc.normalExit()) || (proc.exitStatus() != 0) )
        failedParts << i18n("SSID could not be set.");

  proc.clearArguments();

  proc << "iwconfig"
       << tempInterface;
  proc << "mode"
       << ifconfig.wifimodeAsString();

  kdDebug() << "Command: " << proc.args() << endl;
  proc.start (KProcess::Block);

  if ( (!proc.normalExit()) || (proc.exitStatus() != 0) )
        failedParts << i18n("Operation mode could not be set.");

  proc.clearArguments();

  proc << "iwconfig"
       << tempInterface;
  proc << "rate"
       << ifconfig.speedAsString();

  kdDebug() << "Command: " << proc.args() << endl;
  proc.start (KProcess::Block);

  if ( (!proc.normalExit()) || (proc.exitStatus() != 0) )
        failedParts << i18n("Speed settings could not be modified.");


  proc.clearArguments();

  proc << "iwconfig"
       << tempInterface;

  if (!ifconfig.m_useCrypto )
  {
    proc << "key"
         << "off";
  }
  else
  {
    for ( int i=0; i<4; ++i )
    {
      if ( ifconfig.m_keys[ i ].isValid( ifconfig.m_keys[ i ].key() ) > INVALID )
      {
        proc << "key"
             << QString( "[%1]").arg( i+1 )
	     << QString( "%1").arg( ifconfig.m_keys[ i ].rawKey() );
      }
    }

    // kdDebug() << "All appropriate keys have been fed into iwconfig.\n";

    if ( ifconfig.activeKey().isValid( ifconfig.activeKey().key() ) > INVALID )
    {
      // kdDebug() << "Entering final phase of key setup.\n";
      proc << "key"
           << ifconfig.cryptomodeAsString();
      proc << "key"
           << QString( "[%1]" ).arg( ifconfig.activeKeyId() );
      proc << "key"
           << "on";
    }
  }
  
  kdDebug() << "Command: " << proc.args() << endl;
  proc.start (KProcess::Block);

  if ( (ifconfig.m_useCrypto) && ((!proc.normalExit()) || (proc.exitStatus() != 0)) )
        failedParts << i18n("Encryption settings could not be set.");

  proc.clearArguments();

  proc << "iwconfig"
       << tempInterface;

  if ( !ifconfig.m_pmEnabled )
  {
    proc << "power"
         << "off";
  }
  else
  {
    proc << "power"
	 << "period"
         << QString( "%1" ).arg( ifconfig.m_wakeupPeriod );
    proc << "power"
	 << "timeout"
         << QString( "%1" ).arg( ifconfig.m_sleepTimeout );
    proc << ifconfig.powermodeAsString();
  }

  kdDebug() << "Command: " << proc.args() << endl;
  proc.start (KProcess::Block);

  if ( (ifconfig.m_pmEnabled) && ((!proc.normalExit()) || (proc.exitStatus() != 0)) )
        failedParts << i18n("Power management settings could not be set.");

  proc.clearArguments();
  proc << "ifconfig"
       << tempInterface
       << "up";
  
  kdDebug() << "Command: " << proc.args() << endl;
  proc.start (KProcess::Block);

  if ( (!proc.normalExit()) || (proc.exitStatus() != 0) )
        failedParts << i18n("Interface could not be re-enabled.");

  if ( ifconfig.m_runScript )
  {
    proc.clearArguments();
    proc << QStringList::split( " ", ifconfig.m_connectScript );

    proc.start(KProcess::DontCare);
  }

if (!failedParts.empty()) KMessageBox::informationList(0,i18n("The following settings could not be applied:"),failedParts);

}

extern "C"
{
  void init_wifi()
  {
    KCMWifi::activate();
  }
}


#include "kcmwifi.moc"