2 * wpa_gui - Peers class
3 * Copyright (c) 2009, Atheros Communications
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.
9 * Alternatively, this software may be distributed under the terms of BSD
12 * See README and COPYING for more details.
16 #include <QImageReader>
17 #include <QMessageBox>
20 #include "stringquery.h"
24 static const int peer_role_address = Qt::UserRole + 1;
25 static const int peer_role_type = Qt::UserRole + 2;
29 * - add pending WPS queries (from M1/PIN, PBC?)
30 * - add current AP info (e.g., from WPS) in station mode
31 * - different icons to indicate peer type
35 PEER_TYPE_ASSOCIATED_STATION,
41 Peers::Peers(QWidget *parent, const char *, bool, Qt::WFlags)
46 if (QImageReader::supportedImageFormats().contains(QByteArray("svg")))
47 default_icon = new QIcon(":/icons/wpa_gui.svg");
49 default_icon = new QIcon(":/icons/wpa_gui.png");
51 peers->setModel(&model);
52 peers->setResizeMode(QListView::Adjust);
54 peers->setContextMenuPolicy(Qt::CustomContextMenu);
55 connect(peers, SIGNAL(customContextMenuRequested(const QPoint &)),
56 this, SLOT(context_menu(const QPoint &)));
62 void Peers::setWpaGui(WpaGui *_wpagui)
75 void Peers::languageChange()
81 void Peers::context_menu(const QPoint &pos)
83 QMenu *menu = new QMenu;
87 QModelIndex idx = peers->indexAt(pos);
89 ctx_item = model.itemFromIndex(idx);
90 int type = ctx_item->data(peer_role_type).toInt();
93 case PEER_TYPE_ASSOCIATED_STATION:
94 title = tr("Associated station");
99 case PEER_TYPE_AP_WPS:
100 title = tr("WPS AP");
103 menu->addAction(title)->setEnabled(false);
104 menu->addSeparator();
106 if (type == PEER_TYPE_ASSOCIATED_STATION ||
107 type == PEER_TYPE_AP_WPS) {
108 /* TODO: only for peers that are requesting WPS PIN
110 menu->addAction(QString("Enter WPS PIN"), this,
115 menu->addAction(QString("Refresh"), this, SLOT(ctx_refresh()));
118 menu->exec(peers->mapToGlobal(pos));
122 void Peers::enter_pin()
124 if (ctx_item == NULL)
126 QString addr = ctx_item->data(peer_role_address).toString();
127 StringQuery input(tr("PIN:"));
128 input.setWindowTitle(tr("PIN for ") + ctx_item->text());
129 if (input.exec() != QDialog::Accepted)
135 snprintf(cmd, sizeof(cmd), "WPS_PIN %s %s",
136 addr.toAscii().constData(),
137 input.get_string().toAscii().constData());
138 reply_len = sizeof(reply) - 1;
139 if (wpagui->ctrlRequest(cmd, reply, &reply_len) < 0) {
141 msg.setIcon(QMessageBox::Warning);
142 msg.setText("Failed to set the WPS PIN.");
148 void Peers::ctx_refresh()
154 void Peers::add_stations()
161 reply_len = sizeof(reply) - 1;
162 if (wpagui->ctrlRequest("STA-FIRST", reply, &reply_len) < 0)
166 reply[reply_len] = '\0';
169 while (*txt != '\0' && *txt != '\n')
172 if (strncmp(reply, "FAIL", 4) == 0 ||
173 strncmp(reply, "UNKNOWN", 7) == 0)
176 QStringList lines = info.split(QRegExp("\\n"));
179 for (QStringList::Iterator it = lines.begin();
180 it != lines.end(); it++) {
181 int pos = (*it).indexOf('=') + 1;
185 if ((*it).startsWith("wpsDeviceName="))
186 name = (*it).mid(pos);
192 QStandardItem *item = new QStandardItem(*default_icon, name);
194 item->setData(QString(reply), peer_role_address);
195 item->setData(PEER_TYPE_ASSOCIATED_STATION,
197 item->setToolTip(info);
198 model.appendRow(item);
201 reply_len = sizeof(reply) - 1;
202 snprintf(cmd, sizeof(cmd), "STA-NEXT %s", reply);
203 res = wpagui->ctrlRequest(cmd, reply, &reply_len);
208 void Peers::add_scan_results()
217 snprintf(cmd, sizeof(cmd), "BSS %d", index++);
221 reply_len = sizeof(reply) - 1;
222 if (wpagui->ctrlRequest(cmd, reply, &reply_len) < 0)
224 reply[reply_len] = '\0';
227 if (bss.isEmpty() || bss.startsWith("FAIL"))
230 QString ssid, bssid, flags, wps_name;
232 QStringList lines = bss.split(QRegExp("\\n"));
233 for (QStringList::Iterator it = lines.begin();
234 it != lines.end(); it++) {
235 int pos = (*it).indexOf('=') + 1;
239 if ((*it).startsWith("bssid="))
240 bssid = (*it).mid(pos);
241 else if ((*it).startsWith("flags="))
242 flags = (*it).mid(pos);
243 else if ((*it).startsWith("ssid="))
244 ssid = (*it).mid(pos);
245 else if ((*it).startsWith("wps_device_name="))
246 wps_name = (*it).mid(pos);
249 QString name = wps_name;
251 name = ssid + "\n" + bssid;
253 QStandardItem *item = new QStandardItem(*default_icon, name);
255 item->setData(QString(reply), peer_role_address);
256 if (flags.contains("[WPS"))
257 item->setData(PEER_TYPE_AP_WPS,
260 item->setData(PEER_TYPE_AP, peer_role_type);
262 for (int i = 0; i < lines.size(); i++) {
263 if (lines[i].length() > 60) {
265 60, lines[i].length());
269 item->setToolTip(lines.join("\n"));
270 model.appendRow(item);
276 void Peers::update_peers()