2012-01-11 19:13:25 -05:00
# include <QTreeWidget>
2013-02-24 17:44:14 -05:00
# include <QTextBrowser>
# include <QTimer>
2012-01-11 19:13:25 -05:00
# include <QMenu>
2013-03-02 16:27:38 -05:00
# include <QMessageBox>
2013-02-25 09:16:07 -05:00
# include <time.h>
2012-01-07 14:52:58 -05:00
# include "ChatLobbyWidget.h"
2012-01-11 19:13:25 -05:00
# include "chat/CreateLobbyDialog.h"
2013-02-22 16:42:27 -05:00
# include "chat/ChatTabWidget.h"
2012-01-11 19:13:25 -05:00
# include "common/RSTreeWidgetItem.h"
# include "notifyqt.h"
# include "chat/ChatLobbyDialog.h"
2013-07-01 16:35:07 -04:00
# include "util/HandleRichText.h"
2012-01-07 14:52:58 -05:00
2012-01-11 19:13:25 -05:00
# include "retroshare/rsmsgs.h"
# include "retroshare/rspeers.h"
# include "retroshare/rsnotify.h"
# define COLUMN_NAME 0
# define COLUMN_USER_COUNT 1
2012-03-16 18:47:49 -04:00
# define COLUMN_TOPIC 2
# define COLUMN_COUNT 3
2012-01-11 19:13:25 -05:00
# define COLUMN_DATA 0
# define ROLE_SORT Qt::UserRole
# define ROLE_ID Qt::UserRole + 1
# define ROLE_SUBSCRIBED Qt::UserRole + 2
2012-01-30 18:20:42 -05:00
# define ROLE_PRIVACYLEVEL Qt::UserRole + 3
2013-06-29 12:15:33 -04:00
# define ROLE_AUTOSUBSCRIBE Qt::UserRole + 4
2012-01-11 19:13:25 -05:00
# define TYPE_FOLDER 0
# define TYPE_LOBBY 1
2013-09-04 20:57:48 -04:00
# define IMAGE_CREATE ""
# define IMAGE_PUBLIC ": / images / chat_x24.png"
# define IMAGE_PRIVATE ": / images / chat_red24.png"
# define IMAGE_SUBSCRIBE ": / images / edit_add24.png"
# define IMAGE_UNSUBSCRIBE ": / images / cancel.png"
# define IMAGE_PEER_ENTERING ":images / user / add_user24.png"
# define IMAGE_PEER_LEAVING ":images / user / remove_user24.png"
# define IMAGE_TYPING ":images / typing.png"
# define IMAGE_MESSAGE ":images / chat.png"
# define IMAGE_AUTOSUBSCRIBE ":images / accepted16.png"
2012-01-11 19:13:25 -05:00
2012-01-07 14:52:58 -05:00
ChatLobbyWidget : : ChatLobbyWidget ( QWidget * parent , Qt : : WFlags flags )
2012-02-17 05:03:38 -05:00
: RsAutoUpdatePage ( 5000 , parent , flags )
2012-01-07 14:52:58 -05:00
{
2012-01-11 19:13:25 -05:00
setupUi ( this ) ;
QObject : : connect ( NotifyQt : : getInstance ( ) , SIGNAL ( lobbyListChanged ( ) ) , SLOT ( lobbyChanged ( ) ) ) ;
QObject : : connect ( NotifyQt : : getInstance ( ) , SIGNAL ( chatLobbyEvent ( qulonglong , int , const QString & , const QString & ) ) , this , SLOT ( displayChatLobbyEvent ( qulonglong , int , const QString & , const QString & ) ) ) ;
QObject : : connect ( NotifyQt : : getInstance ( ) , SIGNAL ( chatLobbyInviteReceived ( ) ) , this , SLOT ( readChatLobbyInvites ( ) ) ) ;
2013-02-24 17:44:14 -05:00
QObject : : connect ( lobbyTreeWidget , SIGNAL ( customContextMenuRequested ( QPoint ) ) , this , SLOT ( lobbyTreeWidgetCustomPopupMenu ( QPoint ) ) ) ;
2012-01-11 19:13:25 -05:00
QObject : : connect ( lobbyTreeWidget , SIGNAL ( itemDoubleClicked ( QTreeWidgetItem * , int ) ) , this , SLOT ( itemDoubleClicked ( QTreeWidgetItem * , int ) ) ) ;
2013-02-24 17:44:14 -05:00
QObject : : connect ( lobbyTreeWidget , SIGNAL ( itemSelectionChanged ( ) ) , this , SLOT ( updateCurrentLobby ( ) ) ) ;
2012-01-18 15:02:19 -05:00
2013-09-01 20:08:38 -04:00
QObject : : connect ( filterLineEdit , SIGNAL ( textChanged ( const QString & ) ) , this , SLOT ( filterItems ( QString ) ) ) ;
QObject : : connect ( filterLineEdit , SIGNAL ( filterChanged ( int ) ) , this , SLOT ( filterColumnChanged ( int ) ) ) ;
QObject : : connect ( createlobbytoolButton , SIGNAL ( clicked ( ) ) , this , SLOT ( createChatLobby ( ) ) ) ;
2012-01-11 19:13:25 -05:00
compareRole = new RSTreeWidgetItemCompareRole ;
compareRole - > setRole ( COLUMN_NAME , ROLE_SORT ) ;
lobbyTreeWidget - > setColumnCount ( COLUMN_COUNT ) ;
lobbyTreeWidget - > sortItems ( COLUMN_NAME , Qt : : AscendingOrder ) ;
2013-03-17 11:03:21 -04:00
lobbyTreeWidget - > setContextMenuPolicy ( Qt : : CustomContextMenu ) ;
2012-01-11 19:13:25 -05:00
QTreeWidgetItem * headerItem = lobbyTreeWidget - > headerItem ( ) ;
headerItem - > setText ( COLUMN_NAME , tr ( " Name " ) ) ;
headerItem - > setText ( COLUMN_USER_COUNT , tr ( " Count " ) ) ;
2012-03-16 18:47:49 -04:00
headerItem - > setText ( COLUMN_TOPIC , tr ( " Topic " ) ) ;
2012-01-11 19:13:25 -05:00
headerItem - > setTextAlignment ( COLUMN_NAME , Qt : : AlignHCenter | Qt : : AlignVCenter ) ;
2013-09-01 20:08:38 -04:00
headerItem - > setTextAlignment ( COLUMN_TOPIC , Qt : : AlignHCenter | Qt : : AlignVCenter ) ;
2012-01-11 19:13:25 -05:00
headerItem - > setTextAlignment ( COLUMN_USER_COUNT , Qt : : AlignHCenter | Qt : : AlignVCenter ) ;
QHeaderView * header = lobbyTreeWidget - > header ( ) ;
header - > setResizeMode ( COLUMN_NAME , QHeaderView : : Interactive ) ;
2012-03-16 18:47:49 -04:00
header - > setResizeMode ( COLUMN_USER_COUNT , QHeaderView : : Interactive ) ;
header - > setResizeMode ( COLUMN_TOPIC , QHeaderView : : Stretch ) ;
2012-01-07 16:01:43 -05:00
2012-01-11 19:13:25 -05:00
lobbyTreeWidget - > setColumnWidth ( COLUMN_NAME , 200 ) ;
lobbyTreeWidget - > setColumnWidth ( COLUMN_USER_COUNT , 50 ) ;
privateLobbyItem = new RSTreeWidgetItem ( compareRole , TYPE_FOLDER ) ;
privateLobbyItem - > setText ( COLUMN_NAME , tr ( " Private Lobbies " ) ) ;
privateLobbyItem - > setData ( COLUMN_NAME , ROLE_SORT , " 1 " ) ;
2013-09-02 16:23:52 -04:00
// privateLobbyItem->setIcon(COLUMN_NAME, QIcon(IMAGE_PRIVATE));
2012-01-30 18:20:42 -05:00
privateLobbyItem - > setData ( COLUMN_DATA , ROLE_PRIVACYLEVEL , RS_CHAT_LOBBY_PRIVACY_LEVEL_PRIVATE ) ;
2012-01-11 19:13:25 -05:00
lobbyTreeWidget - > insertTopLevelItem ( 0 , privateLobbyItem ) ;
publicLobbyItem = new RSTreeWidgetItem ( compareRole , TYPE_FOLDER ) ;
publicLobbyItem - > setText ( COLUMN_NAME , tr ( " Public Lobbies " ) ) ;
publicLobbyItem - > setData ( COLUMN_NAME , ROLE_SORT , " 2 " ) ;
2013-09-02 16:23:52 -04:00
// publicLobbyItem->setIcon(COLUMN_NAME, QIcon(IMAGE_PUBLIC));
2012-01-30 18:20:42 -05:00
publicLobbyItem - > setData ( COLUMN_DATA , ROLE_PRIVACYLEVEL , RS_CHAT_LOBBY_PRIVACY_LEVEL_PUBLIC ) ;
2012-01-11 19:13:25 -05:00
lobbyTreeWidget - > insertTopLevelItem ( 1 , publicLobbyItem ) ;
2013-02-24 17:44:14 -05:00
// add one blank page.
//
_lobby_blank_page = new QTextBrowser ( this ) ;
_lobby_blank_page - > setSizePolicy ( QSizePolicy : : MinimumExpanding , QSizePolicy : : MinimumExpanding ) ;
stackedWidget - > addWidget ( _lobby_blank_page ) ;
2012-01-11 19:13:25 -05:00
lobbyTreeWidget - > expandAll ( ) ;
2013-03-19 15:46:39 -04:00
lobbyTreeWidget - > setColumnHidden ( 1 , true ) ;
lobbyTreeWidget - > setColumnHidden ( 2 , true ) ;
lobbyTreeWidget - > setSortingEnabled ( true ) ;
2012-01-11 19:13:25 -05:00
2013-03-19 16:49:24 -04:00
lobbyTreeWidget - > adjustSize ( ) ;
lobbyTreeWidget - > setColumnWidth ( 0 , 100 ) ;
2013-03-19 18:34:30 -04:00
// Set initial size of the splitter
QList < int > sizes ;
sizes < < 200 < < width ( ) ; // Qt calculates the right sizes
splitter - > setSizes ( sizes ) ;
2012-01-11 19:13:25 -05:00
lobbyChanged ( ) ;
2013-02-24 17:44:14 -05:00
showBlankPage ( 0 ) ;
2013-09-01 20:08:38 -04:00
/* add filter actions */
filterLineEdit - > addFilter ( QIcon ( ) , tr ( " Name " ) , COLUMN_NAME , tr ( " Search Name " ) ) ;
filterLineEdit - > setCurrentFilter ( COLUMN_NAME ) ;
2013-07-19 14:36:09 -04:00
QString help_str = tr (
" <h1><img width= \" 32 \" src= \" :/images/64px_help.png \" > Chat Lobbies</h1> \
< p > Chat lobbies are distributed chat rooms , and work pretty much like IRC . \
They allow you to talk anonymously with tons of people without the need to make friends . < / p > \
< p > A chat lobby can be public ( you friends see it ) or private ( your friends can ' t see it , unless you \
invite them with < img src = \ " :/images/add_24x24.png \" width=12/>). Once you have been invited to a private lobby, you will be able to see it when your friends \
are using it . < / p > \
< p > The list at left shows \
chat lobbies your friends are participating into . You can either \
< ul > \
< li > Right click to create a new chat lobby < / li > \
< li > Double click a chat lobby to enter , chat , and show it to your friends < / li > \
< / ul > \
2013-08-28 16:02:09 -04:00
Note : For the chat lobbies to work properly , your computer needs be on time . So check your system clock ! \
2013-07-19 14:36:09 -04:00
< / p > \
" ) ;
registerHelpButton ( helpButton , help_str ) ;
2012-01-07 14:52:58 -05:00
}
2012-01-11 19:13:25 -05:00
2012-01-07 14:52:58 -05:00
ChatLobbyWidget : : ~ ChatLobbyWidget ( )
{
2012-06-09 10:14:04 -04:00
if ( compareRole ) {
delete ( compareRole ) ;
}
2012-01-11 19:13:25 -05:00
}
2013-02-24 17:44:14 -05:00
void ChatLobbyWidget : : lobbyTreeWidgetCustomPopupMenu ( QPoint )
2012-01-11 19:13:25 -05:00
{
2013-02-24 17:44:14 -05:00
std : : cerr < < " Creating customPopupMennu " < < std : : endl ;
2012-01-30 18:20:42 -05:00
QTreeWidgetItem * item = lobbyTreeWidget - > currentItem ( ) ;
2012-01-11 19:13:25 -05:00
QMenu contextMnu ( this ) ;
2012-01-30 18:20:42 -05:00
if ( item & & item - > type ( ) = = TYPE_FOLDER ) {
QAction * action = contextMnu . addAction ( QIcon ( IMAGE_CREATE ) , tr ( " Create chat lobby " ) , this , SLOT ( createChatLobby ( ) ) ) ;
action - > setData ( item - > data ( COLUMN_DATA , ROLE_PRIVACYLEVEL ) . toInt ( ) ) ;
}
2012-01-11 19:13:25 -05:00
if ( item & & item - > type ( ) = = TYPE_LOBBY ) {
if ( item - > data ( COLUMN_DATA , ROLE_SUBSCRIBED ) . toBool ( ) ) {
contextMnu . addAction ( QIcon ( IMAGE_UNSUBSCRIBE ) , tr ( " Unsubscribe " ) , this , SLOT ( unsubscribeItem ( ) ) ) ;
} else {
contextMnu . addAction ( QIcon ( IMAGE_SUBSCRIBE ) , tr ( " Subscribe " ) , this , SLOT ( subscribeItem ( ) ) ) ;
}
2013-06-29 12:15:33 -04:00
if ( item - > data ( COLUMN_DATA , ROLE_AUTOSUBSCRIBE ) . toBool ( ) ) {
contextMnu . addAction ( QIcon ( IMAGE_AUTOSUBSCRIBE ) , tr ( " Remove Auto Subscribe " ) , this , SLOT ( autoSubscribeItem ( ) ) ) ;
} else {
2013-09-04 20:57:48 -04:00
contextMnu . addAction ( QIcon ( IMAGE_SUBSCRIBE ) , tr ( " Add Auto Subscribe " ) , this , SLOT ( autoSubscribeItem ( ) ) ) ;
2013-06-29 12:15:33 -04:00
}
2012-01-11 19:13:25 -05:00
}
2012-01-30 18:20:42 -05:00
if ( contextMnu . children ( ) . count ( ) = = 0 ) {
return ;
}
2012-01-11 19:13:25 -05:00
contextMnu . exec ( QCursor : : pos ( ) ) ;
}
void ChatLobbyWidget : : lobbyChanged ( )
{
updateDisplay ( ) ;
}
2013-07-04 16:09:46 -04:00
static void updateItem ( QTreeWidget * treeWidget , QTreeWidgetItem * item , ChatLobbyId id , const std : : string & name , const std : : string & topic , int count , bool subscribed , bool autoSubscribe )
2012-01-11 19:13:25 -05:00
{
item - > setText ( COLUMN_NAME , QString : : fromUtf8 ( name . c_str ( ) ) ) ;
item - > setData ( COLUMN_NAME , ROLE_SORT , QString : : fromUtf8 ( name . c_str ( ) ) ) ;
2012-03-16 19:07:28 -04:00
if ( topic . empty ( ) )
{
2012-03-30 19:02:52 -04:00
item - > setText ( COLUMN_TOPIC , qApp - > translate ( " ChatLobbyWidget " , " [No topic provided] " ) ) ;
item - > setData ( COLUMN_TOPIC , ROLE_SORT , qApp - > translate ( " ChatLobbyWidget " , " [No topic provided] " ) ) ;
2012-03-16 19:07:28 -04:00
}
else
{
item - > setText ( COLUMN_TOPIC , QString : : fromUtf8 ( topic . c_str ( ) ) ) ;
item - > setData ( COLUMN_TOPIC , ROLE_SORT , QString : : fromUtf8 ( topic . c_str ( ) ) ) ;
}
2012-03-16 18:47:49 -04:00
2012-01-11 19:13:25 -05:00
item - > setText ( COLUMN_USER_COUNT , QString : : number ( count ) ) ;
2012-01-12 06:14:50 -05:00
item - > setData ( COLUMN_DATA , ROLE_ID , ( qulonglong ) id ) ;
2012-01-11 19:13:25 -05:00
item - > setData ( COLUMN_DATA , ROLE_SUBSCRIBED , subscribed ) ;
2013-06-29 12:15:33 -04:00
item - > setData ( COLUMN_DATA , ROLE_AUTOSUBSCRIBE , autoSubscribe ) ;
2012-01-11 19:13:25 -05:00
2013-07-04 16:09:46 -04:00
QColor color = treeWidget - > palette ( ) . color ( QPalette : : Active , QPalette : : Text ) ;
2013-03-02 16:27:38 -05:00
if ( ! subscribed ) {
// Average between Base and Text colors
2013-07-04 16:09:46 -04:00
QColor color2 = treeWidget - > palette ( ) . color ( QPalette : : Active , QPalette : : Base ) ;
2013-03-02 16:27:38 -05:00
color . setRgbF ( ( color2 . redF ( ) + color . redF ( ) ) / 2 , ( color2 . greenF ( ) + color . greenF ( ) ) / 2 , ( color2 . blueF ( ) + color . blueF ( ) ) / 2 ) ;
}
2012-01-11 19:13:25 -05:00
for ( int column = 0 ; column < COLUMN_COUNT ; + + column ) {
2013-03-02 16:27:38 -05:00
item - > setTextColor ( column , color ) ;
2012-01-11 19:13:25 -05:00
}
2013-03-19 15:46:39 -04:00
item - > setToolTip ( 0 , QObject : : tr ( " Subject: " ) + item - > text ( COLUMN_TOPIC ) + " \n " + QObject : : tr ( " Participants: " ) + QString : : number ( count ) ) ;
2012-01-07 14:52:58 -05:00
}
2013-02-24 17:44:14 -05:00
void ChatLobbyWidget : : addChatPage ( ChatLobbyDialog * d )
{
// check that the page does not already exist.
2013-02-27 17:17:49 -05:00
if ( _lobby_infos . find ( d - > id ( ) ) = = _lobby_infos . end ( ) )
2013-02-24 17:44:14 -05:00
{
stackedWidget - > addWidget ( d ) ;
connect ( d , SIGNAL ( lobbyLeave ( ChatLobbyId ) ) , this , SLOT ( unsubscribeChatLobby ( ChatLobbyId ) ) ) ;
connect ( d , SIGNAL ( typingEventReceived ( ChatLobbyId ) ) , this , SLOT ( updateTypingStatus ( ChatLobbyId ) ) ) ;
2013-02-27 17:17:49 -05:00
connect ( d , SIGNAL ( messageReceived ( ChatLobbyId ) ) , this , SLOT ( updateMessageChanged ( ChatLobbyId ) ) ) ;
connect ( d , SIGNAL ( peerJoined ( ChatLobbyId ) ) , this , SLOT ( updatePeerEntering ( ChatLobbyId ) ) ) ;
connect ( d , SIGNAL ( peerLeft ( ChatLobbyId ) ) , this , SLOT ( updatePeerLeaving ( ChatLobbyId ) ) ) ;
2013-02-24 17:44:14 -05:00
2013-08-26 19:37:13 -04:00
ChatLobbyId id = d - > id ( ) ;
_lobby_infos [ id ] . dialog = d ;
_lobby_infos [ id ] . default_icon = QIcon ( ) ;
_lobby_infos [ id ] . last_typing_event = time ( NULL ) ;
std : : list < ChatLobbyInfo > lobbies ;
rsMsgs - > getChatLobbyList ( lobbies ) ;
for ( std : : list < ChatLobbyInfo > : : const_iterator it = lobbies . begin ( ) ; it ! = lobbies . end ( ) ; + + it ) {
if ( it - > lobby_id = = id ) {
_lobby_infos [ id ] . default_icon = ( it - > lobby_privacy_level = = RS_CHAT_LOBBY_PRIVACY_LEVEL_PRIVATE ) ? QIcon ( IMAGE_PRIVATE ) : QIcon ( IMAGE_PUBLIC ) ;
}
}
2013-02-24 17:44:14 -05:00
}
}
void ChatLobbyWidget : : setCurrentChatPage ( ChatLobbyDialog * d )
{
stackedWidget - > setCurrentWidget ( d ) ;
2013-08-27 11:26:11 -04:00
if ( d ) {
QTreeWidgetItem * item = getTreeWidgetItem ( d - > id ( ) ) ;
if ( item ) {
lobbyTreeWidget - > setCurrentItem ( item ) ;
}
}
2013-02-24 17:44:14 -05:00
}
2012-01-07 14:52:58 -05:00
void ChatLobbyWidget : : updateDisplay ( )
{
2012-01-28 08:20:01 -05:00
# ifdef CHAT_LOBBY_GUI_DEBUG
2012-01-07 16:01:43 -05:00
std : : cerr < < " updating chat lobby display! " < < std : : endl ;
2012-01-28 08:20:01 -05:00
# endif
2012-12-04 16:36:05 -05:00
std : : vector < VisibleChatLobbyRecord > visibleLobbies ;
rsMsgs - > getListOfNearbyChatLobbies ( visibleLobbies ) ;
2012-01-07 14:52:58 -05:00
2012-01-11 19:13:25 -05:00
std : : list < ChatLobbyInfo > lobbies ;
rsMsgs - > getChatLobbyList ( lobbies ) ;
2012-01-07 16:01:43 -05:00
2012-01-28 08:20:01 -05:00
# ifdef CHAT_LOBBY_GUI_DEBUG
2012-12-04 16:36:05 -05:00
std : : cerr < < " got " < < visibleLobbies . size ( ) < < " visible lobbies, and " < < lobbies . size ( ) < < " private lobbies. " < < std : : endl ;
2012-01-28 08:20:01 -05:00
# endif
2012-01-07 16:37:31 -05:00
2012-01-11 19:13:25 -05:00
// now, do a nice display of lobbies
2012-01-07 14:52:58 -05:00
2012-01-11 19:13:25 -05:00
std : : string vpid ;
uint32_t i ;
2012-12-04 16:36:05 -05:00
uint32_t size = visibleLobbies . size ( ) ;
2012-01-11 19:13:25 -05:00
std : : list < ChatLobbyInfo > : : const_iterator lobbyIt ;
// remove not existing public lobbies
2012-12-04 16:36:05 -05:00
for ( int p = 0 ; p < 2 ; + + p )
{
QTreeWidgetItem * lobby_item = ( p = = 0 ) ? publicLobbyItem : privateLobbyItem ;
int childCnt = lobby_item - > childCount ( ) ;
int childIndex = 0 ;
while ( childIndex < childCnt ) {
QTreeWidgetItem * itemLoop = lobby_item - > child ( childIndex ) ;
if ( itemLoop - > type ( ) = = TYPE_LOBBY )
{
// check for visible lobby
for ( i = 0 ; i < size ; + + i )
if ( itemLoop - > data ( COLUMN_DATA , ROLE_ID ) . toULongLong ( ) = = visibleLobbies [ i ] . lobby_id )
2012-01-11 19:13:25 -05:00
break ;
2012-12-04 16:36:05 -05:00
if ( i > = size )
{
// Check for participating lobby with public level
//
for ( lobbyIt = lobbies . begin ( ) ; lobbyIt ! = lobbies . end ( ) ; + + lobbyIt )
if ( itemLoop - > data ( COLUMN_DATA , ROLE_ID ) . toULongLong ( ) = = lobbyIt - > lobby_id )
break ;
if ( lobbyIt = = lobbies . end ( ) )
{
delete ( lobby_item - > takeChild ( lobby_item - > indexOfChild ( itemLoop ) ) ) ;
childCnt = lobby_item - > childCount ( ) ;
continue ;
}
2012-01-11 19:13:25 -05:00
}
}
2012-12-04 16:36:05 -05:00
childIndex + + ;
2012-01-11 19:13:25 -05:00
}
}
2012-01-07 16:01:43 -05:00
2012-12-04 16:36:05 -05:00
// Now add visible lobbies
//
for ( i = 0 ; i < size ; + + i )
{
const VisibleChatLobbyRecord & lobby = visibleLobbies [ i ] ;
2012-01-07 16:37:31 -05:00
2012-01-23 17:49:47 -05:00
# ifdef CHAT_LOBBY_GUI_DEBUG
2012-12-04 16:36:05 -05:00
std : : cerr < < " adding " < < lobby . lobby_name < < " topic " < < lobby . lobby_topic < < " # " < < std : : hex < < lobby . lobby_id < < std : : dec < < " public " < < lobby . total_number_of_peers < < " peers. Lobby type: " < < lobby . lobby_privacy_level < < std : : endl ;
2012-01-23 17:49:47 -05:00
# endif
2012-01-07 18:03:59 -05:00
2012-01-11 19:13:25 -05:00
QTreeWidgetItem * item = NULL ;
2013-08-26 19:37:13 -04:00
QTreeWidgetItem * lobby_item = ( lobby . lobby_privacy_level = = RS_CHAT_LOBBY_PRIVACY_LEVEL_PUBLIC ) ? publicLobbyItem : privateLobbyItem ;
2012-12-04 16:36:05 -05:00
// Search existing item
//
int childCnt = lobby_item - > childCount ( ) ;
for ( int childIndex = 0 ; childIndex < childCnt ; childIndex + + )
{
QTreeWidgetItem * itemLoop = lobby_item - > child ( childIndex ) ;
2012-01-11 19:13:25 -05:00
if ( itemLoop - > type ( ) = = TYPE_LOBBY & & itemLoop - > data ( COLUMN_DATA , ROLE_ID ) . toULongLong ( ) = = lobby . lobby_id ) {
item = itemLoop ;
break ;
}
2012-01-07 16:37:31 -05:00
}
2012-01-07 16:01:43 -05:00
2012-01-11 19:13:25 -05:00
if ( item = = NULL ) {
item = new RSTreeWidgetItem ( compareRole , TYPE_LOBBY ) ;
2013-08-26 19:37:13 -04:00
item - > setIcon ( COLUMN_NAME , ( lobby_item = = publicLobbyItem ) ? QIcon ( IMAGE_PUBLIC ) : QIcon ( IMAGE_PRIVATE ) ) ;
2012-12-04 16:36:05 -05:00
lobby_item - > addChild ( item ) ;
2012-01-11 19:13:25 -05:00
}
bool subscribed = false ;
if ( rsMsgs - > getVirtualPeerId ( lobby . lobby_id , vpid ) ) {
subscribed = true ;
}
2012-01-07 16:37:31 -05:00
2013-06-29 12:15:33 -04:00
bool autoSubscribe = rsMsgs - > getLobbyAutoSubscribe ( lobby . lobby_id ) ;
if ( autoSubscribe & & subscribed )
{
if ( _lobby_infos . find ( lobby . lobby_id ) = = _lobby_infos . end ( ) )
{
if ( item = = lobbyTreeWidget - > currentItem ( ) )
{
ChatDialog : : chatFriend ( vpid ) ;
} else {
ChatDialog : : chatFriend ( vpid , false ) ;
}
}
}
2013-07-04 16:09:46 -04:00
updateItem ( lobbyTreeWidget , item , lobby . lobby_id , lobby . lobby_name , lobby . lobby_topic , lobby . total_number_of_peers , subscribed , autoSubscribe ) ;
2013-06-29 12:15:33 -04:00
2012-01-11 19:13:25 -05:00
}
2012-01-07 18:03:59 -05:00
2013-06-10 09:27:24 -04:00
// time_t now = time(NULL) ;
2013-02-27 17:17:49 -05:00
2012-12-04 16:36:05 -05:00
// Now add participating lobbies.
//
for ( lobbyIt = lobbies . begin ( ) ; lobbyIt ! = lobbies . end ( ) ; + + lobbyIt )
{
2012-01-11 19:13:25 -05:00
const ChatLobbyInfo & lobby = * lobbyIt ;
2012-01-23 17:49:47 -05:00
# ifdef CHAT_LOBBY_GUI_DEBUG
2012-03-16 18:47:49 -04:00
std : : cerr < < " adding " < < lobby . lobby_name < < " topic " < < lobby . lobby_topic < < " # " < < std : : hex < < lobby . lobby_id < < std : : dec < < " private " < < lobby . nick_names . size ( ) < < " peers. " < < std : : endl ;
2012-01-23 17:49:47 -05:00
# endif
2012-01-11 19:13:25 -05:00
QTreeWidgetItem * itemParent ;
if ( lobby . lobby_privacy_level = = RS_CHAT_LOBBY_PRIVACY_LEVEL_PUBLIC ) {
itemParent = publicLobbyItem ;
} else {
itemParent = privateLobbyItem ;
}
QTreeWidgetItem * item = NULL ;
// search existing item
2012-12-04 16:36:05 -05:00
int childCount = itemParent - > childCount ( ) ;
for ( int childIndex = 0 ; childIndex < childCount ; childIndex + + ) {
2012-01-11 19:13:25 -05:00
QTreeWidgetItem * itemLoop = itemParent - > child ( childIndex ) ;
if ( itemLoop - > type ( ) = = TYPE_LOBBY & & itemLoop - > data ( COLUMN_DATA , ROLE_ID ) . toULongLong ( ) = = lobby . lobby_id ) {
item = itemLoop ;
break ;
}
}
if ( item = = NULL ) {
item = new RSTreeWidgetItem ( compareRole , TYPE_LOBBY ) ;
2013-08-26 19:37:13 -04:00
item - > setIcon ( COLUMN_NAME , ( itemParent = = publicLobbyItem ) ? QIcon ( IMAGE_PUBLIC ) : QIcon ( IMAGE_PRIVATE ) ) ;
2012-01-11 19:13:25 -05:00
itemParent - > addChild ( item ) ;
}
2013-06-29 12:15:33 -04:00
bool autoSubscribe = rsMsgs - > getLobbyAutoSubscribe ( lobby . lobby_id ) ;
2013-07-04 16:09:46 -04:00
updateItem ( lobbyTreeWidget , item , lobby . lobby_id , lobby . lobby_name , lobby . lobby_topic , lobby . nick_names . size ( ) , true , autoSubscribe ) ;
2012-01-07 16:37:31 -05:00
}
}
2012-01-07 16:01:43 -05:00
2012-01-11 19:13:25 -05:00
void ChatLobbyWidget : : createChatLobby ( )
2012-01-07 16:37:31 -05:00
{
2012-01-30 18:20:42 -05:00
int privacyLevel = 0 ;
QAction * action = qobject_cast < QAction * > ( sender ( ) ) ;
if ( action ) {
privacyLevel = action - > data ( ) . toInt ( ) ;
}
2012-01-11 19:13:25 -05:00
std : : list < std : : string > friends ;
2012-01-30 18:20:42 -05:00
CreateLobbyDialog ( friends , privacyLevel ) . exec ( ) ;
2012-01-11 19:13:25 -05:00
}
2013-02-24 17:44:14 -05:00
void ChatLobbyWidget : : showLobby ( QTreeWidgetItem * item )
{
2013-09-01 20:08:38 -04:00
if ( item = = NULL | | item - > type ( ) ! = TYPE_LOBBY ) {
2013-02-24 17:44:14 -05:00
showBlankPage ( 0 ) ;
return ;
}
ChatLobbyId id = item - > data ( COLUMN_DATA , ROLE_ID ) . toULongLong ( ) ;
2013-02-27 17:17:49 -05:00
if ( _lobby_infos . find ( id ) = = _lobby_infos . end ( ) )
2013-02-24 17:44:14 -05:00
showBlankPage ( id ) ;
else
2013-02-27 17:17:49 -05:00
stackedWidget - > setCurrentWidget ( _lobby_infos [ id ] . dialog ) ;
2013-02-24 17:44:14 -05:00
}
2012-01-27 13:51:27 -05:00
static void subscribeLobby ( QTreeWidgetItem * item )
2012-01-11 19:13:25 -05:00
{
if ( item = = NULL & & item - > type ( ) ! = TYPE_LOBBY ) {
return ;
}
ChatLobbyId id = item - > data ( COLUMN_DATA , ROLE_ID ) . toULongLong ( ) ;
2012-12-04 16:36:05 -05:00
if ( rsMsgs - > joinVisibleChatLobby ( id ) ) {
2012-01-11 19:13:25 -05:00
std : : string vpeer_id ;
if ( rsMsgs - > getVirtualPeerId ( id , vpeer_id ) ) {
2013-06-29 12:15:33 -04:00
ChatDialog : : chatFriend ( vpeer_id , true ) ;
2012-01-11 19:13:25 -05:00
}
}
}
2013-06-29 12:15:33 -04:00
void ChatLobbyWidget : : autoSubscribeLobby ( QTreeWidgetItem * item )
{
if ( item = = NULL & & item - > type ( ) ! = TYPE_LOBBY ) {
return ;
}
ChatLobbyId id = item - > data ( COLUMN_DATA , ROLE_ID ) . toULongLong ( ) ;
bool isAutoSubscribe = rsMsgs - > getLobbyAutoSubscribe ( id ) ;
rsMsgs - > setLobbyAutoSubscribe ( id , ! isAutoSubscribe ) ;
if ( ! isAutoSubscribe ) subscribeLobby ( item ) ;
}
2013-02-24 17:44:14 -05:00
void ChatLobbyWidget : : showBlankPage ( ChatLobbyId id )
{
// show the default blank page.
stackedWidget - > setCurrentWidget ( _lobby_blank_page ) ;
// Update information
std : : vector < VisibleChatLobbyRecord > lobbies ;
rsMsgs - > getListOfNearbyChatLobbies ( lobbies ) ;
for ( std : : vector < VisibleChatLobbyRecord > : : const_iterator it ( lobbies . begin ( ) ) ; it ! = lobbies . end ( ) ; + + it )
if ( ( * it ) . lobby_id = = id )
{
QString lobby_description_string ;
lobby_description_string + = " <h2> " + tr ( " Selected lobby info " ) + " </h2> " ;
2013-07-01 16:35:07 -04:00
lobby_description_string + = " <b> " + tr ( " Lobby name: " ) + " </b> \t " + RsHtml : : plainText ( it - > lobby_name ) + " <br/> " ;
2013-02-24 17:44:14 -05:00
lobby_description_string + = " <b> " + tr ( " Lobby Id: " ) + " </b> \t " + QString : : number ( ( * it ) . lobby_id , 16 ) + " <br/> " ;
2013-07-01 16:35:07 -04:00
lobby_description_string + = " <b> " + tr ( " Topic: " ) + " </b> \t " + RsHtml : : plainText ( it - > lobby_topic ) + " <br/> " ;
2013-02-24 17:44:14 -05:00
lobby_description_string + = " <b> " + tr ( " Type: " ) + " </b> \t " + ( ( ( * it ) . lobby_privacy_level = = RS_CHAT_LOBBY_PRIVACY_LEVEL_PRIVATE ) ? tr ( " Private " ) : tr ( " Public " ) ) + " <br/> " ;
lobby_description_string + = " <b> " + tr ( " Peers: " ) + " </b> \t " + QString : : number ( ( * it ) . total_number_of_peers ) + " <br/> " ;
lobby_description_string + = " <br/><br/> " + tr ( " You're not subscribed to this lobby; Double click-it to enter and chat. " ) ;
_lobby_blank_page - > setText ( lobby_description_string ) ;
return ;
}
QString text = tr ( " No lobby selected. \n \n Select lobbies at left to show details. \n \n Double click lobbies to enter and chat. " ) ;
_lobby_blank_page - > setText ( text ) ;
}
2012-01-27 13:51:27 -05:00
void ChatLobbyWidget : : subscribeItem ( )
{
subscribeLobby ( lobbyTreeWidget - > currentItem ( ) ) ;
}
2013-06-29 12:15:33 -04:00
void ChatLobbyWidget : : autoSubscribeItem ( )
{
autoSubscribeLobby ( lobbyTreeWidget - > currentItem ( ) ) ;
}
2013-02-24 17:44:14 -05:00
QTreeWidgetItem * ChatLobbyWidget : : getTreeWidgetItem ( ChatLobbyId id )
{
for ( int p = 0 ; p < 2 ; + + p )
{
QTreeWidgetItem * lobby_item = ( p = = 0 ) ? publicLobbyItem : privateLobbyItem ;
int childCnt = lobby_item - > childCount ( ) ;
int childIndex = 0 ;
while ( childIndex < childCnt ) {
QTreeWidgetItem * itemLoop = lobby_item - > child ( childIndex ) ;
if ( itemLoop - > type ( ) = = TYPE_LOBBY & & itemLoop - > data ( COLUMN_DATA , ROLE_ID ) . toULongLong ( ) = = id )
return itemLoop ;
2013-02-24 17:55:58 -05:00
+ + childIndex ;
2013-02-24 17:44:14 -05:00
}
}
return NULL ;
}
void ChatLobbyWidget : : updateTypingStatus ( ChatLobbyId id )
{
QTreeWidgetItem * item = getTreeWidgetItem ( id ) ;
if ( item ! = NULL )
{
2013-08-26 19:37:13 -04:00
item - > setIcon ( COLUMN_NAME , QIcon ( IMAGE_TYPING ) ) ;
2013-02-27 17:17:49 -05:00
_lobby_infos [ id ] . last_typing_event = time ( NULL ) ;
2013-02-24 17:44:14 -05:00
2013-02-27 17:17:49 -05:00
QTimer : : singleShot ( 5000 , this , SLOT ( resetLobbyTreeIcons ( ) ) ) ;
}
}
void ChatLobbyWidget : : updatePeerLeaving ( ChatLobbyId id )
{
QTreeWidgetItem * item = getTreeWidgetItem ( id ) ;
if ( item ! = NULL )
{
2013-08-26 19:37:13 -04:00
item - > setIcon ( COLUMN_NAME , QIcon ( IMAGE_PEER_LEAVING ) ) ;
2013-02-27 17:17:49 -05:00
_lobby_infos [ id ] . last_typing_event = time ( NULL ) ;
2013-02-24 17:44:14 -05:00
QTimer : : singleShot ( 5000 , this , SLOT ( resetLobbyTreeIcons ( ) ) ) ;
}
}
2013-02-27 17:17:49 -05:00
void ChatLobbyWidget : : updatePeerEntering ( ChatLobbyId id )
{
QTreeWidgetItem * item = getTreeWidgetItem ( id ) ;
if ( item ! = NULL )
{
2013-08-26 19:37:13 -04:00
item - > setIcon ( COLUMN_NAME , QIcon ( IMAGE_PEER_ENTERING ) ) ;
2013-02-27 17:17:49 -05:00
_lobby_infos [ id ] . last_typing_event = time ( NULL ) ;
2013-02-24 17:44:14 -05:00
2013-02-27 17:17:49 -05:00
QTimer : : singleShot ( 5000 , this , SLOT ( resetLobbyTreeIcons ( ) ) ) ;
}
}
2013-02-24 17:44:14 -05:00
void ChatLobbyWidget : : resetLobbyTreeIcons ( )
{
time_t now = time ( NULL ) ;
2013-02-27 17:17:49 -05:00
for ( std : : map < ChatLobbyId , ChatLobbyInfoStruct > : : iterator it ( _lobby_infos . begin ( ) ) ; it ! = _lobby_infos . end ( ) ; + + it )
if ( it - > second . last_typing_event + 5 < = now )
2013-02-24 17:44:14 -05:00
{
2013-08-26 19:37:13 -04:00
getTreeWidgetItem ( it - > first ) - > setIcon ( COLUMN_NAME , it - > second . default_icon ) ;
2013-03-21 16:55:07 -04:00
// std::cerr << "Reseted 1 lobby icon." << std::endl;
2013-02-24 17:44:14 -05:00
}
}
2012-01-11 19:13:25 -05:00
void ChatLobbyWidget : : unsubscribeItem ( )
{
QTreeWidgetItem * item = lobbyTreeWidget - > currentItem ( ) ;
if ( item = = NULL & & item - > type ( ) ! = TYPE_LOBBY ) {
return ;
}
const ChatLobbyId id = item - > data ( COLUMN_DATA , ROLE_ID ) . toULongLong ( ) ;
2013-02-24 17:44:14 -05:00
unsubscribeChatLobby ( id ) ;
}
void ChatLobbyWidget : : unsubscribeChatLobby ( ChatLobbyId id )
{
std : : cerr < < " Unsubscribing from chat lobby " < < std : : endl ;
// close the tab.
2013-02-27 17:17:49 -05:00
std : : map < ChatLobbyId , ChatLobbyInfoStruct > : : iterator it = _lobby_infos . find ( id ) ;
2013-02-24 17:44:14 -05:00
2013-02-27 17:17:49 -05:00
if ( it ! = _lobby_infos . end ( ) )
2013-02-24 17:44:14 -05:00
{
2013-02-27 17:17:49 -05:00
stackedWidget - > removeWidget ( it - > second . dialog ) ;
_lobby_infos . erase ( it ) ;
2013-02-24 17:44:14 -05:00
}
// Unsubscribe the chat lobby
2012-01-11 19:13:25 -05:00
std : : string vpeer_id ;
2013-02-24 17:44:14 -05:00
if ( rsMsgs - > getVirtualPeerId ( id , vpeer_id ) )
2012-01-17 15:36:36 -05:00
ChatDialog : : closeChat ( vpeer_id ) ;
2012-01-11 19:13:25 -05:00
rsMsgs - > unsubscribeChatLobby ( id ) ;
2013-06-29 12:15:33 -04:00
bool isAutoSubscribe = rsMsgs - > getLobbyAutoSubscribe ( id ) ;
if ( isAutoSubscribe ) rsMsgs - > setLobbyAutoSubscribe ( id , ! isAutoSubscribe ) ;
2013-08-28 17:13:35 -04:00
ChatLobbyDialog * cldCW = NULL ;
if ( NULL ! = ( cldCW = dynamic_cast < ChatLobbyDialog * > ( stackedWidget - > currentWidget ( ) ) ) )
{
QTreeWidgetItem * qtwiFound = getTreeWidgetItem ( cldCW - > id ( ) ) ;
if ( qtwiFound ) {
lobbyTreeWidget - > setCurrentItem ( qtwiFound ) ;
}
} else {
lobbyTreeWidget - > clearSelection ( ) ;
}
2012-01-11 19:13:25 -05:00
}
2013-02-24 17:44:14 -05:00
void ChatLobbyWidget : : updateCurrentLobby ( )
2012-01-11 19:13:25 -05:00
{
2013-02-24 17:44:14 -05:00
QList < QTreeWidgetItem * > items = lobbyTreeWidget - > selectedItems ( ) ;
if ( items . empty ( ) )
showLobby ( 0 ) ;
else
2013-02-27 17:17:49 -05:00
{
2013-08-26 19:37:13 -04:00
QTreeWidgetItem * item = items . front ( ) ;
showLobby ( item ) ;
2013-02-28 16:47:37 -05:00
2013-08-26 19:37:13 -04:00
ChatLobbyId id = item - > data ( COLUMN_DATA , ROLE_ID ) . toULongLong ( ) ;
2013-02-28 16:47:37 -05:00
2013-08-26 19:37:13 -04:00
if ( _lobby_infos . find ( id ) ! = _lobby_infos . end ( ) ) {
QIcon icon = ( item - > parent ( ) = = publicLobbyItem ) ? QIcon ( IMAGE_PUBLIC ) : QIcon ( IMAGE_PRIVATE ) ;
_lobby_infos [ id ] . default_icon = icon ;
item - > setIcon ( COLUMN_NAME , icon ) ;
}
2013-02-27 17:17:49 -05:00
}
2013-09-01 20:08:38 -04:00
if ( filterLineEdit - > text ( ) . isEmpty ( ) = = false ) {
filterItems ( filterLineEdit - > text ( ) ) ;
}
2013-02-27 17:17:49 -05:00
}
void ChatLobbyWidget : : updateMessageChanged ( ChatLobbyId id )
{
2013-03-03 16:41:56 -05:00
QTreeWidgetItem * current_item = lobbyTreeWidget - > currentItem ( ) ;
// Don't show anything for current lobby.
//
if ( current_item ! = NULL & & current_item - > data ( COLUMN_DATA , ROLE_ID ) . toULongLong ( ) = = id )
return ;
2013-02-27 17:17:49 -05:00
_lobby_infos [ id ] . default_icon = QIcon ( IMAGE_MESSAGE ) ;
QTreeWidgetItem * item = getTreeWidgetItem ( id ) ;
2013-03-01 15:51:40 -05:00
if ( item = = NULL )
return ;
2013-08-26 19:37:13 -04:00
item - > setIcon ( COLUMN_NAME , _lobby_infos [ id ] . default_icon ) ;
2012-01-11 19:13:25 -05:00
}
2013-02-24 17:44:14 -05:00
void ChatLobbyWidget : : itemDoubleClicked ( QTreeWidgetItem * item , int /*column*/ )
2013-02-22 16:42:27 -05:00
{
2013-02-24 17:44:14 -05:00
subscribeLobby ( item ) ;
2013-02-22 16:42:27 -05:00
}
2012-01-11 19:13:25 -05:00
void ChatLobbyWidget : : displayChatLobbyEvent ( qulonglong lobby_id , int event_type , const QString & nickname , const QString & str )
{
std : : string vpid ;
if ( rsMsgs - > getVirtualPeerId ( lobby_id , vpid ) ) {
2012-01-17 15:36:36 -05:00
if ( ChatLobbyDialog * cld = dynamic_cast < ChatLobbyDialog * > ( ChatDialog : : getExistingChat ( vpid ) ) ) {
2012-01-11 19:13:25 -05:00
cld - > displayLobbyEvent ( event_type , nickname , str ) ;
}
}
2012-01-07 14:52:58 -05:00
}
2012-01-11 19:13:25 -05:00
void ChatLobbyWidget : : readChatLobbyInvites ( )
{
std : : list < ChatLobbyInvite > invites ;
rsMsgs - > getPendingChatLobbyInvites ( invites ) ;
for ( std : : list < ChatLobbyInvite > : : const_iterator it ( invites . begin ( ) ) ; it ! = invites . end ( ) ; + + it ) {
2013-07-01 16:35:07 -04:00
if ( QMessageBox : : Ok = = QMessageBox : : question ( this , tr ( " Invitation to chat lobby " ) , tr ( " %1 invites you to chat lobby named %2 " ) . arg ( QString : : fromUtf8 ( rsPeers - > getPeerName ( ( * it ) . peer_id ) . c_str ( ) ) ) . arg ( RsHtml : : plainText ( it - > lobby_name ) ) , QMessageBox : : Ok , QMessageBox : : Ignore ) ) {
2012-01-11 19:13:25 -05:00
std : : cerr < < " Accepting invite to lobby " < < ( * it ) . lobby_name < < std : : endl ;
rsMsgs - > acceptLobbyInvite ( ( * it ) . lobby_id ) ;
2012-01-07 14:52:58 -05:00
2012-01-11 19:13:25 -05:00
std : : string vpid ;
if ( rsMsgs - > getVirtualPeerId ( ( * it ) . lobby_id , vpid ) ) {
2013-06-29 12:15:33 -04:00
ChatDialog : : chatFriend ( vpid , true ) ;
2012-01-11 19:13:25 -05:00
} else {
std : : cerr < < " No lobby known with id 0x " < < std : : hex < < ( * it ) . lobby_id < < std : : dec < < std : : endl ;
}
} else {
rsMsgs - > denyLobbyInvite ( ( * it ) . lobby_id ) ;
}
}
}
2013-09-01 20:08:38 -04:00
void ChatLobbyWidget : : filterColumnChanged ( int )
{
filterItems ( filterLineEdit - > text ( ) ) ;
}
void ChatLobbyWidget : : filterItems ( const QString & text )
{
int filterColumn = filterLineEdit - > currentFilter ( ) ;
int count = lobbyTreeWidget - > topLevelItemCount ( ) ;
for ( int index = 0 ; index < count ; index + + ) {
filterItem ( lobbyTreeWidget - > topLevelItem ( index ) , text , filterColumn ) ;
}
}
bool ChatLobbyWidget : : filterItem ( QTreeWidgetItem * item , const QString & text , int filterColumn )
{
bool visible = true ;
if ( text . isEmpty ( ) = = false ) {
if ( item - > text ( filterColumn ) . contains ( text , Qt : : CaseInsensitive ) = = false ) {
visible = false ;
}
}
int visibleChildCount = 0 ;
int count = item - > childCount ( ) ;
for ( int index = 0 ; index < count ; index + + ) {
if ( filterItem ( item - > child ( index ) , text , filterColumn ) ) {
visibleChildCount + + ;
}
}
if ( visible | | visibleChildCount ) {
item - > setHidden ( false ) ;
} else {
item - > setHidden ( true ) ;
}
return ( visible | | visibleChildCount ) ;
}