network.cpp 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. #include "network.h"
  2. #include "mainwindow.h"
  3. #include "ui_network.h"
  4. #include <QClipboard>
  5. #include <QString>
  6. #include <QStringList>
  7. #include <QCoreApplication>
  8. #include <QProcess>
  9. #include <QList>
  10. #include <QMessageBox>
  11. Network::Network(QWidget *parent,const std::string &nwid) :
  12. QWidget(parent),
  13. ui(new Ui::Network),
  14. networkIdStr(nwid)
  15. {
  16. ui->setupUi(this);
  17. ui->networkIdPushButton->setText(QString(nwid.c_str()));
  18. }
  19. Network::~Network()
  20. {
  21. delete ui;
  22. }
  23. void Network::setStatus(const std::string &status)
  24. {
  25. ui->statusLabel->setText(QString(status.c_str()));
  26. }
  27. void Network::setNetworkName(const std::string &name)
  28. {
  29. ui->nameLabel->setText(QString(name.c_str()));
  30. }
  31. void Network::setNetworkType(const std::string &type)
  32. {
  33. ui->networkTypeLabel->setText(QString(type.c_str()));
  34. if (type == "?")
  35. ui->networkTypeLabel->setToolTip("Waiting for configuration...");
  36. else if (type == "public")
  37. ui->networkTypeLabel->setToolTip("This network can be joined by anyone.");
  38. else if (type == "private")
  39. ui->networkTypeLabel->setToolTip("This network is private, only authorized peers can join.");
  40. else ui->networkTypeLabel->setToolTip(QString());
  41. }
  42. void Network::setNetworkDeviceName(const std::string &dev)
  43. {
  44. ui->deviceLabel->setText(QString(dev.c_str()));
  45. }
  46. void Network::setIps(const std::string &commaSeparatedList)
  47. {
  48. QStringList ips(QString(commaSeparatedList.c_str()).split(QChar(','),QString::SkipEmptyParts));
  49. if (commaSeparatedList == "-")
  50. ips.clear();
  51. QStringList tmp;
  52. ips.sort();
  53. for(QStringList::iterator i(ips.begin());i!=ips.end();++i) {
  54. QString ipOnly(*i);
  55. int slashIdx = ipOnly.indexOf('/');
  56. if (slashIdx > 0)
  57. ipOnly.truncate(slashIdx);
  58. tmp.append(ipOnly);
  59. }
  60. ips = tmp;
  61. for(QStringList::iterator i(ips.begin());i!=ips.end();++i) {
  62. if (ui->ipListWidget->findItems(*i,Qt::MatchCaseSensitive).size() == 0)
  63. ui->ipListWidget->addItem(*i);
  64. }
  65. for(int i=0;i<ui->ipListWidget->count();++i) {
  66. QListWidgetItem *item = ui->ipListWidget->item(i);
  67. if (!ips.contains(item->text()))
  68. ui->ipListWidget->removeItemWidget(item);
  69. }
  70. }
  71. const std::string &Network::networkId()
  72. {
  73. return networkIdStr;
  74. }
  75. void Network::on_leaveNetworkButton_clicked()
  76. {
  77. if (QMessageBox::question(this,"Leave Network?",QString("Are you sure you want to leave network '") + networkIdStr.c_str() + "'?",QMessageBox::No,QMessageBox::Yes) == QMessageBox::Yes) {
  78. zeroTierClient->send((QString("leave ") + networkIdStr.c_str()).toStdString());
  79. this->setEnabled(false);
  80. }
  81. }
  82. void Network::on_networkIdPushButton_clicked()
  83. {
  84. QApplication::clipboard()->setText(ui->networkIdPushButton->text());
  85. }