From 69246f573ed3db89fd9bab6792e658da98eb27d9 Mon Sep 17 00:00:00 2001 From: ann0see <20726856+ann0see@users.noreply.github.com> Date: Sun, 15 Sep 2024 12:42:47 +0200 Subject: [PATCH] Add intermediate refactoring --- src/channel.cpp | 2 +- src/channel.h | 2 +- src/client.cpp | 49 ++++++++++++++++++++++------------------------- src/client.h | 9 ++++++--- src/clientdlg.cpp | 22 +++++++++++++-------- src/clientdlg.h | 1 + 6 files changed, 46 insertions(+), 39 deletions(-) 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..b4faa6b1bb 100644 --- a/src/clientdlg.cpp +++ b/src/clientdlg.cpp @@ -279,7 +279,6 @@ CClientDlg::CClientDlg ( CClient* pNCliP, // (no alias)) // initiate connection - // TODO: Refactor this for failing call on Connect() pClient->Connect ( strConnOnStartupAddress, strConnOnStartupAddress ); // TODO: Find out why without this the mixer status issue still occurs. @@ -481,6 +480,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 ); @@ -736,12 +737,8 @@ 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 +748,13 @@ void CClientDlg::OnConnectDlgAccepted() void CClientDlg::OnConnectDisconBut() { // the connect/disconnect button implements a toggle functionality - if ( !pClient->Disconnect() ) + if ( pClient->IsRunning() ) { - // If the client didn't disconnect, we assume that we weren't connected. Thus show the connect dialog + pClient->Disconnect(); + } + else + { + // 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,6 +1222,11 @@ void CClientDlg::OnConnect ( const QString& strMixerBoardLabel ) } } +void CClientDlg::OnConnectingFailed ( const QString& strError ) +{ + QMessageBox::critical ( this, APP_NAME, strError, "Close", nullptr ); +} + void CClientDlg::OnDisconnect() { // change connect button text to "connect" 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();