2013-04-18 17:41:13 -04:00
/****************************************************************
* RetroShare is distributed under the following license :
*
* Copyright ( C ) 2013 , Cyril Soler
*
* 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 .
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2013-04-16 17:13:42 -04:00
# include <QTimer>
2013-06-12 17:10:09 -04:00
# include <QCloseEvent>
2013-06-13 17:12:07 -04:00
# include <QMessageBox>
2014-05-27 04:40:23 -04:00
# include <unistd.h>
2013-06-17 17:58:26 -04:00
# include <retroshare/rsstatus.h>
2014-03-17 16:56:06 -04:00
# include <retroshare/rspeers.h>
2014-05-25 17:43:05 -04:00
# include <retroshare/rsidentity.h>
2013-06-17 17:58:26 -04:00
2013-06-12 17:10:09 -04:00
# include "RsAutoUpdatePage.h"
2013-04-16 17:13:42 -04:00
# include "PopupDistantChatDialog.h"
2015-06-30 12:44:58 -04:00
# define IMAGE_RED_LED ": / icons / bullet_red_64.png"
# define IMAGE_YEL_LED ": / icons / bullet_yellow_64.png"
# define IMAGE_GRN_LED ": / icons / bullet_green_64.png"
# define IMAGE_GRY_LED ": / icons / bullet_grey_64.png"
2013-04-23 18:43:19 -04:00
2013-06-12 17:10:09 -04:00
PopupDistantChatDialog : : ~ PopupDistantChatDialog ( )
{
_update_timer - > stop ( ) ;
delete _update_timer ;
2013-04-18 17:41:13 -04:00
}
2015-11-28 14:55:56 -05:00
PopupDistantChatDialog : : PopupDistantChatDialog ( const DistantChatPeerId & tunnel_id , QWidget * parent , Qt : : WindowFlags flags )
2013-04-16 17:13:42 -04:00
: PopupChatDialog ( parent , flags )
{
2015-11-26 20:40:06 -05:00
_tunnel_id = tunnel_id ;
2015-09-02 07:06:15 -04:00
_status_label = new QToolButton ;
2013-06-12 17:10:09 -04:00
_update_timer = new QTimer ;
2015-09-02 07:06:15 -04:00
_status_label - > setAutoRaise ( true ) ;
_status_label - > setIconSize ( QSize ( 24 , 24 ) ) ;
2013-04-16 17:13:42 -04:00
2013-06-12 17:10:09 -04:00
_update_timer - > setInterval ( 1000 ) ;
QObject : : connect ( _update_timer , SIGNAL ( timeout ( ) ) , this , SLOT ( updateDisplay ( ) ) ) ;
2013-04-16 17:13:42 -04:00
2013-06-12 17:10:09 -04:00
_update_timer - > start ( ) ;
2013-04-23 18:43:19 -04:00
2014-01-01 19:56:46 -05:00
getChatWidget ( ) - > addChatBarWidget ( _status_label ) ;
2013-06-12 17:10:09 -04:00
updateDisplay ( ) ;
2013-04-16 17:13:42 -04:00
}
2015-11-30 00:02:44 -05:00
void PopupDistantChatDialog : : init ( const DistantChatPeerId & peer_id )
2013-04-16 17:13:42 -04:00
{
2015-11-28 14:55:56 -05:00
_tunnel_id = peer_id ;
DistantChatPeerInfo tinfo ;
2015-11-26 20:40:06 -05:00
2015-11-28 14:55:56 -05:00
if ( ! rsMsgs - > getDistantChatStatus ( _tunnel_id , tinfo ) )
2015-11-26 20:40:06 -05:00
return ;
2015-11-30 00:02:44 -05:00
RsIdentityDetails iddetails ;
if ( rsIdentity - > getIdDetails ( tinfo . to_id , iddetails ) )
PopupChatDialog : : init ( ChatId ( peer_id ) , QString : : fromUtf8 ( iddetails . mNickname . c_str ( ) ) ) ;
else
PopupChatDialog : : init ( ChatId ( peer_id ) , QString : : fromStdString ( tinfo . to_id . toStdString ( ) ) ) ;
2015-11-26 20:40:06 -05:00
// Do not use setOwnId, because we don't want the user to change the GXS avatar from the chat window
2015-02-14 15:55:05 -05:00
// it will not be transmitted.
2015-11-30 00:02:44 -05:00
ui . ownAvatarWidget - > setOwnId ( ) ; // sets the flag
ui . ownAvatarWidget - > setId ( ChatId ( peer_id ) ) ; // sets the actual Id
2013-04-16 17:13:42 -04:00
}
2013-06-12 17:10:09 -04:00
void PopupDistantChatDialog : : updateDisplay ( )
2013-04-16 17:13:42 -04:00
{
2015-11-26 20:40:06 -05:00
if ( RsAutoUpdatePage : : eventsLocked ( ) ) // we need to do that by end, because it's not possible to derive from both PopupChatDialog and RsAutoUpdatePage
return ; // which both derive from QObject. Signals-slot connexions won't work anymore.
2013-06-12 17:10:09 -04:00
2015-11-26 20:40:06 -05:00
if ( ! isVisible ( ) )
return ;
2013-04-16 17:13:42 -04:00
2015-11-26 20:40:06 -05:00
//std::cerr << "Checking tunnel..." ;
// make sure about the tunnel status
//
2013-04-16 17:13:42 -04:00
2015-11-28 14:55:56 -05:00
DistantChatPeerInfo tinfo ;
2015-11-26 20:40:06 -05:00
rsMsgs - > getDistantChatStatus ( _tunnel_id , tinfo ) ;
2015-11-30 00:02:44 -05:00
ui . avatarWidget - > setId ( ChatId ( _tunnel_id ) ) ;
2015-02-14 15:55:05 -05:00
2015-01-10 08:13:41 -05:00
QString msg ;
2015-12-04 21:24:51 -05:00
2015-11-28 14:55:56 -05:00
switch ( tinfo . status )
2015-11-26 20:40:06 -05:00
{
2015-11-28 14:55:56 -05:00
case RS_DISTANT_CHAT_STATUS_UNKNOWN : //std::cerr << "Unknown hash. Error!" << std::endl;
2015-12-04 21:24:51 -05:00
_status_label - > setIcon ( QIcon ( IMAGE_GRY_LED ) ) ;
2015-12-03 00:34:13 -05:00
msg = tr ( " Chat remotely closed. Please close this window. " ) ;
2015-11-26 20:40:06 -05:00
_status_label - > setToolTip ( msg ) ;
getChatWidget ( ) - > updateStatusString ( " %1 " , msg , true ) ;
getChatWidget ( ) - > blockSending ( tr ( " Can't send message, because there is no tunnel. " ) ) ;
setPeerStatus ( RS_STATUS_OFFLINE ) ;
break ;
2015-11-28 14:55:56 -05:00
case RS_DISTANT_CHAT_STATUS_REMOTELY_CLOSED : std : : cerr < < " Chat remotely closed. " < < std : : endl ;
2015-12-04 21:24:51 -05:00
_status_label - > setIcon ( QIcon ( IMAGE_RED_LED ) ) ;
2015-11-26 20:40:06 -05:00
_status_label - > setToolTip ( QObject : : tr ( " Distant peer has closed the chat " ) ) ;
getChatWidget ( ) - > updateStatusString ( " %1 " , tr ( " The person you're talking to has deleted the secured chat tunnel. You may remove the chat window now. " ) , true ) ;
getChatWidget ( ) - > blockSending ( tr ( " Can't send message, because the chat partner deleted the secure tunnel. " ) ) ;
setPeerStatus ( RS_STATUS_OFFLINE ) ;
break ;
2015-11-28 14:55:56 -05:00
case RS_DISTANT_CHAT_STATUS_TUNNEL_DN : //std::cerr << "Tunnel asked. Waiting for reponse. " << std::endl;
2015-12-04 21:24:51 -05:00
_status_label - > setIcon ( QIcon ( IMAGE_RED_LED ) ) ;
2015-11-26 20:40:06 -05:00
msg = QObject : : tr ( " Tunnel is pending... " ) ;
_status_label - > setToolTip ( msg ) ;
getChatWidget ( ) - > updateStatusString ( " %1 " , msg , true ) ;
getChatWidget ( ) - > blockSending ( msg ) ;
setPeerStatus ( RS_STATUS_OFFLINE ) ;
break ;
2015-11-28 14:55:56 -05:00
case RS_DISTANT_CHAT_STATUS_CAN_TALK : //std::cerr << "Tunnel is ok and data is transmitted." << std::endl;
2015-12-04 21:24:51 -05:00
_status_label - > setIcon ( QIcon ( IMAGE_GRN_LED ) ) ;
2015-11-26 20:40:06 -05:00
msg = QObject : : tr ( " Secured tunnel is working. You can talk! " ) ;
_status_label - > setToolTip ( msg ) ;
getChatWidget ( ) - > unblockSending ( ) ;
setPeerStatus ( RS_STATUS_ONLINE ) ;
break ;
}
2013-04-16 17:13:42 -04:00
}
2013-06-12 17:10:09 -04:00
void PopupDistantChatDialog : : closeEvent ( QCloseEvent * e )
{
2015-11-26 20:40:06 -05:00
//std::cerr << "Closing window => closing distant chat for hash " << _pid << std::endl;
2013-06-13 17:12:07 -04:00
2015-11-28 14:55:56 -05:00
DistantChatPeerInfo tinfo ;
rsMsgs - > getDistantChatStatus ( _tunnel_id , tinfo ) ;
2013-06-13 17:12:07 -04:00
2015-11-28 14:55:56 -05:00
if ( tinfo . status ! = RS_DISTANT_CHAT_STATUS_REMOTELY_CLOSED )
2013-06-13 17:12:07 -04:00
{
QString msg = tr ( " Closing this window will end the conversation, notify the peer and remove the encrypted tunnel. " ) ;
if ( QMessageBox : : Ok = = QMessageBox : : critical ( NULL , tr ( " Kill the tunnel? " ) , msg , QMessageBox : : Ok | QMessageBox : : Cancel ) )
2015-11-26 20:40:06 -05:00
rsMsgs - > closeDistantChatConnexion ( _tunnel_id ) ;
2013-06-13 17:12:07 -04:00
else
{
e - > ignore ( ) ;
return ;
}
}
2013-06-12 17:10:09 -04:00
e - > accept ( ) ;
PopupChatDialog : : closeEvent ( e ) ;
}
2013-04-16 17:13:42 -04:00
2016-10-22 17:48:19 -04:00
QString PopupDistantChatDialog : : getPeerName ( const ChatId & /*id*/ ) const
2013-06-18 16:13:58 -04:00
{
2015-11-28 14:55:56 -05:00
DistantChatPeerInfo tinfo ;
2015-11-26 20:40:06 -05:00
rsMsgs - > getDistantChatStatus ( _tunnel_id , tinfo ) ;
2014-11-17 16:56:41 -05:00
RsIdentityDetails details ;
2015-11-28 14:55:56 -05:00
if ( rsIdentity - > getIdDetails ( tinfo . to_id , details ) )
2014-11-17 16:56:41 -05:00
return QString : : fromUtf8 ( details . mNickname . c_str ( ) ) ;
else
2015-11-28 14:55:56 -05:00
return QString : : fromStdString ( tinfo . to_id . toStdString ( ) ) ;
2013-06-18 16:13:58 -04:00
}
2014-05-25 17:43:05 -04:00
2014-12-29 16:41:05 -05:00
QString PopupDistantChatDialog : : getOwnName ( ) const
{
2015-11-28 14:55:56 -05:00
DistantChatPeerInfo tinfo ;
2015-02-08 12:38:30 -05:00
2015-11-26 20:40:06 -05:00
rsMsgs - > getDistantChatStatus ( _tunnel_id , tinfo ) ;
2015-02-08 12:38:30 -05:00
RsIdentityDetails details ;
2015-11-28 14:55:56 -05:00
if ( rsIdentity - > getIdDetails ( tinfo . own_id , details ) )
2015-02-08 12:38:30 -05:00
return QString : : fromUtf8 ( details . mNickname . c_str ( ) ) ;
else
2015-11-28 14:55:56 -05:00
return QString : : fromStdString ( tinfo . own_id . toStdString ( ) ) ;
2014-12-29 16:41:05 -05:00
}