2011-07-11 11:53:41 -04:00
/****************************************************************
* RetroShare is distributed under the following license :
*
* Copyright ( C ) 2008 Robert Fernie
*
* 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>
2011-08-21 18:28:19 -04:00
# include <QMessageBox>
2011-07-11 11:53:41 -04:00
# include "SecurityItem.h"
# include "FeedHolder.h"
2012-02-19 10:03:07 -05:00
# include "retroshare-gui/RsAutoUpdatePage.h"
2011-07-11 11:53:41 -04:00
# include "gui/msgs/MessageComposer.h"
# include "gui/common/StatusDefs.h"
2011-08-21 18:28:19 -04:00
# include "gui/connect/ConfCertDialog.h"
2012-08-28 19:39:11 -04:00
# include "gui/connect/ConnectFriendWizard.h"
2011-09-02 06:22:44 -04:00
# include "gui/common/AvatarDefs.h"
2012-11-15 16:35:37 -05:00
# include "util/DateTime.h"
2011-07-11 11:53:41 -04:00
# include "gui/notifyqt.h"
# include <retroshare/rsmsgs.h>
# include <retroshare/rspeers.h>
/*****
* # define DEBUG_ITEM 1
* * * */
/** Constructor */
2013-01-19 14:24:08 -05:00
SecurityItem : : SecurityItem ( FeedHolder * parent , uint32_t feedId , const std : : string & gpgId , const std : : string & sslId , const std : : string & sslCn , const std : : string & ip_address , uint32_t type , bool isHome )
2011-07-11 11:53:41 -04:00
: QWidget ( NULL ) , mParent ( parent ) , mFeedId ( feedId ) ,
2013-06-10 09:27:24 -04:00
mGpgId ( gpgId ) , mSslId ( sslId ) , mSslCn ( sslCn ) , mIP ( ip_address ) , mType ( type ) , mIsHome ( isHome )
2011-07-11 11:53:41 -04:00
{
/* Invoke the Qt Designer generated object setup routine */
setupUi ( this ) ;
2012-03-31 19:05:30 -04:00
quickmsgButton - > hide ( ) ;
chatButton - > hide ( ) ;
2011-08-21 18:28:19 -04:00
removeFriendButton - > setEnabled ( false ) ;
removeFriendButton - > hide ( ) ;
peerDetailsButton - > setEnabled ( false ) ;
2012-04-04 11:44:35 -04:00
friendRequesttoolButton - > hide ( ) ;
2012-09-07 13:30:23 -04:00
requestLabel - > hide ( ) ;
2011-07-11 11:53:41 -04:00
/* general ones */
connect ( expandButton , SIGNAL ( clicked ( void ) ) , this , SLOT ( toggle ( void ) ) ) ;
connect ( clearButton , SIGNAL ( clicked ( void ) ) , this , SLOT ( removeItem ( void ) ) ) ;
/* specific ones */
connect ( chatButton , SIGNAL ( clicked ( void ) ) , this , SLOT ( openChat ( void ) ) ) ;
2013-08-08 11:09:20 -04:00
connect ( quickmsgButton , SIGNAL ( clicked ( ) ) , this , SLOT ( sendMsg ( ) ) ) ;
2011-07-11 11:53:41 -04:00
2012-04-04 11:44:35 -04:00
connect ( removeFriendButton , SIGNAL ( clicked ( ) ) , this , SLOT ( removeFriend ( ) ) ) ;
connect ( peerDetailsButton , SIGNAL ( clicked ( ) ) , this , SLOT ( peerDetails ( ) ) ) ;
connect ( friendRequesttoolButton , SIGNAL ( clicked ( ) ) , this , SLOT ( friendRequest ( ) ) ) ;
2011-07-11 11:53:41 -04:00
2011-08-21 18:28:19 -04:00
connect ( NotifyQt : : getInstance ( ) , SIGNAL ( friendsChanged ( ) ) , this , SLOT ( updateItem ( ) ) ) ;
2011-07-11 11:53:41 -04:00
2011-09-05 17:19:07 -04:00
avatar - > setId ( mSslId , false ) ;
2012-11-10 13:59:58 -05:00
expandFrame - > hide ( ) ;
2011-07-11 11:53:41 -04:00
updateItemStatic ( ) ;
updateItem ( ) ;
}
bool SecurityItem : : isSame ( const std : : string & sslId , uint32_t type )
{
if ( ( mSslId = = sslId ) & & ( mType = = type ) )
{
return true ;
}
return false ;
}
void SecurityItem : : updateItemStatic ( )
{
if ( ! rsPeers )
return ;
/* fill in */
# ifdef DEBUG_ITEM
std : : cerr < < " SecurityItem::updateItemStatic() " ;
std : : cerr < < std : : endl ;
# endif
QString title ;
switch ( mType )
{
2014-01-15 15:19:17 -05:00
case RS_FEED_ITEM_SEC_CONNECT_ATTEMPT :
2011-07-11 11:53:41 -04:00
title = tr ( " Connect Attempt " ) ;
2012-09-07 13:30:23 -04:00
requestLabel - > show ( ) ;
2012-03-30 05:30:52 -04:00
avatar - > setDefaultAvatar ( " :images/avatar_request.png " ) ;
2011-07-11 11:53:41 -04:00
break ;
2014-01-15 15:19:17 -05:00
case RS_FEED_ITEM_SEC_AUTH_DENIED :
2013-02-22 16:42:27 -05:00
title = tr ( " Connection refused by remote peer " ) ;
2012-09-07 13:30:23 -04:00
requestLabel - > hide ( ) ;
2013-06-21 07:45:11 -04:00
avatar - > setDefaultAvatar ( " :images/avatar_refused.png " ) ;
2011-07-11 11:53:41 -04:00
break ;
2014-01-15 15:19:17 -05:00
case RS_FEED_ITEM_SEC_UNKNOWN_IN :
2011-07-11 11:53:41 -04:00
title = tr ( " Unknown (Incoming) Connect Attempt " ) ;
2012-09-07 13:30:23 -04:00
requestLabel - > hide ( ) ;
2012-03-30 05:30:52 -04:00
avatar - > setDefaultAvatar ( " :images/avatar_request_unknown.png " ) ;
2011-07-11 11:53:41 -04:00
break ;
2014-01-15 15:19:17 -05:00
case RS_FEED_ITEM_SEC_UNKNOWN_OUT :
2011-07-11 11:53:41 -04:00
title = tr ( " Unknown (Outgoing) Connect Attempt " ) ;
2012-09-07 13:30:23 -04:00
requestLabel - > hide ( ) ;
2011-07-11 11:53:41 -04:00
break ;
2014-01-15 15:19:17 -05:00
case RS_FEED_ITEM_SEC_WRONG_SIGNATURE :
2014-01-16 15:51:13 -05:00
title = tr ( " Certificate has wrong signature!! This peer is not who he claims to be. " ) ;
2014-01-15 15:19:17 -05:00
requestLabel - > hide ( ) ;
break ;
2014-01-28 16:33:17 -05:00
case RS_FEED_ITEM_SEC_MISSING_CERTIFICATE :
title = tr ( " Peer/location not in friendlist (PGP id= " ) + QString : : fromStdString ( mGpgId ) + " ) " ;
avatar - > setDefaultAvatar ( " :images/avatar_request_unknown.png " ) ;
requestLabel - > show ( ) ;
break ;
2014-01-15 15:19:17 -05:00
case RS_FEED_ITEM_SEC_BAD_CERTIFICATE :
2014-01-16 15:20:17 -05:00
{
RsPeerDetails details ;
if ( rsPeers - > getPeerDetails ( mGpgId , details ) )
2014-01-16 15:51:13 -05:00
title = tr ( " Missing/Damaged SSL certificate for key " ) + QString : : fromStdString ( mGpgId ) ;
2014-01-16 15:20:17 -05:00
else
title = tr ( " Missing/Damaged certificate. Not a real Retroshare user. " ) ;
2014-01-15 15:19:17 -05:00
requestLabel - > hide ( ) ;
2014-01-16 15:20:17 -05:00
}
2014-01-15 15:19:17 -05:00
break ;
case RS_FEED_ITEM_SEC_INTERNAL_ERROR :
title = tr ( " Certificate caused an internal error. " ) ;
requestLabel - > hide ( ) ;
break ;
2011-07-11 11:53:41 -04:00
default :
title = tr ( " Unknown Security Issue " ) ;
2012-09-07 13:30:23 -04:00
requestLabel - > hide ( ) ;
2011-07-11 11:53:41 -04:00
break ;
}
titleLabel - > setText ( title ) ;
2011-08-22 05:32:03 -04:00
QDateTime currentTime = QDateTime : : currentDateTime ( ) ;
2012-11-15 16:35:37 -05:00
timeLabel - > setText ( DateTime : : formatLongDateTime ( currentTime . toTime_t ( ) ) ) ;
2011-08-22 05:32:03 -04:00
2011-07-11 11:53:41 -04:00
if ( mIsHome )
{
/* disable buttons */
clearButton - > setEnabled ( false ) ;
2011-08-21 18:28:19 -04:00
/* hide buttons */
2011-07-11 11:53:41 -04:00
clearButton - > hide ( ) ;
}
}
void SecurityItem : : updateItem ( )
{
if ( ! rsPeers )
return ;
/* fill in */
# ifdef DEBUG_ITEM
std : : cerr < < " SecurityItem::updateItem() " ;
std : : cerr < < std : : endl ;
# endif
2011-08-21 18:28:19 -04:00
2011-07-11 11:53:41 -04:00
if ( ! RsAutoUpdatePage : : eventsLocked ( ) ) {
RsPeerDetails details ;
2011-08-21 18:28:19 -04:00
/* first try sslid */
if ( ! rsPeers - > getPeerDetails ( mSslId , details ) )
2011-07-11 11:53:41 -04:00
{
2011-08-21 18:28:19 -04:00
/* then gpgid */
2013-08-08 11:09:20 -04:00
if ( ! rsPeers - > getPeerDetails ( mGpgId , details ) )
2011-08-21 18:28:19 -04:00
{
/* it is very likely that we will end up here for some of the
* Unknown peer cases . . . . so allow them here
*/
/* set peer name */
2013-01-19 14:24:08 -05:00
peerNameLabel - > setText ( QString ( " %1 (%2) " ) . arg ( tr ( " Unknown Peer " ) , QString : : fromUtf8 ( mSslCn . c_str ( ) ) ) ) ;
2011-08-21 18:28:19 -04:00
2013-01-19 14:24:08 -05:00
nameLabel - > setText ( QString : : fromUtf8 ( mSslCn . c_str ( ) ) + " ( " + QString : : fromStdString ( mGpgId ) + " ) " ) ;
2011-08-21 18:28:19 -04:00
idLabel - > setText ( QString : : fromStdString ( mSslId ) ) ;
statusLabel - > setText ( tr ( " Unknown Peer " ) ) ;
trustLabel - > setText ( tr ( " Unknown Peer " ) ) ;
locLabel - > setText ( tr ( " Unknown Peer " ) ) ;
2012-09-09 16:25:39 -04:00
ipLabel - > setText ( QString : : fromStdString ( mIP ) ) ; //tr("Unknown Peer"));
2011-08-21 18:28:19 -04:00
connLabel - > setText ( tr ( " Unknown Peer " ) ) ;
2012-03-31 19:05:30 -04:00
chatButton - > hide ( ) ;
2012-10-08 19:06:34 -04:00
quickmsgButton - > hide ( ) ;
requestLabel - > hide ( ) ;
2012-10-08 08:48:38 -04:00
2013-08-08 11:09:20 -04:00
removeFriendButton - > setEnabled ( false ) ;
removeFriendButton - > hide ( ) ;
peerDetailsButton - > setEnabled ( false ) ;
2011-08-21 18:28:19 -04:00
return ;
}
2011-07-11 11:53:41 -04:00
}
2011-08-21 18:28:19 -04:00
/* set peer name */
2012-09-07 13:30:23 -04:00
peerNameLabel - > setText ( QString : : fromUtf8 ( details . name . c_str ( ) ) ) ;
2011-08-21 18:28:19 -04:00
/* expanded Info */
2012-09-09 16:25:39 -04:00
nameLabel - > setText ( QString : : fromUtf8 ( details . name . c_str ( ) ) + " ( " + QString : : fromStdString ( mGpgId ) + " ) " ) ;
//idLabel->setText(QString::fromStdString(details.id));
idLabel - > setText ( QString : : fromStdString ( mSslId ) ) ;
2011-08-21 18:28:19 -04:00
locLabel - > setText ( QString : : fromUtf8 ( details . location . c_str ( ) ) ) ;
2011-07-11 11:53:41 -04:00
/* top Level info */
QString status = StatusDefs : : peerStateString ( details . state ) ;
#if 0
/* Append additional status info from status service */
StatusInfo statusInfo ;
if ( ( rsStatus ) & & ( rsStatus - > getStatus ( * it , statusInfo ) ) )
{
status . append ( QString : : fromStdString ( " / " + RsStatusString ( statusInfo . status ) ) ) ;
}
# endif
statusLabel - > setText ( status ) ;
2011-08-21 18:28:19 -04:00
trustLabel - > setText ( QString : : fromStdString ( RsPeerTrustString ( details . trustLvl ) ) ) ;
2011-07-11 11:53:41 -04:00
2012-09-09 16:25:39 -04:00
ipLabel - > setText ( QString : : fromStdString ( mIP ) ) ; //QString("%1:%2/%3:%4").arg(QString::fromStdString(details.localAddr)).arg(details.localPort).arg(QString::fromStdString(details.extAddr)).arg(details.extPort));
2011-07-11 11:53:41 -04:00
connLabel - > setText ( StatusDefs : : connectStateString ( details ) ) ;
/* do buttons */
2011-08-21 18:28:19 -04:00
peerDetailsButton - > setEnabled ( true ) ;
2012-03-31 19:05:30 -04:00
if ( details . state & RS_PEER_STATE_CONNECTED )
{
chatButton - > show ( ) ;
}
else
{
chatButton - > hide ( ) ;
}
2011-08-21 18:28:19 -04:00
if ( details . accept_connection )
{
2012-04-04 11:44:35 -04:00
friendRequesttoolButton - > hide ( ) ;
2012-09-22 08:25:47 -04:00
requestLabel - > hide ( ) ;
2011-08-21 18:28:19 -04:00
removeFriendButton - > setEnabled ( true ) ;
removeFriendButton - > show ( ) ;
}
else
{
2012-04-04 11:44:35 -04:00
friendRequesttoolButton - > show ( ) ;
2012-09-22 08:25:47 -04:00
requestLabel - > show ( ) ;
2011-08-21 18:28:19 -04:00
removeFriendButton - > setEnabled ( false ) ;
removeFriendButton - > hide ( ) ;
}
2013-08-08 11:09:20 -04:00
quickmsgButton - > show ( ) ;
2011-07-11 11:53:41 -04:00
}
/* slow Tick */
int msec_rate = 10129 ;
2011-08-21 18:28:19 -04:00
2011-07-11 11:53:41 -04:00
QTimer : : singleShot ( msec_rate , this , SLOT ( updateItem ( void ) ) ) ;
return ;
}
void SecurityItem : : toggle ( )
{
2012-10-08 19:06:34 -04:00
mParent - > lockLayout ( this , true ) ;
2011-07-11 11:53:41 -04:00
if ( expandFrame - > isHidden ( ) )
{
expandFrame - > show ( ) ;
expandButton - > setIcon ( QIcon ( QString ( " :/images/edit_remove24.png " ) ) ) ;
2011-08-21 18:28:19 -04:00
expandButton - > setToolTip ( tr ( " Hide " ) ) ;
2011-07-11 11:53:41 -04:00
}
else
{
expandFrame - > hide ( ) ;
expandButton - > setIcon ( QIcon ( QString ( " :/images/edit_add24.png " ) ) ) ;
2011-08-21 18:28:19 -04:00
expandButton - > setToolTip ( tr ( " Expand " ) ) ;
2011-07-11 11:53:41 -04:00
}
2012-10-08 19:06:34 -04:00
mParent - > lockLayout ( this , false ) ;
2011-07-11 11:53:41 -04:00
}
void SecurityItem : : removeItem ( )
{
# ifdef DEBUG_ITEM
std : : cerr < < " SecurityItem::removeItem() " ;
std : : cerr < < std : : endl ;
# endif
2012-10-08 19:06:34 -04:00
mParent - > lockLayout ( this , true ) ;
2011-07-11 11:53:41 -04:00
hide ( ) ;
2012-10-08 19:06:34 -04:00
mParent - > lockLayout ( this , false ) ;
2011-07-11 11:53:41 -04:00
if ( mParent )
{
mParent - > deleteFeedItem ( this , mFeedId ) ;
}
}
/*********** SPECIFIC FUNCTIOSN ***********************/
void SecurityItem : : removeFriend ( )
{
# ifdef DEBUG_ITEM
std : : cerr < < " SecurityItem::removeFriend() " ;
std : : cerr < < std : : endl ;
# endif
2011-08-21 18:28:19 -04:00
if ( ( QMessageBox : : question ( this , " RetroShare " , tr ( " Do you want to remove this Friend? " ) , QMessageBox : : Yes | QMessageBox : : No , QMessageBox : : Yes ) ) = = QMessageBox : : Yes )
{
rsPeers - > removeFriend ( mGpgId ) ;
}
2011-07-11 11:53:41 -04:00
}
2012-04-04 11:44:35 -04:00
void SecurityItem : : friendRequest ( )
{
# ifdef DEBUG_ITEM
std : : cerr < < " SecurityItem::friendReguest() " ;
std : : cerr < < std : : endl ;
# endif
2013-04-19 07:54:39 -04:00
ConnectFriendWizard * connectFriendWizard = new ConnectFriendWizard ;
2012-08-29 18:40:16 -04:00
connectFriendWizard - > setAttribute ( Qt : : WA_DeleteOnClose , true ) ;
2013-09-01 18:18:39 -04:00
connectFriendWizard - > setGpgId ( mGpgId , mSslId , true ) ;
2013-04-19 07:54:39 -04:00
connectFriendWizard - > show ( ) ;
2012-04-04 11:44:35 -04:00
}
2013-04-19 07:54:39 -04:00
2011-08-21 18:28:19 -04:00
void SecurityItem : : peerDetails ( )
{
# ifdef DEBUG_ITEM
std : : cerr < < " SecurityItem::peerDetails() " ;
std : : cerr < < std : : endl ;
# endif
RsPeerDetails details ;
/* first try sslid */
if ( rsPeers - > getPeerDetails ( mSslId , details ) )
{
ConfCertDialog : : showIt ( mSslId , ConfCertDialog : : PageDetails ) ;
return ;
}
2011-07-11 11:53:41 -04:00
2011-08-21 18:28:19 -04:00
/* then gpgid */
if ( rsPeers - > getPeerDetails ( mGpgId , details ) )
{
ConfCertDialog : : showIt ( mGpgId , ConfCertDialog : : PageDetails ) ;
}
}
2011-07-11 11:53:41 -04:00
void SecurityItem : : sendMsg ( )
{
# ifdef DEBUG_ITEM
std : : cerr < < " SecurityItem::sendMsg() " ;
std : : cerr < < std : : endl ;
# endif
2011-08-21 18:28:19 -04:00
MessageComposer * nMsgDialog = MessageComposer : : newMsg ( ) ;
if ( nMsgDialog = = NULL ) {
return ;
}
2011-07-11 11:53:41 -04:00
2013-08-08 11:09:20 -04:00
std : : string hash ;
if ( rsMsgs - > getDistantMessageHash ( mGpgId , hash ) )
{
nMsgDialog - > addRecipient ( MessageComposer : : TO , hash , mGpgId ) ;
nMsgDialog - > show ( ) ;
nMsgDialog - > activateWindow ( ) ;
}
2011-07-11 11:53:41 -04:00
2011-08-21 18:28:19 -04:00
/* window will destroy itself! */
2011-07-11 11:53:41 -04:00
}
void SecurityItem : : openChat ( )
{
# ifdef DEBUG_ITEM
std : : cerr < < " SecurityItem::openChat() " ;
std : : cerr < < std : : endl ;
# endif
if ( mParent )
{
mParent - > openChat ( mGpgId ) ;
}
}