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
* SPDX-FileCopyrightText: (C) 2025 Deskflow Developers
* SPDX-FileCopyrightText: (C) 2025 - 2026 Deskflow Developers
* SPDX-License-Identifier: GPL-2.0-only WITH LicenseRef-OpenSSL-Exception
*/
#pragma once
#include <QString>
inline static const auto kSynergyProtocolOption = "synergy";
inline static const auto kBarrierProtocolOption = "barrier";
enum class NetworkProtocol
{
Unknown = -1,
Synergy,
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());
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());
setWin32KeepForeground(settings().value("win32KeepForeground", 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)
{
using enum NetworkProtocol;
outStream << "section: screens" << Qt::endl;
for (const Screen &s : config.screens()) {
@ -261,13 +259,9 @@ QTextStream &operator<<(QTextStream &outStream, const ServerConfig &config)
if (config.hasHeartbeat())
outStream << "\t" << "heartbeat = " << config.heartbeat() << Qt::endl;
if (config.protocol() == Synergy) {
outStream << "\t" << "protocol = synergy" << Qt::endl;
} else if (config.protocol() == Barrier) {
outStream << "\t" << "protocol = barrier" << Qt::endl;
} else {
if (config.protocol() == NetworkProtocol::Unknown)
qFatal("unrecognized protocol when writing config");
}
outStream << "\t" << "protocol = " << networkProtocolToOption(config.protocol()) << Qt::endl;
outStream << "\t"
<< "relativeMouseMoves = " << (config.relativeMouseMoves() ? "true" : "false") << Qt::endl;

View File

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

View File

@ -25,12 +25,6 @@
using namespace deskflow::string;
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
//
@ -1321,15 +1315,10 @@ std::string Config::getOptionValue(OptionID id, OptionValue value)
return result;
}
if (id == kOptionProtocol) {
using enum NetworkProtocol;
const auto enumValue = static_cast<NetworkProtocol>(value);
if (enumValue == Synergy) {
return kSynergyProtocolOption;
} else if (enumValue == Barrier) {
return kBarrierProtocolOption;
} else {
const auto enumValue = networkProtocolFromInt(value);
if (enumValue == NetworkProtocol::Unknown)
throw InvalidProtocolException();
}
return networkProtocolToOption(enumValue).toStdString();
}
return "";
@ -1808,12 +1797,10 @@ OptionValue ConfigReadContext::parseCorner(const std::string &arg) const
OptionValue ConfigReadContext::parseProtocol(const std::string &args) const
{
if (CaselessCmp::equal(args, kSynergyProtocolOption)) {
return static_cast<OptionValue>(NetworkProtocol::Synergy);
} else if (CaselessCmp::equal(args, kBarrierProtocolOption)) {
return static_cast<OptionValue>(NetworkProtocol::Barrier);
}
throw ServerConfigReadException(*this, "invalid protocol argument \"%{1}\"", args);
const auto protoValue = networkProtocolFromString(QString::fromStdString(args));
if (protoValue == NetworkProtocol::Unknown)
throw ServerConfigReadException(*this, "invalid protocol argument \"%{1}\"", args);
return static_cast<OptionValue>(protoValue);
}
OptionValue ConfigReadContext::parseCorners(const std::string &args) const

View File

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