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"
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>
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 ) ,
2013-11-20 21:16:30 +00:00
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
}
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-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-12-12 23:47:00 +00:00
void NetworkWidget : : 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
}
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 = = " ? " )
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 ( ) ) ;
}
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 ) ;
}
}
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 ) {
zeroTierClient - > send ( ( QString ( " leave " ) + networkIdStr . c_str ( ) ) . toStdString ( ) ) ;
this - > setEnabled ( false ) ;
}
2013-11-18 17:01:33 +00:00
}
2013-12-12 23:47:00 +00:00
void NetworkWidget : : on_networkIdPushButton_clicked ( )
2013-11-18 17:01:33 +00:00
{
QApplication : : clipboard ( ) - > setText ( ui - > networkIdPushButton - > text ( ) ) ;
}
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
{
2013-12-19 00:52:21 +00:00
if ( item )
QApplication : : clipboard ( ) - > setText ( item - > text ( ) ) ;
2013-12-18 02:20:20 +00:00
}