2011-11-27 16:04:10 -05:00
/****************************************************************
*
* RetroShare is distributed under the following license :
*
* Copyright ( C ) 2011 , csoler
*
* 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 <QMessageBox>
2012-01-28 10:11:58 -05:00
# include <QInputDialog>
2011-11-27 16:04:10 -05:00
# include "ChatLobbyDialog.h"
2012-01-18 15:02:19 -05:00
# include "ChatTabWidget.h"
2012-01-17 15:36:36 -05:00
# include "gui/settings/rsharesettings.h"
# include "gui/settings/RsharePeerSettings.h"
2012-03-30 19:02:52 -04:00
# include "gui/MainWindow.h"
2012-01-18 15:02:19 -05:00
# include "gui/FriendsDialog.h"
2012-05-11 16:53:27 -04:00
# include <gui/common/html.h>
2012-01-17 15:36:36 -05:00
# include <retroshare/rsnotify.h>
# include <time.h>
2011-11-27 16:04:10 -05:00
/** Default constructor */
2012-01-17 15:36:36 -05:00
ChatLobbyDialog : : ChatLobbyDialog ( const ChatLobbyId & lid , QWidget * parent , Qt : : WFlags flags )
: ChatDialog ( parent , flags ) , lobbyId ( lid )
2011-11-27 16:04:10 -05:00
{
2012-01-17 15:36:36 -05:00
/* Invoke Qt Designer generated QObject setup routine */
ui . setupUi ( this ) ;
connect ( ui . participantsFrameButton , SIGNAL ( toggled ( bool ) ) , this , SLOT ( showParticipantsFrame ( bool ) ) ) ;
2012-01-28 10:11:58 -05:00
connect ( ui . actionChangeNickname , SIGNAL ( triggered ( ) ) , this , SLOT ( changeNickname ( ) ) ) ;
2012-02-09 16:24:51 -05:00
2012-05-06 18:19:59 -04:00
// Mute a Participant
connect ( ui . participantsList , SIGNAL ( itemClicked ( QListWidgetItem * ) ) , SLOT ( changePartipationState ( QListWidgetItem * ) ) ) ;
2012-02-09 16:24:51 -05:00
ui . participantsList - > sortItems ( Qt : : AscendingOrder ) ;
2012-01-17 15:36:36 -05:00
}
2012-01-17 19:32:15 -05:00
void ChatLobbyDialog : : init ( const std : : string & peerId , const QString & title )
2012-01-17 15:36:36 -05:00
{
2012-03-20 19:05:17 -04:00
std : : list < ChatLobbyInfo > lobbies ;
rsMsgs - > getChatLobbyList ( lobbies ) ;
std : : list < ChatLobbyInfo > : : const_iterator lobbyIt ;
for ( lobbyIt = lobbies . begin ( ) ; lobbyIt ! = lobbies . end ( ) ; + + lobbyIt ) {
std : : string vpid ;
if ( rsMsgs - > getVirtualPeerId ( lobbyIt - > lobby_id , vpid ) ) {
if ( vpid = = peerId ) {
QString msg = tr ( " Welcome to lobby %1 " ) . arg ( QString : : fromUtf8 ( lobbyIt - > lobby_name . c_str ( ) ) ) ;
if ( ! lobbyIt - > lobby_topic . empty ( ) ) {
msg + = " \n " + tr ( " Topic: %1 " ) . arg ( QString : : fromUtf8 ( lobbyIt - > lobby_topic . c_str ( ) ) ) ;
}
ui . chatWidget - > setWelcomeMessage ( msg ) ;
break ;
}
}
}
2012-01-17 19:32:15 -05:00
ChatDialog : : init ( peerId , title ) ;
std : : string nickName ;
rsMsgs - > getNickNameForChatLobby ( lobbyId , nickName ) ;
ui . chatWidget - > setName ( QString : : fromUtf8 ( nickName . c_str ( ) ) ) ;
2012-01-17 15:36:36 -05:00
2012-01-28 10:11:58 -05:00
ui . chatWidget - > addToolsAction ( ui . actionChangeNickname ) ;
2012-01-17 15:36:36 -05:00
lastUpdateListTime = 0 ;
/* Hide or show the participants frames */
showParticipantsFrame ( PeerSettings - > getShowParticipantsFrame ( peerId ) ) ;
// add to window
2012-01-18 15:02:19 -05:00
ChatTabWidget * tabWidget = FriendsDialog : : getTabWidget ( ) ;
2012-01-17 15:36:36 -05:00
if ( tabWidget ) {
tabWidget - > addDialog ( this ) ;
}
2012-05-05 10:22:21 -04:00
/** List of muted Participants */
mutedParticipants = new QStringList ;
2012-01-17 15:36:36 -05:00
// load settings
processSettings ( true ) ;
2011-11-27 16:04:10 -05:00
}
/** Destructor. */
ChatLobbyDialog : : ~ ChatLobbyDialog ( )
{
// announce leaving of lobby
2012-01-11 19:13:25 -05:00
2012-01-06 17:17:08 -05:00
// check that the lobby still exists.
2012-01-11 19:13:25 -05:00
ChatLobbyId lid ;
2012-01-17 15:36:36 -05:00
if ( rsMsgs - > isLobbyId ( getPeerId ( ) , lid ) ) {
rsMsgs - > unsubscribeChatLobby ( lobbyId ) ;
2012-01-11 19:13:25 -05:00
}
2012-01-17 15:36:36 -05:00
// save settings
processSettings ( false ) ;
2012-01-11 19:13:25 -05:00
}
2012-01-06 17:17:08 -05:00
2012-01-17 15:36:36 -05:00
ChatWidget * ChatLobbyDialog : : getChatWidget ( )
2012-01-11 19:13:25 -05:00
{
2012-01-17 15:36:36 -05:00
return ui . chatWidget ;
}
void ChatLobbyDialog : : processSettings ( bool load )
{
Settings - > beginGroup ( QString ( " ChatLobbyDialog " ) ) ;
if ( load ) {
// load settings
} else {
// save settings
2012-01-11 19:13:25 -05:00
}
2012-01-17 15:36:36 -05:00
Settings - > endGroup ( ) ;
2011-12-26 17:43:54 -05:00
}
2012-05-06 18:19:59 -04:00
/**
* Change your Nickname
*
* - send a Message to all Members = > later : send hidden message to clients , so they can actualize there mutedParticipants list
*/
2012-01-28 10:11:58 -05:00
void ChatLobbyDialog : : setNickname ( const QString & nickname )
2011-11-27 16:04:10 -05:00
{
2012-01-28 10:11:58 -05:00
rsMsgs - > setNickNameForChatLobby ( lobbyId , nickname . toUtf8 ( ) . constData ( ) ) ;
ui . chatWidget - > setName ( nickname ) ;
}
2012-05-06 18:19:59 -04:00
/**
* Dialog : Change your Nickname in the ChatLobby
*/
2012-01-28 10:11:58 -05:00
void ChatLobbyDialog : : changeNickname ( )
{
QInputDialog dialog ;
dialog . setWindowTitle ( tr ( " Change nick name " ) ) ;
dialog . setLabelText ( tr ( " Please enter your new nick name " ) ) ;
dialog . setWindowIcon ( QIcon ( " :/images/rstray3.png " ) ) ;
std : : string nickName ;
rsMsgs - > getNickNameForChatLobby ( lobbyId , nickName ) ;
dialog . setTextValue ( QString : : fromUtf8 ( nickName . c_str ( ) ) ) ;
if ( dialog . exec ( ) = = QDialog : : Accepted ) {
2012-05-11 16:53:27 -04:00
// Informa other peers of change the Nickname, to update their mute list
2012-05-06 18:57:08 -04:00
rsMsgs - > sendLobbyStatusPeerChangedNickname ( lobbyId , dialog . textValue ( ) . toUtf8 ( ) . constData ( ) ) ;
2012-01-28 10:11:58 -05:00
setNickname ( dialog . textValue ( ) ) ;
}
2011-11-27 16:04:10 -05:00
}
2012-05-05 10:22:21 -04:00
/**
* We get a new Message from a chat participant
*
* - Ignore Messages from muted chat participants
*/
2011-12-28 04:15:28 -05:00
void ChatLobbyDialog : : addIncomingChatMsg ( const ChatInfo & info )
{
QDateTime sendTime = QDateTime : : fromTime_t ( info . sendTime ) ;
QDateTime recvTime = QDateTime : : fromTime_t ( info . recvTime ) ;
QString message = QString : : fromStdWString ( info . msg ) ;
2012-01-11 19:13:25 -05:00
QString name = QString : : fromUtf8 ( info . peer_nickname . c_str ( ) ) ;
2012-05-06 18:19:59 -04:00
QString rsid = QString : : fromUtf8 ( info . rsid . c_str ( ) ) ;
2011-12-28 04:15:28 -05:00
2012-05-06 18:19:59 -04:00
std : : cerr < < " message from rsid " < < info . rsid . c_str ( ) < < std : : endl ;
2012-05-05 10:22:21 -04:00
if ( ! isParticipantMuted ( name ) ) {
2012-05-06 18:19:59 -04:00
// ui.chatWidget->addChatMsg(true, name.append(" ").append(rsid), sendTime, recvTime, message, ChatWidget::TYPE_NORMAL);
2012-05-05 10:22:21 -04:00
ui . chatWidget - > addChatMsg ( true , name , sendTime , recvTime , message , ChatWidget : : TYPE_NORMAL ) ;
} else {
2012-05-11 16:53:27 -04:00
// ui.chatWidget->addChatMsg(true, name, sendTime, recvTime, message.append(" (BLOCKED)"), ChatWidget::TYPE_NORMAL);
2012-05-05 10:22:21 -04:00
}
2011-12-29 09:19:53 -05:00
// also update peer list.
2012-01-17 15:36:36 -05:00
time_t now = time ( NULL ) ;
2011-12-29 09:19:53 -05:00
2012-01-17 15:36:36 -05:00
if ( now > lastUpdateListTime ) {
lastUpdateListTime = now ;
updateParticipantsList ( ) ;
2011-12-29 09:19:53 -05:00
}
}
2012-05-05 10:22:21 -04:00
/**
* Regenerate the QListWidget participant list of a Chat Lobby
*
* Show unchecked Checkbox for muted Participants
*/
2012-01-17 15:36:36 -05:00
void ChatLobbyDialog : : updateParticipantsList ( )
2011-12-29 09:19:53 -05:00
{
2012-01-17 15:36:36 -05:00
ui . participantsList - > clear ( ) ;
2011-12-29 09:19:53 -05:00
2012-01-11 19:13:25 -05:00
std : : list < ChatLobbyInfo > linfos ;
2011-12-29 09:19:53 -05:00
rsMsgs - > getChatLobbyList ( linfos ) ;
std : : list < ChatLobbyInfo > : : const_iterator it ( linfos . begin ( ) ) ;
2012-05-06 18:19:59 -04:00
// Set it to the current ChatLobby
2012-01-17 15:36:36 -05:00
for ( ; it ! = linfos . end ( ) & & ( * it ) . lobby_id ! = lobbyId ; + + it ) ;
2011-12-29 09:19:53 -05:00
2012-01-11 19:13:25 -05:00
if ( it ! = linfos . end ( ) ) {
2012-01-14 16:09:04 -05:00
for ( std : : map < std : : string , time_t > : : const_iterator it2 ( ( * it ) . nick_names . begin ( ) ) ; it2 ! = ( * it ) . nick_names . end ( ) ; + + it2 ) {
2012-05-05 10:22:21 -04:00
QString participant = QString : : fromUtf8 ( ( it2 - > first ) . c_str ( ) ) ;
// TE: Add Wigdet to participantsList with Checkbox, to mute Participant
QListWidgetItem * widgetitem = new QListWidgetItem ( ui . participantsList ) ;
if ( isParticipantMuted ( participant ) ) {
widgetitem - > setCheckState ( Qt : : Unchecked ) ;
} else {
widgetitem - > setCheckState ( Qt : : Checked ) ;
}
widgetitem - > setText ( participant ) ;
2012-05-06 18:19:59 -04:00
widgetitem - > setToolTip ( tr ( " Uncheck to mute participant " ) ) ;
2012-05-05 10:22:21 -04:00
ui . participantsList - > addItem ( widgetitem ) ;
2012-01-11 19:13:25 -05:00
}
}
2011-12-28 04:15:28 -05:00
}
2012-05-05 10:22:21 -04:00
/**
* Called when a Participant in QList get Clicked / Changed
*
* Check if the Checkbox altered and Mute User
*
2012-05-06 18:19:59 -04:00
* @ todo auf rsid
*
2012-05-05 10:22:21 -04:00
* @ param QListWidgetItem Participant to check
*/
void ChatLobbyDialog : : changePartipationState ( QListWidgetItem * item )
{
QString nickname = item - > text ( ) ;
std : : cerr < < " check Partipation status for ' " < < nickname . toStdString ( ) < < std : : endl ;
if ( item - > checkState ( ) = = Qt : : Unchecked ) {
2012-05-11 16:53:27 -04:00
muteParticipant ( nickname ) ;
2012-05-05 10:22:21 -04:00
} else {
2012-05-11 16:53:27 -04:00
unMuteParticipant ( nickname ) ;
2012-05-05 10:22:21 -04:00
}
2012-05-06 18:19:59 -04:00
mutedParticipants - > removeDuplicates ( ) ;
2012-05-05 10:22:21 -04:00
updateParticipantsList ( ) ;
}
2012-05-11 16:53:27 -04:00
void ChatLobbyDialog : : muteParticipant ( const QString & nickname ) {
std : : cerr < < " Mute " < < std : : endl ;
mutedParticipants - > append ( nickname ) ;
}
void ChatLobbyDialog : : unMuteParticipant ( const QString & nickname ) {
std : : cerr < < " UnMute " < < std : : endl ;
mutedParticipants - > removeAll ( nickname ) ;
}
/**
* Is this nickName already known in the lobby
*/
bool ChatLobbyDialog : : isNicknameInLobby ( const QString & nickname ) {
std : : list < ChatLobbyInfo > linfos ;
rsMsgs - > getChatLobbyList ( linfos ) ;
std : : list < ChatLobbyInfo > : : const_iterator it ( linfos . begin ( ) ) ;
// Set it to the current ChatLobby
for ( ; it ! = linfos . end ( ) & & ( * it ) . lobby_id ! = lobbyId ; + + it ) ;
if ( it ! = linfos . end ( ) ) {
for ( std : : map < std : : string , time_t > : : const_iterator it2 ( ( * it ) . nick_names . begin ( ) ) ; it2 ! = ( * it ) . nick_names . end ( ) ; + + it2 ) {
QString participant = QString : : fromUtf8 ( ( it2 - > first ) . c_str ( ) ) ;
if ( participant = = nickname ) {
return true ;
}
}
}
return false ;
}
2012-05-05 10:22:21 -04:00
/**
* Should Messages from this Nickname be muted ?
*
2012-05-06 18:19:59 -04:00
* At the moment it is not possible to 100 % know which peer sendet the message , and only
* the nickname is available . So this couldn ' t work for 100 % . So , for example , if a peer
* change his name to the name of a other peer , we couldn ' t block him . A real implementation
* will be possible if we transfer a temporary Session ID from the sending Retroshare client
* version 0.6
*
2012-05-05 10:22:21 -04:00
* @ param QString nickname to check
*/
2012-05-11 16:53:27 -04:00
bool ChatLobbyDialog : : isParticipantMuted ( const QString & participant )
2012-05-05 10:22:21 -04:00
{
2012-05-11 16:53:27 -04:00
// nickname in Mute list
return mutedParticipants - > contains ( participant ) ;
2012-05-05 10:22:21 -04:00
}
2012-01-11 19:13:25 -05:00
void ChatLobbyDialog : : displayLobbyEvent ( int event_type , const QString & nickname , const QString & str )
2012-01-06 17:17:08 -05:00
{
2012-01-17 15:36:36 -05:00
switch ( event_type ) {
case RS_CHAT_LOBBY_EVENT_PEER_LEFT :
2012-02-03 16:43:09 -05:00
ui . chatWidget - > addChatMsg ( true , tr ( " Lobby management " ) , QDateTime : : currentDateTime ( ) , QDateTime : : currentDateTime ( ) , tr ( " %1 has left the lobby. " ) . arg ( str ) , ChatWidget : : TYPE_SYSTEM ) ;
2012-01-17 15:36:36 -05:00
break ;
case RS_CHAT_LOBBY_EVENT_PEER_JOINED :
2012-02-03 16:43:09 -05:00
ui . chatWidget - > addChatMsg ( true , tr ( " Lobby management " ) , QDateTime : : currentDateTime ( ) , QDateTime : : currentDateTime ( ) , tr ( " %1 joined the lobby. " ) . arg ( str ) , ChatWidget : : TYPE_SYSTEM ) ;
2012-01-17 15:36:36 -05:00
break ;
case RS_CHAT_LOBBY_EVENT_PEER_STATUS :
ui . chatWidget - > updateStatusString ( nickname + " %1 " , str ) ;
break ;
2012-05-06 18:19:59 -04:00
case RS_CHAT_LOBBY_EVENT_PEER_CHANGE_NICKNAME :
2012-05-06 18:57:08 -04:00
ui . chatWidget - > addChatMsg ( true , tr ( " Lobby management " ) , QDateTime : : currentDateTime ( ) , QDateTime : : currentDateTime ( ) , tr ( " %1 changed his name to: %2 " ) . arg ( nickname , str ) , ChatWidget : : TYPE_SYSTEM ) ;
2012-05-11 16:53:27 -04:00
// TODO if a user was muted and changed his name, update mute list, but only, when the muted peer, dont change his name to a other peer in your chat lobby
if ( isParticipantMuted ( nickname ) & & ! isNicknameInLobby ( str ) ) {
muteParticipant ( str ) ;
}
2012-05-06 18:19:59 -04:00
break ;
2012-01-23 17:49:47 -05:00
case RS_CHAT_LOBBY_EVENT_KEEP_ALIVE :
2012-02-12 15:45:42 -05:00
//std::cerr << "Received keep alive packet from " << nickname.toStdString() << " in lobby " << getPeerId() << std::endl;
2012-01-23 17:49:47 -05:00
break ;
2012-01-17 15:36:36 -05:00
default :
std : : cerr < < " ChatLobbyDialog::displayLobbyEvent() Unhandled lobby event type " < < event_type < < std : : endl ;
2012-01-06 17:17:08 -05:00
}
2012-01-23 16:10:34 -05:00
updateParticipantsList ( ) ;
2012-01-06 17:17:08 -05:00
}
2012-01-11 19:13:25 -05:00
bool ChatLobbyDialog : : canClose ( )
{
// check that the lobby still exists.
ChatLobbyId lid ;
2012-01-17 15:36:36 -05:00
if ( ! rsMsgs - > isLobbyId ( getPeerId ( ) , lid ) ) {
2012-01-11 19:13:25 -05:00
return true ;
}
2012-01-17 15:36:36 -05:00
if ( QMessageBox : : Yes = = QMessageBox : : question ( this , tr ( " Unsubscribe to lobby " ) , tr ( " Do you want to unsubscribe to this chat lobby? " ) , QMessageBox : : Yes | QMessageBox : : No ) ) {
2012-01-11 19:13:25 -05:00
return true ;
}
return false ;
}
2012-01-17 15:36:36 -05:00
void ChatLobbyDialog : : showDialog ( uint chatflags )
{
if ( chatflags & RS_CHAT_FOCUS ) {
2012-03-30 19:02:52 -04:00
MainWindow : : showWindow ( MainWindow : : Friends ) ;
2012-01-18 15:02:19 -05:00
ChatTabWidget * tabWidget = FriendsDialog : : getTabWidget ( ) ;
2012-01-17 15:36:36 -05:00
if ( tabWidget ) {
tabWidget - > setCurrentWidget ( this ) ;
}
}
}
void ChatLobbyDialog : : showParticipantsFrame ( bool show )
{
ui . participantsFrame - > setVisible ( show ) ;
ui . participantsFrameButton - > setChecked ( show ) ;
if ( show ) {
ui . participantsFrameButton - > setToolTip ( tr ( " Hide Participants " ) ) ;
ui . participantsFrameButton - > setIcon ( QIcon ( " :images/hide_toolbox_frame.png " ) ) ;
} else {
ui . participantsFrameButton - > setToolTip ( tr ( " Show Participants " ) ) ;
ui . participantsFrameButton - > setIcon ( QIcon ( " :images/show_toolbox_frame.png " ) ) ;
}
PeerSettings - > setShowParticipantsFrame ( getPeerId ( ) , show ) ;
}