2012-06-10 16:50:54 +00:00
|
|
|
/*
|
2024-09-17 19:00:25 +00:00
|
|
|
* Deskflow -- mouse and keyboard sharing utility
|
2026-06-09 02:03:09 +00:00
|
|
|
* SPDX-FileCopyrightText: (C) 2025 - 2026 Chris Rizzitello <sithlord48@gmail.com>
|
2026-06-08 20:37:23 +00:00
|
|
|
* SPDX-FileCopyrightText: (C) 2012 Synergy App Ltd
|
2025-01-24 00:11:17 +00:00
|
|
|
* SPDX-FileCopyrightText: (C) 2008 Volker Lanz <vl@fidra.de>
|
|
|
|
|
* SPDX-License-Identifier: GPL-2.0-only WITH LicenseRef-OpenSSL-Exception
|
2012-06-10 16:50:54 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#include "ServerConfig.h"
|
2024-07-22 16:48:02 +00:00
|
|
|
|
2012-06-10 16:50:54 +00:00
|
|
|
#include "Hotkey.h"
|
2025-03-08 15:59:53 +00:00
|
|
|
#include "common/Settings.h"
|
2012-06-10 16:50:54 +00:00
|
|
|
|
2014-08-26 14:49:27 +00:00
|
|
|
#include <QAbstractButton>
|
|
|
|
|
#include <QPushButton>
|
2012-06-10 16:50:54 +00:00
|
|
|
|
2024-08-08 17:41:59 +00:00
|
|
|
using enum ScreenConfig::Modifier;
|
|
|
|
|
using enum ScreenConfig::SwitchCorner;
|
|
|
|
|
using enum ScreenConfig::Fix;
|
|
|
|
|
|
2024-10-17 18:04:35 +00:00
|
|
|
static const struct
|
|
|
|
|
{
|
2024-07-02 19:07:06 +00:00
|
|
|
int x;
|
|
|
|
|
int y;
|
|
|
|
|
const char *name;
|
|
|
|
|
} neighbourDirs[] = {
|
|
|
|
|
{1, 0, "right"},
|
|
|
|
|
{-1, 0, "left"},
|
|
|
|
|
{0, -1, "up"},
|
|
|
|
|
{0, 1, "down"},
|
2014-08-21 17:32:40 +00:00
|
|
|
|
2012-06-10 16:50:54 +00:00
|
|
|
};
|
|
|
|
|
|
2014-08-21 17:32:40 +00:00
|
|
|
const int serverDefaultIndex = 7;
|
2012-06-10 16:50:54 +00:00
|
|
|
|
2026-06-08 23:48:04 +00:00
|
|
|
ServerConfig::ServerConfig(int columns, int rows) : m_Screens(columns), m_columns(columns), m_rows(rows)
|
2024-10-17 18:04:35 +00:00
|
|
|
{
|
2024-08-03 00:17:29 +00:00
|
|
|
recall();
|
2012-06-10 16:50:54 +00:00
|
|
|
}
|
|
|
|
|
|
2024-10-17 18:04:35 +00:00
|
|
|
bool ServerConfig::save(const QString &fileName) const
|
|
|
|
|
{
|
2024-07-02 19:07:06 +00:00
|
|
|
QFile file(fileName);
|
|
|
|
|
if (!file.open(QIODevice::WriteOnly | QIODevice::Text))
|
|
|
|
|
return false;
|
2012-06-10 16:50:54 +00:00
|
|
|
|
2024-07-02 19:07:06 +00:00
|
|
|
save(file);
|
|
|
|
|
file.close();
|
2012-06-10 16:50:54 +00:00
|
|
|
|
2024-07-02 19:07:06 +00:00
|
|
|
return true;
|
2012-06-10 16:50:54 +00:00
|
|
|
}
|
|
|
|
|
|
2024-10-17 18:04:35 +00:00
|
|
|
bool ServerConfig::operator==(const ServerConfig &sc) const
|
|
|
|
|
{
|
2026-06-19 21:04:57 +00:00
|
|
|
return m_Screens == sc.m_Screens && //
|
|
|
|
|
m_Hotkeys == sc.m_Hotkeys; //
|
2021-08-20 12:22:07 +00:00
|
|
|
}
|
|
|
|
|
|
2024-10-17 18:04:35 +00:00
|
|
|
void ServerConfig::save(QFile &file) const
|
|
|
|
|
{
|
2024-07-02 19:07:06 +00:00
|
|
|
QTextStream outStream(&file);
|
|
|
|
|
outStream << *this;
|
2012-06-10 16:50:54 +00:00
|
|
|
}
|
|
|
|
|
|
2024-10-17 18:04:35 +00:00
|
|
|
void ServerConfig::setupScreens()
|
|
|
|
|
{
|
2024-07-02 19:07:06 +00:00
|
|
|
screens().clear();
|
|
|
|
|
hotkeys().clear();
|
2012-06-10 16:50:54 +00:00
|
|
|
|
2024-07-02 19:07:06 +00:00
|
|
|
// There must always be screen objects for each cell in the screens QList.
|
|
|
|
|
// Unused screens are identified by having an empty name.
|
2026-06-08 23:48:04 +00:00
|
|
|
for (int i = 0; i < m_columns * m_rows; i++)
|
2024-07-02 19:07:06 +00:00
|
|
|
addScreen(Screen());
|
2012-06-10 16:50:54 +00:00
|
|
|
}
|
|
|
|
|
|
2024-10-17 18:04:35 +00:00
|
|
|
void ServerConfig::commit()
|
|
|
|
|
{
|
2024-08-03 00:17:29 +00:00
|
|
|
qDebug("committing server config");
|
|
|
|
|
|
2024-07-02 19:07:06 +00:00
|
|
|
settings().beginGroup("internalConfig");
|
|
|
|
|
settings().remove("");
|
|
|
|
|
|
|
|
|
|
settings().beginWriteArray("screens");
|
|
|
|
|
for (int i = 0; i < screens().size(); i++) {
|
|
|
|
|
settings().setArrayIndex(i);
|
2025-05-23 19:43:13 +00:00
|
|
|
const auto &screen = screens()[i];
|
2024-07-02 19:07:06 +00:00
|
|
|
screen.saveSettings(settings());
|
2026-02-01 15:44:38 +00:00
|
|
|
auto screenName = Settings::value(Settings::Core::ComputerName).toString();
|
2025-03-09 03:09:38 +00:00
|
|
|
if (screen.isServer() && screenName != screen.name()) {
|
2026-02-01 15:44:38 +00:00
|
|
|
Settings::setValue(Settings::Core::ComputerName, screen.name());
|
2016-12-28 11:50:32 +00:00
|
|
|
}
|
2024-07-02 19:07:06 +00:00
|
|
|
}
|
|
|
|
|
settings().endArray();
|
2016-12-28 11:50:32 +00:00
|
|
|
|
2024-07-02 19:07:06 +00:00
|
|
|
settings().beginWriteArray("hotkeys");
|
|
|
|
|
for (int i = 0; i < hotkeys().size(); i++) {
|
|
|
|
|
settings().setArrayIndex(i);
|
2024-08-08 17:41:59 +00:00
|
|
|
hotkeys()[i].saveSettings(settings().get());
|
2024-07-02 19:07:06 +00:00
|
|
|
}
|
|
|
|
|
settings().endArray();
|
2016-12-28 11:50:32 +00:00
|
|
|
|
2024-07-02 19:07:06 +00:00
|
|
|
settings().endGroup();
|
2012-06-10 16:50:54 +00:00
|
|
|
}
|
|
|
|
|
|
2024-10-17 18:04:35 +00:00
|
|
|
void ServerConfig::recall()
|
|
|
|
|
{
|
2024-08-03 00:17:29 +00:00
|
|
|
qDebug("recalling server config");
|
|
|
|
|
|
2024-07-02 19:07:06 +00:00
|
|
|
settings().beginGroup("internalConfig");
|
|
|
|
|
|
2026-06-08 23:48:04 +00:00
|
|
|
m_columns = Settings::value(Settings::Server::GridWidth).toInt();
|
|
|
|
|
m_rows = Settings::value(Settings::Server::GridHeight).toInt();
|
2024-07-02 19:07:06 +00:00
|
|
|
|
|
|
|
|
// we need to know the number of columns and rows before we can set up
|
|
|
|
|
// ourselves
|
2024-08-03 00:17:29 +00:00
|
|
|
setupScreens();
|
2024-07-02 19:07:06 +00:00
|
|
|
|
|
|
|
|
int numScreens = settings().beginReadArray("screens");
|
|
|
|
|
Q_ASSERT(numScreens <= screens().size());
|
|
|
|
|
for (int i = 0; i < numScreens; i++) {
|
|
|
|
|
settings().setArrayIndex(i);
|
|
|
|
|
screens()[i].loadSettings(settings());
|
|
|
|
|
if (getServerName() == screens()[i].name()) {
|
|
|
|
|
screens()[i].markAsServer();
|
2016-12-28 11:50:32 +00:00
|
|
|
}
|
2024-07-02 19:07:06 +00:00
|
|
|
}
|
|
|
|
|
settings().endArray();
|
|
|
|
|
|
|
|
|
|
int numHotkeys = settings().beginReadArray("hotkeys");
|
|
|
|
|
for (int i = 0; i < numHotkeys; i++) {
|
|
|
|
|
settings().setArrayIndex(i);
|
|
|
|
|
Hotkey h;
|
2024-08-08 17:41:59 +00:00
|
|
|
h.loadSettings(settings().get());
|
2024-07-02 19:07:06 +00:00
|
|
|
hotkeys().append(h);
|
|
|
|
|
}
|
|
|
|
|
settings().endArray();
|
|
|
|
|
|
|
|
|
|
settings().endGroup();
|
2012-06-10 16:50:54 +00:00
|
|
|
}
|
|
|
|
|
|
2024-10-17 18:04:35 +00:00
|
|
|
int ServerConfig::adjacentScreenIndex(int idx, int deltaColumn, int deltaRow) const
|
|
|
|
|
{
|
2024-07-02 19:07:06 +00:00
|
|
|
if (screens()[idx].isNull())
|
|
|
|
|
return -1;
|
2012-06-10 16:50:54 +00:00
|
|
|
|
2024-07-02 19:07:06 +00:00
|
|
|
// if we're at the left or right end of the table, don't find results going
|
|
|
|
|
// further left or right
|
2026-06-08 23:48:04 +00:00
|
|
|
if ((deltaColumn > 0 && (idx + 1) % m_columns == 0) || (deltaColumn < 0 && idx % m_columns == 0))
|
2024-07-02 19:07:06 +00:00
|
|
|
return -1;
|
2012-06-10 16:50:54 +00:00
|
|
|
|
2026-06-08 23:48:04 +00:00
|
|
|
int arrayPos = idx + deltaColumn + deltaRow * m_columns;
|
2012-06-10 16:50:54 +00:00
|
|
|
|
2024-07-02 19:07:06 +00:00
|
|
|
if (arrayPos >= screens().size() || arrayPos < 0)
|
|
|
|
|
return -1;
|
2012-06-10 16:50:54 +00:00
|
|
|
|
2024-07-02 19:07:06 +00:00
|
|
|
return arrayPos;
|
2012-06-10 16:50:54 +00:00
|
|
|
}
|
|
|
|
|
|
2024-10-17 18:04:35 +00:00
|
|
|
QTextStream &operator<<(QTextStream &outStream, const ServerConfig &config)
|
|
|
|
|
{
|
2024-07-08 14:37:51 +00:00
|
|
|
outStream << "section: screens" << Qt::endl;
|
2012-06-10 16:50:54 +00:00
|
|
|
|
2024-10-21 02:58:12 +00:00
|
|
|
for (const Screen &s : config.screens()) {
|
2024-07-02 19:07:06 +00:00
|
|
|
if (!s.isNull())
|
2025-01-30 01:49:13 +00:00
|
|
|
outStream << s.screensSection();
|
2024-10-21 02:58:12 +00:00
|
|
|
}
|
2012-06-10 16:50:54 +00:00
|
|
|
|
2024-07-08 14:37:51 +00:00
|
|
|
outStream << "end" << Qt::endl << Qt::endl;
|
2012-06-10 16:50:54 +00:00
|
|
|
|
2024-07-08 14:37:51 +00:00
|
|
|
outStream << "section: links" << Qt::endl;
|
2012-06-10 16:50:54 +00:00
|
|
|
|
2025-08-04 14:16:28 +00:00
|
|
|
for (int i = 0; const auto &screen : config.screens()) {
|
2025-08-04 14:21:54 +00:00
|
|
|
if (!screen.isNull()) {
|
|
|
|
|
outStream << "\t" << screen.name() << ":\n";
|
|
|
|
|
for (const auto &neighbour : std::as_const(neighbourDirs)) {
|
|
|
|
|
int idx = config.adjacentScreenIndex(i, neighbour.x, neighbour.y);
|
|
|
|
|
if (idx != -1 && !config.screens()[idx].isNull())
|
|
|
|
|
outStream << "\t\t" << neighbour.name << " = " << config.screens()[idx].name() << Qt::endl;
|
|
|
|
|
}
|
2025-08-04 14:16:28 +00:00
|
|
|
}
|
|
|
|
|
i++;
|
2024-10-21 02:58:12 +00:00
|
|
|
}
|
2022-09-22 08:12:13 +00:00
|
|
|
|
2024-07-08 14:37:51 +00:00
|
|
|
outStream << "end" << Qt::endl << Qt::endl;
|
2024-07-02 19:07:06 +00:00
|
|
|
|
2024-07-08 14:37:51 +00:00
|
|
|
outStream << "section: options" << Qt::endl;
|
2024-07-02 19:07:06 +00:00
|
|
|
|
2024-10-21 02:58:12 +00:00
|
|
|
for (const Hotkey &hotkey : config.hotkeys())
|
2024-07-02 19:07:06 +00:00
|
|
|
outStream << hotkey;
|
|
|
|
|
|
2024-07-08 14:37:51 +00:00
|
|
|
outStream << "end" << Qt::endl << Qt::endl;
|
2024-07-02 19:07:06 +00:00
|
|
|
|
|
|
|
|
return outStream;
|
2012-06-10 16:50:54 +00:00
|
|
|
}
|
|
|
|
|
|
2024-10-17 18:04:35 +00:00
|
|
|
int ServerConfig::numScreens() const
|
|
|
|
|
{
|
2024-07-02 19:07:06 +00:00
|
|
|
int rval = 0;
|
2012-06-10 16:50:54 +00:00
|
|
|
|
2024-10-21 02:58:12 +00:00
|
|
|
for (const Screen &s : screens()) {
|
2024-07-02 19:07:06 +00:00
|
|
|
if (!s.isNull())
|
|
|
|
|
rval++;
|
2024-10-21 02:58:12 +00:00
|
|
|
}
|
2012-06-10 16:50:54 +00:00
|
|
|
|
2024-07-02 19:07:06 +00:00
|
|
|
return rval;
|
2012-06-10 16:50:54 +00:00
|
|
|
}
|
2014-08-21 17:32:40 +00:00
|
|
|
|
2025-08-07 02:59:32 +00:00
|
|
|
QString ServerConfig::getServerName() const
|
2024-10-17 18:04:35 +00:00
|
|
|
{
|
2026-02-01 15:44:38 +00:00
|
|
|
return Settings::value(Settings::Core::ComputerName).toString();
|
2021-02-22 11:33:18 +00:00
|
|
|
}
|
|
|
|
|
|
2024-10-17 18:04:35 +00:00
|
|
|
void ServerConfig::updateServerName()
|
|
|
|
|
{
|
2024-07-02 19:07:06 +00:00
|
|
|
for (auto &screen : screens()) {
|
|
|
|
|
if (screen.isServer()) {
|
2026-02-01 15:44:38 +00:00
|
|
|
screen.setName(Settings::value(Settings::Core::ComputerName).toString());
|
2024-07-02 19:07:06 +00:00
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
2021-02-22 11:33:18 +00:00
|
|
|
}
|
|
|
|
|
|
2025-08-07 02:59:32 +00:00
|
|
|
QString ServerConfig::configFile() const
|
2024-10-17 18:04:35 +00:00
|
|
|
{
|
2025-03-08 16:37:04 +00:00
|
|
|
return Settings::value(Settings::Server::ExternalConfigFile).toString();
|
2021-04-12 09:03:37 +00:00
|
|
|
}
|
|
|
|
|
|
2024-10-17 18:04:35 +00:00
|
|
|
bool ServerConfig::useExternalConfig() const
|
|
|
|
|
{
|
2025-03-08 15:59:53 +00:00
|
|
|
return Settings::value(Settings::Server::ExternalConfig).toBool();
|
2021-04-12 09:03:37 +00:00
|
|
|
}
|
|
|
|
|
|
2024-10-17 18:04:35 +00:00
|
|
|
bool ServerConfig::isFull() const
|
|
|
|
|
{
|
2024-07-02 19:07:06 +00:00
|
|
|
bool isFull = true;
|
|
|
|
|
|
|
|
|
|
for (const auto &screen : screens()) {
|
|
|
|
|
if (screen.isNull()) {
|
|
|
|
|
isFull = false;
|
|
|
|
|
break;
|
2021-04-22 07:29:46 +00:00
|
|
|
}
|
2024-07-02 19:07:06 +00:00
|
|
|
}
|
2021-04-22 07:29:46 +00:00
|
|
|
|
2024-07-02 19:07:06 +00:00
|
|
|
return isFull;
|
2021-04-22 07:29:46 +00:00
|
|
|
}
|
|
|
|
|
|
2024-10-17 18:04:35 +00:00
|
|
|
bool ServerConfig::screenExists(const QString &screenName) const
|
|
|
|
|
{
|
2024-07-02 19:07:06 +00:00
|
|
|
bool isExists = false;
|
|
|
|
|
|
|
|
|
|
for (const auto &screen : screens()) {
|
|
|
|
|
if (!screen.isNull() && screen.name() == screenName) {
|
|
|
|
|
isExists = true;
|
|
|
|
|
break;
|
2021-04-28 12:49:27 +00:00
|
|
|
}
|
2024-07-02 19:07:06 +00:00
|
|
|
}
|
2021-04-28 12:49:27 +00:00
|
|
|
|
2024-07-02 19:07:06 +00:00
|
|
|
return isExists;
|
2021-04-28 12:49:27 +00:00
|
|
|
}
|
|
|
|
|
|
2024-10-17 18:04:35 +00:00
|
|
|
void ServerConfig::addClient(const QString &clientName)
|
|
|
|
|
{
|
2024-07-02 19:07:06 +00:00
|
|
|
int serverIndex = -1;
|
2026-02-01 15:44:38 +00:00
|
|
|
const auto screenName = Settings::value(Settings::Core::ComputerName).toString();
|
2021-06-09 08:23:09 +00:00
|
|
|
|
2025-03-09 03:09:38 +00:00
|
|
|
if (findScreenName(screenName, serverIndex)) {
|
2024-07-02 19:07:06 +00:00
|
|
|
m_Screens[serverIndex].markAsServer();
|
|
|
|
|
} else {
|
2025-03-09 03:09:38 +00:00
|
|
|
fixNoServer(screenName, serverIndex);
|
2024-07-02 19:07:06 +00:00
|
|
|
}
|
2021-06-09 08:23:09 +00:00
|
|
|
|
2024-08-08 17:41:59 +00:00
|
|
|
m_Screens.addScreenByPriority(Screen(clientName));
|
2021-05-18 10:05:59 +00:00
|
|
|
}
|
|
|
|
|
|
2025-05-20 02:19:19 +00:00
|
|
|
void ServerConfig::setConfigFile(const QString &configFile) const
|
2024-10-17 18:04:35 +00:00
|
|
|
{
|
2025-03-08 16:37:04 +00:00
|
|
|
Settings::setValue(Settings::Server::ExternalConfigFile, configFile);
|
2021-04-12 09:03:37 +00:00
|
|
|
}
|
|
|
|
|
|
2025-05-20 02:19:19 +00:00
|
|
|
void ServerConfig::setUseExternalConfig(bool useExternalConfig) const
|
2024-10-17 18:04:35 +00:00
|
|
|
{
|
2025-03-08 15:59:53 +00:00
|
|
|
Settings::setValue(Settings::Server::ExternalConfig, useExternalConfig);
|
2021-04-12 09:03:37 +00:00
|
|
|
}
|
|
|
|
|
|
2024-10-17 18:04:35 +00:00
|
|
|
bool ServerConfig::findScreenName(const QString &name, int &index)
|
|
|
|
|
{
|
2024-07-02 19:07:06 +00:00
|
|
|
bool found = false;
|
|
|
|
|
for (int i = 0; i < screens().size(); i++) {
|
|
|
|
|
if (!screens()[i].isNull() && screens()[i].name().compare(name) == 0) {
|
|
|
|
|
index = i;
|
|
|
|
|
found = true;
|
|
|
|
|
break;
|
2016-12-28 11:50:32 +00:00
|
|
|
}
|
2024-07-02 19:07:06 +00:00
|
|
|
}
|
|
|
|
|
return found;
|
2014-08-21 17:32:40 +00:00
|
|
|
}
|
|
|
|
|
|
2024-10-17 18:04:35 +00:00
|
|
|
bool ServerConfig::fixNoServer(const QString &name, int &index)
|
|
|
|
|
{
|
2024-07-02 19:07:06 +00:00
|
|
|
bool fixed = false;
|
|
|
|
|
if (screens()[serverDefaultIndex].isNull()) {
|
|
|
|
|
m_Screens[serverDefaultIndex].setName(name);
|
|
|
|
|
m_Screens[serverDefaultIndex].markAsServer();
|
|
|
|
|
index = serverDefaultIndex;
|
|
|
|
|
fixed = true;
|
|
|
|
|
}
|
2016-12-28 11:50:32 +00:00
|
|
|
|
2024-07-02 19:07:06 +00:00
|
|
|
return fixed;
|
2014-08-21 17:32:40 +00:00
|
|
|
}
|
2014-08-26 14:49:27 +00:00
|
|
|
|
2024-10-17 18:04:35 +00:00
|
|
|
QSettingsProxy &ServerConfig::settings()
|
|
|
|
|
{
|
2025-03-15 02:35:40 +00:00
|
|
|
return Settings::proxy();
|
2020-02-25 16:32:46 +00:00
|
|
|
}
|