diff --git a/src/channel.cpp b/src/channel.cpp index 53f91766a8..182323b8a1 100644 --- a/src/channel.cpp +++ b/src/channel.cpp @@ -654,7 +654,7 @@ EGetDataStat CChannel::GetData ( CVector& vecbyData, const int iNumByte Protocol.Reset(); // emit message - emit Disconnected(); + emit DisconnectClient(); } return eGetStatus; diff --git a/src/channel.h b/src/channel.h index 9f70bfee6f..e5b16728e3 100644 --- a/src/channel.h +++ b/src/channel.h @@ -280,7 +280,7 @@ public slots: void LicenceRequired ( ELicenceType eLicenceType ); void VersionAndOSReceived ( COSUtil::EOpSystemType eOSType, QString strVersion ); void RecorderStateReceived ( ERecorderState eRecorderState ); - void Disconnected(); + void DisconnectClient(); void DetectedCLMessage ( CVector vecbyMesBodyData, int iRecID, CHostAddress RecHostAddr ); diff --git a/src/client.cpp b/src/client.cpp index 1c039b4f33..6aa16ef6ae 100644 --- a/src/client.cpp +++ b/src/client.cpp @@ -122,7 +122,7 @@ CClient::CClient ( const quint16 iPortNumber, QObject::connect ( &Channel, &CChannel::ConClientListMesReceived, this, &CClient::OnConClientListMesReceived ); QObject::connect ( &Channel, &CChannel::ConClientListMesReceived, this, &CClient::ConClientListMesReceived ); - QObject::connect ( &Channel, &CChannel::Disconnected, this, &CClient::Disconnected ); + QObject::connect ( &Channel, &CChannel::DisconnectClient, this, &CClient::Disconnect ); QObject::connect ( &Channel, &CChannel::NewConnection, this, &CClient::OnNewConnection ); @@ -907,51 +907,48 @@ void CClient::Stop() /// @method /// @brief Connects to strServerAddress -/// @emit Connecting (strServerName) if the client wasn't running and SetServerAddr returned true. +/// @emit Connecting (strServerName) if the client wasn't running and SetServerAddr was valid +/// @emit ConnectingFailed (error) if an error occurred /// @param strServerAddress - the server address to connect to /// @param strServerName - the String argument to be passed to Connecting() -/// @result true if client wasn't running and SetServerAddr returned true, false otherwise -bool CClient::Connect ( QString strServerAddress, QString strServerName ) +void CClient::Connect ( QString strServerAddress, QString strServerName ) { - if ( !IsRunning() ) - { - // Set server address and connect if valid address was supplied - if ( SetServerAddr ( strServerAddress ) ) + try { + if ( !IsRunning() ) { - - Start(); - - emit Connecting ( strServerName ); - - return true; + // Set server address and connect if valid address was supplied + if ( SetServerAddr ( strServerAddress ) ) + { + Start(); + emit Connecting ( strServerName ); + } + else { + throw CGenErr ( "Received invalid server address. Please check for typos in the provided server address." ); + } } } - - return false; + catch ( const CGenErr& generr ) + { + Disconnect(); + emit ConnectingFailed ( generr.GetErrorText() ); + } } /// @method -/// @brief Disconnects client +/// @brief Disconnects client. If the client is not running, it just stops Sound /// @emit Disconnected -/// @result true if client wasn't running, false otherwise -bool CClient::Disconnect() +void CClient::Disconnect() { if ( IsRunning() ) { Stop(); - emit Disconnected(); - - return true; } else { - // make sure sound is stopped too + // make sure sound is stopped in any case Sound.Stop(); - emit Disconnected(); - - return false; } } diff --git a/src/client.h b/src/client.h index 72adfbcecf..d77c7bea80 100644 --- a/src/client.h +++ b/src/client.h @@ -121,8 +121,8 @@ class CClient : public QObject void Start(); void Stop(); - bool Connect ( QString strServerAddress, QString strServerName ); - bool Disconnect(); + void Connect ( QString strServerAddress, QString strServerName ); + void Disconnect(); bool IsRunning() { return Sound.IsRunning(); } bool IsCallbackEntered() const { return Sound.IsCallbackEntered(); } @@ -390,7 +390,7 @@ protected slots: { if ( InetAddr == Channel.GetAddress() ) { - emit Disconnected(); + Disconnect(); } } void OnCLPingReceived ( CHostAddress InetAddr, int iMs ); @@ -430,7 +430,10 @@ protected slots: void CLChannelLevelListReceived ( CHostAddress InetAddr, CVector vecLevelList ); + void ConnectClient ( QString strServerAddress ); void Connecting ( QString strServerName ); + void ConnectingFailed ( QString errorMessage ); + void DisconnectClient(); void Disconnected(); void SoundDeviceChanged ( QString strError ); diff --git a/src/clientdlg.cpp b/src/clientdlg.cpp index 6489535a9b..309c5d5ca3 100644 --- a/src/clientdlg.cpp +++ b/src/clientdlg.cpp @@ -481,6 +481,8 @@ CClientDlg::CClientDlg ( CClient* pNCliP, QObject::connect ( pClient, &CClient::Connecting, this, &CClientDlg::OnConnect ); + QObject::connect ( pClient, &CClient::ConnectingFailed, this, &CClientDlg::OnConnectingFailed ); + QObject::connect ( pClient, &CClient::Disconnected, this, &CClientDlg::OnDisconnect ); QObject::connect ( pClient, &CClient::ChatTextReceived, this, &CClientDlg::OnChatTextReceived ); @@ -738,10 +740,7 @@ void CClientDlg::OnConnectDlgAccepted() // initiate connection // TODO: Refactor this for failing call on Connect() - if ( pClient->Connect ( strSelectedAddress, strMixerBoardLabel ) ) - { - OnConnect ( strMixerBoardLabel ); - } + pClient->Connect ( strSelectedAddress, strMixerBoardLabel ); // reset flag bConnectDlgWasShown = false; @@ -751,9 +750,13 @@ void CClientDlg::OnConnectDlgAccepted() void CClientDlg::OnConnectDisconBut() { // the connect/disconnect button implements a toggle functionality - if ( !pClient->Disconnect() ) + if ( pClient->IsRunning() ) + { + pClient->Disconnect(); + } + else { - // If the client didn't disconnect, we assume that we weren't connected. Thus show the connect dialog + // If the client isn't running, we assume that we weren't connected. Thus show the connect dialog // TODO: Refactor to have robust error handling ShowConnectionSetupDialog(); } @@ -1221,8 +1224,15 @@ void CClientDlg::OnConnect ( const QString& strMixerBoardLabel ) } } +void CClientDlg::OnConnectingFailed ( const QString& strError ) +{ + QMessageBox::critical ( this, APP_NAME, strError, "Close", nullptr ); +} + void CClientDlg::OnDisconnect() { + // channel.cpp also issues Disconnected() + // change connect button text to "connect" butConnect->setText ( tr ( "C&onnect" ) ); diff --git a/src/clientdlg.h b/src/clientdlg.h index 83acfb62a2..c8e67b3065 100644 --- a/src/clientdlg.h +++ b/src/clientdlg.h @@ -126,6 +126,7 @@ class CClientDlg : public CBaseDlg, private Ui_CClientDlgBase public slots: void OnConnect ( const QString& strServerName ); + void OnConnectingFailed ( const QString& strErrorText ); void OnDisconnect(); void OnConnectDisconBut(); void OnTimerSigMet();