From 23e84273ed28937eab179a392fc098b7cb4b0540 Mon Sep 17 00:00:00 2001 From: Fabio Bas Date: Wed, 7 Oct 2015 16:49:11 +0200 Subject: [PATCH] If the user request to join a room and we are already in, just focus the correct tab Also, handle all the Command_JoinRoom return values --- cockatrice/src/tab_server.cpp | 77 ++++++++++++++++++++++------------- cockatrice/src/tab_server.h | 7 ++-- 2 files changed, 52 insertions(+), 32 deletions(-) diff --git a/cockatrice/src/tab_server.cpp b/cockatrice/src/tab_server.cpp index d15f6d8c..56556a7e 100644 --- a/cockatrice/src/tab_server.cpp +++ b/cockatrice/src/tab_server.cpp @@ -12,6 +12,7 @@ #include "tab_server.h" #include "abstractclient.h" #include "userlist.h" +#include "tab_supervisor.h" #include #include "pending_command.h" @@ -111,43 +112,19 @@ void RoomSelector::processListRoomsEvent(const Event_ListRooms &event) roomList->addTopLevelItem(twi); if (room.has_auto_join()) if (room.auto_join()) - joinRoom(room.room_id(), false); + emit joinRoomRequest(room.room_id(), false); } } -void RoomSelector::joinRoom(int id, bool setCurrent) -{ - Command_JoinRoom cmd; - cmd.set_room_id(id); - - PendingCommand *pend = client->prepareSessionCommand(cmd); - pend->setExtraData(setCurrent); - connect(pend, SIGNAL(finished(Response, CommandContainer, QVariant)), this, SLOT(joinFinished(Response, CommandContainer, QVariant))); - - client->sendCommand(pend); -} - void RoomSelector::joinClicked() { QTreeWidgetItem *twi = roomList->currentItem(); if (!twi) return; - joinRoom(twi->data(0, Qt::UserRole).toInt(), true); -} + int id = twi->data(0, Qt::UserRole).toInt(); -void RoomSelector::joinFinished(const Response &r, const CommandContainer & /*commandContainer*/, const QVariant &extraData) -{ - switch (r.response_code()) { - case Response::RespOk: break; - case Response::RespUserLevelTooLow: QMessageBox::critical(this, tr("Error"), tr("You do not have the proper permission to join this room.")); return; - default: - QMessageBox::critical(this, tr("Error"), tr("Failed to join the room due to an unknown error.")); - return; - } - - const Response_JoinRoom &resp = r.GetExtension(Response_JoinRoom::ext); - emit roomJoined(resp.room_info(), extraData.toBool()); + emit joinRoomRequest(id, true); } TabServer::TabServer(TabSupervisor *_tabSupervisor, AbstractClient *_client, QWidget *parent) @@ -157,7 +134,7 @@ TabServer::TabServer(TabSupervisor *_tabSupervisor, AbstractClient *_client, QWi serverInfoBox = new QTextBrowser; serverInfoBox->setOpenExternalLinks(true); - connect(roomSelector, SIGNAL(roomJoined(const ServerInfo_Room &, bool)), this, SIGNAL(roomJoined(const ServerInfo_Room &, bool))); + connect(roomSelector, SIGNAL(joinRoomRequest(int, bool)), this, SLOT(joinRoom(int, bool))); connect(client, SIGNAL(serverMessageEventReceived(const Event_ServerMessage &)), this, SLOT(processServerMessageEvent(const Event_ServerMessage &))); @@ -178,3 +155,47 @@ void TabServer::processServerMessageEvent(const Event_ServerMessage &event) serverInfoBox->setHtml(QString::fromStdString(event.message())); emit userEvent(); } + +void TabServer::joinRoom(int id, bool setCurrent) +{ + TabRoom *room = tabSupervisor->getRoomTabs().value(id); + if(!room) + { + Command_JoinRoom cmd; + cmd.set_room_id(id); + + PendingCommand *pend = client->prepareSessionCommand(cmd); + pend->setExtraData(setCurrent); + connect(pend, SIGNAL(finished(Response, CommandContainer, QVariant)), this, SLOT(joinRoomFinished(Response, CommandContainer, QVariant))); + + client->sendCommand(pend); + + return; + } + + if(setCurrent) + tabSupervisor->setCurrentWidget((QWidget*)room); +} + +void TabServer::joinRoomFinished(const Response &r, const CommandContainer & /*commandContainer*/, const QVariant &extraData) +{ + switch (r.response_code()) { + case Response::RespOk: + break; + case Response::RespNameNotFound: + QMessageBox::critical(this, tr("Error"), tr("Failed to join the room: it doesn't exists on the server.")); + return; + case Response::RespContextError: + QMessageBox::critical(this, tr("Error"), tr("The server thinks you are in the room but Cockatrice is unable to display it. Try restarting Cockatrice.")); + return; + case Response::RespUserLevelTooLow: + QMessageBox::critical(this, tr("Error"), tr("You do not have the required permission to join this room.")); + return; + default: + QMessageBox::critical(this, tr("Error"), tr("Failed to join the room due to an unknown error: %1.").arg(r.response_code())); + return; + } + + const Response_JoinRoom &resp = r.GetExtension(Response_JoinRoom::ext); + emit roomJoined(resp.room_info(), extraData.toBool()); +} diff --git a/cockatrice/src/tab_server.h b/cockatrice/src/tab_server.h index a057660b..b7cd4e2a 100644 --- a/cockatrice/src/tab_server.h +++ b/cockatrice/src/tab_server.h @@ -24,14 +24,11 @@ private: QTreeWidget *roomList; QPushButton *joinButton; AbstractClient *client; - - void joinRoom(int id, bool setCurrent); private slots: void processListRoomsEvent(const Event_ListRooms &event); void joinClicked(); - void joinFinished(const Response &resp, const CommandContainer &commandContainer, const QVariant &extraData); signals: - void roomJoined(const ServerInfo_Room &info, bool setCurrent); + void joinRoomRequest(int, bool setCurrent); public: RoomSelector(AbstractClient *_client, QWidget *parent = 0); void retranslateUi(); @@ -43,6 +40,8 @@ signals: void roomJoined(const ServerInfo_Room &info, bool setCurrent); private slots: void processServerMessageEvent(const Event_ServerMessage &event); + void joinRoom(int id, bool setCurrent); + void joinRoomFinished(const Response &resp, const CommandContainer &commandContainer, const QVariant &extraData); private: AbstractClient *client; RoomSelector *roomSelector;