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-04-04 11:44:35 -04:00
# include "gui/connect/FriendRequest.h"
2011-09-02 06:22:44 -04:00
# include "gui/common/AvatarDefs.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 */
2012-02-01 07:21:14 -05:00
SecurityItem : : SecurityItem ( FeedHolder * parent , uint32_t feedId , const std : : string & gpgId , const std : : string & sslId , uint32_t type , bool isHome )
2011-07-11 11:53:41 -04:00
: QWidget ( NULL ) , mParent ( parent ) , mFeedId ( feedId ) ,
mSslId ( sslId ) , mGpgId ( gpgId ) , mType ( type ) , mIsHome ( isHome )
{
/* Invoke the Qt Designer generated object setup routine */
setupUi ( this ) ;
messageframe - > setVisible ( false ) ;
sendmsgButton - > setEnabled ( false ) ;
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 ( ) ;
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 ) ) ) ;
connect ( actionNew_Message , SIGNAL ( triggered ( ) ) , this , SLOT ( sendMsg ( void ) ) ) ;
connect ( quickmsgButton , SIGNAL ( clicked ( ) ) , this , SLOT ( togglequickmessage ( ) ) ) ;
connect ( cancelButton , SIGNAL ( clicked ( ) ) , this , SLOT ( togglequickmessage ( ) ) ) ;
connect ( sendmsgButton , SIGNAL ( clicked ( ) ) , this , SLOT ( sendMessage ( ) ) ) ;
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
QMenu * msgmenu = new QMenu ( ) ;
msgmenu - > addAction ( actionNew_Message ) ;
quickmsgButton - > setMenu ( msgmenu ) ;
2011-09-05 17:19:07 -04:00
avatar - > setId ( mSslId , false ) ;
2011-07-11 11:53:41 -04:00
small ( ) ;
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 )
{
case SEC_TYPE_CONNECT_ATTEMPT :
title = tr ( " Connect Attempt " ) ;
2012-03-30 05:30:52 -04:00
avatar - > setDefaultAvatar ( " :images/avatar_request.png " ) ;
2011-07-11 11:53:41 -04:00
break ;
case SEC_TYPE_AUTH_DENIED :
title = tr ( " Not Yet Friends " ) ;
2012-03-31 19:05:30 -04:00
avatar - > setDefaultAvatar ( " :images/avatar_request.png " ) ;
2011-07-11 11:53:41 -04:00
break ;
case SEC_TYPE_UNKNOWN_IN :
title = tr ( " Unknown (Incoming) Connect Attempt " ) ;
2012-03-30 05:30:52 -04:00
avatar - > setDefaultAvatar ( " :images/avatar_request_unknown.png " ) ;
2011-07-11 11:53:41 -04:00
break ;
case SEC_TYPE_UNKNOWN_OUT :
title = tr ( " Unknown (Outgoing) Connect Attempt " ) ;
break ;
default :
title = tr ( " Unknown Security Issue " ) ;
break ;
}
titleLabel - > setText ( title ) ;
2011-08-22 05:32:03 -04:00
QDateTime currentTime = QDateTime : : currentDateTime ( ) ;
2012-03-31 19:05:30 -04:00
timeLabel - > setText ( currentTime . toString ( " dd.MMMM yyyy hh:mm " ) ) ;
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 ( ) ) {
2011-08-21 18:28:19 -04:00
/* set textcolor for peer name */
QString nameStr ( " <span style= \" font-size:14pt; font-weight:500;color:#990033; \" >%1</span> " ) ;
2011-07-11 11:53:41 -04:00
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 */
if ( ! rsPeers - > getPeerDetails ( mGpgId , details ) )
{
/* it is very likely that we will end up here for some of the
* Unknown peer cases . . . . so allow them here
*/
/* set peer name */
peernameLabel - > setText ( nameStr . arg ( tr ( " Unknown Peer " ) ) ) ;
nameLabel - > setText ( QString : : fromStdString ( mGpgId ) ) ;
idLabel - > setText ( QString : : fromStdString ( mSslId ) ) ;
statusLabel - > setText ( tr ( " Unknown Peer " ) ) ;
trustLabel - > setText ( tr ( " Unknown Peer " ) ) ;
locLabel - > setText ( tr ( " Unknown Peer " ) ) ;
ipLabel - > setText ( tr ( " Unknown Peer " ) ) ;
connLabel - > setText ( tr ( " Unknown Peer " ) ) ;
2012-03-31 19:05:30 -04:00
chatButton - > hide ( ) ;
quickmsgButton - > hide ( ) ;
2011-08-21 18:28:19 -04:00
2012-04-04 11:44:35 -04:00
friendRequesttoolButton - > show ( ) ;
2011-08-21 18:28:19 -04:00
removeFriendButton - > setEnabled ( false ) ;
removeFriendButton - > hide ( ) ;
peerDetailsButton - > setEnabled ( false ) ;
return ;
}
2011-07-11 11:53:41 -04:00
}
2011-08-21 18:28:19 -04:00
/* set peer name */
QString peername = QString : : fromUtf8 ( details . name . c_str ( ) ) ;
peernameLabel - > setText ( nameStr . arg ( peername ) ) ;
/* expanded Info */
nameLabel - > setText ( QString : : fromUtf8 ( details . name . c_str ( ) ) ) ;
idLabel - > setText ( QString : : fromStdString ( details . id ) ) ;
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-03-28 14:02:49 -04:00
ipLabel - > setText ( 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 ( ) ;
2011-08-21 18:28:19 -04:00
removeFriendButton - > setEnabled ( true ) ;
removeFriendButton - > show ( ) ;
}
else
{
2012-04-04 11:44:35 -04:00
friendRequesttoolButton - > show ( ) ;
2011-08-21 18:28:19 -04:00
removeFriendButton - > setEnabled ( false ) ;
removeFriendButton - > hide ( ) ;
}
2011-07-11 11:53:41 -04:00
if ( details . state & RS_PEER_STATE_FRIEND )
{
2012-03-31 19:05:30 -04:00
quickmsgButton - > show ( ) ;
2011-07-11 11:53:41 -04:00
}
else
{
2012-03-31 19:05:30 -04:00
quickmsgButton - > hide ( ) ;
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 : : small ( )
{
expandFrame - > hide ( ) ;
}
void SecurityItem : : toggle ( )
{
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
}
}
void SecurityItem : : removeItem ( )
{
# ifdef DEBUG_ITEM
std : : cerr < < " SecurityItem::removeItem() " ;
std : : cerr < < std : : endl ;
# endif
hide ( ) ;
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
FriendRequest * frDlg = new FriendRequest ( mGpgId ) ;
frDlg - > show ( ) ;
}
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
2011-08-21 18:28:19 -04:00
nMsgDialog - > addRecipient ( MessageComposer : : TO , mGpgId , false ) ;
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 ) ;
}
}
void SecurityItem : : togglequickmessage ( )
{
if ( messageframe - > isHidden ( ) )
{
2011-08-21 18:28:19 -04:00
messageframe - > setVisible ( true ) ;
}
2011-07-11 11:53:41 -04:00
else
{
2011-08-21 18:28:19 -04:00
messageframe - > setVisible ( false ) ;
}
2011-07-11 11:53:41 -04:00
}
void SecurityItem : : sendMessage ( )
{
2011-08-21 18:28:19 -04:00
/* construct a message */
MessageInfo mi ;
mi . title = tr ( " Quick Message " ) . toStdWString ( ) ;
mi . msg = quickmsgText - > toHtml ( ) . toStdWString ( ) ;
mi . msgto . push_back ( mGpgId ) ;
rsMsgs - > MessageSend ( mi ) ;
quickmsgText - > clear ( ) ;
messageframe - > setVisible ( false ) ;
2011-07-11 11:53:41 -04:00
}
void SecurityItem : : on_quickmsgText_textChanged ( )
{
2011-08-21 18:28:19 -04:00
if ( quickmsgText - > toPlainText ( ) . isEmpty ( ) )
{
sendmsgButton - > setEnabled ( false ) ;
}
else
{
sendmsgButton - > setEnabled ( true ) ;
}
2011-07-11 11:53:41 -04:00
}