peers.cpp 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. /*
  2. * wpa_gui - Peers class
  3. * Copyright (c) 2009, Atheros Communications
  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 <QImageReader>
  16. #include "wpagui.h"
  17. #include "peers.h"
  18. /*
  19. * TODO:
  20. * - add pending WPS queries (from M1/PIN, PBC?)
  21. * - add current AP info (e.g., from WPS) in station mode
  22. * - different icons to indicate peer type
  23. */
  24. Peers::Peers(QWidget *parent, const char *, bool, Qt::WFlags)
  25. : QDialog(parent)
  26. {
  27. setupUi(this);
  28. connect(peers, SIGNAL(clicked(QModelIndex)), this,
  29. SLOT(clicked(QModelIndex)));
  30. if (QImageReader::supportedImageFormats().contains(QByteArray("svg")))
  31. default_icon = new QIcon(":/icons/wpa_gui.svg");
  32. else
  33. default_icon = new QIcon(":/icons/wpa_gui.png");
  34. peers->setModel(&model);
  35. peers->setResizeMode(QListView::Adjust);
  36. wpagui = NULL;
  37. }
  38. void Peers::setWpaGui(WpaGui *_wpagui)
  39. {
  40. wpagui = _wpagui;
  41. update_peers();
  42. }
  43. Peers::~Peers()
  44. {
  45. delete default_icon;
  46. }
  47. void Peers::languageChange()
  48. {
  49. retranslateUi(this);
  50. }
  51. void Peers::clicked(const QModelIndex & /*index*/)
  52. {
  53. /* QStandardItem *item = model.itemFromIndex(index); */
  54. /* TODO: give an option to provide PIN for WPS, etc. */
  55. /* printf("Clicked: %s\n", item->text().toAscii().constData()); */
  56. }
  57. void Peers::update_peers()
  58. {
  59. char reply[2048];
  60. size_t reply_len;
  61. char cmd[20];
  62. int res;
  63. model.clear();
  64. if (wpagui == NULL)
  65. return;
  66. reply_len = sizeof(reply) - 1;
  67. if (wpagui->ctrlRequest("STA-FIRST", reply, &reply_len) < 0)
  68. return;
  69. do {
  70. reply[reply_len] = '\0';
  71. QString info(reply);
  72. char *txt = reply;
  73. while (*txt != '\0' && *txt != '\n')
  74. txt++;
  75. *txt++ = '\0';
  76. if (strncmp(reply, "FAIL", 4) == 0)
  77. break;
  78. QStringList lines = info.split(QRegExp("\\n"));
  79. QString name;
  80. for (QStringList::Iterator it = lines.begin();
  81. it != lines.end(); it++) {
  82. int pos = (*it).indexOf('=') + 1;
  83. if (pos < 1)
  84. continue;
  85. if ((*it).startsWith("wpsDeviceName="))
  86. name = (*it).mid(pos);
  87. }
  88. if (name.isEmpty())
  89. name = reply;
  90. QStandardItem *item = new QStandardItem(*default_icon, name);
  91. if (item) {
  92. item->setToolTip(info);
  93. model.appendRow(item);
  94. }
  95. reply_len = sizeof(reply) - 1;
  96. snprintf(cmd, sizeof(cmd), "STA-NEXT %s", reply);
  97. res = wpagui->ctrlRequest(cmd, reply, &reply_len);
  98. } while (res >= 0);
  99. }