refactor: networkprotocol, add string / enum conversion methods use them in config and elsewhere

This commit is contained in:
sithlord48 2026-01-22 23:29:22 -05:00 committed by Nick Bolton
parent 0d3768d838
commit dbed37e9ba
5 changed files with 66 additions and 46 deletions

View file

@ -1,13 +1,62 @@
/* /*
* Deskflow -- mouse and keyboard sharing utility * Deskflow -- mouse and keyboard sharing utility
* SPDX-FileCopyrightText: (C) 2025 Deskflow Developers * SPDX-FileCopyrightText: (C) 2025 - 2026 Deskflow Developers
* SPDX-License-Identifier: GPL-2.0-only WITH LicenseRef-OpenSSL-Exception * SPDX-License-Identifier: GPL-2.0-only WITH LicenseRef-OpenSSL-Exception
*/ */
#pragma once #pragma once
#include <QString>
inline static const auto kSynergyProtocolOption = "synergy";
inline static const auto kBarrierProtocolOption = "barrier";
enum class NetworkProtocol enum class NetworkProtocol
{ {
Unknown = -1,
Synergy, Synergy,
Barrier Barrier
}; };
static int networkProtocolToInt(const NetworkProtocol proto)
{
return static_cast<int>(proto);
}
static NetworkProtocol networkProtocolFromInt(const int value)
{
return static_cast<NetworkProtocol>(value);
}
static QString networkProtocolToOption(const NetworkProtocol proto)
{
switch (proto) {
case NetworkProtocol::Synergy:
return kSynergyProtocolOption;
case NetworkProtocol::Barrier:
return kBarrierProtocolOption;
default:
return {};
}
}
static QString networkProtocolToName(const NetworkProtocol proto)
{
switch (proto) {
case NetworkProtocol::Synergy:
return QStringLiteral("Synergy");
case NetworkProtocol::Barrier:
return QStringLiteral("Barrier");
default:
return {};
}
}
static NetworkProtocol networkProtocolFromString(const QString &proto)
{
using enum NetworkProtocol;
if (proto.compare(kBarrierProtocolOption, Qt::CaseInsensitive) == 0)
return Barrier;
if (proto.compare(kSynergyProtocolOption, Qt::CaseInsensitive) == 0)
return Synergy;
return Unknown;
}

View file

@ -160,7 +160,7 @@ void ServerConfig::recall()
haveHeartbeat(settings().value("hasHeartbeat", false).toBool()); haveHeartbeat(settings().value("hasHeartbeat", false).toBool());
setHeartbeat(settings().value("heartbeat", 5000).toInt()); setHeartbeat(settings().value("heartbeat", 5000).toInt());
setProtocol(static_cast<NetworkProtocol>(settings().value("protocol", static_cast<int>(protocol())).toInt())); setProtocol(networkProtocolFromInt(settings().value("protocol", networkProtocolToInt(protocol())).toInt()));
setRelativeMouseMoves(settings().value("relativeMouseMoves", false).toBool()); setRelativeMouseMoves(settings().value("relativeMouseMoves", false).toBool());
setWin32KeepForeground(settings().value("win32KeepForeground", false).toBool()); setWin32KeepForeground(settings().value("win32KeepForeground", false).toBool());
haveSwitchDelay(settings().value("hasSwitchDelay", false).toBool()); haveSwitchDelay(settings().value("hasSwitchDelay", false).toBool());
@ -220,8 +220,6 @@ int ServerConfig::adjacentScreenIndex(int idx, int deltaColumn, int deltaRow) co
QTextStream &operator<<(QTextStream &outStream, const ServerConfig &config) QTextStream &operator<<(QTextStream &outStream, const ServerConfig &config)
{ {
using enum NetworkProtocol;
outStream << "section: screens" << Qt::endl; outStream << "section: screens" << Qt::endl;
for (const Screen &s : config.screens()) { for (const Screen &s : config.screens()) {
@ -261,13 +259,9 @@ QTextStream &operator<<(QTextStream &outStream, const ServerConfig &config)
if (config.hasHeartbeat()) if (config.hasHeartbeat())
outStream << "\t" << "heartbeat = " << config.heartbeat() << Qt::endl; outStream << "\t" << "heartbeat = " << config.heartbeat() << Qt::endl;
if (config.protocol() == Synergy) { if (config.protocol() == NetworkProtocol::Unknown)
outStream << "\t" << "protocol = synergy" << Qt::endl;
} else if (config.protocol() == Barrier) {
outStream << "\t" << "protocol = barrier" << Qt::endl;
} else {
qFatal("unrecognized protocol when writing config"); qFatal("unrecognized protocol when writing config");
} outStream << "\t" << "protocol = " << networkProtocolToOption(config.protocol()) << Qt::endl;
outStream << "\t" outStream << "\t"
<< "relativeMouseMoves = " << (config.relativeMouseMoves() ? "true" : "false") << Qt::endl; << "relativeMouseMoves = " << (config.relativeMouseMoves() ? "true" : "false") << Qt::endl;

View file

@ -20,7 +20,6 @@
#include <QMessageBox> #include <QMessageBox>
using enum ScreenConfig::SwitchCorner; using enum ScreenConfig::SwitchCorner;
using enum NetworkProtocol;
ServerConfigDialog::ServerConfigDialog(QWidget *parent, ServerConfig &config) ServerConfigDialog::ServerConfigDialog(QWidget *parent, ServerConfig &config)
: QDialog(parent, Qt::WindowTitleHint | Qt::WindowSystemMenuHint), : QDialog(parent, Qt::WindowTitleHint | Qt::WindowSystemMenuHint),

View file

@ -25,12 +25,6 @@
using namespace deskflow::string; using namespace deskflow::string;
namespace deskflow::server { namespace deskflow::server {
// Protocol options used in configuration files (lowercase).
// Note that @ref kSynergyProtocolName / @ref kBarrierProtocolName use capitalized names.
const auto kSynergyProtocolOption = "synergy";
const auto kBarrierProtocolOption = "barrier";
// //
// Config // Config
// //
@ -1321,15 +1315,10 @@ std::string Config::getOptionValue(OptionID id, OptionValue value)
return result; return result;
} }
if (id == kOptionProtocol) { if (id == kOptionProtocol) {
using enum NetworkProtocol; const auto enumValue = networkProtocolFromInt(value);
const auto enumValue = static_cast<NetworkProtocol>(value); if (enumValue == NetworkProtocol::Unknown)
if (enumValue == Synergy) {
return kSynergyProtocolOption;
} else if (enumValue == Barrier) {
return kBarrierProtocolOption;
} else {
throw InvalidProtocolException(); throw InvalidProtocolException();
} return networkProtocolToOption(enumValue).toStdString();
} }
return ""; return "";
@ -1808,12 +1797,10 @@ OptionValue ConfigReadContext::parseCorner(const std::string &arg) const
OptionValue ConfigReadContext::parseProtocol(const std::string &args) const OptionValue ConfigReadContext::parseProtocol(const std::string &args) const
{ {
if (CaselessCmp::equal(args, kSynergyProtocolOption)) { const auto protoValue = networkProtocolFromString(QString::fromStdString(args));
return static_cast<OptionValue>(NetworkProtocol::Synergy); if (protoValue == NetworkProtocol::Unknown)
} else if (CaselessCmp::equal(args, kBarrierProtocolOption)) { throw ServerConfigReadException(*this, "invalid protocol argument \"%{1}\"", args);
return static_cast<OptionValue>(NetworkProtocol::Barrier); return static_cast<OptionValue>(protoValue);
}
throw ServerConfigReadException(*this, "invalid protocol argument \"%{1}\"", args);
} }
OptionValue ConfigReadContext::parseCorners(const std::string &args) const OptionValue ConfigReadContext::parseCorners(const std::string &args) const

View file

@ -273,13 +273,9 @@ void Server::disconnect()
std::string Server::protocolString() const std::string Server::protocolString() const
{ {
using enum NetworkProtocol; if (m_protocol == NetworkProtocol::Unknown)
if (m_protocol == Synergy) { throw InvalidProtocolException();
return kSynergyProtocolName; return networkProtocolToName(m_protocol).toStdString();
} else if (m_protocol == Barrier) {
return kBarrierProtocolName;
}
throw InvalidProtocolException();
} }
uint32_t Server::getNumClients() const uint32_t Server::getNumClients() const
@ -1074,15 +1070,10 @@ void Server::processOptions()
const OptionID id = optionId; const OptionID id = optionId;
const OptionValue value = optionValue; const OptionValue value = optionValue;
if (id == kOptionProtocol) { if (id == kOptionProtocol) {
using enum NetworkProtocol; const auto enumValue = networkProtocolFromInt(value);
const auto enumValue = static_cast<NetworkProtocol>(value); if (enumValue == NetworkProtocol::Unknown)
if (enumValue == Synergy) {
m_protocol = Synergy;
} else if (enumValue == Barrier) {
m_protocol = Barrier;
} else {
throw InvalidProtocolException(); throw InvalidProtocolException();
} m_protocol = enumValue;
} else if (id == kOptionScreenSwitchDelay) { } else if (id == kOptionScreenSwitchDelay) {
m_switchWaitDelay = 1.0e-3 * static_cast<double>(value); m_switchWaitDelay = 1.0e-3 * static_cast<double>(value);
if (m_switchWaitDelay < 0.0) { if (m_switchWaitDelay < 0.0) {