networkwidget.cpp 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  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. #include <QFont>
  38. #include "../node/Constants.hpp"
  39. NetworkWidget::NetworkWidget(QWidget *parent,const std::string &nwid) :
  40. QWidget(parent),
  41. ui(new Ui::NetworkWidget),
  42. networkIdStr(nwid)
  43. {
  44. ui->setupUi(this);
  45. ui->networkIdButton->setText(QString(nwid.c_str()));
  46. QFontMetrics fm(ui->ipListWidget->font());
  47. int lineHeight = ui->ipListWidget->spacing() + fm.height();
  48. ui->ipListWidget->setMinimumHeight(lineHeight * 4);
  49. ui->ipListWidget->setMaximumHeight(lineHeight * 4);
  50. #ifdef __APPLE__
  51. QWidgetList widgets = this->findChildren<QWidget*>();
  52. foreach(QWidget* widget, widgets)
  53. widget->setAttribute(Qt::WA_MacShowFocusRect,false);
  54. #endif
  55. #ifdef __WINDOWS__
  56. QWidgetList widgets = this->findChildren<QWidget*>();
  57. foreach(QWidget *widget, widgets) {
  58. QFont font(widget->font());
  59. font.setPointSizeF(font.pointSizeF() * 0.75);
  60. widget->setFont(font);
  61. }
  62. #endif
  63. }
  64. NetworkWidget::~NetworkWidget()
  65. {
  66. delete ui;
  67. }
  68. void NetworkWidget::setStatus(const std::string &status,const std::string &age)
  69. {
  70. ui->statusLabel->setText(QString(status.c_str()));
  71. if (status == "OK")
  72. ui->ageLabel->setText(QString("[") + age.c_str() + "s ago]");
  73. else ui->ageLabel->setText(QString());
  74. }
  75. void NetworkWidget::setNetworkName(const std::string &name)
  76. {
  77. if (name == "?") {
  78. ui->nameLabel->setText("... waiting ...");
  79. ui->nameLabel->setEnabled(false);
  80. } else {
  81. ui->nameLabel->setText(QString(name.c_str()));
  82. ui->nameLabel->setEnabled(true);
  83. }
  84. }
  85. void NetworkWidget::setNetworkType(const std::string &type)
  86. {
  87. ui->networkTypeLabel->setText(QString(type.c_str()));
  88. if (type == "?")
  89. ui->networkTypeLabel->setStatusTip("Waiting for configuration...");
  90. else if (type == "public")
  91. ui->networkTypeLabel->setStatusTip("This network can be joined by anyone in the world.");
  92. else if (type == "private")
  93. ui->networkTypeLabel->setStatusTip("This network is private; only authorized peers can join.");
  94. else ui->networkTypeLabel->setStatusTip("Unknown network type.");
  95. }
  96. void NetworkWidget::setNetworkDeviceName(const std::string &dev)
  97. {
  98. ui->deviceLabel->setText(QString(dev.c_str()));
  99. }
  100. void NetworkWidget::setIps(const std::string &commaSeparatedList)
  101. {
  102. QStringList ips(QString(commaSeparatedList.c_str()).split(QChar(','),QString::SkipEmptyParts));
  103. if (commaSeparatedList == "-")
  104. ips.clear();
  105. QStringList tmp;
  106. ips.sort();
  107. for(QStringList::iterator i(ips.begin());i!=ips.end();++i) {
  108. QString ipOnly(*i);
  109. int slashIdx = ipOnly.indexOf('/');
  110. if (slashIdx > 0)
  111. ipOnly.truncate(slashIdx);
  112. tmp.append(ipOnly);
  113. }
  114. ips = tmp;
  115. for(QStringList::iterator i(ips.begin());i!=ips.end();++i) {
  116. if (ui->ipListWidget->findItems(*i,Qt::MatchCaseSensitive).size() == 0)
  117. ui->ipListWidget->addItem(*i);
  118. }
  119. for(int i=0;i<ui->ipListWidget->count();++i) {
  120. QListWidgetItem *item = ui->ipListWidget->item(i);
  121. if (!ips.contains(item->text()))
  122. ui->ipListWidget->removeItemWidget(item);
  123. }
  124. }
  125. const std::string &NetworkWidget::networkId()
  126. {
  127. return networkIdStr;
  128. }
  129. void NetworkWidget::on_leaveNetworkButton_clicked()
  130. {
  131. if (QMessageBox::question(this,"Leave Network?",QString("Are you sure you want to leave network '") + networkIdStr.c_str() + "'?",QMessageBox::No,QMessageBox::Yes) == QMessageBox::Yes) {
  132. this->setEnabled(false);
  133. zeroTierClient->send((QString("leave ") + networkIdStr.c_str()).toStdString());
  134. }
  135. }
  136. void NetworkWidget::on_networkIdButton_clicked()
  137. {
  138. QApplication::clipboard()->setText(ui->networkIdButton->text());
  139. }
  140. void NetworkWidget::on_ipListWidget_itemActivated(QListWidgetItem *item)
  141. {
  142. if (item)
  143. QApplication::clipboard()->setText(item->text());
  144. }
  145. void NetworkWidget::on_ipListWidget_currentItemChanged(QListWidgetItem *current, QListWidgetItem *previous)
  146. {
  147. if (current)
  148. QApplication::clipboard()->setText(current->text());
  149. }