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();
}
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)

View File

@ -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);

View File

@ -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);

View File

@ -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;
};

View File

@ -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);