Re-initialize hostapd/wpa_supplicant git repository based on 0.6.3 release
[wpasupplicant] / wpa_supplicant / wpa_gui-qt4 / scanresults.cpp
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 "scanresults.h"
16 #include "wpagui.h"
17 #include "networkconfig.h"
18
19
20 ScanResults::ScanResults(QWidget *parent, const char *, bool, Qt::WFlags)
21         : QDialog(parent)
22 {
23         setupUi(this);
24
25         connect(closeButton, SIGNAL(clicked()), this, SLOT(close()));
26         connect(scanButton, SIGNAL(clicked()), this, SLOT(scanRequest()));
27         connect(scanResultsWidget,
28                 SIGNAL(itemDoubleClicked(QTreeWidgetItem *, int)), this,
29                 SLOT(bssSelected(QTreeWidgetItem *)));
30
31         wpagui = NULL;
32         scanResultsWidget->setItemsExpandable(FALSE);
33         scanResultsWidget->setRootIsDecorated(FALSE);
34 }
35
36
37 ScanResults::~ScanResults()
38 {
39 }
40
41
42 void ScanResults::languageChange()
43 {
44         retranslateUi(this);
45 }
46
47
48 void ScanResults::setWpaGui(WpaGui *_wpagui)
49 {
50         wpagui = _wpagui;
51         updateResults();
52 }
53
54
55 void ScanResults::updateResults()
56 {
57         char reply[2048];
58         size_t reply_len;
59         int index;
60         char cmd[20];
61
62         scanResultsWidget->clear();
63
64         index = 0;
65         while (wpagui) {
66                 snprintf(cmd, sizeof(cmd), "BSS %d", index++);
67                 if (index > 1000)
68                         break;
69
70                 reply_len = sizeof(reply) - 1;
71                 if (wpagui->ctrlRequest(cmd, reply, &reply_len) < 0)
72                         break;
73                 reply[reply_len] = '\0';
74
75                 QString bss(reply);
76                 if (bss.isEmpty() || bss.startsWith("FAIL"))
77                         break;
78
79                 QString ssid, bssid, freq, signal, flags;
80
81                 QStringList lines = bss.split(QRegExp("\\n"));
82                 for (QStringList::Iterator it = lines.begin();
83                      it != lines.end(); it++) {
84                         int pos = (*it).indexOf('=') + 1;
85                         if (pos < 1)
86                                 continue;
87
88                         if ((*it).startsWith("bssid="))
89                                 bssid = (*it).mid(pos);
90                         else if ((*it).startsWith("freq="))
91                                 freq = (*it).mid(pos);
92                         else if ((*it).startsWith("qual="))
93                                 signal = (*it).mid(pos);
94                         else if ((*it).startsWith("flags="))
95                                 flags = (*it).mid(pos);
96                         else if ((*it).startsWith("ssid="))
97                                 ssid = (*it).mid(pos);
98                 }
99
100                 QTreeWidgetItem *item = new QTreeWidgetItem(scanResultsWidget);
101                 if (item) {
102                         item->setText(0, ssid);
103                         item->setText(1, bssid);
104                         item->setText(2, freq);
105                         item->setText(3, signal);
106                         item->setText(4, flags);
107                 }
108
109                 if (bssid.isEmpty())
110                         break;
111         }
112 }
113
114
115 void ScanResults::scanRequest()
116 {
117         char reply[10];
118         size_t reply_len = sizeof(reply);
119     
120         if (wpagui == NULL)
121                 return;
122     
123         wpagui->ctrlRequest("SCAN", reply, &reply_len);
124 }
125
126
127 void ScanResults::getResults()
128 {
129         updateResults();
130 }
131
132
133 void ScanResults::bssSelected(QTreeWidgetItem *sel)
134 {
135         NetworkConfig *nc = new NetworkConfig();
136         if (nc == NULL)
137                 return;
138         nc->setWpaGui(wpagui);
139         nc->paramsFromScanResults(sel);
140         nc->show();
141         nc->exec();
142 }