peers.cpp 2.5 KB

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