refactor: ServerConnection return a bool for the results of the newClientPrompt no Enum needed

This commit is contained in:
sithlord48 2025-11-28 15:02:06 -05:00 committed by Chris Rizzitello
parent cca6f80cb5
commit 11fa5d636e
5 changed files with 16 additions and 38 deletions

View file

@ -242,10 +242,8 @@ void showClientConnectError(QWidget *parent, deskflow::client::ErrorType error,
dialog.exec(); 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) { if (serverRequiresPeerAuth) {
// When peer auth is enabled you will be prompted to allow the connection before seeing this dialog. // 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 // 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.") QObject::tr("A new client called '%1' has been accepted. You'll need to add it to your server's screen layout.")
.arg(clientName) .arg(clientName)
); );
return Add; return true;
} 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();
}
} }
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) bool showClearSettings(QWidget *parent)

View file

@ -15,12 +15,6 @@ class QWidget;
namespace deskflow::gui::messages { namespace deskflow::gui::messages {
enum class NewClientPromptResult
{
Add,
Ignore
};
void messageHandler(QtMsgType type, const QMessageLogContext &context, const QString &msg); void messageHandler(QtMsgType type, const QMessageLogContext &context, const QString &msg);
void raiseCriticalDialog(); void raiseCriticalDialog();
@ -33,8 +27,7 @@ void showCloseReminder(QWidget *parent);
void showClientConnectError(QWidget *parent, deskflow::client::ErrorType error, const QString &address); void showClientConnectError(QWidget *parent, deskflow::client::ErrorType error, const QString &address);
NewClientPromptResult bool showNewClientPrompt(QWidget *parent, const QString &clientName, bool serverRequiresPeerAuth = false);
showNewClientPrompt(QWidget *parent, const QString &clientName, bool serverRequiresPeerAuth = false);
bool showClearSettings(QWidget *parent); bool showClearSettings(QWidget *parent);

View file

@ -19,7 +19,7 @@ namespace deskflow::gui {
// ServerConnection::Deps // ServerConnection::Deps
// //
messages::NewClientPromptResult ServerConnection::Deps::showNewClientPrompt( bool ServerConnection::Deps::showNewClientPrompt(
QWidget *parent, const QString &clientName, bool serverRequiresPeerAuth QWidget *parent, const QString &clientName, bool serverRequiresPeerAuth
) const ) const
{ {
@ -83,8 +83,6 @@ void ServerConnection::handleLogLine(const QString &logLine)
void ServerConnection::handleNewClient(const QString &clientName) void ServerConnection::handleNewClient(const QString &clientName)
{ {
using enum messages::NewClientPromptResult;
if (m_serverConfig.isFull()) { if (m_serverConfig.isFull()) {
qDebug("server config full, skipping new client prompt for: %s", qPrintable(clientName)); qDebug("server config full, skipping new client prompt for: %s", qPrintable(clientName));
return; return;
@ -103,13 +101,11 @@ void ServerConnection::handleNewClient(const QString &clientName)
const auto result = m_pDeps->showNewClientPrompt(m_pParent, clientName, tlsEnabled && requireCerts); const auto result = m_pDeps->showNewClientPrompt(m_pParent, clientName, tlsEnabled && requireCerts);
m_messageShowing = false; m_messageShowing = false;
if (result == Add) { if (result) {
qDebug("accepted dialog, adding client: %s", qPrintable(clientName)); qDebug("accepted dialog, adding client: %s", qPrintable(clientName));
Q_EMIT configureClient(clientName); Q_EMIT configureClient(clientName);
} else if (result == Ignore) {
qDebug("declined dialog, ignoring client: %s", qPrintable(clientName));
} else { } else {
qFatal("unexpected add client result"); qDebug("declined dialog, ignoring client: %s", qPrintable(clientName));
} }
m_connectedClients.insert(clientName); m_connectedClients.insert(clientName);

View file

@ -22,7 +22,7 @@ public:
struct Deps struct Deps
{ {
virtual ~Deps() = default; virtual ~Deps() = default;
virtual messages::NewClientPromptResult virtual bool
showNewClientPrompt(QWidget *parent, const QString &clientName, bool serverRequiresPeerAuth = false) const; showNewClientPrompt(QWidget *parent, const QString &clientName, bool serverRequiresPeerAuth = false) const;
}; };

View file

@ -23,8 +23,8 @@ namespace {
struct DepsMock : public ServerConnection::Deps struct DepsMock : public ServerConnection::Deps
{ {
MOCK_METHOD( MOCK_METHOD(
messages::NewClientPromptResult, showNewClientPrompt, bool, showNewClientPrompt, (QWidget * parent, const QString &clientName, bool previousyAccepted),
(QWidget * parent, const QString &clientName, bool previousyAccepted), (const, override) (const, override)
); );
}; };
@ -50,8 +50,7 @@ TEST_F(ServerConnectionTests, handleLogLine_newClient_shouldShowPrompt)
TEST_F(ServerConnectionTests, handleLogLine_ignoredClient_shouldNotShowPrompt) TEST_F(ServerConnectionTests, handleLogLine_ignoredClient_shouldNotShowPrompt)
{ {
ServerConnection serverConnection(nullptr, m_serverConfig, m_pDeps); ServerConnection serverConnection(nullptr, m_serverConfig, m_pDeps);
ON_CALL(*m_pDeps, showNewClientPrompt(_, _, false)) ON_CALL(*m_pDeps, showNewClientPrompt(_, _, false)).WillByDefault(testing::Return(false));
.WillByDefault(testing::Return(messages::NewClientPromptResult::Ignore));
serverConnection.handleLogLine(R"(unrecognised client name "stub")"); serverConnection.handleLogLine(R"(unrecognised client name "stub")");
EXPECT_CALL(*m_pDeps, showNewClientPrompt(_, _, false)).Times(0); EXPECT_CALL(*m_pDeps, showNewClientPrompt(_, _, false)).Times(0);