2016-03-16 19:27:12 +01:00
from PyQt4 import QtCore , QtGui
2016-04-25 18:17:02 +02:00
import time
2016-03-16 19:27:12 +01:00
import shared
2017-03-19 22:08:00 +01:00
2016-03-16 19:27:12 +01:00
from tr import _translate
2017-03-19 22:08:00 +01:00
from inventory import Inventory , PendingDownloadQueue , PendingUpload
2016-03-16 19:27:12 +01:00
import l10n
2017-05-25 14:59:18 +02:00
import network . stats
2016-03-24 13:33:03 +01:00
from retranslateui import RetranslateMixin
2016-03-16 19:27:12 +01:00
from uisignaler import UISignaler
import widgets
2016-03-24 13:33:03 +01:00
class NetworkStatus ( QtGui . QWidget , RetranslateMixin ) :
2016-03-16 19:27:12 +01:00
def __init__ ( self , parent = None ) :
super ( NetworkStatus , self ) . __init__ ( parent )
widgets . load ( ' networkstatus.ui ' , self )
2017-06-24 12:16:12 +02:00
self . tableWidgetConnectionCount . horizontalHeader ( ) . setResizeMode ( QtGui . QHeaderView . ResizeToContents )
2016-04-25 18:17:02 +02:00
self . startup = time . localtime ( )
2016-03-24 10:46:26 +01:00
self . labelStartupTime . setText ( _translate ( " networkstatus " , " Since startup on % 1 " ) . arg (
2016-04-25 18:17:02 +02:00
l10n . formatTimestamp ( self . startup ) ) )
2016-03-16 19:27:12 +01:00
self . UISignalThread = UISignaler . get ( )
QtCore . QObject . connect ( self . UISignalThread , QtCore . SIGNAL (
" updateNumberOfMessagesProcessed() " ) , self . updateNumberOfMessagesProcessed )
QtCore . QObject . connect ( self . UISignalThread , QtCore . SIGNAL (
" updateNumberOfPubkeysProcessed() " ) , self . updateNumberOfPubkeysProcessed )
QtCore . QObject . connect ( self . UISignalThread , QtCore . SIGNAL (
" updateNumberOfBroadcastsProcessed() " ) , self . updateNumberOfBroadcastsProcessed )
QtCore . QObject . connect ( self . UISignalThread , QtCore . SIGNAL (
" updateNetworkStatusTab() " ) , self . updateNetworkStatusTab )
self . timer = QtCore . QTimer ( )
self . timer . start ( 2000 ) # milliseconds
QtCore . QObject . connect ( self . timer , QtCore . SIGNAL ( " timeout() " ) , self . runEveryTwoSeconds )
2016-03-17 19:03:17 +01:00
def formatBytes ( self , num ) :
2016-04-27 13:26:39 +02:00
for x in [ _translate ( " networkstatus " , " byte(s) " , None , QtCore . QCoreApplication . CodecForTr , num ) , " kB " , " MB " , " GB " ] :
2016-03-17 19:03:17 +01:00
if num < 1000.0 :
return " %3.0f %s " % ( num , x )
num / = 1000.0
return " %3.0f %s " % ( num , ' TB ' )
def formatByteRate ( self , num ) :
num / = 1000
2016-04-25 18:17:02 +02:00
return " %4.0f kB " % num
2016-04-27 09:48:53 +02:00
def updateNumberOfObjectsToBeSynced ( self ) :
2017-05-29 00:24:07 +02:00
self . labelSyncStatus . setText ( _translate ( " networkstatus " , " Object(s) to be synced: % n " , None , QtCore . QCoreApplication . CodecForTr , network . stats . pendingDownload ( ) + network . stats . pendingUpload ( ) ) )
2016-03-17 19:03:17 +01:00
2016-03-16 19:27:12 +01:00
def updateNumberOfMessagesProcessed ( self ) :
2017-05-31 10:15:47 +02:00
# self.updateNumberOfObjectsToBeSynced()
2016-03-16 19:27:12 +01:00
self . labelMessageCount . setText ( _translate (
2016-04-27 13:26:39 +02:00
" networkstatus " , " Processed % n person-to-person message(s). " , None , QtCore . QCoreApplication . CodecForTr , shared . numberOfMessagesProcessed ) )
2016-03-16 19:27:12 +01:00
def updateNumberOfBroadcastsProcessed ( self ) :
2017-05-31 10:15:47 +02:00
# self.updateNumberOfObjectsToBeSynced()
2016-03-16 19:27:12 +01:00
self . labelBroadcastCount . setText ( _translate (
2016-04-27 13:26:39 +02:00
" networkstatus " , " Processed % n broadcast message(s). " , None , QtCore . QCoreApplication . CodecForTr , shared . numberOfBroadcastsProcessed ) )
2016-03-16 19:27:12 +01:00
def updateNumberOfPubkeysProcessed ( self ) :
2017-05-31 10:15:47 +02:00
# self.updateNumberOfObjectsToBeSynced()
2016-03-16 19:27:12 +01:00
self . labelPubkeyCount . setText ( _translate (
2016-04-27 13:26:39 +02:00
" networkstatus " , " Processed % n public key(s). " , None , QtCore . QCoreApplication . CodecForTr , shared . numberOfPubkeysProcessed ) )
2016-03-16 19:27:12 +01:00
def updateNumberOfBytes ( self ) :
"""
This function is run every two seconds , so we divide the rate of bytes
sent and received by 2.
"""
self . labelBytesRecvCount . setText ( _translate (
2017-05-25 14:59:18 +02:00
" networkstatus " , " Down: % 1/s Total: % 2 " ) . arg ( self . formatByteRate ( network . stats . downloadSpeed ( ) ) , self . formatBytes ( network . stats . receivedBytes ( ) ) ) )
2016-03-16 19:27:12 +01:00
self . labelBytesSentCount . setText ( _translate (
2017-05-25 14:59:18 +02:00
" networkstatus " , " Up: % 1/s Total: % 2 " ) . arg ( self . formatByteRate ( network . stats . uploadSpeed ( ) ) , self . formatBytes ( network . stats . sentBytes ( ) ) ) )
2016-03-16 19:27:12 +01:00
def updateNetworkStatusTab ( self ) :
2017-05-25 14:59:18 +02:00
connectedHosts = network . stats . connectedHostsList ( )
2016-03-16 19:27:12 +01:00
2017-06-24 12:16:12 +02:00
self . tableWidgetConnectionCount . setUpdatesEnabled ( False )
#self.tableWidgetConnectionCount.setSortingEnabled(False)
#self.tableWidgetConnectionCount.clearContents()
self . tableWidgetConnectionCount . setRowCount ( 0 )
for i in connectedHosts :
2016-03-16 19:27:12 +01:00
self . tableWidgetConnectionCount . insertRow ( 0 )
2017-06-24 12:16:12 +02:00
self . tableWidgetConnectionCount . setItem ( 0 , 0 ,
QtGui . QTableWidgetItem ( " %s : %i " % ( i . destination . host , i . destination . port ) )
)
self . tableWidgetConnectionCount . setItem ( 0 , 1 ,
QtGui . QTableWidgetItem ( " %s " % ( i . userAgent ) )
)
self . tableWidgetConnectionCount . setItem ( 0 , 2 ,
QtGui . QTableWidgetItem ( " %s " % ( i . tlsVersion ) )
)
self . tableWidgetConnectionCount . setItem ( 0 , 3 ,
QtGui . QTableWidgetItem ( " %s " % ( " , " . join ( map ( str , i . streams ) ) ) )
)
if i . isOutbound :
brush = QtGui . QBrush ( QtGui . QColor ( " yellow " ) , QtCore . Qt . SolidPattern )
2016-03-16 19:27:12 +01:00
else :
2017-06-24 12:16:12 +02:00
brush = QtGui . QBrush ( QtGui . QColor ( " green " ) , QtCore . Qt . SolidPattern )
for j in ( range ( 1 ) ) :
self . tableWidgetConnectionCount . item ( 0 , j ) . setBackground ( brush )
self . tableWidgetConnectionCount . setUpdatesEnabled ( True )
#self.tableWidgetConnectionCount.setSortingEnabled(True)
#self.tableWidgetConnectionCount.horizontalHeader().setSortIndicator(1, QtCore.Qt.AscendingOrder)
2016-03-16 19:27:12 +01:00
self . labelTotalConnections . setText ( _translate (
2017-05-25 14:59:18 +02:00
" networkstatus " , " Total Connections: % 1 " ) . arg ( str ( len ( connectedHosts ) ) ) )
2017-06-24 12:16:12 +02:00
# FYI: The 'singlelistener' thread sets the icon color to green when it receives an incoming connection, meaning that the user's firewall is configured correctly.
if connectedHosts and shared . statusIconColor == ' red ' :
2016-03-16 19:27:12 +01:00
self . window ( ) . setStatusIcon ( ' yellow ' )
2017-06-24 12:16:12 +02:00
elif not connectedHosts and shared . statusIconColor != " red " :
2016-03-16 19:27:12 +01:00
self . window ( ) . setStatusIcon ( ' red ' )
# timer driven
def runEveryTwoSeconds ( self ) :
self . labelLookupsPerSecond . setText ( _translate (
2017-01-10 21:15:35 +01:00
" networkstatus " , " Inventory lookups per second: % 1 " ) . arg ( str ( Inventory ( ) . numberOfInventoryLookupsPerformed / 2 ) ) )
Inventory ( ) . numberOfInventoryLookupsPerformed = 0
2016-03-16 19:27:12 +01:00
self . updateNumberOfBytes ( )
2016-04-27 12:16:21 +02:00
self . updateNumberOfObjectsToBeSynced ( )
2017-05-31 10:15:47 +02:00
self . updateNumberOfMessagesProcessed ( )
self . updateNumberOfBroadcastsProcessed ( )
self . updateNumberOfPubkeysProcessed ( )
2016-04-25 18:17:02 +02:00
def retranslateUi ( self ) :
2017-06-24 12:16:12 +02:00
super ( NetworkStatus , self ) . retranslateUi ( )
2016-04-25 18:17:02 +02:00
self . labelStartupTime . setText ( _translate ( " networkstatus " , " Since startup on % 1 " ) . arg (
l10n . formatTimestamp ( self . startup ) ) )