scanresults.cpp 2.8 KB

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