diff --git a/src/apps/deskflow-gui/main.cpp b/src/apps/deskflow-gui/main.cpp index bdbaac204..a80633a78 100644 --- a/src/apps/deskflow-gui/main.cpp +++ b/src/apps/deskflow-gui/main.cpp @@ -145,10 +145,6 @@ int main(int argc, char *argv[]) AppConfig appConfig(configScopes); - QObject::connect( - &configScopes, &ConfigScopes::saving, &appConfig, [&appConfig]() { appConfig.commit(); }, Qt::DirectConnection - ); - MainWindow mainWindow(configScopes, appConfig); mainWindow.open(); diff --git a/src/lib/gui/config/AppConfig.cpp b/src/lib/gui/config/AppConfig.cpp index 7000f9634..6d67e67c6 100644 --- a/src/lib/gui/config/AppConfig.cpp +++ b/src/lib/gui/config/AppConfig.cpp @@ -8,6 +8,8 @@ #include "AppConfig.h" +#include "common/Settings.h" + #include "ConfigScopes.h" #include @@ -19,173 +21,9 @@ using namespace deskflow::gui; -// TODO: instead, use key value pair table, which would be less fragile. -const char *const AppConfig::m_SettingsName[] = { - "", // screenName moved to deskflow settings - "", // port moved to deskflow settings - "", // interface moved to deskflow settings - "", // log level moved to deskflow settings - "", // Log to file Moved to Deskflow settings - "", // 5 logFilename, moved to deskflow settings - "", // 6 wizardLastRun, obsolete - "", // 7 statedBefore moved to deskflow settings - "", // 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 - "", // 13 = serialKey, obsolete - "", // 14 last Version moved ot deskflow settings - "", // 15 = lastExpiringWarningTime, obsolete - "", // 16 = activationHasRun, obsolete - "", // 17 = minimizeToTray, obsolete - "", // 18 = ActivateEmail, obsolete - "loadFromSystemScope", - "", // kServerGroupChecked - "", // 21 = use external config moved to deskflow settings - "", // 22 config file moved to dekflow settings - "", // 23 use internal config unsed, removed - "", // Client groupchecked - "", // 25 server host name moved to deskflow settings - "", // 26 cert path moved to deskflow settings - "", // 27 key length Moved to Deskflow settings - "", // 28 Prevent sleep moved to deskflow settings - "", // 29 Language sync moved to deskflow settings - "", // 30 = invertscrolldriection moved to deskflow settings - "", // 31 = guid, obsolete - "", // 32 = licenseRegistryUrl, obsolete - "", // 33 = licenseNextCheck, obsolete - "", // 34 = kInvertConnection, obsolete - "", // 35 = clientHostMode, obsolete - "", // 36 = serverClientMode, obsolete - "", // 37 enable service moved to deskflow settings - "", // 38 Moved to deskflow settings - "", // 39 window size moved to deskflow settings - "", // 40 window position moved to deskflow settings - "", // 41 = Show dev thanks, obsolete - "", // 42 show Close Reminder moved to deskflow settings - "", // 43 Moved to deskflow settings - "", // 44, Moved to deskflow settings. - "", // 45 Moved to deskflow settings - "", // 46 require peer certs, Moved to deskflow settings -}; - AppConfig::AppConfig(deskflow::gui::IConfigScopes &scopes, std::shared_ptr deps) : m_Scopes(scopes), m_pDeps(deps) { - determineScope(); - recall(); -} - -void AppConfig::recall() -{ - using enum AppConfig::Setting; - - qDebug("recalling app config"); - - recallFromAllScopes(); -} - -void AppConfig::recallFromAllScopes() -{ - using enum Setting; - m_LoadFromSystemScope = findInAllScopes(kLoadSystemSettings, m_LoadFromSystemScope).toBool(); -} - -void AppConfig::commit() -{ - using enum Setting; - - qDebug("committing app config"); - - saveToAllScopes(kLoadSystemSettings, m_LoadFromSystemScope); -} - -void AppConfig::determineScope() -{ - - qDebug("determining config scope"); - - // first, try to determine if the system scope should be used according to the - // user scope... - if (m_Scopes.scopeContains(settingName(Setting::kLoadSystemSettings), ConfigScopes::Scope::User)) { - auto loadFromSystemScope = - m_Scopes - .getFromScope(settingName(Setting::kLoadSystemSettings), m_LoadFromSystemScope, ConfigScopes::Scope::User) - .toBool(); - if (loadFromSystemScope) { - qDebug("user settings indicates system scope should be used"); - } else { - qDebug("user settings indicates user scope should be used"); - } - setLoadFromSystemScope(loadFromSystemScope); - } - - // ...failing that, check the system scope instead to see if an arbitrary - // required setting is present. if it is, then we can assume that the system - // scope should be used. - else if (m_Scopes.scopeContains(settingName(Setting::kLoadSystemSettings), ConfigScopes::Scope::System)) { - qDebug("system settings scope contains screen name, using system scope"); - setLoadFromSystemScope(true); - } -} - -QString AppConfig::settingName(Setting name) -{ - auto index = static_cast(name); - return m_SettingsName[index]; -} - -template void AppConfig::setInCurrentScope(Setting name, T value) -{ - m_Scopes.setInScope(settingName(name), value); -} - -template void AppConfig::saveToAllScopes(Setting name, T value) -{ - m_Scopes.setInScope(settingName(name), value, ConfigScopes::Scope::User); - m_Scopes.setInScope(settingName(name), value, ConfigScopes::Scope::System); -} - -QVariant AppConfig::getFromCurrentScope(Setting name, const QVariant &defaultValue) const -{ - return m_Scopes.getFromScope(settingName(name), defaultValue); -} - -template -std::optional AppConfig::getFromCurrentScope(Setting name, std::function toType) const -{ - if (m_Scopes.scopeContains(settingName(name))) { - return toType(m_Scopes.getFromScope(settingName(name))); - } else { - return std::nullopt; - } -} - -template void AppConfig::setInCurrentScope(Setting name, const std::optional &value) -{ - if (value.has_value()) { - m_Scopes.setInScope(settingName(name), value.value()); - } -} - -QVariant AppConfig::findInAllScopes(Setting name, const QVariant &defaultValue) const -{ - using enum ConfigScopes::Scope; - - QVariant result(defaultValue); - QString setting(settingName(name)); - - if (m_Scopes.scopeContains(setting)) { - result = m_Scopes.getFromScope(setting, defaultValue); - } else if (m_Scopes.activeScope() == System) { - if (m_Scopes.scopeContains(setting, User)) { - result = m_Scopes.getFromScope(setting, defaultValue, User); - } - } else if (m_Scopes.scopeContains(setting, System)) { - result = m_Scopes.getFromScope(setting, defaultValue, System); - } - - return result; + setLoadFromSystemScope(Settings::isSystemScope()); } void AppConfig::loadScope(ConfigScopes::Scope scope) @@ -216,7 +54,7 @@ void AppConfig::loadScope(ConfigScopes::Scope scope) // only signal ready if there is at least one setting in the required scope. // this prevents the current settings from being set back to default. - if (m_Scopes.scopeContains(settingName(Setting::kLoadSystemSettings), m_Scopes.activeScope())) { + if (m_Scopes.scopeContains("core", m_Scopes.activeScope())) { m_Scopes.signalReady(); } else { qDebug("no screen name in scope, skipping"); @@ -226,15 +64,11 @@ void AppConfig::loadScope(ConfigScopes::Scope scope) void AppConfig::setLoadFromSystemScope(bool value) { using enum ConfigScopes::Scope; - if (value) { loadScope(System); } else { loadScope(User); } - - // set after loading scope since it may have been overridden. - m_LoadFromSystemScope = value; } bool AppConfig::isActiveScopeWritable() const diff --git a/src/lib/gui/config/AppConfig.h b/src/lib/gui/config/AppConfig.h index 736292f6d..62c012e76 100644 --- a/src/lib/gui/config/AppConfig.h +++ b/src/lib/gui/config/AppConfig.h @@ -31,61 +31,8 @@ */ class AppConfig : public QObject, public deskflow::gui::IAppConfig { - using IConfigScopes = deskflow::gui::IConfigScopes; - Q_OBJECT - -private: - enum class Setting - { - // kScreenName = 0, Moved to deskflow settings - // kPort = 1, moved to deskflow settings - // kInterface = 2, moved to deskflow settings - // kLogLevel = 3, moved to deskflow settings - // 4 = LogToFile moved to deskflow settings - // 5 = logfilename moved to deskflow settings - // 6 = show first run wizard, obsolete - // 7 Started before moved to deskflow settings - // kElevateModeLegacy = 8, - // kElevateMode = 9, - // 10 = edition, obsolete (related to obsolete licensing) - // 11 = tlsEnagled moved to Settigns - // 12 = auto hide, Moved to Settings - // 13 = serial key, obsolete - // 14 = kLastVersion, moved to deskflow settings - // 15 = last expire time, obsolete - // 16 = activation has run, obsolete - // 17 = minimize to tray, obsolete - // 18 = activate email, obsolete - kLoadSystemSettings = 19, - // kServerGroupChecked = 20, - // 21 = use external config moved to deskflow settings - // 22 = k config file moved to deskflow settings - // 23 kUseInternalConfig removed - // kClientGroupChecked = 24, - // 25 = serverHostName moved to deskflow settings - // 26 = kTlsCertPath moved to deskflow settings - // 27 = tlsKeyLength Moved to deskflow settings - // 28 = Prevent Sleep moved to deskflow settings - // 29 = language Sync moved to deskflow settings - // 30 = InvertScrollDirection moved to deskflow settings - // 31 = guid, obsolete - // 32 = license registry url, obsolete - // 33 = license next check, obsolete - // 34 = InvertConnection, obsolete - // 35 = client-host-mode, obsolete - // 36 = server-client-mode, obsolete - // kEnableService = 37, moved to deskflow settings - // 38, close to tray moved to deskflow settings - // 39 window size moved to deskflow settings - // 40 window position moved to deskflow settings - // 41 = show dev thanks, obsolete - // 42, close reminder moved to deskflow settings - // 43 = Enable Update Check, - // 44 = LogExpanded, Moved to deskflow settings - // 45 = Colorful Icon, Moved to deskflow settings - // kRequireClientCert = 46 Moved to deskflow settings - }; + using IConfigScopes = deskflow::gui::IConfigScopes; public: struct Deps @@ -97,16 +44,6 @@ public: void determineScope(); - /** - * @brief Commits the current settings to the active scope. - * This should only be called when the settings are about to be saved. - */ - void commit(); - - // - // Getters (overrides) - // - IConfigScopes &scopes() const override; bool isActiveScopeWritable() const override; bool isActiveScopeSystem() const override; @@ -119,53 +56,10 @@ public: void setLoadFromSystemScope(bool value) override; private: - static QString settingName(AppConfig::Setting name); - - void recall(); - void recallScreenName(); - void recallFromAllScopes(); - - /** - * @brief Loads a setting if it exists, otherwise returns `std::nullopt` - * - * @param toType A function to convert the QVariant to the desired type. - */ - template - std::optional getFromCurrentScope(Setting name, std::function toType) const; - - /** - * @brief Sets a setting if the value is not `std::nullopt`. - */ - template void setInCurrentScope(Setting name, const std::optional &value); - - /// @brief Sets the value of a setting - /// @param [in] name The Setting to be saved - /// @param [in] value The Value to be saved - template void setInCurrentScope(AppConfig::Setting name, T value); - - /// @brief Sets the value of a common setting - /// which should have the same value for all scopes - /// @param [in] name The Setting to be saved - /// @param [in] value The Value to be saved - template void saveToAllScopes(AppConfig::Setting name, T value); - - QVariant getFromCurrentScope(AppConfig::Setting name, const QVariant &defaultValue = QVariant()) const; - - /** - * @brief Finds a value by searching each scope starting with the current - * scope. - */ - QVariant findInAllScopes(AppConfig::Setting name, const QVariant &defaultValue = QVariant()) const; - /// @brief This method loads config from specified scope /// @param [in] scope which should be loaded. void loadScope(IConfigScopes::Scope scope); - /// @brief Contains the string values of the settings names that will be saved - static const char *const m_SettingsName[]; - - bool m_LoadFromSystemScope = false; - deskflow::gui::IConfigScopes &m_Scopes; std::shared_ptr m_pDeps; }; diff --git a/src/test/shared/gui/mocks/AppConfigMock.h b/src/test/shared/gui/mocks/AppConfigMock.h deleted file mode 100644 index 82b6a0e02..000000000 --- a/src/test/shared/gui/mocks/AppConfigMock.h +++ /dev/null @@ -1,37 +0,0 @@ -/* - * Deskflow -- mouse and keyboard sharing utility - * SPDX-FileCopyrightText: (C) 2024 Symless Ltd. - * SPDX-License-Identifier: GPL-2.0-only WITH LicenseRef-OpenSSL-Exception - */ - -#pragma once - -#include "gui/config/IAppConfig.h" - -#include - -class AppConfigMock : public deskflow::gui::IAppConfig -{ - -public: - AppConfigMock() - { - } - - // - // Getters - // - - MOCK_METHOD(deskflow::gui::IConfigScopes &, scopes, (), (const, override)); - MOCK_METHOD(bool, isActiveScopeSystem, (), (const, override)); - MOCK_METHOD(bool, isActiveScopeWritable, (), (const, override)); - - // - // Setters - // - - MOCK_METHOD(void, setLoadFromSystemScope, (bool loadFromSystemScope), (override)); - -private: - const QString m_stub = "stub"; -}; diff --git a/src/test/unittests/gui/config/AppConfigTests.cpp b/src/test/unittests/gui/config/AppConfigTests.cpp deleted file mode 100644 index be70a2ce2..000000000 --- a/src/test/unittests/gui/config/AppConfigTests.cpp +++ /dev/null @@ -1,80 +0,0 @@ -/* - * Deskflow -- mouse and keyboard sharing utility - * SPDX-FileCopyrightText: (C) 2024 Symless Ltd. - * SPDX-License-Identifier: GPL-2.0-only WITH LicenseRef-OpenSSL-Exception - */ - -#include "gui/config/AppConfig.h" - -#include "gui/proxy/QSettingsProxy.h" - -#include "gmock/gmock.h" -#include -#include - -using namespace testing; -using namespace deskflow::gui::proxy; - -namespace { - -class ConfigScopesMock : public deskflow::gui::IConfigScopes -{ - using QSettingsProxy = deskflow::gui::proxy::QSettingsProxy; - -public: - MOCK_METHOD(void, signalReady, (), (override)); - MOCK_METHOD(bool, scopeContains, (const QString &name, Scope scope), (const, override)); - MOCK_METHOD( - QVariant, getFromScope, (const QString &name, const QVariant &defaultValue, Scope scope), (const, override) - ); - MOCK_METHOD(void, setInScope, (const QString &name, const QVariant &value, Scope scope), (override)); - MOCK_METHOD(Scope, activeScope, (), (const, override)); - MOCK_METHOD(void, setActiveScope, (Scope scope), (override)); - MOCK_METHOD(bool, isActiveScopeWritable, (), (const, override)); - MOCK_METHOD(const QSettingsProxy &, activeSettings, (), (const, override)); - MOCK_METHOD(QSettingsProxy &, activeSettings, (), (override)); - MOCK_METHOD(void, save, (bool), (override)); - MOCK_METHOD(QString, activeFilePath, (), (const, override)); -}; - -struct DepsMock : public AppConfig::Deps -{ - DepsMock() - { - } - - static std::shared_ptr> makeNice() - { - return std::make_shared>(); - } -}; - -} // namespace - -class AppConfigTests : public Test -{ -}; - -TEST_F(AppConfigTests, ctor_byDefault_getsFromScope) -{ - NiceMock scopes; - auto deps = DepsMock::makeNice(); - - ON_CALL(scopes, scopeContains(_, _)).WillByDefault(Return(true)); - ON_CALL(scopes, getFromScope(_, _, _)).WillByDefault(Return(QVariant("test screen"))); - EXPECT_CALL(scopes, getFromScope(_, _, _)).Times(AnyNumber()); - - AppConfig appConfig(scopes, deps); -} - -TEST_F(AppConfigTests, commit_byDefault_setsToScope) -{ - NiceMock scopes; - auto deps = DepsMock::makeNice(); - AppConfig appConfig(scopes, deps); - - ON_CALL(scopes, isActiveScopeWritable()).WillByDefault(Return(true)); - EXPECT_CALL(scopes, setInScope(_, _, _)).Times(AnyNumber()); - - appConfig.commit(); -} diff --git a/src/test/unittests/gui/core/CoreProcessTests.cpp b/src/test/unittests/gui/core/CoreProcessTests.cpp index 978aaf943..734c1355c 100644 --- a/src/test/unittests/gui/core/CoreProcessTests.cpp +++ b/src/test/unittests/gui/core/CoreProcessTests.cpp @@ -8,7 +8,6 @@ #include "gui/config/IAppConfig.h" #include "gui/core/CoreProcess.h" #include "gui/proxy/QProcessProxy.h" -#include "shared/gui/mocks/AppConfigMock.h" #include "shared/gui/mocks/ServerConfigMock.h" #include "gmock/gmock.h"