2013-11-13 21:50:49 +00:00
# include "network.h"
2013-11-20 21:16:30 +00:00
# include "mainwindow.h"
2013-11-13 21:50:49 +00:00
# include "ui_network.h"
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>
2013-11-18 17:01:33 +00:00
2013-11-20 21:16:30 +00:00
Network : : Network ( QWidget * parent , const std : : string & nwid ) :
2013-11-18 17:01:33 +00:00
QWidget ( parent ) ,
2013-11-20 21:16:30 +00:00
ui ( new Ui : : Network ) ,
networkIdStr ( nwid )
2013-11-13 21:50:49 +00:00
{
ui - > setupUi ( this ) ;
2013-11-20 21:16:30 +00:00
ui - > networkIdPushButton - > setText ( QString ( nwid . c_str ( ) ) ) ;
2013-11-21 18:45:44 +00:00
QFontMetrics fm ( ui - > ipListWidget - > font ( ) ) ;
int lineHeight = ui - > ipListWidget - > spacing ( ) + fm . height ( ) ;
ui - > ipListWidget - > setMinimumHeight ( lineHeight * 3 ) ;
ui - > ipListWidget - > setMaximumHeight ( lineHeight * 3 ) ;
2013-11-13 21:50:49 +00:00
}
Network : : ~ Network ( )
{
delete ui ;
}
2013-11-18 17:01:33 +00:00
2013-11-21 20:11:22 +00:00
void Network : : 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-11-21 20:11:22 +00:00
if ( status = = " OK " )
ui - > ageLabel - > setText ( QString ( " (configuration is " ) + age . c_str ( ) + " seconds old) " ) ;
else ui - > ageLabel - > setText ( QString ( ) ) ;
2013-11-20 21:16:30 +00:00
}
2013-11-20 23:29:02 +00:00
void Network : : setNetworkName ( const std : : string & name )
2013-11-20 21:16:30 +00:00
{
2013-11-20 23:29:02 +00:00
ui - > nameLabel - > setText ( QString ( name . c_str ( ) ) ) ;
2013-11-20 21:16:30 +00:00
}
void Network : : setNetworkType ( const std : : string & type )
{
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 = = " ? " )
ui - > networkTypeLabel - > setToolTip ( " Waiting for configuration... " ) ;
else if ( type = = " public " )
ui - > networkTypeLabel - > setToolTip ( " This network can be joined by anyone. " ) ;
else if ( type = = " private " )
ui - > networkTypeLabel - > setToolTip ( " This network is private, only authorized peers can join. " ) ;
else ui - > networkTypeLabel - > setToolTip ( QString ( ) ) ;
}
void Network : : setNetworkDeviceName ( const std : : string & dev )
{
ui - > deviceLabel - > setText ( QString ( dev . c_str ( ) ) ) ;
}
void Network : : setIps ( const std : : string & commaSeparatedList )
{
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 ) ;
}
}
const std : : string & Network : : networkId ( )
{
return networkIdStr ;
}
2013-11-18 17:01:33 +00:00
void Network : : on_leaveNetworkButton_clicked ( )
{
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 ) {
zeroTierClient - > send ( ( QString ( " leave " ) + networkIdStr . c_str ( ) ) . toStdString ( ) ) ;
this - > setEnabled ( false ) ;
}
2013-11-18 17:01:33 +00:00
}
void Network : : on_networkIdPushButton_clicked ( )
{
QApplication : : clipboard ( ) - > setText ( ui - > networkIdPushButton - > text ( ) ) ;
}