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