From e19dcdfed2da5405c714bc0af56927fa8e4c2442 Mon Sep 17 00:00:00 2001 From: Simon Rettberg Date: Wed, 26 Oct 2016 18:31:21 +0200 Subject: [client] Move ServerConnection instance to ClientApp --- src/client/clientapp/clientapp.cpp | 19 +++++++++++ src/client/clientapp/clientapp.h | 20 ++++++++++-- src/client/connectwindow/connectwindow.cpp | 31 ++++++++++-------- src/client/connectwindow/connectwindow.h | 4 +-- src/client/net/serverconnection.cpp | 2 +- src/client/net/serverconnection.h | 2 +- src/client/net/serverdiscovery.cpp | 2 +- src/client/toolbar/toolbar.cpp | 51 ++++++++++++------------------ src/client/toolbar/toolbar.h | 5 +-- 9 files changed, 81 insertions(+), 55 deletions(-) diff --git a/src/client/clientapp/clientapp.cpp b/src/client/clientapp/clientapp.cpp index 3b90dee..5528c65 100644 --- a/src/client/clientapp/clientapp.cpp +++ b/src/client/clientapp/clientapp.cpp @@ -1,5 +1,8 @@ #include "clientapp.h" #include +#include "../connectwindow/connectwindow.h" +#include "../toolbar/toolbar.h" +#include "../net/serverconnection.h" ClientApp::ClientApp(int& argc, char** argv) : QApplication(argc, argv), _connectionMode(ConnectionMode::None), _examMode(false), _isManagerPc(false) @@ -26,6 +29,8 @@ ClientApp::ClientApp(int& argc, char** argv) translator->load(":pvsclient"); installTranslator(translator); + _connectWindow = new ConnectWindow(NULL); + connect(_connectWindow, SIGNAL(connected(ServerConnection*)), this, SLOT(connected(ServerConnection*))); /* TODO: Move the connection handling to ClientApp */ if (_connectionMode == ConnectionMode::Auto) { _toolbar = new Toolbar(true); // auto connect client without session ID. @@ -103,3 +108,17 @@ void ClientApp::readIsManagerPc() } } +void ClientApp::connected(ServerConnection* connection) +{ + _connection = connection; +} + +void ClientApp::disconnected(ServerConnection* connection) +{ + if (connection != NULL) + connection->blockSignals(true); + if (_connection == connection) { + _connection = NULL; + } +} + diff --git a/src/client/clientapp/clientapp.h b/src/client/clientapp/clientapp.h index 31b2399..8a7fd83 100644 --- a/src/client/clientapp/clientapp.h +++ b/src/client/clientapp/clientapp.h @@ -1,7 +1,7 @@ #include -#include "../toolbar/toolbar.h" #include "../util/util.h" - +#include +#include /* define a macro `clientApp` that can be used anywhere in the program and returns a reference to the current ClientApp instance */ #if defined(clientApp) @@ -9,6 +9,10 @@ #endif #define clientApp (static_cast(QCoreApplication::instance())) +class Toolbar; +class ConnectWindow; +class ServerConnection; + /* this class is supposed to (after complete refactoring) to encapsulate all * state of the application. At the moment, the state is distributed within * several widgets. With this class information access will also be easier as @@ -27,6 +31,8 @@ private: bool _examMode; QString _sessionName; /* only set when _connectionMode == Session */ Toolbar* _toolbar; + ConnectWindow* _connectWindow; + ServerConnection* _connection; QStringList _arguments; QString _iniPath; bool _isManagerPc; @@ -45,5 +51,15 @@ public: QSharedPointer getSettings(); + ServerConnection* connection() const { return _connection; } + + ConnectWindow* connectWindow() const { return _connectWindow; } + const bool isManagerPc() const { return _isManagerPc; } + +private slots: + + void connected(ServerConnection* connection); + void disconnected(ServerConnection* connection); + }; diff --git a/src/client/connectwindow/connectwindow.cpp b/src/client/connectwindow/connectwindow.cpp index 2f3797a..18ceb9c 100644 --- a/src/client/connectwindow/connectwindow.cpp +++ b/src/client/connectwindow/connectwindow.cpp @@ -10,6 +10,7 @@ #include "../../shared/network.h" #include "../../shared/util.h" #include "../net/serverconnection.h" +#include "../clientapp/clientapp.h" #include "connectwindow.h" #include "ui_connect.h" @@ -24,9 +25,9 @@ ConnectWindow::ConnectWindow(QWidget *parent) : QWidget(parent) { _ui = new Ui::ConnectWindow; _timerHide = 0; - _connection = NULL; _state = Idle; _hashSslErrorCount = 0; + _pendingConnection = NULL; // Initialize the GUI _ui->setupUi(this); @@ -74,7 +75,7 @@ void ConnectWindow::updateUserInterface() if (_state == Connected) { _ui->btn_connection->setEnabled(true); _ui->btn_connection->setText(tr("&Disconnect")); - _ui->lblStatus->setText(tr("Connected to %1").arg(_connection->getPeerAdress())); + _ui->lblStatus->setText(tr("Connected to %1").arg(clientApp->connection()->getPeerAdress())); _ui->lineEditName->setEnabled(false); _ui->stackedWidget->setCurrentIndex(1); return; @@ -290,10 +291,10 @@ void ConnectWindow::onRoomSelection(int index) */ void ConnectWindow::onServerDetected(const QString& host, const quint16 port, const QByteArray& sessionName, const QByteArray& certHash, bool autoConnect) { - _connection = new ServerConnection(host, port, sessionName, certHash, autoConnect); - connect(_connection, SIGNAL(stateChange(ConnectWindow::ConnectionState)), this, SLOT(onConnectionStateChange(ConnectWindow::ConnectionState))); - connect(_connection, SIGNAL(destroyed(QObject*)), this, SLOT(onConnectionClosed(QObject*))); - connect(_connection, SIGNAL(disconnected()), this, SLOT(onConnectionDisconnected())); + _pendingConnection = new ServerConnection(host, port, sessionName, certHash, autoConnect); + connect(_pendingConnection, SIGNAL(stateChange(ConnectWindow::ConnectionState)), this, SLOT(onConnectionStateChange(ConnectWindow::ConnectionState))); + connect(_pendingConnection, SIGNAL(destroyed(QObject*)), this, SLOT(onConnectionClosed(QObject*))); + connect(_pendingConnection, SIGNAL(disconnected(ServerConnection*)), this, SLOT(onConnectionDisconnected(ServerConnection*))); } @@ -308,21 +309,23 @@ void ConnectWindow::onConnectionStateChange(ConnectWindow::ConnectionState state ++_hashSslErrorCount; _state = state; - this->updateUserInterface(); if (reset) { _state = Scanning; } if (state == Connected) { - QObject::disconnect(_connection, SIGNAL(stateChange(ConnectWindow::ConnectionState)), this, SLOT(onConnectionStateChange(ConnectWindow::ConnectionState))); - QObject::disconnect(_connection, SIGNAL(destroyed(QObject*)), this, SLOT(onConnectionClosed(QObject*))); - emit connected(_connection); + QObject::disconnect(_pendingConnection, SIGNAL(stateChange(ConnectWindow::ConnectionState)), this, SLOT(onConnectionStateChange(ConnectWindow::ConnectionState))); + QObject::disconnect(_pendingConnection, SIGNAL(destroyed(QObject*)), this, SLOT(onConnectionClosed(QObject*))); + emit connected(_pendingConnection); + _pendingConnection = NULL; this->updateUserInterface(); - _connection = NULL; _timerHide = startTimer(2000); + } else { + this->updateUserInterface(); } + } void ConnectWindow::onComboBox_keyPressed(QKeyEvent* e) @@ -335,18 +338,18 @@ void ConnectWindow::onComboBox_keyPressed(QKeyEvent* e) } /***************************************************************************//** - * If connection is closed set _connection = NULL. + * If connection is closed set _pendingConnection = NULL. * @param connection */ void ConnectWindow::onConnectionClosed(QObject* connection) { - _connection = NULL; + _pendingConnection = NULL; } /***************************************************************************//** * @brief ConnectWindow::onConnectionDisconnected */ -void ConnectWindow::onConnectionDisconnected() +void ConnectWindow::onConnectionDisconnected(ServerConnection*) { _state = Idle; this->updateUserInterface(); diff --git a/src/client/connectwindow/connectwindow.h b/src/client/connectwindow/connectwindow.h index aa89094..53af662 100644 --- a/src/client/connectwindow/connectwindow.h +++ b/src/client/connectwindow/connectwindow.h @@ -62,9 +62,9 @@ public: private: Ui::ConnectWindow *_ui; - ServerConnection *_connection; int _hashSslErrorCount; ServerDiscovery _serverDiscovery; + ServerConnection* _pendingConnection; ConnectionState _state; QByteArray _currentSession; QString _currentIp; @@ -89,7 +89,7 @@ protected slots: void onConnectionStateChange(ConnectWindow::ConnectionState state); void onConnectionClosed(QObject* connection); - void onConnectionDisconnected(); + void onConnectionDisconnected(ServerConnection* connection); // void onUdpReadyRead(); void onServerDetected(const QString& host, const quint16 port, const QByteArray& sessionName, const QByteArray& certHash, bool autoConnect); diff --git a/src/client/net/serverconnection.cpp b/src/client/net/serverconnection.cpp index 02c35fd..448b200 100644 --- a/src/client/net/serverconnection.cpp +++ b/src/client/net/serverconnection.cpp @@ -84,7 +84,7 @@ void ServerConnection::disconnectFromServer() if (_timerDelete == 0) { VncServer::instance()->stop(); emit closeVnc(); - emit disconnected(); + emit disconnected(this); _timerDelete = startTimer(500); qDebug("Closing connection to server"); if (_socket != NULL) { diff --git a/src/client/net/serverconnection.h b/src/client/net/serverconnection.h index e299a04..c36b238 100644 --- a/src/client/net/serverconnection.h +++ b/src/client/net/serverconnection.h @@ -63,7 +63,7 @@ signals: void openVnc(const QString& host, int port, const QString& passwd, bool ro, bool fullscreen, const QString& caption, const int clientId, const QByteArray& rawThumb); void closeVnc(); void stateChange(ConnectWindow::ConnectionState state); - void disconnected(); + void disconnected(ServerConnection* connection); void attentionChanged(bool state); }; diff --git a/src/client/net/serverdiscovery.cpp b/src/client/net/serverdiscovery.cpp index 4a69e91..99a0dcd 100644 --- a/src/client/net/serverdiscovery.cpp +++ b/src/client/net/serverdiscovery.cpp @@ -150,7 +150,7 @@ void ServerDiscovery::onUdpReadyRead() } const qint64 size = _discoverySocket.readDatagram(data, UDPBUFSIZ, &addr, &port); - if (size <= 0) //|| _connection != NULL) // TODO CHECK + if (size <= 0) //|| clientApp->connection() != NULL) // TODO CHECK continue; _packet.reset(); diff --git a/src/client/toolbar/toolbar.cpp b/src/client/toolbar/toolbar.cpp index 3d370dd..d6bcf22 100644 --- a/src/client/toolbar/toolbar.cpp +++ b/src/client/toolbar/toolbar.cpp @@ -34,7 +34,7 @@ Toolbar::Toolbar(const QByteArray sessionName, QWidget *parent) qDebug() << "sessionName - constructor"; init(); - _connectWindow->connectToSession(sessionName, ""); + clientApp->connectWindow()->connectToSession(sessionName, ""); } /***************************************************************************//** @@ -58,7 +58,7 @@ Toolbar::Toolbar(const bool autoConnect, QWidget *parent) // Try getting manager ip. QString mgrIp = identifyMgrIP(); if (!mgrIp.isEmpty()) - _connectWindow->connectToSession("", mgrIp); + clientApp->connectWindow()->connectToSession("", mgrIp); } } @@ -87,7 +87,6 @@ void Toolbar::exit() void Toolbar::init() { _ui = new Ui::Toolbar; - _connection = NULL; /* Initialize the GUI */ _ui->setupUi(this); @@ -100,11 +99,10 @@ void Toolbar::init() _vnc = new VncWindow(NULL); /* Create the connect window */ - _connectWindow = new ConnectWindow(NULL); - _connectWindow->setAvailableRooms(myRooms()); + clientApp->connectWindow()->setAvailableRooms(myRooms()); // Connect the signals - connect(_connectWindow, SIGNAL(disconnect()), this, SLOT(onDoDisconnect())); - connect(_connectWindow, SIGNAL(connected(ServerConnection*)), this, SLOT(onConnected(ServerConnection*))); + connect(clientApp->connectWindow(), SIGNAL(disconnect()), this, SLOT(onDoDisconnect())); + connect(clientApp->connectWindow(), SIGNAL(connected(ServerConnection*)), this, SLOT(onConnected(ServerConnection*))); /* Setup menu */ initMenu(); @@ -183,8 +181,8 @@ void Toolbar::initMenu() // Connect the signals connect(_menu, SIGNAL(aboutToHide()), this, SLOT(hideBar())); - connect(_acnConnect, SIGNAL(triggered()), _connectWindow, SLOT(doShow())); - connect(_acnDisconnect, SIGNAL(triggered()), _connectWindow, SLOT(DoDisconnect())); + connect(_acnConnect, SIGNAL(triggered()), clientApp->connectWindow(), SLOT(doShow())); + connect(_acnDisconnect, SIGNAL(triggered()), clientApp->connectWindow(), SLOT(DoDisconnect())); connect(_acnInformation, SIGNAL(triggered()), this, SLOT(showInformationDialog())); connect(_acnAbout, SIGNAL(triggered()), this, SLOT(showAboutDialog())); connect(_acnQuit, SIGNAL(triggered()), this, SLOT(exit())); @@ -198,7 +196,6 @@ Toolbar::~Toolbar() { VncServer::instance()->stop(); _vnc->deleteLater(); - _connectWindow->deleteLater(); delete _ui; } @@ -348,11 +345,11 @@ void Toolbar::onVncServerIsRunning(int port) * A slot for the onDisconnected signal of the ConnectWindow. This slot will * change the UI according to the state fo the connection. */ -void Toolbar::onDisconnected() +void Toolbar::onDisconnected(ServerConnection* connection) { - if (_connection != NULL) - _connection->blockSignals(true); - _connection = NULL; + if (connection != NULL) { + disconnect(connection, SIGNAL(disconnected(ServerConnection*)), this, SLOT(onDisconnected(ServerConnection*))); + } _ui->lblStatus->setStyleSheet("color:red"); _ui->lblStatus->setText(tr("Offline")); @@ -380,18 +377,12 @@ void Toolbar::onConnected(ServerConnection* connection) /* connected, show button */ _ui->btnAttention->setVisible(true); // - if (_connection != NULL) { - disconnect(_connection, SIGNAL(disconnected()), this, SLOT(onDisconnected())); - _connection->blockSignals(true); - _connection->disconnectFromServer(); - } - _connection = connection; - connect(_connection, SIGNAL(disconnected()), this, SLOT(onDisconnected())); - connect(_connection, SIGNAL(openVnc(const QString&, int, const QString&, bool, bool, const QString&, const int, const QByteArray&)), + connect(connection, SIGNAL(disconnected(ServerConnection*)), this, SLOT(onDisconnected(ServerConnection*))); + connect(connection, SIGNAL(openVnc(const QString&, int, const QString&, bool, bool, const QString&, const int, const QByteArray&)), _vnc, SLOT(open(const QString&, int, const QString&, bool, bool, const QString&, const int, const QByteArray&))); - connect(_connection, SIGNAL(closeVnc()), _vnc, SLOT(close())); - connect(_connection, SIGNAL(attentionChanged(const bool)), this, SLOT(onServerAttentionChanged(const bool))); - connect(_vnc, SIGNAL(running(const bool, const int)), _connection, SLOT(onVncViewerStartStop(const bool, const int))); + connect(connection, SIGNAL(closeVnc()), _vnc, SLOT(close())); + connect(connection, SIGNAL(attentionChanged(const bool)), this, SLOT(onServerAttentionChanged(const bool))); + connect(_vnc, SIGNAL(running(const bool, const int)), connection, SLOT(onVncViewerStartStop(const bool, const int))); } /***************************************************************************//** @@ -399,8 +390,8 @@ void Toolbar::onConnected(ServerConnection* connection) */ void Toolbar::onDoDisconnect() { - if (_connection != NULL) - _connection->disconnectFromServer(); + if (clientApp->connection() != NULL) + clientApp->connection()->disconnectFromServer(); } void Toolbar::onServerAttentionChanged(const bool on) @@ -463,13 +454,13 @@ void Toolbar::showBar() void Toolbar::onBtnAttention() { - const bool on = _connection != NULL && _ui->btnAttention->isChecked(); + const bool on = clientApp->connection() != NULL && _ui->btnAttention->isChecked(); if (on != _ui->btnAttention->isChecked()) { _ui->btnAttention->setChecked(on); return; } - if (_connection != NULL) { - _connection->sendAttention(on); + if (clientApp->connection() != NULL) { + clientApp->connection()->sendAttention(on); } } diff --git a/src/client/toolbar/toolbar.h b/src/client/toolbar/toolbar.h index 5fb4ea5..a84286f 100644 --- a/src/client/toolbar/toolbar.h +++ b/src/client/toolbar/toolbar.h @@ -19,7 +19,6 @@ class ServerConnection; class VncWindow; -class ConnectWindow; class BlankScreen; @@ -46,9 +45,7 @@ private: QAction *_acnInformation; QAction *_acnAbout; QAction *_acnQuit; - ConnectWindow *_connectWindow; QTimer _hideTimer; - ServerConnection *_connection; QTimer _blinkTimer; VncWindow *_vnc; bool _isManagerPc; @@ -66,7 +63,7 @@ private: private slots: void onVncServerIsRunning(int port); - void onDisconnected(); + void onDisconnected(ServerConnection* connection); void onConnected(ServerConnection* connection); void onServerAttentionChanged(const bool on); void onDoDisconnect(); -- cgit v1.2.3-55-g7522