diff --git a/src/lib/gui/CMakeLists.txt b/src/lib/gui/CMakeLists.txt index 2e0aab341..ec36456e3 100644 --- a/src/lib/gui/CMakeLists.txt +++ b/src/lib/gui/CMakeLists.txt @@ -31,9 +31,6 @@ add_library(${target} STATIC string_utils.h style_utils.h styles.h - config/ConfigScopes.cpp - config/ConfigScopes.h - config/IConfigScopes.h config/IServerConfig.h config/Screen.cpp config/Screen.h diff --git a/src/lib/gui/config/ConfigScopes.cpp b/src/lib/gui/config/ConfigScopes.cpp deleted file mode 100644 index b69c2b1d1..000000000 --- a/src/lib/gui/config/ConfigScopes.cpp +++ /dev/null @@ -1,147 +0,0 @@ -/* - * Deskflow -- mouse and keyboard sharing utility - * SPDX-FileCopyrightText: (C) 2020 Symless Ltd. - * SPDX-License-Identifier: GPL-2.0-only WITH LicenseRef-OpenSSL-Exception - */ - -#include "ConfigScopes.h" - -#include "common/QSettingsProxy.h" - -#include -#include -#include -#include -#include -#include - -namespace deskflow::gui { - -// -// ConfigScopes::Deps -// - -std::shared_ptr ConfigScopes::Deps::makeUserSettings() -{ - return std::make_shared(); -} - -std::shared_ptr ConfigScopes::Deps::makeSystemSettings() -{ - return std::make_shared(); -} - -// -// ConfigScopes -// - -ConfigScopes::ConfigScopes(std::shared_ptr deps) - : m_pUserSettingsProxy(deps->makeUserSettings()), - m_pSystemSettingsProxy(deps->makeSystemSettings()) -{ - - m_pUserSettingsProxy->loadUser(); - m_pSystemSettingsProxy->loadSystem(); -} - -void ConfigScopes::clear() const -{ - m_pUserSettingsProxy->clear(); - m_pSystemSettingsProxy->clear(); -} - -void ConfigScopes::signalReady() -{ - Q_EMIT ready(); -} - -void ConfigScopes::save(bool emitSaving) -{ - if (emitSaving) { - qDebug("emitting config saving signal"); - Q_EMIT saving(); - } - - qDebug("writing config to filesystem"); - m_pUserSettingsProxy->sync(); - m_pSystemSettingsProxy->sync(); -} - -bool ConfigScopes::isActiveScopeWritable() const -{ - return activeSettings().isWritable(); -} - -void ConfigScopes::setActiveScope(ConfigScopes::Scope scope) -{ - m_currentScope = scope; -} - -ConfigScopes::Scope ConfigScopes::activeScope() const -{ - return m_currentScope; -} - -bool ConfigScopes::scopeContains(const QString &name, Scope scope) const -{ - switch (scope) { - case Scope::User: - return m_pUserSettingsProxy->contains(name); - case Scope::System: - return m_pSystemSettingsProxy->contains(name); - default: - return activeSettings().contains(name); - } -} - -QSettingsProxy &ConfigScopes::activeSettings() -{ - if (m_currentScope == Scope::User) { - return *m_pUserSettingsProxy; - } else { - return *m_pSystemSettingsProxy; - } -} - -const QSettingsProxy &ConfigScopes::activeSettings() const -{ - if (m_currentScope == Scope::User) { - return *m_pUserSettingsProxy; - } else { - return *m_pSystemSettingsProxy; - } -} - -QString ConfigScopes::activeFilePath() const -{ - return activeSettings().fileName(); -} - -QVariant ConfigScopes::getFromScope(const QString &name, const QVariant &defaultValue, Scope scope) const -{ - switch (scope) { - case Scope::User: - return m_pUserSettingsProxy->value(name, defaultValue); - case Scope::System: - return m_pSystemSettingsProxy->value(name, defaultValue); - default: - return activeSettings().value(name, defaultValue); - } -} - -void ConfigScopes::setInScope(const QString &name, const QVariant &value, Scope scope) -{ - switch (scope) { - case Scope::User: - m_pUserSettingsProxy->setValue(name, value); - break; - case Scope::System: - m_pSystemSettingsProxy->setValue(name, value); - break; - default: - activeSettings().setValue(name, value); - break; - } -} - -} // namespace deskflow::gui diff --git a/src/lib/gui/config/ConfigScopes.h b/src/lib/gui/config/ConfigScopes.h deleted file mode 100644 index c891e994e..000000000 --- a/src/lib/gui/config/ConfigScopes.h +++ /dev/null @@ -1,61 +0,0 @@ -/* - * Deskflow -- mouse and keyboard sharing utility - * SPDX-FileCopyrightText: (C) 2020 Symless Ltd. - * SPDX-License-Identifier: GPL-2.0-only WITH LicenseRef-OpenSSL-Exception - */ - -#pragma once - -#include "IConfigScopes.h" -#include "common/QSettingsProxy.h" - -#include -#include -#include -#include - -namespace deskflow::gui { - -/// @brief Encapsulates Qt config for both user and global scopes. -class ConfigScopes : public QObject, public IConfigScopes -{ - Q_OBJECT - -public: - struct Deps - { - virtual ~Deps() = default; - virtual std::shared_ptr makeUserSettings(); - virtual std::shared_ptr makeSystemSettings(); - }; - - explicit ConfigScopes(std::shared_ptr deps = std::make_shared()); - ~ConfigScopes() override = default; - - void clear() const; - - void signalReady() override; - void save(bool emitSaving = true) override; - bool scopeContains(const QString &name, Scope scope = Scope::Current) const override; - bool isActiveScopeWritable() const override; - void setInScope(const QString &name, const QVariant &value, Scope scope = Scope::Current) override; - QVariant getFromScope( - const QString &name, const QVariant &defaultValue = QVariant(), Scope scope = Scope::Current - ) const override; - void setActiveScope(Scope scope = Scope::User) override; - Scope activeScope() const override; - QSettingsProxy &activeSettings() override; - const QSettingsProxy &activeSettings() const override; - QString activeFilePath() const override; - -signals: - void ready(); - void saving(); - -private: - Scope m_currentScope = Scope::User; - std::shared_ptr m_pUserSettingsProxy; - std::shared_ptr m_pSystemSettingsProxy; -}; - -} // namespace deskflow::gui diff --git a/src/lib/gui/config/IConfigScopes.h b/src/lib/gui/config/IConfigScopes.h deleted file mode 100644 index 6a0ca3ca1..000000000 --- a/src/lib/gui/config/IConfigScopes.h +++ /dev/null @@ -1,68 +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 "common/QSettingsProxy.h" - -#include -#include -#include - -namespace deskflow::gui { - -class IConfigScopes -{ - -public: - enum class Scope - { - Current, - System, - User - }; - - virtual ~IConfigScopes() = default; - - virtual Scope activeScope() const = 0; - virtual void setActiveScope(Scope scope = Scope::User) = 0; - virtual bool isActiveScopeWritable() const = 0; - virtual QSettingsProxy &activeSettings() = 0; - virtual const QSettingsProxy &activeSettings() const = 0; - virtual QString activeFilePath() const = 0; - - /** - * @brief Signals to listeners that the settings that they should read. - */ - virtual void signalReady() = 0; - - /** - * @brief Signals to listeners to save and calls `sync` on underlying Qt - * config. - * - * @param emitSaving Whether to emit the saving signal which typically - * triggers listeners to write their current state to the config. - */ - virtual void save(bool emitSaving = true) = 0; - - /** - * @brief Check a scope for a config value (default is current scope). - */ - virtual bool scopeContains(const QString &name, Scope scope = Scope::Current) const = 0; - - /** - * @brief Load a config value from a scope (default is current scope). - */ - virtual QVariant - getFromScope(const QString &name, const QVariant &defaultValue = QVariant(), Scope scope = Scope::Current) const = 0; - - /** - * @brief Set a config value in a scope (default is current scope). - */ - virtual void setInScope(const QString &name, const QVariant &value, Scope scope = Scope::Current) = 0; -}; - -} // namespace deskflow::gui diff --git a/src/test/unittests/gui/config/ConfigScopesTests.cpp b/src/test/unittests/gui/config/ConfigScopesTests.cpp deleted file mode 100644 index 70a6d49ab..000000000 --- a/src/test/unittests/gui/config/ConfigScopesTests.cpp +++ /dev/null @@ -1,183 +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/ConfigScopes.h" - -#include "gmock/gmock.h" -#include -#include -#include - -using namespace testing; -using namespace deskflow::gui; - -namespace { - -class QSettingsProxyMock : public QSettingsProxy -{ -public: - MOCK_METHOD(void, loadSystem, (), (override)); - MOCK_METHOD(void, loadUser, (), (override)); - MOCK_METHOD(QString, fileName, (), (const, override)); - MOCK_METHOD(void, sync, (), (override)); - MOCK_METHOD(bool, isWritable, (), (const, override)); - MOCK_METHOD(bool, contains, (const QString &), (const, override)); - MOCK_METHOD(QVariant, value, (const QString &), (const, override)); - MOCK_METHOD(QVariant, value, (const QString &, const QVariant &), (const, override)); - MOCK_METHOD(void, setValue, (const QString &, const QVariant &), (override)); -}; - -struct DepsMock : public ConfigScopes::Deps -{ - DepsMock() - { - ON_CALL(*this, makeUserSettings()).WillByDefault(Return(m_pUserSettings)); - ON_CALL(*this, makeSystemSettings()).WillByDefault(Return(m_pSystemSettings)); - } - - MOCK_METHOD(std::shared_ptr, makeUserSettings, (), (override)); - MOCK_METHOD(std::shared_ptr, makeSystemSettings, (), (override)); - - std::shared_ptr m_pUserSettings = std::make_shared>(); - std::shared_ptr m_pSystemSettings = std::make_shared>(); -}; - -} // namespace - -TEST(ConfigScopesTests, ctor_callsMakeUserSettings) -{ - auto deps = std::make_shared>(); - - EXPECT_CALL(*deps, makeUserSettings()).Times(1); - - ConfigScopes configScopes(deps); -} - -TEST(ConfigScopesTests, ctor_callsMakeSystemSettings) -{ - auto deps = std::make_shared>(); - - EXPECT_CALL(*deps, makeSystemSettings()).Times(1); - - ConfigScopes configScopes(deps); -} - -TEST(ConfigScopesTests, save_syncsBothScopes) -{ - auto deps = std::make_shared>(); - - ConfigScopes configScopes(deps); - - EXPECT_CALL(*deps->m_pUserSettings, sync()).Times(1); - EXPECT_CALL(*deps->m_pSystemSettings, sync()).Times(1); - - configScopes.save(); -} - -TEST(ConfigScopesTests, activeSettings_returnsUserSettingsByDefault) -{ - auto deps = std::make_shared>(); - - ConfigScopes configScopes(deps); - - EXPECT_EQ(&configScopes.activeSettings(), deps->m_pUserSettings.get()); -} - -TEST(ConfigScopesTests, activeSettings_returnsSystemSettingsWhenSystemScope) -{ - auto deps = std::make_shared>(); - - ConfigScopes configScopes(deps); - configScopes.setActiveScope(ConfigScopes::Scope::System); - - EXPECT_EQ(&configScopes.activeSettings(), deps->m_pSystemSettings.get()); -} - -TEST(ConfigScopesTests, setActiveScope_setsCurrentScope) -{ - auto deps = std::make_shared>(); - - ConfigScopes configScopes(deps); - - configScopes.setActiveScope(ConfigScopes::Scope::System); - - EXPECT_EQ(configScopes.activeScope(), ConfigScopes::Scope::System); -} - -TEST(ConfigScopesTests, isActiveScopeWritable_returnsTrueWhenUserSettingsWritable) -{ - auto deps = std::make_shared>(); - - ConfigScopes configScopes(deps); - - EXPECT_CALL(*deps->m_pUserSettings, isWritable()).WillOnce(Return(true)); - - EXPECT_TRUE(configScopes.isActiveScopeWritable()); -} - -TEST(ConfigScopesTests, scopeContains_byDefault_returnsTrueWhenUserSettingsContainsKey) -{ - auto deps = std::make_shared>(); - - ON_CALL(*deps->m_pUserSettings, contains(_)).WillByDefault(Return(true)); - - ConfigScopes configScopes(deps); - - EXPECT_TRUE(configScopes.scopeContains("stub")); -} - -TEST(ConfigScopesTests, scopeContains_userScope_returnsTrueWhenUserSettingsContainsKey) -{ - auto deps = std::make_shared>(); - - ON_CALL(*deps->m_pUserSettings, contains(_)).WillByDefault(Return(true)); - - ConfigScopes configScopes(deps); - - EXPECT_TRUE(configScopes.scopeContains("stub", ConfigScopes::Scope::User)); -} - -TEST(ConfigScopesTests, scopeContains_systemScope_returnsTrueWhenSystemSettingsContainsKey) -{ - auto deps = std::make_shared>(); - - ON_CALL(*deps->m_pSystemSettings, contains(_)).WillByDefault(Return(true)); - - ConfigScopes configScopes(deps); - - EXPECT_TRUE(configScopes.scopeContains("stub", ConfigScopes::Scope::System)); -} - -TEST(ConfigScopesTests, activeFilePath_returnsUserSettingsFileNameByDefault) -{ - auto deps = std::make_shared>(); - ON_CALL(*deps->m_pUserSettings, fileName()).WillByDefault(Return("test")); - - ConfigScopes configScopes(deps); - - EXPECT_EQ(configScopes.activeFilePath(), "test"); -} - -TEST(ConfigScopesTests, getFromScope_byDefault_returnsValueFromActiveSettings) -{ - auto deps = std::make_shared>(); - ON_CALL(*deps->m_pUserSettings, value(_, _)).WillByDefault(Return("test")); - - ConfigScopes configScopes(deps); - - EXPECT_EQ(configScopes.getFromScope("stub"), "test"); -} - -TEST(ConfigScopesTests, setInScope_byDefault_setsValueInActiveSettings) -{ - auto deps = std::make_shared>(); - - ConfigScopes configScopes(deps); - - EXPECT_CALL(*deps->m_pUserSettings, setValue(_, _)).Times(1); - - configScopes.setInScope("stub", "test"); -}