summaryrefslogtreecommitdiffstats
path: root/wifi/networkscanning.cpp
blob: 08fb28b83db39132c2d6467d2eb59f39d52333b1 (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
/***************************************************************************
                          networkscanning.cpp  -  description
                             -------------------
    begin                : Sam Apr 24 11:44:20 CEST 2005
    copyright            : (C) 2005 by Stefan Winter
    email                : swinter@kde.org
 ***************************************************************************/

/***************************************************************************
 *                                                                         *
 *   This program 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.                                   *
 *                                                                         *
 ***************************************************************************/

#include <stdio.h>

#include <tqstring.h>
#include <tqwidget.h>
#include <tqlayout.h>
#include <tqpushbutton.h>
#include <tqtable.h>

#include <kdebug.h>
#include <tdelocale.h>
#include <kprocess.h>
#include <tdemessagebox.h>
#include <tdetempfile.h>

#include "networkscanning.h"
#include "interface_wireless.h"

NetworkScanning::NetworkScanning (Interface_wireless * dev, TQWidget * parent, const char * name ) : TQWidget ( parent, name ) {
  device = dev;
  networkScan();
}

void
NetworkScanning::networkScan ()
{
  networks = device->get_available_networks ();

  if ( networks->numRows() > 0 ) {

      networks->setColumnReadOnly( 0, true);
      networks->setColumnReadOnly( 1, true);
      networks->setColumnReadOnly( 2, true);
      networks->setColumnReadOnly( 3, false);
      for ( int i = 0; i < networks->numRows(); i++) {
	if ( networks->text( i, 3 ) == i18n( "off" )) networks->setRowReadOnly( i, true );
      }

      networks->setSelectionMode(TQTable::SingleRow);
      connect(networks,TQT_SIGNAL(selectionChanged()),this,TQT_SLOT(checkWEP()));

      this->setCaption( i18n( "Scan Results" ) );

      TQGridLayout* networkSelectionLayout = new TQGridLayout ( this, 2, 3, 0, 5);
      switchNet = new TQPushButton( i18n( "Switch to Network..." ), this );
      switchNet->setEnabled(false);
      TQPushButton* close = new TQPushButton( i18n( "Close" ), this );

      networks->reparent( this, TQPoint( 0, 0 ) );
      networks->setLeftMargin( 0 );
      networks->verticalHeader()->hide();

      connect ( close, TQT_SIGNAL( clicked() ), this, TQT_SLOT( hide() ) );
      connect ( switchNet, TQT_SIGNAL( clicked() ), this, TQT_SLOT( switchToNetwork() ) );
      connect ( networks, TQT_SIGNAL( currentChanged(int,int)), this, TQT_SLOT( checkSettings(int,int)));
      connect ( networks, TQT_SIGNAL( valueChanged(int,int)), this, TQT_SLOT( checkSettings(int,int)));

      networkSelectionLayout->addMultiCellWidget( networks, 0, 0, 0, 2 );
      networkSelectionLayout->addWidget( switchNet, 1, 0 );
      networkSelectionLayout->addWidget( close, 1, 2 );

      this->show();

    } else
    {
      KMessageBox::sorry(0,i18n("The scan is complete, but no networks have been found."),i18n("No Network Available"));
    }
}

void NetworkScanning::checkSettings(int row, int)
{
	if ((networks->text(row,0)!=i18n("(hidden cell)")) && (checkWEP()!=INVALID)) switchNet->setEnabled(true);
	else switchNet->setEnabled(false);
}

WEP_KEY
NetworkScanning::checkWEP()
{
  kdDebug() << "In checkWEP()\n";
  if (  (networks->text( networks->currentRow() , 3 ) == i18n( "off" )) ||
	(networks->text( networks->currentRow() , 3 ) == "" ) ) return NONE;
  if (  (networks->text( networks->currentRow() , 3 ).length()== 5 ) || 
	(networks->text( networks->currentRow() , 3 ).length()== 13 ) ) return VALID_STRING;
  if (  (networks->text( networks->currentRow() , 3 ).length()== 10 ) ||
        (networks->text( networks->currentRow() , 3 ).length()== 26 ) ) return VALID_HEX;
  return INVALID;
}

void
NetworkScanning::switchToNetwork()
{

  WEP_KEY encryption = checkWEP();

  if (encryption == INVALID) {
    KMessageBox::sorry(0,i18n( "Aborting network switching due to invalid WEP key specification." ), i18n( "Invalid WEP Key" ));
    return;
  }

  TQString cmdline;

  KTempFile* tempfile = new KTempFile( TQString(), TQString(), 0700 );
  TQString tempfilename = tempfile->name();

  cmdline = (TQString)"ifconfig %1 down\n";
  cmdline = cmdline.arg( device->get_interface_name() );
  write( tempfile->handle(), cmdline.ascii(), strlen( cmdline.ascii() ) );

  cmdline = (TQString)"iwconfig %1 essid %2 mode %3 enc %4\n";
  cmdline = cmdline.arg( device->get_interface_name() );
  cmdline = cmdline.arg( TDEProcess::quote( networks->text( networks->currentRow(), 0 ) ) );

  TQString modetemp;
  if (networks->text( networks->currentRow(), 1 ) == i18n("Managed") ) modetemp = "Managed"; else modetemp = "Ad-Hoc"; 
  cmdline = cmdline.arg( modetemp );

  if ( encryption != NONE ) {
    cmdline = cmdline.arg( (encryption == VALID_STRING ? "s:" : "" ) + TDEProcess::quote( networks->text( networks->currentRow(), 3 ) ) );
  } else {
    cmdline = cmdline.arg("off");
  }
  write( tempfile->handle(), cmdline.ascii(), strlen( cmdline.ascii() ) );

  cmdline = (TQString)"ifconfig %1 up\n";
  cmdline = cmdline.arg( device->get_interface_name() );
  write( tempfile->handle(), cmdline.ascii(), strlen( cmdline.ascii() ) );

  delete tempfile; // autoDeletion off, so the file remains on disk

  TDEProcess switchProc;
  switchProc << "tdesu" << tempfilename;
  switchProc.start( TDEProcess::Block );

  remove(tempfilename.ascii());

}


#include "networkscanning.moc"