scanresults.cpp 2.8 KB

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