scanresults.cpp 2.8 KB

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