2008-11-15 09:29:34 -05:00
# include <math.h>
2008-11-15 17:23:16 -05:00
# include <QTimer>
2008-11-15 09:29:34 -05:00
# include <QWheelEvent>
2008-11-29 12:55:13 -05:00
# include <QHeaderView>
2010-08-06 05:40:23 -04:00
# include <retroshare/rsiface.h>
# include <retroshare/rspeers.h>
2008-11-15 09:29:34 -05:00
# include <QTableView>
# include "TrustView.h"
using namespace std ;
TrustView : : TrustView ( )
2010-03-28 16:46:45 -04:00
: RsAutoUpdatePage ( 10000 )
2008-11-15 09:29:34 -05:00
{
setupUi ( this ) ;
trustTableTW - > setMouseTracking ( true ) ;
2008-11-29 12:55:13 -05:00
// trustInconsistencyCB->setEnabled(false) ;
2008-11-15 09:29:34 -05:00
zoomHS - > setValue ( 100 ) ;
QObject : : connect ( zoomHS , SIGNAL ( valueChanged ( int ) ) , this , SLOT ( updateZoom ( int ) ) ) ;
QObject : : connect ( updatePB , SIGNAL ( clicked ( ) ) , this , SLOT ( update ( ) ) ) ;
QObject : : connect ( trustTableTW , SIGNAL ( cellClicked ( int , int ) ) , this , SLOT ( selectCell ( int , int ) ) ) ;
2008-11-29 12:55:13 -05:00
QObject : : connect ( trustTableTW - > verticalHeader ( ) , SIGNAL ( sectionClicked ( int ) ) , this , SLOT ( hideShowPeers ( int ) ) ) ;
QObject : : connect ( trustTableTW - > horizontalHeader ( ) , SIGNAL ( sectionClicked ( int ) ) , this , SLOT ( hideShowPeers ( int ) ) ) ;
2008-11-15 09:29:34 -05:00
2010-03-09 16:12:07 -05:00
updatePB - > setToolTip ( tr ( " This table normaly auto-updates every 10 seconds. " ) ) ;
2008-11-15 09:29:34 -05:00
}
2009-08-29 07:25:06 -04:00
void TrustView : : showEvent ( QShowEvent * e )
{
QWidget : : showEvent ( e ) ;
update ( ) ;
}
2008-11-15 09:29:34 -05:00
void TrustView : : wheelEvent ( QWheelEvent * e )
{
if ( e - > modifiers ( ) & Qt : : ShiftModifier )
{
if ( e - > delta ( ) > 0 )
zoomHS - > setValue ( zoomHS - > value ( ) - 5 ) ;
else
zoomHS - > setValue ( zoomHS - > value ( ) + 5 ) ;
}
}
void TrustView : : selectCell ( int row , int col )
{
static int last_row = - 1 ;
static int last_col = - 1 ;
2010-03-28 16:46:45 -04:00
# ifdef DEBUG_TRUSTVIEW
2008-11-29 12:55:13 -05:00
cout < < " row = " < < row < < " , column = " < < col < < endl ;
if ( row = = 0 | | col = = 0 )
cout < < " *********************************************** " < < endl ;
2010-03-28 16:46:45 -04:00
# endif
2008-11-15 09:29:34 -05:00
if ( last_row > - 1 )
{
int col_s , row_s ;
getCellSize ( zoomHS - > value ( ) , col_s , row_s ) ;
trustTableTW - > setColumnWidth ( last_col , col_s ) ;
trustTableTW - > setRowHeight ( last_row , row_s ) ;
}
if ( row ! = last_row | | col ! = last_col )
{
trustTableTW - > setColumnWidth ( col , _base_cell_width ) ;
trustTableTW - > setRowHeight ( row , _base_cell_height ) ;
2008-11-29 12:55:13 -05:00
last_col = col ;
last_row = row ;
}
else
{
last_col = - 1 ;
last_row = - 1 ;
}
2008-11-15 09:29:34 -05:00
}
void TrustView : : getCellSize ( int z , int & col_s , int & row_s ) const
{
col_s = max ( 10 , ( int ) rint ( z / 100.0 * _base_cell_width ) ) ;
row_s = max ( 10 , ( int ) rint ( z / 100.0 * _base_cell_height ) ) ;
}
void TrustView : : updateZoom ( int z )
{
int col_s , row_s ;
getCellSize ( z , col_s , row_s ) ;
for ( int i = 0 ; i < trustTableTW - > columnCount ( ) ; + + i )
trustTableTW - > setColumnWidth ( i , col_s ) ;
for ( int i = 0 ; i < trustTableTW - > rowCount ( ) ; + + i )
trustTableTW - > setRowHeight ( i , row_s ) ;
2008-11-15 17:23:16 -05:00
// cout << "updated zoom" << endl;
2008-11-15 09:29:34 -05:00
}
2009-08-29 07:25:06 -04:00
int TrustView : : getRowColId ( const string & peerid )
2008-11-15 09:29:34 -05:00
{
2009-08-29 07:25:06 -04:00
static map < string , int > peeridToRow ;
2008-11-15 09:29:34 -05:00
2009-08-29 07:25:06 -04:00
map < string , int > : : const_iterator itpr ( peeridToRow . find ( peerid ) ) ;
2008-11-15 09:29:34 -05:00
int i ;
2009-08-29 07:25:06 -04:00
if ( itpr = = peeridToRow . end ( ) )
2008-11-15 09:29:34 -05:00
{
i = trustTableTW - > columnCount ( ) ;
trustTableTW - > insertColumn ( i ) ;
trustTableTW - > insertRow ( i ) ;
2009-08-29 07:25:06 -04:00
peeridToRow [ peerid ] = i ;
std : : string name = rsPeers - > getPeerName ( peerid ) ;
2008-11-15 09:29:34 -05:00
trustTableTW - > setHorizontalHeaderItem ( i , new QTableWidgetItem ( QString ( name . c_str ( ) ) ) ) ;
trustTableTW - > setVerticalHeaderItem ( i , new QTableWidgetItem ( QString ( name . c_str ( ) ) ) ) ;
trustTableTW - > setColumnWidth ( i , _base_cell_width ) ;
trustTableTW - > setRowHeight ( i , _base_cell_height ) ;
}
else
i = ( * itpr ) . second ;
return i ;
}
2010-03-28 16:46:45 -04:00
void TrustView : : updateDisplay ( )
{
update ( ) ;
}
2008-11-15 09:29:34 -05:00
void TrustView : : update ( )
{
std : : list < std : : string > neighs ;
2010-03-28 16:46:45 -04:00
if ( ! rsPeers - > getGPGAllList ( neighs ) )
2008-11-15 09:29:34 -05:00
return ;
2010-03-28 16:46:45 -04:00
neighs . push_back ( rsPeers - > getGPGOwnId ( ) ) ;
2008-11-15 09:29:34 -05:00
trustTableTW - > setSortingEnabled ( false ) ;
RsPeerDetails details ;
// Fill everything
for ( list < string > : : const_iterator it1 ( neighs . begin ( ) ) ; it1 ! = neighs . end ( ) ; + + it1 )
{
2009-11-17 07:45:06 -05:00
std : : list < std : : string > friends_ids ;
2008-11-15 09:29:34 -05:00
if ( ! rsPeers - > getPeerDetails ( * it1 , details ) )
continue ;
2009-08-29 07:25:06 -04:00
int i = getRowColId ( details . id ) ;
2009-11-17 07:45:06 -05:00
std : : string issuer ( details . issuer ) ; // the one we check for trust.
2008-11-15 09:29:34 -05:00
2010-03-28 16:46:45 -04:00
for ( list < string > : : const_iterator it2 ( details . gpgSigners . begin ( ) ) ; it2 ! = details . gpgSigners . end ( ) ; + + it2 )
2008-11-15 09:29:34 -05:00
{
2010-03-28 16:46:45 -04:00
# ifdef DEBUG_TRUSTVIEW
2009-08-29 07:25:06 -04:00
cout < < * it2 < < " " ;
2010-03-28 16:46:45 -04:00
# endif
2008-11-15 09:29:34 -05:00
int j = getRowColId ( * it2 ) ;
QString trr ( ( i = = j ) ? " Self " : " Trust " ) ;
if ( trustTableTW - > item ( i , j ) = = NULL )
trustTableTW - > setItem ( i , j , new QTableWidgetItem ( trr ) ) ;
else
trustTableTW - > item ( i , j ) - > setText ( trr ) ;
}
2010-03-28 16:46:45 -04:00
// cout << endl ;
2008-11-15 09:29:34 -05:00
}
// assign colors
2008-11-16 15:17:36 -05:00
vector < int > ni ( trustTableTW - > rowCount ( ) , 0 ) ;
vector < int > nj ( trustTableTW - > columnCount ( ) , 0 ) ;
2008-11-15 09:29:34 -05:00
for ( int i = 0 ; i < trustTableTW - > rowCount ( ) ; + + i )
for ( int j = 0 ; j < trustTableTW - > columnCount ( ) ; + + j )
{
QTableWidgetItem * i_ij ( trustTableTW - > item ( i , j ) ) ;
QTableWidgetItem * i_ji ( trustTableTW - > item ( j , i ) ) ;
QColor color ;
// check bidirectional trust
//
if ( i_ij ! = NULL )
{
2008-11-16 15:17:36 -05:00
+ + ni [ i ] ;
+ + nj [ j ] ;
2008-11-15 09:29:34 -05:00
if ( i_ji = = NULL )
{
i_ij - > setBackgroundColor ( Qt : : yellow ) ;
2010-03-28 16:46:45 -04:00
i_ij - > setToolTip ( trustTableTW - > horizontalHeaderItem ( i ) - > text ( ) + tr ( " is athenticated (one way) by " ) + trustTableTW - > verticalHeaderItem ( j ) - > text ( ) ) ;
2010-03-09 16:12:07 -05:00
i_ij - > setText ( tr ( " Half " ) ) ;
2008-11-15 09:29:34 -05:00
}
else
{
if ( i = = j )
{
i_ij - > setBackgroundColor ( Qt : : red ) ;
2010-03-28 16:46:45 -04:00
i_ij - > setToolTip ( trustTableTW - > horizontalHeaderItem ( i ) - > text ( ) + tr ( " athenticated himself " ) ) ;
2008-11-15 09:29:34 -05:00
}
else
{
i_ij - > setBackgroundColor ( Qt : : green ) ;
2010-03-28 16:46:45 -04:00
i_ij - > setToolTip ( trustTableTW - > horizontalHeaderItem ( i ) - > text ( ) + " and " + trustTableTW - > verticalHeaderItem ( j ) - > text ( ) + tr ( " athenticated each others " ) ) ;
2010-03-09 16:12:07 -05:00
i_ij - > setText ( tr ( " Full " ) ) ;
2008-11-15 09:29:34 -05:00
}
}
}
}
2008-11-16 15:17:36 -05:00
for ( int i = 0 ; i < trustTableTW - > rowCount ( ) ; + + i )
2010-03-28 16:46:45 -04:00
trustTableTW - > verticalHeaderItem ( i ) - > setToolTip ( trustTableTW - > verticalHeaderItem ( i ) - > text ( ) + tr ( " is athenticated by " ) + QString : : number ( ni [ i ] ) + tr ( " peers, including him(her)self. " ) ) ;
2008-11-16 15:17:36 -05:00
for ( int j = 0 ; j < trustTableTW - > columnCount ( ) ; + + j )
2010-03-28 16:46:45 -04:00
trustTableTW - > horizontalHeaderItem ( j ) - > setToolTip ( trustTableTW - > horizontalHeaderItem ( j ) - > text ( ) + tr ( " athenticated " ) + QString : : number ( nj [ j ] ) + tr ( " peers, including him(her)self. " ) ) ;
2008-11-29 12:55:13 -05:00
}
void TrustView : : hideShowPeers ( int col )
{
// Choose what to show/hide
//
static int last_col = - 1 ;
if ( col = = last_col )
{
for ( int i = 0 ; i < trustTableTW - > rowCount ( ) ; + + i )
{
trustTableTW - > setColumnHidden ( i , false ) ;
trustTableTW - > setRowHidden ( i , false ) ;
}
last_col = - 1 ;
2010-03-09 16:12:07 -05:00
showingLabel - > setText ( tr ( " Showing: whole network " ) ) ;
2008-11-29 12:55:13 -05:00
}
else
{
for ( int i = 0 ; i < trustTableTW - > rowCount ( ) ; + + i )
if ( trustTableTW - > item ( i , col ) = = NULL & & trustTableTW - > item ( col , i ) = = NULL )
{
trustTableTW - > setColumnHidden ( i , true ) ;
trustTableTW - > setRowHidden ( i , true ) ;
}
else
{
trustTableTW - > setColumnHidden ( i , false ) ;
trustTableTW - > setRowHidden ( i , false ) ;
}
last_col = col ;
2010-03-09 16:12:07 -05:00
showingLabel - > setText ( tr ( " Showing: peers connected to " ) + trustTableTW - > verticalHeaderItem ( col ) - > text ( ) ) ;
2008-11-29 12:55:13 -05:00
}
2008-11-15 09:29:34 -05:00
}