networkwidget.cpp 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. /*
  2. * ZeroTier One - Global Peer to Peer Ethernet
  3. * Copyright (C) 2012-2013 ZeroTier Networks LLC
  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 as published by
  7. * the Free Software Foundation, either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. *
  18. * --
  19. *
  20. * ZeroTier may be used and distributed under the terms of the GPLv3, which
  21. * are available at: http://www.gnu.org/licenses/gpl-3.0.html
  22. *
  23. * If you would like to embed ZeroTier into a commercial application or
  24. * redistribute it in a modified binary form, please contact ZeroTier Networks
  25. * LLC. Start here: http://www.zerotier.com/
  26. */
  27. #include "networkwidget.h"
  28. #include "mainwindow.h"
  29. #include "ui_networkwidget.h"
  30. #include <QClipboard>
  31. #include <QString>
  32. #include <QStringList>
  33. #include <QCoreApplication>
  34. #include <QProcess>
  35. #include <QList>
  36. #include <QMessageBox>
  37. NetworkWidget::NetworkWidget(QWidget *parent,const std::string &nwid) :
  38. QWidget(parent),
  39. ui(new Ui::NetworkWidget),
  40. networkIdStr(nwid)
  41. {
  42. ui->setupUi(this);
  43. ui->networkIdPushButton->setText(QString(nwid.c_str()));
  44. QFontMetrics fm(ui->ipListWidget->font());
  45. int lineHeight = ui->ipListWidget->spacing() + fm.height();
  46. ui->ipListWidget->setMinimumHeight(lineHeight * 4);
  47. ui->ipListWidget->setMaximumHeight(lineHeight * 4);
  48. QWidgetList widgets = this->findChildren<QWidget*>();
  49. foreach(QWidget* widget, widgets)
  50. widget->setAttribute(Qt::WA_MacShowFocusRect,false);
  51. }
  52. NetworkWidget::~NetworkWidget()
  53. {
  54. delete ui;
  55. }
  56. void NetworkWidget::setStatus(const std::string &status,const std::string &age)
  57. {
  58. ui->statusLabel->setText(QString(status.c_str()));
  59. if (status == "OK")
  60. ui->ageLabel->setText(QString("(configuration is ") + age.c_str() + " seconds old)");
  61. else ui->ageLabel->setText(QString());
  62. }
  63. void NetworkWidget::setNetworkName(const std::string &name)
  64. {
  65. if (name == "?") {
  66. ui->nameLabel->setText("... waiting ...");
  67. ui->nameLabel->setEnabled(false);
  68. } else {
  69. ui->nameLabel->setText(QString(name.c_str()));
  70. ui->nameLabel->setEnabled(true);
  71. }
  72. }
  73. void NetworkWidget::setNetworkType(const std::string &type)
  74. {
  75. ui->networkTypeLabel->setText(QString(type.c_str()));
  76. if (type == "?")
  77. ui->networkTypeLabel->setStatusTip("Waiting for configuration...");
  78. else if (type == "public")
  79. ui->networkTypeLabel->setStatusTip("This network can be joined by anyone in the world.");
  80. else if (type == "private")
  81. ui->networkTypeLabel->setStatusTip("This network is private; only authorized peers can join.");
  82. else ui->networkTypeLabel->setStatusTip("Unknown network type.");
  83. }
  84. void NetworkWidget::setNetworkDeviceName(const std::string &dev)
  85. {
  86. ui->deviceLabel->setText(QString(dev.c_str()));
  87. }
  88. void NetworkWidget::setIps(const std::string &commaSeparatedList)
  89. {
  90. QStringList ips(QString(commaSeparatedList.c_str()).split(QChar(','),QString::SkipEmptyParts));
  91. if (commaSeparatedList == "-")
  92. ips.clear();
  93. QStringList tmp;
  94. ips.sort();
  95. for(QStringList::iterator i(ips.begin());i!=ips.end();++i) {
  96. QString ipOnly(*i);
  97. int slashIdx = ipOnly.indexOf('/');
  98. if (slashIdx > 0)
  99. ipOnly.truncate(slashIdx);
  100. tmp.append(ipOnly);
  101. }
  102. ips = tmp;
  103. for(QStringList::iterator i(ips.begin());i!=ips.end();++i) {
  104. if (ui->ipListWidget->findItems(*i,Qt::MatchCaseSensitive).size() == 0)
  105. ui->ipListWidget->addItem(*i);
  106. }
  107. for(int i=0;i<ui->ipListWidget->count();++i) {
  108. QListWidgetItem *item = ui->ipListWidget->item(i);
  109. if (!ips.contains(item->text()))
  110. ui->ipListWidget->removeItemWidget(item);
  111. }
  112. }
  113. const std::string &NetworkWidget::networkId()
  114. {
  115. return networkIdStr;
  116. }
  117. void NetworkWidget::on_leaveNetworkButton_clicked()
  118. {
  119. if (QMessageBox::question(this,"Leave Network?",QString("Are you sure you want to leave network '") + networkIdStr.c_str() + "'?",QMessageBox::No,QMessageBox::Yes) == QMessageBox::Yes) {
  120. this->setEnabled(false);
  121. zeroTierClient->send((QString("leave ") + networkIdStr.c_str()).toStdString());
  122. }
  123. }
  124. void NetworkWidget::on_networkIdPushButton_clicked()
  125. {
  126. QApplication::clipboard()->setText(ui->networkIdPushButton->text());
  127. }
  128. void NetworkWidget::on_ipListWidget_itemActivated(QListWidgetItem *item)
  129. {
  130. if (item)
  131. QApplication::clipboard()->setText(item->text());
  132. }