diff --git a/cockatrice/CMakeLists.txt b/cockatrice/CMakeLists.txt index 405889af..50828658 100644 --- a/cockatrice/CMakeLists.txt +++ b/cockatrice/CMakeLists.txt @@ -193,7 +193,7 @@ QT4_ADD_RESOURCES(cockatrice_RESOURCES_RCC ${cockatrice_RESOURCES}) INCLUDE(${QT_USE_FILE}) INCLUDE_DIRECTORIES(../common) INCLUDE_DIRECTORIES(${PROTOBUF_INCLUDE_DIR}) -INCLUDE_DIRECTORIES(${CMAKE_CURRENT_BINARY_DIR}/../common) +INCLUDE_DIRECTORIES(${CMAKE_BINARY_DIR}/common) INCLUDE_DIRECTORIES(${CMAKE_CURRENT_BINARY_DIR}) INCLUDE_DIRECTORIES(${QT_MOBILITY_INCLUDE_DIR}) INCLUDE_DIRECTORIES(${QT_MOBILITY_MULTIMEDIAKIT_INCLUDE_DIR}) diff --git a/cockatrice/src/abstractclient.cpp b/cockatrice/src/abstractclient.cpp index 1b0e3c25..19d9c316 100644 --- a/cockatrice/src/abstractclient.cpp +++ b/cockatrice/src/abstractclient.cpp @@ -22,8 +22,10 @@ AbstractClient::AbstractClient(QObject *parent) : QObject(parent), nextCmdId(0), status(StatusDisconnected) { + qRegisterMetaType("QVariant"); qRegisterMetaType("CommandContainer"); qRegisterMetaType("Response"); + qRegisterMetaType("Response::ResponseCode"); qRegisterMetaType("ClientStatus"); qRegisterMetaType("RoomEvent"); qRegisterMetaType("GameEventContainer"); diff --git a/cockatrice/src/remoteclient.cpp b/cockatrice/src/remoteclient.cpp index 3914946f..e8a56495 100644 --- a/cockatrice/src/remoteclient.cpp +++ b/cockatrice/src/remoteclient.cpp @@ -24,18 +24,20 @@ RemoteClient::RemoteClient(QObject *parent) connect(socket, SIGNAL(error(QAbstractSocket::SocketError)), this, SLOT(slotSocketError(QAbstractSocket::SocketError))); connect(this, SIGNAL(serverIdentificationEventReceived(const Event_ServerIdentification &)), this, SLOT(processServerIdentificationEvent(const Event_ServerIdentification &))); + connect(this, SIGNAL(sigConnectToServer(QString, unsigned int, QString, QString)), this, SLOT(doConnectToServer(QString, unsigned int, QString, QString))); + connect(this, SIGNAL(sigDisconnectFromServer()), this, SLOT(doDisconnectFromServer())); } RemoteClient::~RemoteClient() { - disconnectFromServer(); + doDisconnectFromServer(); thread()->quit(); } void RemoteClient::slotSocketError(QAbstractSocket::SocketError /*error*/) { QString errorString = socket->errorString(); - disconnectFromServer(); + doDisconnectFromServer(); emit socketError(errorString); } @@ -118,7 +120,7 @@ void RemoteClient::readData() } while (!inputBuffer.isEmpty()); if (status == StatusDisconnecting) - disconnectFromServer(); + doDisconnectFromServer(); } void RemoteClient::sendCommandContainer(const CommandContainer &cont) @@ -135,9 +137,9 @@ void RemoteClient::sendCommandContainer(const CommandContainer &cont) socket->write(buf); } -void RemoteClient::connectToServer(const QString &hostname, unsigned int port, const QString &_userName, const QString &_password) +void RemoteClient::doConnectToServer(const QString &hostname, unsigned int port, const QString &_userName, const QString &_password) { - disconnectFromServer(); + doDisconnectFromServer(); userName = _userName; password = _password; @@ -145,7 +147,7 @@ void RemoteClient::connectToServer(const QString &hostname, unsigned int port, c setStatus(StatusConnecting); } -void RemoteClient::disconnectFromServer() +void RemoteClient::doDisconnectFromServer() { timer->stop(); @@ -163,14 +165,15 @@ void RemoteClient::disconnectFromServer() void RemoteClient::ping() { -/* QMutableMapIterator i(pendingCommands); - while (i.hasNext()) - if (i.next().value()->tick() > maxTimeout) { - CommandContainer *cont = i.value(); + QMutableMapIterator i(pendingCommands); + while (i.hasNext()) { + PendingCommand *pend = i.next().value(); + if (pend->tick() > maxTimeout) { i.remove(); - cont->deleteLater(); + pend->deleteLater(); } -*/ + } + int maxTime = timeRunning - lastDataReceived; emit maxPingTime(maxTime, maxTimeout); if (maxTime >= maxTimeout) { @@ -181,3 +184,13 @@ void RemoteClient::ping() ++timeRunning; } } + +void RemoteClient::connectToServer(const QString &hostname, unsigned int port, const QString &_userName, const QString &_password) +{ + emit sigConnectToServer(hostname, port, _userName, _password); +} + +void RemoteClient::disconnectFromServer() +{ + emit sigDisconnectFromServer(); +} diff --git a/cockatrice/src/remoteclient.h b/cockatrice/src/remoteclient.h index 798cbb7f..8f2e1b2b 100644 --- a/cockatrice/src/remoteclient.h +++ b/cockatrice/src/remoteclient.h @@ -14,6 +14,8 @@ signals: void socketError(const QString &errorString); void protocolVersionMismatch(int clientVersion, int serverVersion); void protocolError(); + void sigConnectToServer(const QString &hostname, unsigned int port, const QString &_userName, const QString &_password); + void sigDisconnectFromServer(); private slots: void slotConnected(); void readData(); @@ -21,6 +23,8 @@ private slots: void ping(); void processServerIdentificationEvent(const Event_ServerIdentification &event); void loginResponse(const Response &response); + void doConnectToServer(const QString &hostname, unsigned int port, const QString &_userName, const QString &_password); + void doDisconnectFromServer(); private: static const int maxTimeout = 10; int timeRunning, lastDataReceived; @@ -31,13 +35,12 @@ private: QTimer *timer; QTcpSocket *socket; - +protected slots: void sendCommandContainer(const CommandContainer &cont); public: RemoteClient(QObject *parent = 0); ~RemoteClient(); QString peerName() const { return socket->peerName(); } - void connectToServer(const QString &hostname, unsigned int port, const QString &_userName, const QString &_password); void disconnectFromServer(); }; diff --git a/cockatrice/src/tab_supervisor.cpp b/cockatrice/src/tab_supervisor.cpp index c883f862..e14ee537 100644 --- a/cockatrice/src/tab_supervisor.cpp +++ b/cockatrice/src/tab_supervisor.cpp @@ -72,13 +72,19 @@ void CloseButton::paintEvent(QPaintEvent * /*event*/) style()->drawPrimitive(QStyle::PE_IndicatorTabClose, &opt, &p, this); } -TabSupervisor::TabSupervisor(QWidget *parent) - : QTabWidget(parent), client(0), tabServer(0), tabDeckStorage(0), tabAdmin(0) +TabSupervisor::TabSupervisor(AbstractClient *_client, QWidget *parent) + : QTabWidget(parent), client(_client), tabServer(0), tabDeckStorage(0), tabAdmin(0) { tabChangedIcon = new QIcon(":/resources/icon_tab_changed.svg"); setElideMode(Qt::ElideRight); setIconSize(QSize(15, 15)); connect(this, SIGNAL(currentChanged(int)), this, SLOT(updateCurrent(int))); + + connect(client, SIGNAL(roomEventReceived(const RoomEvent &)), this, SLOT(processRoomEvent(const RoomEvent &))); + connect(client, SIGNAL(gameEventContainerReceived(const GameEventContainer &)), this, SLOT(processGameEventContainer(const GameEventContainer &))); + connect(client, SIGNAL(gameJoinedEventReceived(const Event_GameJoined &)), this, SLOT(gameJoined(const Event_GameJoined &))); + connect(client, SIGNAL(userMessageEventReceived(const Event_UserMessage &)), this, SLOT(processUserMessageEvent(const Event_UserMessage &))); + connect(client, SIGNAL(maxPingTime(int, int)), this, SLOT(updatePingTime(int, int))); } TabSupervisor::~TabSupervisor() @@ -113,17 +119,10 @@ int TabSupervisor::myAddTab(Tab *tab) return addTab(tab, tab->getTabText()); } -void TabSupervisor::start(AbstractClient *_client, const ServerInfo_User &_userInfo) +void TabSupervisor::start(const ServerInfo_User &_userInfo) { - client = _client; userInfo = new ServerInfo_User(_userInfo); - connect(client, SIGNAL(roomEventReceived(const RoomEvent &)), this, SLOT(processRoomEvent(const RoomEvent &))); - connect(client, SIGNAL(gameEventContainerReceived(const GameEventContainer &)), this, SLOT(processGameEventContainer(const GameEventContainer &))); - connect(client, SIGNAL(gameJoinedEventReceived(const Event_GameJoined &)), this, SLOT(gameJoined(const Event_GameJoined &))); - connect(client, SIGNAL(userMessageEventReceived(const Event_UserMessage &)), this, SLOT(processUserMessageEvent(const Event_UserMessage &))); - connect(client, SIGNAL(maxPingTime(int, int)), this, SLOT(updatePingTime(int, int))); - tabServer = new TabServer(this, client); connect(tabServer, SIGNAL(roomJoined(const ServerInfo_Room &, bool)), this, SLOT(addRoomTab(const ServerInfo_Room &, bool))); myAddTab(tabServer); @@ -176,11 +175,6 @@ void TabSupervisor::stop() if ((!client) && localClients.isEmpty()) return; - if (client) { - disconnect(client, 0, this, 0); - client = 0; - } - if (!localClients.isEmpty()) { for (int i = 0; i < localClients.size(); ++i) localClients[i]->deleteLater(); diff --git a/cockatrice/src/tab_supervisor.h b/cockatrice/src/tab_supervisor.h index 9f72cc4e..a0d83409 100644 --- a/cockatrice/src/tab_supervisor.h +++ b/cockatrice/src/tab_supervisor.h @@ -55,10 +55,10 @@ private: int myAddTab(Tab *tab); void addCloseButtonToTab(Tab *tab, int tabIndex); public: - TabSupervisor(QWidget *parent = 0); + TabSupervisor(AbstractClient *_client, QWidget *parent = 0); ~TabSupervisor(); void retranslateUi(); - void start(AbstractClient *_client, const ServerInfo_User &userInfo); + void start(const ServerInfo_User &userInfo); void startLocal(const QList &_clients); void stop(); int getGameCount() const { return gameTabs.size(); } diff --git a/cockatrice/src/window_main.cpp b/cockatrice/src/window_main.cpp index 4d845e3e..d5f365fe 100644 --- a/cockatrice/src/window_main.cpp +++ b/cockatrice/src/window_main.cpp @@ -108,7 +108,7 @@ void MainWindow::statusChanged(ClientStatus _status) void MainWindow::userInfoReceived(const ServerInfo_User &info) { - tabSupervisor->start(client, info); + tabSupervisor->start(info); } // Actions @@ -352,7 +352,7 @@ MainWindow::MainWindow(QWidget *parent) client->moveToThread(clientThread); clientThread->start(); - tabSupervisor = new TabSupervisor; + tabSupervisor = new TabSupervisor(client); connect(tabSupervisor, SIGNAL(setMenu(QMenu *)), this, SLOT(updateTabMenu(QMenu *))); connect(tabSupervisor, SIGNAL(localGameEnded()), this, SLOT(localGameEnded()));