2015-06-02 17:36:26 -04:00
/****************************************************************
* RetroShare is distributed under the following license :
*
* Copyright ( C ) 2015 , RetroShare Team
*
* 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 2
* 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 , write to the Free Software
* Foundation , Inc . , 51 Franklin Street , Fifth Floor ,
* Boston , MA 02110 - 1301 , USA .
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
# include <QDateTime>
# include <QTimer>
# include "SecurityIpItem.h"
# include "FeedHolder.h"
# include"ui_SecurityIpItem.h"
# include "retroshare-gui/RsAutoUpdatePage.h"
# include "gui/connect/ConfCertDialog.h"
# include "util/DateTime.h"
# include "gui/common/PeerDefs.h"
# include "gui/common/RsBanListDefs.h"
# include <retroshare/rspeers.h>
# include <retroshare/rsbanlist.h>
2015-06-03 08:38:23 -04:00
# include <retroshare/rsnotify.h>
2015-06-02 17:36:26 -04:00
/*****
* # define DEBUG_ITEM 1
* * * */
/** Constructor */
2015-06-03 08:38:23 -04:00
SecurityIpItem : : SecurityIpItem ( FeedHolder * parent , const RsPeerId & sslId , const std : : string & ipAddr , uint32_t result , uint32_t type , bool isTest ) :
FeedItem ( NULL ) , mParent ( parent ) , mType ( type ) , mSslId ( sslId ) , mIpAddr ( ipAddr ) , mResult ( result ) , mIsTest ( isTest ) ,
2015-06-02 17:36:26 -04:00
ui ( new ( Ui : : SecurityIpItem ) )
2015-06-03 08:38:23 -04:00
{
setup ( ) ;
}
SecurityIpItem : : SecurityIpItem ( FeedHolder * parent , const RsPeerId & sslId , const std : : string & ipAddr , const std : : string & ipAddrReported , uint32_t type , bool isTest ) :
FeedItem ( NULL ) , mParent ( parent ) , mType ( type ) , mSslId ( sslId ) , mIpAddr ( ipAddr ) , mIpAddrReported ( ipAddrReported ) , mResult ( 0 ) , mIsTest ( isTest ) ,
ui ( new ( Ui : : SecurityIpItem ) )
{
setup ( ) ;
}
void SecurityIpItem : : setup ( )
2015-06-02 17:36:26 -04:00
{
/* Invoke the Qt Designer generated object setup routine */
ui - > setupUi ( this ) ;
ui - > peerDetailsButton - > setEnabled ( false ) ;
/* general ones */
connect ( ui - > expandButton , SIGNAL ( clicked ( void ) ) , this , SLOT ( toggle ( void ) ) ) ;
connect ( ui - > clearButton , SIGNAL ( clicked ( void ) ) , this , SLOT ( removeItem ( void ) ) ) ;
/* specific ones */
connect ( ui - > peerDetailsButton , SIGNAL ( clicked ( ) ) , this , SLOT ( peerDetails ( ) ) ) ;
2015-06-03 13:16:06 -04:00
connect ( ui - > rsBanListButton , SIGNAL ( banListChanged ( QString ) ) , this , SLOT ( banIpListChanged ( QString ) ) ) ;
2015-06-02 17:36:26 -04:00
ui - > avatar - > setId ( ChatId ( mSslId ) ) ;
ui - > rsBanListButton - > setMode ( RsBanListToolButton : : LIST_WHITELIST , RsBanListToolButton : : MODE_ADD ) ;
2015-06-03 11:14:26 -04:00
ui - > rsBanListChangedLabel - > hide ( ) ;
2015-06-02 17:36:26 -04:00
ui - > expandFrame - > hide ( ) ;
updateItemStatic ( ) ;
updateItem ( ) ;
}
2015-06-03 08:38:23 -04:00
bool SecurityIpItem : : isSame ( const RsPeerId & sslId , uint32_t type )
2015-06-02 17:36:26 -04:00
{
2015-06-03 08:38:23 -04:00
if ( mSslId = = sslId & & mType = = type ) {
2015-06-02 17:36:26 -04:00
return true ;
}
return false ;
}
void SecurityIpItem : : updateItemStatic ( )
{
if ( ! rsPeers )
return ;
/* fill in */
# ifdef DEBUG_ITEM
std : : cerr < < " SecurityIpItem::updateItemStatic() " ;
std : : cerr < < std : : endl ;
# endif
2015-06-03 08:38:23 -04:00
/* Specific type */
switch ( mType ) {
case RS_FEED_ITEM_SEC_IP_BLACKLISTED :
ui - > rsBanListButton - > setDisabled ( mIsTest ) ;
ui - > ipAddrReported - > hide ( ) ;
ui - > ipAddrReportedLabel - > hide ( ) ;
break ;
case RS_FEED_ITEM_SEC_WRONG_EXTERNAL_IP_REPORTED :
ui - > rsBanListButton - > hide ( ) ;
break ;
default :
std : : cerr < < " SecurityIpItem::updateItem() Wrong type " < < std : : endl ;
}
2015-06-02 17:36:26 -04:00
QDateTime currentTime = QDateTime : : currentDateTime ( ) ;
ui - > timeLabel - > setText ( DateTime : : formatLongDateTime ( currentTime . toTime_t ( ) ) ) ;
}
void SecurityIpItem : : updateItem ( )
{
if ( ! rsPeers )
return ;
/* fill in */
# ifdef DEBUG_ITEM
std : : cerr < < " SecurityIpItem::updateItem() " ;
std : : cerr < < std : : endl ;
# endif
if ( ! RsAutoUpdatePage : : eventsLocked ( ) ) {
2015-06-03 08:38:23 -04:00
switch ( mType ) {
case RS_FEED_ITEM_SEC_IP_BLACKLISTED :
ui - > titleLabel - > setText ( RsBanListDefs : : resultString ( mResult ) ) ;
ui - > ipAddr - > setText ( QString : : fromStdString ( mIpAddr ) ) ;
if ( ! mIsTest ) {
switch ( mResult ) {
case RSBANLIST_CHECK_RESULT_NOCHECK :
case RSBANLIST_CHECK_RESULT_ACCEPTED :
ui - > rsBanListButton - > hide ( ) ;
break ;
2015-06-03 10:01:46 -04:00
case RSBANLIST_CHECK_RESULT_NOT_WHITELISTED :
2015-06-03 08:38:23 -04:00
case RSBANLIST_CHECK_RESULT_BLACKLISTED :
ui - > rsBanListButton - > setVisible ( ui - > rsBanListButton - > setIpAddress ( QString : : fromStdString ( mIpAddr ) ) ) ;
break ;
default :
ui - > rsBanListButton - > hide ( ) ;
}
2015-06-02 17:36:26 -04:00
}
2015-06-03 08:38:23 -04:00
break ;
case RS_FEED_ITEM_SEC_WRONG_EXTERNAL_IP_REPORTED :
ui - > titleLabel - > setText ( tr ( " Wrong external ip address reported " ) ) ;
ui - > ipAddr - > setText ( QString : : fromStdString ( mIpAddr ) ) ;
2015-06-06 07:30:31 -04:00
ui - > ipAddr - > setToolTip ( tr ( " <p>This is the external IP your Retroshare node thinks it is using.</p> " ) ) ;
ui - > ipAddrReported - > setText ( QString : : fromStdString ( mIpAddrReported ) ) ;
2015-06-06 09:50:11 -04:00
ui - > ipAddrReported - > setToolTip ( tr ( " <p>This is the IP your friend claims it is connected to. If you just changed IPs, this is a false warning. If not, that means your connection to this friend is forwarded by an intermediate peer, which would be suspicious.</p> " ) ) ;
2015-06-06 07:30:31 -04:00
break ;
2015-06-03 08:38:23 -04:00
default :
std : : cerr < < " SecurityIpItem::updateItem() Wrong type " < < std : : endl ;
2015-06-02 17:36:26 -04:00
}
RsPeerDetails details ;
if ( ! rsPeers - > getPeerDetails ( mSslId , details ) )
{
/* set peer name */
ui - > peer - > setText ( tr ( " Unknown Peer " ) ) ;
/* expanded Info */
ui - > peerID - > setText ( QString : : fromStdString ( mSslId . toStdString ( ) ) ) ;
ui - > peerName - > setText ( tr ( " Unknown Peer " ) ) ;
ui - > locationLabel - > setText ( tr ( " Unknown Peer " ) ) ;
} else {
/* set peer name */
ui - > peer - > setText ( PeerDefs : : nameWithLocation ( details ) ) ;
/* expanded Info */
ui - > peerID - > setText ( QString : : fromStdString ( details . id . toStdString ( ) ) ) ;
ui - > peerName - > setText ( QString : : fromUtf8 ( details . name . c_str ( ) ) ) ;
ui - > location - > setText ( QString : : fromUtf8 ( details . location . c_str ( ) ) ) ;
/* Buttons */
ui - > peerDetailsButton - > setEnabled ( true ) ;
}
}
/* slow Tick */
int msec_rate = 10129 ;
QTimer : : singleShot ( msec_rate , this , SLOT ( updateItem ( void ) ) ) ;
}
void SecurityIpItem : : toggle ( )
{
expand ( ui - > expandFrame - > isHidden ( ) ) ;
}
void SecurityIpItem : : expand ( bool open )
{
if ( mParent ) {
mParent - > lockLayout ( this , true ) ;
}
if ( open )
{
ui - > expandFrame - > show ( ) ;
ui - > expandButton - > setIcon ( QIcon ( " :/images/edit_remove24.png " ) ) ;
ui - > expandButton - > setToolTip ( tr ( " Hide " ) ) ;
}
else
{
ui - > expandFrame - > hide ( ) ;
ui - > expandButton - > setIcon ( QIcon ( " :/images/edit_add24.png " ) ) ;
ui - > expandButton - > setToolTip ( tr ( " Expand " ) ) ;
}
emit sizeChanged ( this ) ;
if ( mParent ) {
mParent - > lockLayout ( this , false ) ;
}
}
void SecurityIpItem : : removeItem ( )
{
# ifdef DEBUG_ITEM
std : : cerr < < " SecurityIpItem::removeItem() " ;
std : : cerr < < std : : endl ;
# endif
mParent - > lockLayout ( this , true ) ;
hide ( ) ;
mParent - > lockLayout ( this , false ) ;
if ( mParent )
{
mParent - > deleteFeedItem ( this , mFeedId ) ;
}
}
///*********** SPECIFIC FUNCTIONS ***********************/
void SecurityIpItem : : peerDetails ( )
{
# ifdef DEBUG_ITEM
std : : cerr < < " SecurityIpItem::peerDetails() " ;
std : : cerr < < std : : endl ;
# endif
RsPeerDetails details ;
if ( rsPeers - > getPeerDetails ( mSslId , details ) ) {
ConfCertDialog : : showIt ( mSslId , ConfCertDialog : : PageDetails ) ;
}
}
2015-06-03 13:16:06 -04:00
void SecurityIpItem : : banIpListChanged ( const QString & ipAddress )
{
ui - > rsBanListChangedLabel - > setText ( tr ( " IP address %1 was added to the whitelist " ) . arg ( ipAddress ) ) ;
ui - > rsBanListChangedLabel - > show ( ) ;
}