From 085a70d5a5a4a15a09e3896a23735662a7f6655c Mon Sep 17 00:00:00 2001 From: sithlord48 Date: Sun, 9 Mar 2025 00:54:47 -0500 Subject: [PATCH] refactor: move elevateMode to Settings newKey: core/elevateMode <= General/elevateMode + General/elevateModeEnum Remove elevate mode from appConfig --- .../deskflow-gui/dialogs/SettingsDialog.cpp | 5 ++- src/lib/common/Settings.cpp | 3 ++ src/lib/common/Settings.h | 25 +++++++++++++ src/lib/gui/CMakeLists.txt | 1 - src/lib/gui/config/AppConfig.cpp | 36 ++----------------- src/lib/gui/config/AppConfig.h | 20 ++--------- src/lib/gui/config/ElevateMode.h | 32 ----------------- src/lib/gui/config/IAppConfig.h | 4 --- src/lib/gui/core/CoreProcess.cpp | 6 ++-- src/lib/gui/ipc/DaemonIpcClient.cpp | 5 +-- src/lib/gui/ipc/DaemonIpcClient.h | 4 +-- src/test/shared/gui/mocks/AppConfigMock.h | 4 --- 12 files changed, 43 insertions(+), 102 deletions(-) delete mode 100644 src/lib/gui/config/ElevateMode.h diff --git a/src/apps/deskflow-gui/dialogs/SettingsDialog.cpp b/src/apps/deskflow-gui/dialogs/SettingsDialog.cpp index 45ca8251e..53f6c7e43 100644 --- a/src/apps/deskflow-gui/dialogs/SettingsDialog.cpp +++ b/src/apps/deskflow-gui/dialogs/SettingsDialog.cpp @@ -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(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(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()) { diff --git a/src/lib/common/Settings.cpp b/src/lib/common/Settings.cpp index db93400a4..08d27afbf 100644 --- a/src/lib/common/Settings.cpp +++ b/src/lib/common/Settings.cpp @@ -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); diff --git a/src/lib/common/Settings.h b/src/lib/common/Settings.h index e860d6b67..3969c5e6a 100644 --- a/src/lib/common/Settings.h +++ b/src/lib/common/Settings.h @@ -1,6 +1,7 @@ /* * Deskflow -- mouse and keyboard sharing utility * SPDX-FileCopyrightText: (C) 2025 Chris Rizzitello + * 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 diff --git a/src/lib/gui/CMakeLists.txt b/src/lib/gui/CMakeLists.txt index edd5b4feb..03548d89b 100644 --- a/src/lib/gui/CMakeLists.txt +++ b/src/lib/gui/CMakeLists.txt @@ -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 diff --git a/src/lib/gui/config/AppConfig.cpp b/src/lib/gui/config/AppConfig.cpp index 63caa8ef8..5f51b40a7 100644 --- a/src/lib/gui/config/AppConfig.cpp +++ b/src/lib/gui/config/AppConfig.cpp @@ -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(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(kDefaultElevateMode))); - } - - m_ElevateMode = static_cast(elevateMode.toInt()); -} - QString AppConfig::settingName(Setting name) { auto index = static_cast(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 /////////////////////////////////////////////////////////////////////////////// diff --git a/src/lib/gui/config/AppConfig.h b/src/lib/gui/config/AppConfig.h index cbe80b78c..cd36b7dcc 100644 --- a/src/lib/gui/config/AppConfig.h +++ b/src/lib/gui/config/AppConfig.h @@ -8,7 +8,6 @@ #pragma once -#include "ElevateMode.h" #include "IAppConfig.h" #include "IConfigScopes.h" #include "common/constants.h" @@ -22,12 +21,6 @@ #include #include -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; diff --git a/src/lib/gui/config/ElevateMode.h b/src/lib/gui/config/ElevateMode.h deleted file mode 100644 index 94fe95dbb..000000000 --- a/src/lib/gui/config/ElevateMode.h +++ /dev/null @@ -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 -}; diff --git a/src/lib/gui/config/IAppConfig.h b/src/lib/gui/config/IAppConfig.h index 4482ec98c..be420e22c 100644 --- a/src/lib/gui/config/IAppConfig.h +++ b/src/lib/gui/config/IAppConfig.h @@ -6,8 +6,6 @@ #pragma once -#include "ElevateMode.h" - #include "gui/config/IConfigScopes.h" #include @@ -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 diff --git a/src/lib/gui/core/CoreProcess.cpp b/src/lib/gui/core/CoreProcess.cpp index d1ae6cf88..d72fdc614 100644 --- a/src/lib/gui/core/CoreProcess.cpp +++ b/src/lib/gui/core/CoreProcess.cpp @@ -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(); + 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(); + if (elevateMode == Settings::ElevateMode::Automatic) { args << "--stop-on-desk-switch"; } #endif diff --git a/src/lib/gui/ipc/DaemonIpcClient.cpp b/src/lib/gui/ipc/DaemonIpcClient.cpp index e04d802e8..c0cd7f083 100644 --- a/src/lib/gui/ipc/DaemonIpcClient.cpp +++ b/src/lib/gui/ipc/DaemonIpcClient.cpp @@ -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; } diff --git a/src/lib/gui/ipc/DaemonIpcClient.h b/src/lib/gui/ipc/DaemonIpcClient.h index b4dc52f7e..988926ae3 100644 --- a/src/lib/gui/ipc/DaemonIpcClient.h +++ b/src/lib/gui/ipc/DaemonIpcClient.h @@ -8,7 +8,7 @@ #include -#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(); diff --git a/src/test/shared/gui/mocks/AppConfigMock.h b/src/test/shared/gui/mocks/AppConfigMock.h index 9f25a73e0..0ba3850a9 100644 --- a/src/test/shared/gui/mocks/AppConfigMock.h +++ b/src/test/shared/gui/mocks/AppConfigMock.h @@ -8,8 +8,6 @@ #include "gui/config/IAppConfig.h" -#include "gui/config/ElevateMode.h" - #include 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";