networkwidget.cpp 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. /*
  2. * ZeroTier One - Global Peer to Peer Ethernet
  3. * Copyright (C) 2011-2014 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 "onetimedialog.h"
  31. #include "main.h"
  32. #include <QClipboard>
  33. #include <QString>
  34. #include <QStringList>
  35. #include <QCoreApplication>
  36. #include <QProcess>
  37. #include <QList>
  38. #include <QMessageBox>
  39. #include <QFont>
  40. #include "../node/Constants.hpp"
  41. NetworkWidget::NetworkWidget(QWidget *parent,const std::string &nwid) :
  42. QWidget(parent),
  43. ui(new Ui::NetworkWidget),
  44. networkIdStr(nwid),
  45. publicWarningShown(false)
  46. {
  47. ui->setupUi(this);
  48. ui->networkIdButton->setText(QString(nwid.c_str()));
  49. QFontMetrics fm(ui->ipListWidget->font());
  50. int lineHeight = ui->ipListWidget->spacing() + fm.height();
  51. ui->ipListWidget->setMinimumHeight(lineHeight * 6);
  52. ui->ipListWidget->setMaximumHeight(lineHeight * 6);
  53. #ifdef __APPLE__
  54. QWidgetList widgets = this->findChildren<QWidget*>();
  55. foreach(QWidget* widget, widgets)
  56. widget->setAttribute(Qt::WA_MacShowFocusRect,false);
  57. #endif
  58. #ifdef __WINDOWS__
  59. QWidgetList widgets = this->findChildren<QWidget*>();
  60. foreach(QWidget *widget, widgets) {
  61. QFont font(widget->font());
  62. font.setPointSizeF(font.pointSizeF() * 0.75);
  63. widget->setFont(font);
  64. }
  65. #endif
  66. }
  67. NetworkWidget::~NetworkWidget()
  68. {
  69. delete ui;
  70. }
  71. void NetworkWidget::setStatus(const std::string &status,const std::string &age)
  72. {
  73. ui->statusLabel->setText(QString(status.c_str()));
  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. if ((!publicWarningShown)&&(!settings->value("shown_publicWarning",false).toBool())) {
  92. publicWarningShown = true;
  93. OneTimeDialog *d = new OneTimeDialog(mainWindow,"shown_publicWarning","Security Notice","Security Notice:"ZT_EOL_S""ZT_EOL_S"You have joined a public network. Anyone can join these. We recommend making sure that your system's automatic software updates are enabled and turning off any shared network services that you do not want people to access.");
  94. d->setModal(false);
  95. d->show();
  96. }
  97. ui->networkTypeLabel->setStatusTip("This network can be joined by anyone in the world.");
  98. } else if (type == "private")
  99. ui->networkTypeLabel->setStatusTip("This network is private; only authorized peers can join.");
  100. else ui->networkTypeLabel->setStatusTip("Unknown network type.");
  101. }
  102. void NetworkWidget::setNetworkDeviceName(const std::string &dev)
  103. {
  104. ui->deviceLabel->setText(QString(dev.c_str()));
  105. }
  106. void NetworkWidget::setIps(const std::string &commaSeparatedList)
  107. {
  108. QStringList ips(QString(commaSeparatedList.c_str()).split(QChar(','),QString::SkipEmptyParts));
  109. if (commaSeparatedList == "-")
  110. ips.clear();
  111. QStringList tmp;
  112. ips.sort();
  113. for(QStringList::iterator i(ips.begin());i!=ips.end();++i) {
  114. QString ipOnly(*i);
  115. int slashIdx = ipOnly.indexOf('/');
  116. if (slashIdx > 0)
  117. ipOnly.truncate(slashIdx);
  118. tmp.append(ipOnly);
  119. }
  120. ips = tmp;
  121. for(QStringList::iterator i(ips.begin());i!=ips.end();++i) {
  122. if (ui->ipListWidget->findItems(*i,Qt::MatchCaseSensitive).size() == 0)
  123. ui->ipListWidget->addItem(*i);
  124. }
  125. for(int i=0;i<ui->ipListWidget->count();++i) {
  126. QListWidgetItem *item = ui->ipListWidget->item(i);
  127. if (!ips.contains(item->text()))
  128. ui->ipListWidget->removeItemWidget(item);
  129. }
  130. }
  131. void NetworkWidget::setMAC(const std::string &mac)
  132. {
  133. ui->macLabel->setText(QString(mac.c_str()));
  134. }
  135. const std::string &NetworkWidget::networkId()
  136. {
  137. return networkIdStr;
  138. }
  139. void NetworkWidget::on_leaveNetworkButton_clicked()
  140. {
  141. if (QMessageBox::question(this,"Leave Network?",QString("Are you sure you want to leave network '") + networkIdStr.c_str() + "'?",QMessageBox::No,QMessageBox::Yes) == QMessageBox::Yes) {
  142. this->setEnabled(false);
  143. zeroTierClient->send((QString("leave ") + networkIdStr.c_str()).toStdString());
  144. }
  145. }
  146. void NetworkWidget::on_networkIdButton_clicked()
  147. {
  148. QApplication::clipboard()->setText(ui->networkIdButton->text());
  149. }
  150. void NetworkWidget::on_ipListWidget_itemActivated(QListWidgetItem *item)
  151. {
  152. if (item)
  153. QApplication::clipboard()->setText(item->text());
  154. }
  155. void NetworkWidget::on_ipListWidget_currentItemChanged(QListWidgetItem *current, QListWidgetItem *previous)
  156. {
  157. if (current)
  158. QApplication::clipboard()->setText(current->text());
  159. }