Skip to content

Commit 8bacce4

Browse files
committed
Add intermediate refactoring
1 parent 0b6a2bb commit 8bacce4

File tree

4 files changed

+44
-33
lines changed

4 files changed

+44
-33
lines changed

src/client.cpp

Lines changed: 23 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -907,51 +907,49 @@ void CClient::Stop()
907907

908908
/// @method
909909
/// @brief Connects to strServerAddress
910-
/// @emit Connecting (strServerName) if the client wasn't running and SetServerAddr returned true.
910+
/// @emit Connecting (strServerName) if the client wasn't running and SetServerAddr was valid
911+
/// @emit ConnectingFailed (error) if an error occurred
911912
/// @param strServerAddress - the server address to connect to
912913
/// @param strServerName - the String argument to be passed to Connecting()
913-
/// @result true if client wasn't running and SetServerAddr returned true, false otherwise
914-
bool CClient::Connect ( QString strServerAddress, QString strServerName )
914+
void CClient::Connect ( QString strServerAddress, QString strServerName )
915915
{
916-
if ( !IsRunning() )
917-
{
918-
// Set server address and connect if valid address was supplied
919-
if ( SetServerAddr ( strServerAddress ) )
916+
try {
917+
if ( !IsRunning() )
920918
{
921-
922-
Start();
923-
924-
emit Connecting ( strServerName );
925-
926-
return true;
919+
// Set server address and connect if valid address was supplied
920+
if ( SetServerAddr ( strServerAddress ) )
921+
{
922+
Start();
923+
emit Connecting ( strServerName );
924+
}
925+
else {
926+
throw CGenErr ( "Received invalid server address. Please check for typos in the provided server address." );
927+
}
927928
}
928929
}
929-
930-
return false;
930+
catch ( const CGenErr& generr )
931+
{
932+
Disconnect();
933+
emit ConnectingFailed ( generr.GetErrorText() );
934+
}
931935
}
932936

933937
/// @method
934-
/// @brief Disconnects client
938+
/// @brief Disconnects client. If the client is not running, it just stops Sound
935939
/// @emit Disconnected
936-
/// @result true if client wasn't running, false otherwise
937-
bool CClient::Disconnect()
940+
void CClient::Disconnect()
938941
{
942+
qDebug( "CClient::Disconnect executed" );
939943
if ( IsRunning() )
940944
{
941945
Stop();
942-
943946
emit Disconnected();
944-
945-
return true;
946947
}
947948
else
948949
{
949-
// make sure sound is stopped too
950+
// make sure sound is stopped in any case
950951
Sound.Stop();
951-
952952
emit Disconnected();
953-
954-
return false;
955953
}
956954
}
957955

src/client.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -121,8 +121,8 @@ class CClient : public QObject
121121

122122
void Start();
123123
void Stop();
124-
bool Connect ( QString strServerAddress, QString strServerName );
125-
bool Disconnect();
124+
void Connect ( QString strServerAddress, QString strServerName );
125+
void Disconnect();
126126

127127
bool IsRunning() { return Sound.IsRunning(); }
128128
bool IsCallbackEntered() const { return Sound.IsCallbackEntered(); }
@@ -430,7 +430,9 @@ protected slots:
430430

431431
void CLChannelLevelListReceived ( CHostAddress InetAddr, CVector<uint16_t> vecLevelList );
432432

433+
void ConnectClient ( QString strServerAddress );
433434
void Connecting ( QString strServerName );
435+
void ConnectingFailed ( QString errorMessage );
434436
void Disconnected();
435437

436438
void SoundDeviceChanged ( QString strError );

src/clientdlg.cpp

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -481,6 +481,8 @@ CClientDlg::CClientDlg ( CClient* pNCliP,
481481

482482
QObject::connect ( pClient, &CClient::Connecting, this, &CClientDlg::OnConnect );
483483

484+
QObject::connect ( pClient, &CClient::ConnectingFailed, this, &CClientDlg::OnConnectingFailed );
485+
484486
QObject::connect ( pClient, &CClient::Disconnected, this, &CClientDlg::OnDisconnect );
485487

486488
QObject::connect ( pClient, &CClient::ChatTextReceived, this, &CClientDlg::OnChatTextReceived );
@@ -738,10 +740,7 @@ void CClientDlg::OnConnectDlgAccepted()
738740
// initiate connection
739741
// TODO: Refactor this for failing call on Connect()
740742

741-
if ( pClient->Connect ( strSelectedAddress, strMixerBoardLabel ) )
742-
{
743-
OnConnect ( strMixerBoardLabel );
744-
}
743+
pClient->Connect ( strSelectedAddress, strMixerBoardLabel );
745744

746745
// reset flag
747746
bConnectDlgWasShown = false;
@@ -751,9 +750,13 @@ void CClientDlg::OnConnectDlgAccepted()
751750
void CClientDlg::OnConnectDisconBut()
752751
{
753752
// the connect/disconnect button implements a toggle functionality
754-
if ( !pClient->Disconnect() )
753+
if ( pClient->IsRunning() )
754+
{
755+
pClient->Disconnect();
756+
}
757+
else
755758
{
756-
// If the client didn't disconnect, we assume that we weren't connected. Thus show the connect dialog
759+
// If the client isn't running, we assume that we weren't connected. Thus show the connect dialog
757760
// TODO: Refactor to have robust error handling
758761
ShowConnectionSetupDialog();
759762
}
@@ -1221,8 +1224,15 @@ void CClientDlg::OnConnect ( const QString& strMixerBoardLabel )
12211224
}
12221225
}
12231226

1227+
void CClientDlg::OnConnectingFailed ( const QString& strError )
1228+
{
1229+
QMessageBox::critical ( this, APP_NAME, strError, "Close", nullptr );
1230+
}
1231+
12241232
void CClientDlg::OnDisconnect()
12251233
{
1234+
// channel.cpp also issues Disconnected()
1235+
12261236
// change connect button text to "connect"
12271237
butConnect->setText ( tr ( "C&onnect" ) );
12281238

src/clientdlg.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,7 @@ class CClientDlg : public CBaseDlg, private Ui_CClientDlgBase
126126

127127
public slots:
128128
void OnConnect ( const QString& strServerName );
129+
void OnConnectingFailed ( const QString& strErrorText );
129130
void OnDisconnect();
130131
void OnConnectDisconBut();
131132
void OnTimerSigMet();

0 commit comments

Comments
 (0)