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-01-19 14:24:08 -05:00
mGpgId ( gpgId ) , mSslId ( sslId ) , mSslCn ( sslCn ) , mType ( type ) , mIsHome ( isHome ) , mIP ( ip_address )
2011-07-11 11:53:41 -04:00
{
/* 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 ( ) ;
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 ) ) ) ;
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 ) ;
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 )
{
case SEC_TYPE_CONNECT_ATTEMPT :
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 ;
case SEC_TYPE_AUTH_DENIED :
title = tr ( " Not Yet Friends " ) ;
2012-09-07 13:30:23 -04:00
requestLabel - > hide ( ) ;
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-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 ;
case SEC_TYPE_UNKNOWN_OUT :
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 ;
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 */
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 */
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
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 */
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 ( ) ;
}
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 : : 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
2012-08-28 19:39:11 -04:00
ConnectFriendWizard * connectFriendWizard = new ConnectFriendWizard ;
2012-08-29 18:40:16 -04:00
connectFriendWizard - > setAttribute ( Qt : : WA_DeleteOnClose , true ) ;
2012-08-28 19:39:11 -04:00
connectFriendWizard - > setGpgId ( mGpgId , true ) ;
connectFriendWizard - > show ( ) ;
2012-04-04 11:44:35 -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
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
}