From 11fa5d636e9d77f4da5be74d4b5ffe3ff5d0cedb Mon Sep 17 00:00:00 2001 From: sithlord48 Date: Fri, 28 Nov 2025 15:02:06 -0500 Subject: [PATCH] refactor: ServerConnection return a bool for the results of the newClientPrompt no Enum needed --- src/lib/gui/Messages.cpp | 26 ++++++------------- src/lib/gui/Messages.h | 9 +------ src/lib/gui/core/ServerConnection.cpp | 10 +++---- src/lib/gui/core/ServerConnection.h | 2 +- .../gui/core/ServerConnectionTests.cpp | 7 +++-- 5 files changed, 16 insertions(+), 38 deletions(-) diff --git a/src/lib/gui/Messages.cpp b/src/lib/gui/Messages.cpp index bb5878b06..7c913ea23 100644 --- a/src/lib/gui/Messages.cpp +++ b/src/lib/gui/Messages.cpp @@ -242,10 +242,8 @@ void showClientConnectError(QWidget *parent, deskflow::client::ErrorType error, dialog.exec(); } -NewClientPromptResult showNewClientPrompt(QWidget *parent, const QString &clientName, bool serverRequiresPeerAuth) +bool showNewClientPrompt(QWidget *parent, const QString &clientName, bool serverRequiresPeerAuth) { - using enum NewClientPromptResult; - if (serverRequiresPeerAuth) { // When peer auth is enabled you will be prompted to allow the connection before seeing this dialog. // This is why we do not show a dialog with an option to ignore the new client @@ -254,22 +252,14 @@ NewClientPromptResult showNewClientPrompt(QWidget *parent, const QString &client QObject::tr("A new client called '%1' has been accepted. You'll need to add it to your server's screen layout.") .arg(clientName) ); - return Add; - } else { - QMessageBox message(parent); - const QPushButton *ignore = message.addButton(QObject::tr("Ignore"), QMessageBox::RejectRole); - const QPushButton *add = message.addButton(QObject::tr("Add client"), QMessageBox::AcceptRole); - message.setText(QObject::tr("A new client called '%1' wants to connect").arg(clientName)); - message.exec(); - if (message.clickedButton() == add) { - return Add; - } else if (message.clickedButton() == ignore) { - return Ignore; - } else { - qFatal("no expected dialog button was clicked"); - abort(); - } + return true; } + QMessageBox message(parent); + message.addButton(QObject::tr("Ignore"), QMessageBox::RejectRole); + message.addButton(QObject::tr("Add client"), QMessageBox::AcceptRole); + message.setText(QObject::tr("A new client called '%1' wants to connect").arg(clientName)); + message.exec(); + return message.buttonRole(message.clickedButton()) == QMessageBox::AcceptRole; } bool showClearSettings(QWidget *parent) diff --git a/src/lib/gui/Messages.h b/src/lib/gui/Messages.h index 2b0431e2e..7e27392c9 100644 --- a/src/lib/gui/Messages.h +++ b/src/lib/gui/Messages.h @@ -15,12 +15,6 @@ class QWidget; namespace deskflow::gui::messages { -enum class NewClientPromptResult -{ - Add, - Ignore -}; - void messageHandler(QtMsgType type, const QMessageLogContext &context, const QString &msg); void raiseCriticalDialog(); @@ -33,8 +27,7 @@ void showCloseReminder(QWidget *parent); void showClientConnectError(QWidget *parent, deskflow::client::ErrorType error, const QString &address); -NewClientPromptResult -showNewClientPrompt(QWidget *parent, const QString &clientName, bool serverRequiresPeerAuth = false); +bool showNewClientPrompt(QWidget *parent, const QString &clientName, bool serverRequiresPeerAuth = false); bool showClearSettings(QWidget *parent); diff --git a/src/lib/gui/core/ServerConnection.cpp b/src/lib/gui/core/ServerConnection.cpp index 7b02b8d14..17b0e6915 100644 --- a/src/lib/gui/core/ServerConnection.cpp +++ b/src/lib/gui/core/ServerConnection.cpp @@ -19,7 +19,7 @@ namespace deskflow::gui { // ServerConnection::Deps // -messages::NewClientPromptResult ServerConnection::Deps::showNewClientPrompt( +bool ServerConnection::Deps::showNewClientPrompt( QWidget *parent, const QString &clientName, bool serverRequiresPeerAuth ) const { @@ -83,8 +83,6 @@ void ServerConnection::handleLogLine(const QString &logLine) void ServerConnection::handleNewClient(const QString &clientName) { - using enum messages::NewClientPromptResult; - if (m_serverConfig.isFull()) { qDebug("server config full, skipping new client prompt for: %s", qPrintable(clientName)); return; @@ -103,13 +101,11 @@ void ServerConnection::handleNewClient(const QString &clientName) const auto result = m_pDeps->showNewClientPrompt(m_pParent, clientName, tlsEnabled && requireCerts); m_messageShowing = false; - if (result == Add) { + if (result) { qDebug("accepted dialog, adding client: %s", qPrintable(clientName)); Q_EMIT configureClient(clientName); - } else if (result == Ignore) { - qDebug("declined dialog, ignoring client: %s", qPrintable(clientName)); } else { - qFatal("unexpected add client result"); + qDebug("declined dialog, ignoring client: %s", qPrintable(clientName)); } m_connectedClients.insert(clientName); diff --git a/src/lib/gui/core/ServerConnection.h b/src/lib/gui/core/ServerConnection.h index 48f85ac5b..f9e8ed21f 100644 --- a/src/lib/gui/core/ServerConnection.h +++ b/src/lib/gui/core/ServerConnection.h @@ -22,7 +22,7 @@ public: struct Deps { virtual ~Deps() = default; - virtual messages::NewClientPromptResult + virtual bool showNewClientPrompt(QWidget *parent, const QString &clientName, bool serverRequiresPeerAuth = false) const; }; diff --git a/src/unittests/legacytests/legacytests/gui/core/ServerConnectionTests.cpp b/src/unittests/legacytests/legacytests/gui/core/ServerConnectionTests.cpp index 4222fb634..c8d2e0123 100644 --- a/src/unittests/legacytests/legacytests/gui/core/ServerConnectionTests.cpp +++ b/src/unittests/legacytests/legacytests/gui/core/ServerConnectionTests.cpp @@ -23,8 +23,8 @@ namespace { struct DepsMock : public ServerConnection::Deps { MOCK_METHOD( - messages::NewClientPromptResult, showNewClientPrompt, - (QWidget * parent, const QString &clientName, bool previousyAccepted), (const, override) + bool, showNewClientPrompt, (QWidget * parent, const QString &clientName, bool previousyAccepted), + (const, override) ); }; @@ -50,8 +50,7 @@ TEST_F(ServerConnectionTests, handleLogLine_newClient_shouldShowPrompt) TEST_F(ServerConnectionTests, handleLogLine_ignoredClient_shouldNotShowPrompt) { ServerConnection serverConnection(nullptr, m_serverConfig, m_pDeps); - ON_CALL(*m_pDeps, showNewClientPrompt(_, _, false)) - .WillByDefault(testing::Return(messages::NewClientPromptResult::Ignore)); + ON_CALL(*m_pDeps, showNewClientPrompt(_, _, false)).WillByDefault(testing::Return(false)); serverConnection.handleLogLine(R"(unrecognised client name "stub")"); EXPECT_CALL(*m_pDeps, showNewClientPrompt(_, _, false)).Times(0);