2014-01-10 23:27:44 +00:00
/*
* ZeroTier One - Global Peer to Peer Ethernet
2015-01-06 01:47:59 +00:00
* Copyright ( C ) 2011 - 2015 ZeroTier Networks
2014-01-10 23:27:44 +00:00
*
* This program is free software : you can redistribute it and / or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation , either version 3 of the License , or
* ( at your option ) any later version .
*
* This program is distributed in the hope that it will be useful ,
* but WITHOUT ANY WARRANTY ; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE . See the
* GNU General Public License for more details .
*
* You should have received a copy of the GNU General Public License
* along with this program . If not , see < http : //www.gnu.org/licenses/>.
*
* - -
*
* ZeroTier may be used and distributed under the terms of the GPLv3 , which
* are available at : http : //www.gnu.org/licenses/gpl-3.0.html
*
* If you would like to embed ZeroTier into a commercial application or
* redistribute it in a modified binary form , please contact ZeroTier Networks
* LLC . Start here : http : //www.zerotier.com/
*/
2013-12-12 23:47:00 +00:00
# include "networkwidget.h"
2013-11-20 21:16:30 +00:00
# include "mainwindow.h"
2013-12-12 23:47:00 +00:00
# include "ui_networkwidget.h"
2014-02-05 20:38:37 +00:00
# include "onetimedialog.h"
# include "main.h"
2013-11-13 21:50:49 +00:00
2013-11-18 17:01:33 +00:00
# include <QClipboard>
2013-11-20 21:16:30 +00:00
# include <QString>
# include <QStringList>
# include <QCoreApplication>
# include <QProcess>
# include <QList>
# include <QMessageBox>
2014-01-27 06:24:29 +00:00
# include <QFont>
# include "../node/Constants.hpp"
2013-11-18 17:01:33 +00:00
2013-12-12 23:47:00 +00:00
NetworkWidget : : NetworkWidget ( QWidget * parent , const std : : string & nwid ) :
2013-11-18 17:01:33 +00:00
QWidget ( parent ) ,
2013-12-12 23:47:00 +00:00
ui ( new Ui : : NetworkWidget ) ,
2014-02-05 20:38:37 +00:00
networkIdStr ( nwid ) ,
publicWarningShown ( false )
2013-11-13 21:50:49 +00:00
{
ui - > setupUi ( this ) ;
2014-01-24 21:26:24 +00:00
ui - > networkIdButton - > setText ( QString ( nwid . c_str ( ) ) ) ;
2013-12-19 22:59:52 +00:00
2013-11-21 18:45:44 +00:00
QFontMetrics fm ( ui - > ipListWidget - > font ( ) ) ;
int lineHeight = ui - > ipListWidget - > spacing ( ) + fm . height ( ) ;
2014-08-20 00:42:17 +00:00
ui - > ipListWidget - > setMinimumHeight ( lineHeight * 6 ) ;
ui - > ipListWidget - > setMaximumHeight ( lineHeight * 6 ) ;
2013-12-19 22:59:52 +00:00
2014-01-27 06:24:29 +00:00
# ifdef __APPLE__
2013-12-19 22:59:52 +00:00
QWidgetList widgets = this - > findChildren < QWidget * > ( ) ;
foreach ( QWidget * widget , widgets )
widget - > setAttribute ( Qt : : WA_MacShowFocusRect , false ) ;
2014-01-27 06:24:29 +00:00
# endif
# ifdef __WINDOWS__
QWidgetList widgets = this - > findChildren < QWidget * > ( ) ;
foreach ( QWidget * widget , widgets ) {
QFont font ( widget - > font ( ) ) ;
font . setPointSizeF ( font . pointSizeF ( ) * 0.75 ) ;
widget - > setFont ( font ) ;
}
# endif
2013-11-13 21:50:49 +00:00
}
2013-12-12 23:47:00 +00:00
NetworkWidget : : ~ NetworkWidget ( )
2013-11-13 21:50:49 +00:00
{
delete ui ;
}
2013-11-18 17:01:33 +00:00
2013-12-12 23:47:00 +00:00
void NetworkWidget : : setStatus ( const std : : string & status , const std : : string & age )
2013-11-20 21:16:30 +00:00
{
ui - > statusLabel - > setText ( QString ( status . c_str ( ) ) ) ;
}
2013-12-12 23:47:00 +00:00
void NetworkWidget : : setNetworkName ( const std : : string & name )
2013-11-20 21:16:30 +00:00
{
2013-12-19 22:59:52 +00:00
if ( name = = " ? " ) {
2013-12-20 02:15:34 +00:00
ui - > nameLabel - > setText ( " ... waiting ... " ) ;
ui - > nameLabel - > setEnabled ( false ) ;
2013-12-19 22:59:52 +00:00
} else {
ui - > nameLabel - > setText ( QString ( name . c_str ( ) ) ) ;
2013-12-20 02:15:34 +00:00
ui - > nameLabel - > setEnabled ( true ) ;
2013-12-19 22:59:52 +00:00
}
2013-11-20 21:16:30 +00:00
}
2013-12-12 23:47:00 +00:00
void NetworkWidget : : setNetworkType ( const std : : string & type )
2013-11-20 21:16:30 +00:00
{
2013-11-20 23:29:02 +00:00
ui - > networkTypeLabel - > setText ( QString ( type . c_str ( ) ) ) ;
2013-11-20 21:16:30 +00:00
if ( type = = " ? " )
2013-12-19 22:59:52 +00:00
ui - > networkTypeLabel - > setStatusTip ( " Waiting for configuration... " ) ;
2014-02-05 20:38:37 +00:00
else if ( type = = " public " ) {
if ( ( ! publicWarningShown ) & & ( ! settings - > value ( " shown_publicWarning " , false ) . toBool ( ) ) ) {
publicWarningShown = true ;
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. " ) ;
d - > setModal ( false ) ;
d - > show ( ) ;
}
2013-12-19 22:59:52 +00:00
ui - > networkTypeLabel - > setStatusTip ( " This network can be joined by anyone in the world. " ) ;
2014-02-05 20:38:37 +00:00
} else if ( type = = " private " )
2013-12-19 22:59:52 +00:00
ui - > networkTypeLabel - > setStatusTip ( " This network is private; only authorized peers can join. " ) ;
else ui - > networkTypeLabel - > setStatusTip ( " Unknown network type. " ) ;
2013-11-20 21:16:30 +00:00
}
2013-12-12 23:47:00 +00:00
void NetworkWidget : : setNetworkDeviceName ( const std : : string & dev )
2013-11-20 21:16:30 +00:00
{
ui - > deviceLabel - > setText ( QString ( dev . c_str ( ) ) ) ;
}
2013-12-12 23:47:00 +00:00
void NetworkWidget : : setIps ( const std : : string & commaSeparatedList )
2013-11-20 21:16:30 +00:00
{
QStringList ips ( QString ( commaSeparatedList . c_str ( ) ) . split ( QChar ( ' , ' ) , QString : : SkipEmptyParts ) ) ;
if ( commaSeparatedList = = " - " )
ips . clear ( ) ;
QStringList tmp ;
ips . sort ( ) ;
for ( QStringList : : iterator i ( ips . begin ( ) ) ; i ! = ips . end ( ) ; + + i ) {
QString ipOnly ( * i ) ;
int slashIdx = ipOnly . indexOf ( ' / ' ) ;
if ( slashIdx > 0 )
ipOnly . truncate ( slashIdx ) ;
tmp . append ( ipOnly ) ;
}
ips = tmp ;
for ( QStringList : : iterator i ( ips . begin ( ) ) ; i ! = ips . end ( ) ; + + i ) {
2013-11-20 23:29:02 +00:00
if ( ui - > ipListWidget - > findItems ( * i , Qt : : MatchCaseSensitive ) . size ( ) = = 0 )
2013-11-20 21:16:30 +00:00
ui - > ipListWidget - > addItem ( * i ) ;
}
2013-11-20 23:29:02 +00:00
for ( int i = 0 ; i < ui - > ipListWidget - > count ( ) ; + + i ) {
QListWidgetItem * item = ui - > ipListWidget - > item ( i ) ;
2013-11-20 21:16:30 +00:00
if ( ! ips . contains ( item - > text ( ) ) )
ui - > ipListWidget - > removeItemWidget ( item ) ;
}
}
2014-08-20 00:42:17 +00:00
void NetworkWidget : : setMAC ( const std : : string & mac )
{
ui - > macLabel - > setText ( QString ( mac . c_str ( ) ) ) ;
}
2013-12-12 23:47:00 +00:00
const std : : string & NetworkWidget : : networkId ( )
2013-11-20 21:16:30 +00:00
{
return networkIdStr ;
}
2013-12-12 23:47:00 +00:00
void NetworkWidget : : on_leaveNetworkButton_clicked ( )
2013-11-18 17:01:33 +00:00
{
2013-11-20 21:16:30 +00:00
if ( QMessageBox : : question ( this , " Leave Network? " , QString ( " Are you sure you want to leave network ' " ) + networkIdStr . c_str ( ) + " '? " , QMessageBox : : No , QMessageBox : : Yes ) = = QMessageBox : : Yes ) {
this - > setEnabled ( false ) ;
2013-12-21 00:07:20 +00:00
zeroTierClient - > send ( ( QString ( " leave " ) + networkIdStr . c_str ( ) ) . toStdString ( ) ) ;
2013-11-20 21:16:30 +00:00
}
2013-11-18 17:01:33 +00:00
}
2014-01-24 21:26:24 +00:00
void NetworkWidget : : on_networkIdButton_clicked ( )
2013-11-18 17:01:33 +00:00
{
2014-01-24 21:26:24 +00:00
QApplication : : clipboard ( ) - > setText ( ui - > networkIdButton - > text ( ) ) ;
2013-11-18 17:01:33 +00:00
}
2013-12-18 02:20:20 +00:00
2013-12-19 00:52:21 +00:00
void NetworkWidget : : on_ipListWidget_itemActivated ( QListWidgetItem * item )
2013-12-18 02:20:20 +00:00
{
2014-01-27 22:55:56 +00:00
if ( item )
QApplication : : clipboard ( ) - > setText ( item - > text ( ) ) ;
}
void NetworkWidget : : on_ipListWidget_currentItemChanged ( QListWidgetItem * current , QListWidgetItem * previous )
{
if ( current )
QApplication : : clipboard ( ) - > setText ( current - > text ( ) ) ;
2013-12-18 02:20:20 +00:00
}