Home | History | Annotate | Download | only in wpa_gui-qt4
      1 /*
      2  * wpa_gui - ScanResults class
      3  * Copyright (c) 2005-2006, Jouni Malinen <j (at) w1.fi>
      4  *
      5  * This software may be distributed under the terms of the BSD license.
      6  * See README for more details.
      7  */
      8 
      9 #include <cstdio>
     10 
     11 #include "scanresults.h"
     12 #include "signalbar.h"
     13 #include "wpagui.h"
     14 #include "networkconfig.h"
     15 #include "scanresultsitem.h"
     16 
     17 
     18 ScanResults::ScanResults(QWidget *parent, const char *, bool, Qt::WindowFlags)
     19 	: QDialog(parent)
     20 {
     21 	setupUi(this);
     22 
     23 	connect(closeButton, SIGNAL(clicked()), this, SLOT(close()));
     24 	connect(scanButton, SIGNAL(clicked()), this, SLOT(scanRequest()));
     25 	connect(scanResultsWidget,
     26 		SIGNAL(itemDoubleClicked(QTreeWidgetItem *, int)), this,
     27 		SLOT(bssSelected(QTreeWidgetItem *)));
     28 
     29 	wpagui = NULL;
     30 	scanResultsWidget->setItemsExpandable(false);
     31 	scanResultsWidget->setRootIsDecorated(false);
     32 	scanResultsWidget->setItemDelegate(new SignalBar(scanResultsWidget));
     33 }
     34 
     35 
     36 ScanResults::~ScanResults()
     37 {
     38 }
     39 
     40 
     41 void ScanResults::languageChange()
     42 {
     43 	retranslateUi(this);
     44 }
     45 
     46 
     47 void ScanResults::setWpaGui(WpaGui *_wpagui)
     48 {
     49 	wpagui = _wpagui;
     50 	updateResults();
     51 }
     52 
     53 
     54 void ScanResults::updateResults()
     55 {
     56 	char reply[2048];
     57 	size_t reply_len;
     58 	int index;
     59 	char cmd[20];
     60 
     61 	scanResultsWidget->clear();
     62 
     63 	index = 0;
     64 	while (wpagui) {
     65 		snprintf(cmd, sizeof(cmd), "BSS %d", index++);
     66 		if (index > 1000)
     67 			break;
     68 
     69 		reply_len = sizeof(reply) - 1;
     70 		if (wpagui->ctrlRequest(cmd, reply, &reply_len) < 0)
     71 			break;
     72 		reply[reply_len] = '\0';
     73 
     74 		QString bss(reply);
     75 		if (bss.isEmpty() || bss.startsWith("FAIL"))
     76 			break;
     77 
     78 		QString ssid, bssid, freq, signal, flags;
     79 
     80 		QStringList lines = bss.split(QRegExp("\\n"));
     81 		for (QStringList::Iterator it = lines.begin();
     82 		     it != lines.end(); it++) {
     83 			int pos = (*it).indexOf('=') + 1;
     84 			if (pos < 1)
     85 				continue;
     86 
     87 			if ((*it).startsWith("bssid="))
     88 				bssid = (*it).mid(pos);
     89 			else if ((*it).startsWith("freq="))
     90 				freq = (*it).mid(pos);
     91 			else if ((*it).startsWith("level="))
     92 				signal = (*it).mid(pos);
     93 			else if ((*it).startsWith("flags="))
     94 				flags = (*it).mid(pos);
     95 			else if ((*it).startsWith("ssid="))
     96 				ssid = (*it).mid(pos);
     97 		}
     98 
     99 		ScanResultsItem *item = new ScanResultsItem(scanResultsWidget);
    100 		if (item) {
    101 			item->setText(0, ssid);
    102 			item->setText(1, bssid);
    103 			item->setText(2, freq);
    104 			item->setText(3, signal);
    105 			item->setText(4, flags);
    106 		}
    107 
    108 		if (bssid.isEmpty())
    109 			break;
    110 	}
    111 }
    112 
    113 
    114 void ScanResults::scanRequest()
    115 {
    116 	char reply[10];
    117 	size_t reply_len = sizeof(reply);
    118 
    119 	if (wpagui == NULL)
    120 		return;
    121 
    122 	wpagui->ctrlRequest("SCAN", reply, &reply_len);
    123 }
    124 
    125 
    126 void ScanResults::getResults()
    127 {
    128 	updateResults();
    129 }
    130 
    131 
    132 void ScanResults::bssSelected(QTreeWidgetItem *sel)
    133 {
    134 	NetworkConfig *nc = new NetworkConfig();
    135 	if (nc == NULL)
    136 		return;
    137 	nc->setWpaGui(wpagui);
    138 	nc->paramsFromScanResults(sel);
    139 	nc->show();
    140 	nc->exec();
    141 }
    142