refactor: move elevateMode to Settings

newKey: core/elevateMode <= General/elevateMode + General/elevateModeEnum
Remove elevate mode from appConfig
This commit is contained in:
sithlord48 2025-03-09 00:54:47 -05:00 committed by Nick Bolton
parent 93abf4217b
commit 085a70d5a5
12 changed files with 43 additions and 102 deletions

View file

@ -148,7 +148,7 @@ void SettingsDialog::accept()
Settings::setValue(Settings::Log::Level, ui->comboLogLevel->currentIndex());
Settings::setValue(Settings::Log::ToFile, ui->cbLogToFile->isChecked());
Settings::setValue(Settings::Log::File, ui->lineLogFilename->text());
m_appConfig.setElevateMode(static_cast<ElevateMode>(ui->comboElevate->currentIndex()));
Settings::setValue(Settings::Core::ElevateMode, ui->comboElevate->currentIndex());
Settings::setValue(Settings::Gui::Autohide, ui->cbAutoHide->isChecked());
Settings::setValue(Settings::Gui::AutoUpdateCheck, ui->cbAutoUpdate->isChecked());
Settings::setValue(Settings::Core::PreventSleep, ui->cbPreventSleep->isChecked());
@ -194,8 +194,7 @@ void SettingsDialog::loadFromConfig()
ui->cbLanguageSync->setChecked(Settings::value(Settings::Client::LanguageSync).toBool());
ui->cbScrollDirection->setChecked(Settings::value(Settings::Client::InvertScrollDirection).toBool());
ui->cbCloseToTray->setChecked(Settings::value(Settings::Gui::CloseToTray).toBool());
ui->comboElevate->setCurrentIndex(static_cast<int>(m_appConfig.elevateMode()));
ui->comboElevate->setCurrentIndex(Settings::value(Settings::Core::ElevateMode).toInt());
ui->cbAutoUpdate->setChecked(Settings::value(Settings::Gui::Autohide).toBool());
if (m_appConfig.isActiveScopeSystem()) {

View file

@ -106,6 +106,9 @@ QVariant Settings::defaultValue(const QString &key)
if (key == Server::Binary)
return kServerBinName;
if (key == Core::ElevateMode)
return Settings::ElevateMode::Always;
if (key == Server::ExternalConfigFile)
return QStringLiteral("%1/%2.conf").arg(instance()->settingsPath(), kAppId);

View file

@ -1,6 +1,7 @@
/*
* Deskflow -- mouse and keyboard sharing utility
* SPDX-FileCopyrightText: (C) 2025 Chris Rizzitello <sithlord48@gmail.com>
* SPDX-FileCopyrightText: (C) 2016 - 2025 Symless Ltd.
* SPDX-License-Identifier: GPL-2.0-only WITH LicenseRef-OpenSSL-Exception
*/
@ -38,6 +39,7 @@ public:
};
struct Core
{
inline static const auto ElevateMode = QStringLiteral("core/elevateMode");
inline static const auto Interface = QStringLiteral("core/interface");
inline static const auto LastVersion = QStringLiteral("core/lastVersion");
inline static const auto Port = QStringLiteral("core/port");
@ -91,6 +93,28 @@ public:
};
Q_ENUM(ProcessMode)
/**
* @brief The elevate mode tristate determines two behaviors on Windows.
* The matrix for these two behaviors is as follows:
* | sods | elevate |
* |-----------|------------|
* kAutomatic | true | false |
* kAlways | false | true |
* kNever | false | false |
* The first, --stop-on-desk-switch (sods), is passed through the daemon as a
* command line argument to the server/client, and determines if it restarts
* when switching Windows desktops (e.g. when Windows UAC dialog pops up).
* The second, elevate, is passed as a boolean flag to the daemon over IPC,
* and determines whether the server/client should be started with elevated privileges.
*/
enum ElevateMode
{
Automatic = 0,
Always = 1,
Never = 2
};
Q_ENUM(ElevateMode)
static Settings *instance();
static void setSettingFile(const QString &settingsFile = QString());
static void setValue(const QString &key = QString(), const QVariant &value = QVariant());
@ -134,6 +158,7 @@ private:
, Client::InvertScrollDirection
, Client::LanguageSync
, Client::RemoteHost
, Core::ElevateMode
, Core::Interface
, Core::LastVersion
, Core::Port

View file

@ -36,7 +36,6 @@ add_library(${target} STATIC
config/AppConfig.h
config/ConfigScopes.cpp
config/ConfigScopes.h
config/ElevateMode.h
config/IAppConfig.h
config/IConfigScopes.h
config/IServerConfig.h

View file

@ -39,8 +39,8 @@ const char *const AppConfig::m_SettingsName[] = {
"", // 5 logFilename, moved to deskflow settings
"", // 6 wizardLastRun, obsolete
"", // 7 statedBefore moved to deskflow settings
"elevateMode",
"elevateModeEnum",
"", // 8 elevateMode,
"", // 9 elevateModeEnum,
"", // 10 = edition, obsolete (using serial key instead)
"", // 11 = kTlsEnabled (retain legacy string value) Moved to Settings
"", // 12 AutoHide, moved to Settings
@ -106,8 +106,6 @@ void AppConfig::recallFromCurrentScope()
{
using enum Setting;
recallElevateMode();
m_ServerGroupChecked = getFromCurrentScope(kServerGroupChecked, m_ServerGroupChecked).toBool();
m_UseInternalConfig = getFromCurrentScope(kUseInternalConfig, m_UseInternalConfig).toBool();
m_ClientGroupChecked = getFromCurrentScope(kClientGroupChecked, m_ClientGroupChecked).toBool();
@ -124,8 +122,6 @@ void AppConfig::commit()
saveToAllScopes(kServerGroupChecked, m_ServerGroupChecked);
if (isActiveScopeWritable()) {
setInCurrentScope(kElevateMode, static_cast<int>(m_ElevateMode));
setInCurrentScope(kElevateModeLegacy, m_ElevateMode == ElevateMode::kAlways);
setInCurrentScope(kUseInternalConfig, m_UseInternalConfig);
}
}
@ -159,24 +155,6 @@ void AppConfig::determineScope()
}
}
void AppConfig::recallElevateMode()
{
using enum Setting;
if (!m_Scopes.scopeContains(settingName(kElevateMode))) {
qDebug("elevate mode not set yet, skipping");
return;
}
QVariant elevateMode = getFromCurrentScope(kElevateMode);
if (!elevateMode.isValid()) {
qDebug("elevate mode not valid, loading legacy setting");
elevateMode = getFromCurrentScope(kElevateModeLegacy, QVariant(static_cast<int>(kDefaultElevateMode)));
}
m_ElevateMode = static_cast<ElevateMode>(elevateMode.toInt());
}
QString AppConfig::settingName(Setting name)
{
auto index = static_cast<int>(name);
@ -304,11 +282,6 @@ IConfigScopes &AppConfig::scopes() const
return m_Scopes;
}
ElevateMode AppConfig::elevateMode() const
{
return m_ElevateMode;
}
bool AppConfig::serverGroupChecked() const
{
return m_ServerGroupChecked;
@ -347,11 +320,6 @@ void AppConfig::setClientGroupChecked(bool newValue)
m_ClientGroupChecked = newValue;
}
void AppConfig::setElevateMode(ElevateMode em)
{
m_ElevateMode = em;
}
///////////////////////////////////////////////////////////////////////////////
// End setters
///////////////////////////////////////////////////////////////////////////////

View file

@ -8,7 +8,6 @@
#pragma once
#include "ElevateMode.h"
#include "IAppConfig.h"
#include "IConfigScopes.h"
#include "common/constants.h"
@ -22,12 +21,6 @@
#include <QVariant>
#include <optional>
namespace deskflow::gui {
const ElevateMode kDefaultElevateMode = ElevateMode::kAutomatic;
} // namespace deskflow::gui
/**
* @brief Simply reads and writes app settings.
*
@ -53,8 +46,8 @@ private:
// 5 = logfilename moved to deskflow settings
// 6 = show first run wizard, obsolete
// 7 Started before moved to deskflow settings
kElevateModeLegacy = 8,
kElevateMode = 9,
// kElevateModeLegacy = 8,
// kElevateMode = 9,
// 10 = edition, obsolete (related to obsolete licensing)
// 11 = tlsEnagled moved to Settigns
// 12 = auto hide, Moved to Settings
@ -115,7 +108,6 @@ public:
//
IConfigScopes &scopes() const override;
ElevateMode elevateMode() const override;
bool isActiveScopeWritable() const override;
bool isActiveScopeSystem() const override;
bool clientGroupChecked() const override;
@ -127,12 +119,6 @@ public:
bool serverGroupChecked() const;
bool useInternalConfig() const;
//
// Setters (overrides)
//
void setElevateMode(ElevateMode em) override;
//
// Setters (new methods)
//
@ -153,7 +139,6 @@ private:
void recall();
void recallScreenName();
void recallElevateMode();
void recallFromAllScopes();
void recallFromCurrentScope();
@ -198,7 +183,6 @@ private:
/// @brief Contains the string values of the settings names that will be saved
static const char *const m_SettingsName[];
ElevateMode m_ElevateMode = deskflow::gui::kDefaultElevateMode;
bool m_ServerGroupChecked = false;
bool m_UseInternalConfig = false;
bool m_ClientGroupChecked = false;

View file

@ -1,32 +0,0 @@
/*
* Deskflow -- mouse and keyboard sharing utility
* SPDX-FileCopyrightText: (C) 2016 - 2025 Symless Ltd.
* SPDX-License-Identifier: GPL-2.0-only WITH LicenseRef-OpenSSL-Exception
*/
#pragma once
/**
* @brief The elevate mode tristate determines two behaviors on Windows.
*
* The matrix for these two behaviors is as follows:
*
* | sods | elevate |
* |-----------|------------|
* kAutomatic | true | false |
* kAlways | false | true |
* kNever | false | false |
*
* The first, --stop-on-desk-switch (sods), is passed through the daemon as a
* command line argument to the server/client, and determines if it restarts
* when switching Windows desktops (e.g. when Windows UAC dialog pops up).
*
* The second, elevate, is passed as a boolean flag to the daemon over IPC,
* and determines whether the server/client should be started with elevated privileges.
*/
enum class ElevateMode
{
kAutomatic = 0,
kAlways = 1,
kNever = 2
};

View file

@ -6,8 +6,6 @@
#pragma once
#include "ElevateMode.h"
#include "gui/config/IConfigScopes.h"
#include <QString>
@ -26,7 +24,6 @@ public:
//
virtual IConfigScopes &scopes() const = 0;
virtual ElevateMode elevateMode() const = 0;
virtual bool isActiveScopeSystem() const = 0;
virtual bool isActiveScopeWritable() const = 0;
virtual bool clientGroupChecked() const = 0;
@ -36,7 +33,6 @@ public:
//
virtual void setLoadFromSystemScope(bool loadFromSystemScope) = 0;
virtual void setElevateMode(ElevateMode elevateMode) = 0;
};
} // namespace deskflow::gui

View file

@ -281,7 +281,8 @@ void CoreProcess::startProcessFromDaemon(const QString &app, const QStringList &
qInfo("running command: %s", qPrintable(commandQuoted));
if (!m_daemonIpcClient->sendStartProcess(commandQuoted, m_appConfig.elevateMode())) {
auto elevateMode = Settings::value(Settings::Core::ElevateMode).value<Settings::ElevateMode>();
if (!m_daemonIpcClient->sendStartProcess(commandQuoted, elevateMode)) {
qCritical("cannot start process, ipc command failed");
return;
}
@ -486,7 +487,8 @@ bool CoreProcess::addGenericArgs(QStringList &args, const ProcessMode processMod
// unnecessary restarts when deskflow was started elevated or
// when it is not allowed to elevate. In these cases restarting
// the server is fruitless.
if (m_appConfig.elevateMode() == ElevateMode::kAutomatic) {
auto elevateMode = Settings::value(Settings::Core::ElevateMode).value<Settings::ElevateMode>();
if (elevateMode == Settings::ElevateMode::Automatic) {
args << "--stop-on-desk-switch";
}
#endif

View file

@ -132,12 +132,13 @@ bool DaemonIpcClient::sendLogLevel(const QString &logLevel)
return true;
}
bool DaemonIpcClient::sendStartProcess(const QString &command, ElevateMode elevateMode)
bool DaemonIpcClient::sendStartProcess(const QString &command, Settings::ElevateMode elevateMode)
{
if (!keepAlive())
return false;
if (!sendMessage("elevate=" + (elevateMode == ElevateMode::kAlways ? QStringLiteral("yes") : QStringLiteral("no")))) {
using ElevateMode = Settings::ElevateMode;
if (!sendMessage("elevate=" + (elevateMode == ElevateMode::Always ? QStringLiteral("yes") : QStringLiteral("no")))) {
return false;
}

View file

@ -8,7 +8,7 @@
#include <QObject>
#include "gui/config/ElevateMode.h"
#include "common/Settings.h"
class QLocalSocket;
@ -22,7 +22,7 @@ public:
explicit DaemonIpcClient(QObject *parent = nullptr);
bool connectToServer();
bool sendLogLevel(const QString &logLevel);
bool sendStartProcess(const QString &command, ElevateMode elevateMode);
bool sendStartProcess(const QString &command, Settings::ElevateMode elevateMode);
bool sendStopProcess();
bool sendClearSettings();
QString requestLogPath();

View file

@ -8,8 +8,6 @@
#include "gui/config/IAppConfig.h"
#include "gui/config/ElevateMode.h"
#include <gmock/gmock.h>
class AppConfigMock : public deskflow::gui::IAppConfig
@ -25,7 +23,6 @@ public:
//
MOCK_METHOD(deskflow::gui::IConfigScopes &, scopes, (), (const, override));
MOCK_METHOD(ElevateMode, elevateMode, (), (const, override));
MOCK_METHOD(bool, isActiveScopeSystem, (), (const, override));
MOCK_METHOD(bool, isActiveScopeWritable, (), (const, override));
MOCK_METHOD(bool, clientGroupChecked, (), (const, override));
@ -35,7 +32,6 @@ public:
//
MOCK_METHOD(void, setLoadFromSystemScope, (bool loadFromSystemScope), (override));
MOCK_METHOD(void, setElevateMode, (ElevateMode elevateMode), (override));
private:
const QString m_stub = "stub";