Port KProgressBar's to QProgressBar's.
[kdenetwork.git] / wifi / networkscanning.cpp
blobf9e3fb33e30e72b66525655918d342e893242b66
1 /***************************************************************************
2 networkscanning.cpp - description
3 -------------------
4 begin : Sam Apr 24 11:44:20 CEST 2005
5 copyright : (C) 2005 by Stefan Winter
6 email : swinter@kde.org
7 ***************************************************************************/
9 /***************************************************************************
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 ***************************************************************************/
18 #include <stdio.h>
20 #include <QString>
21 #include <QWidget>
22 #include <QLayout>
23 #include <QPushButton>
24 #include <QGridLayout>
25 #include <QTableView>
27 #include <kdebug.h>
28 #include <klocale.h>
29 #include <kprocess.h>
30 #include <kmessagebox.h>
31 #include <ktempfile.h>
33 #include "networkscanning.h"
34 #include "interface_wireless.h"
35 #include "ui_scanresults.h"
37 NetworkScanning::NetworkScanning ( Interface_wireless * dev, QWidget * parent ):QWidget ( parent )
39 device = dev;
40 networkScan ( );
43 void
44 NetworkScanning::networkScan ( )
46 kDebug ( ) << "Scanning...\n";
47 networks = device->get_available_networks ( );
48 // we will get a signal when things are ready to be processed
49 connect ( networks, SIGNAL ( finishedScanning ( ) ), this, SLOT ( displayResults ( ) ) );
52 void
53 NetworkScanning::displayResults ( )
55 QModelIndex x;
57 if ( networks->rowCount ( x ) == 0 )
59 KMessageBox::sorry ( 0, i18n ( "The scan is complete, but no networks have been found." ),
60 i18n ( "No Network Available" ) );
61 return;
64 scanResultsContainer = new QWidget ( 0 );
65 scanresults_ui.setupUi ( scanResultsContainer );
67 scanresults_ui.resultsTableView->setModel ( networks );
68 scanresults_ui.resultsTableView->setSelectionBehavior ( QAbstractItemView::SelectRows );
69 for ( int i = 0; i < 4; i++ )
70 scanresults_ui.resultsTableView->resizeColumnToContents ( i );
72 connect ( scanresults_ui.resultsTableView, SIGNAL ( clicked ( const QModelIndex & ) ), this,
73 SLOT ( changeSwitchButtonState ( const QModelIndex & ) ) );
75 scanResultsContainer->show ( );
78 void
79 NetworkScanning::changeSwitchButtonState ( const QModelIndex & index )
81 scanresults_ui.switchNetworkButton->setEnabled ( index.isValid ( ) );
84 void
85 NetworkScanning::switchToNetwork ( )
88 * FIXME: make this work again
90 WEP_KEY encryption = checkWEP();
92 if (encryption == INVALID) {
93 KMessageBox::sorry(0,i18n( "Aborting network switching due to invalid WEP key specification." ), i18n( "Invalid WEP Key" ));
94 return;
97 QString cmdline;
99 KTempFile* tempfile = new KTempFile( QString::null, QString::null, 0700 );
100 QString tempfilename = tempfile->name();
102 cmdline = (QString)"ifconfig %1 down\n";
103 cmdline = cmdline.arg( device->get_interface_name() );
104 write( tempfile->handle(), cmdline.ascii(), strlen( cmdline.ascii() ) );
106 cmdline = (QString)"iwconfig %1 essid %2 mode %3 enc %4\n";
107 cmdline = cmdline.arg( device->get_interface_name() );
108 cmdline = cmdline.arg( KProcess::quote( networks->text( networks->currentRow(), 0 ) ) );
109 cmdline = cmdline.arg( networks->text( networks->currentRow(), 1 ) );
110 if ( encryption != NONE ) {
111 cmdline = cmdline.arg( (encryption == VALID_STRING ? "s:" : "" ) + KProcess::quote( networks->text( networks->currentRow(), 3 ) ) );
112 } else {
113 cmdline = cmdline.arg("off");
115 write( tempfile->handle(), cmdline.ascii(), strlen( cmdline.ascii() ) );
117 cmdline = (QString)"ifconfig %1 up\n";
118 cmdline = cmdline.arg( device->get_interface_name() );
119 write( tempfile->handle(), cmdline.ascii(), strlen( cmdline.ascii() ) );
121 delete tempfile; // autoDeletion off, so the file remains on disk
123 KProcess switchProc;
124 switchProc << "kdesu" << tempfilename;
125 switchProc.start( KProcess::Block );
127 remove(tempfilename.ascii());
132 #include "networkscanning.moc"