2012-06-10 16:50:54 +00:00
|
|
|
/*
|
2024-09-17 19:00:25 +00:00
|
|
|
* Deskflow -- mouse and keyboard sharing utility
|
2024-11-29 17:03:51 +00:00
|
|
|
* SPDX-FileCopyrightText: (C) 2025 Deskflow Developers
|
2025-01-24 00:11:17 +00:00
|
|
|
* SPDX-FileCopyrightText: (C) 2012 Symless Ltd.
|
|
|
|
|
* 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
|
|
|
*/
|
|
|
|
|
|
2024-07-22 16:48:02 +00:00
|
|
|
#pragma once
|
2012-06-10 16:50:54 +00:00
|
|
|
|
2024-08-01 00:13:01 +00:00
|
|
|
#include "ElevateMode.h"
|
|
|
|
|
#include "IAppConfig.h"
|
2024-08-07 14:05:18 +00:00
|
|
|
#include "IConfigScopes.h"
|
2024-10-28 19:20:35 +00:00
|
|
|
#include "common/constants.h"
|
2024-08-10 23:58:24 +00:00
|
|
|
#include "gui/paths.h"
|
2024-07-22 16:48:02 +00:00
|
|
|
|
2024-08-01 00:13:01 +00:00
|
|
|
#include <QDir>
|
|
|
|
|
#include <QHostInfo>
|
2016-10-04 12:41:27 +00:00
|
|
|
#include <QObject>
|
2024-08-01 00:13:01 +00:00
|
|
|
#include <QPoint>
|
|
|
|
|
#include <QSize>
|
2012-06-10 16:50:54 +00:00
|
|
|
#include <QString>
|
2020-02-13 09:21:02 +00:00
|
|
|
#include <QVariant>
|
2024-08-03 00:17:29 +00:00
|
|
|
#include <optional>
|
2012-06-10 16:50:54 +00:00
|
|
|
|
2024-09-17 19:00:25 +00:00
|
|
|
namespace deskflow::gui {
|
2024-07-02 19:07:06 +00:00
|
|
|
|
2024-08-07 14:05:18 +00:00
|
|
|
const ElevateMode kDefaultElevateMode = ElevateMode::kAutomatic;
|
2024-10-28 19:20:35 +00:00
|
|
|
const QString kDefaultLogFile = QStringLiteral("%1.log").arg(kAppId);
|
2024-07-22 16:48:02 +00:00
|
|
|
|
|
|
|
|
#if defined(Q_OS_WIN)
|
|
|
|
|
const ProcessMode kDefaultProcessMode = ProcessMode::kService;
|
|
|
|
|
#else
|
|
|
|
|
const ProcessMode kDefaultProcessMode = ProcessMode::kDesktop;
|
2024-08-03 00:17:29 +00:00
|
|
|
#endif // Q_OS_WIN
|
|
|
|
|
|
2024-09-17 19:00:25 +00:00
|
|
|
} // namespace deskflow::gui
|
2024-08-07 14:05:18 +00:00
|
|
|
|
2024-07-26 22:53:52 +00:00
|
|
|
/**
|
|
|
|
|
* @brief Simply reads and writes app settings.
|
|
|
|
|
*
|
|
|
|
|
* Important: Maintain a clear separation of concerns and keep it simple.
|
|
|
|
|
* It is tempting to add logic (e.g. license checks) to this class since it
|
|
|
|
|
* instance is widely accessible, but that has previously led to this class
|
|
|
|
|
* becoming a god object.
|
|
|
|
|
*/
|
2024-10-17 18:04:35 +00:00
|
|
|
class AppConfig : public QObject, public deskflow::gui::IAppConfig
|
|
|
|
|
{
|
2024-09-17 19:00:25 +00:00
|
|
|
using ProcessMode = deskflow::gui::ProcessMode;
|
|
|
|
|
using IConfigScopes = deskflow::gui::IConfigScopes;
|
2024-08-07 14:05:18 +00:00
|
|
|
|
2024-07-02 19:07:06 +00:00
|
|
|
Q_OBJECT
|
|
|
|
|
|
2024-07-26 22:53:52 +00:00
|
|
|
private:
|
2024-10-17 18:04:35 +00:00
|
|
|
enum class Setting
|
|
|
|
|
{
|
2024-07-26 22:53:52 +00:00
|
|
|
kScreenName = 0,
|
|
|
|
|
kPort = 1,
|
|
|
|
|
kInterface = 2,
|
|
|
|
|
kLogLevel = 3,
|
|
|
|
|
kLogToFile = 4,
|
|
|
|
|
kLogFilename = 5,
|
2025-03-04 14:05:52 +00:00
|
|
|
// 6 = show first run wizard, obsolete
|
2025-03-05 05:03:48 +00:00
|
|
|
// 7 Started before moved to deskflow settings
|
2024-08-01 00:13:01 +00:00
|
|
|
kElevateModeLegacy = 8,
|
|
|
|
|
kElevateMode = 9,
|
2024-10-01 13:30:24 +00:00
|
|
|
// 10 = edition, obsolete (related to obsolete licensing)
|
2025-03-06 23:08:08 +00:00
|
|
|
// 11 = tlsEnagled moved to Settigns
|
2025-03-05 03:29:28 +00:00
|
|
|
// 12 = auto hide, Moved to Settings
|
2024-10-01 13:30:24 +00:00
|
|
|
// 13 = serial key, obsolete
|
2025-03-07 02:11:19 +00:00
|
|
|
// 14 = kLastVersion, moved to deskflow settings
|
2024-07-26 22:53:52 +00:00
|
|
|
// 15 = last expire time, obsolete
|
2024-10-01 13:30:24 +00:00
|
|
|
// 16 = activation has run, obsolete
|
2024-08-01 00:13:01 +00:00
|
|
|
// 17 = minimize to tray, obsolete
|
2024-07-26 22:53:52 +00:00
|
|
|
// 18 = activate email, obsolete
|
|
|
|
|
kLoadSystemSettings = 19,
|
|
|
|
|
kServerGroupChecked = 20,
|
|
|
|
|
kUseExternalConfig = 21,
|
|
|
|
|
kConfigFile = 22,
|
|
|
|
|
kUseInternalConfig = 23,
|
|
|
|
|
kClientGroupChecked = 24,
|
|
|
|
|
kServerHostname = 25,
|
2025-03-07 00:04:55 +00:00
|
|
|
// 26 = kTlsCertPath moved to deskflow settings
|
2025-03-06 23:32:14 +00:00
|
|
|
// 27 = tlsKeyLength Moved to deskflow settings
|
2025-03-07 03:07:14 +00:00
|
|
|
// 28 = Prevent Sleep moved to deskflow settings
|
2024-07-26 22:53:52 +00:00
|
|
|
kLanguageSync = 29,
|
|
|
|
|
kInvertScrollDirection = 30,
|
|
|
|
|
// 31 = guid, obsolete
|
|
|
|
|
// 32 = license registry url, obsolete
|
2024-10-01 13:30:24 +00:00
|
|
|
// 33 = license next check, obsolete
|
2025-02-22 06:01:58 +00:00
|
|
|
// 34 = InvertConnection, obsolete
|
2024-07-26 22:53:52 +00:00
|
|
|
// 35 = client-host-mode, obsolete
|
|
|
|
|
// 36 = server-client-mode, obsolete
|
2024-08-03 00:17:29 +00:00
|
|
|
kEnableService = 37,
|
2025-03-05 04:45:14 +00:00
|
|
|
// 38, close to tray moved to deskflow settings
|
2025-03-05 05:28:05 +00:00
|
|
|
// 39 window size moved to deskflow settings
|
|
|
|
|
// 40 window position moved to deskflow settings
|
2024-10-31 18:47:40 +00:00
|
|
|
// 41 = show dev thanks, obsolete
|
2025-03-05 05:11:41 +00:00
|
|
|
// 42, close reminder moved to deskflow settings
|
2025-03-05 04:25:38 +00:00
|
|
|
// 43 = Enable Update Check,
|
2025-03-08 15:10:42 +00:00
|
|
|
// 44 = LogExpanded, Moved to deskflow settings
|
2025-03-05 04:11:32 +00:00
|
|
|
// 45 = Colorful Icon, Moved to deskflow settings
|
2025-03-07 00:42:16 +00:00
|
|
|
// kRequireClientCert = 46 Moved to deskflow settings
|
2024-07-26 22:53:52 +00:00
|
|
|
};
|
2024-07-02 19:07:06 +00:00
|
|
|
|
|
|
|
|
public:
|
2024-10-17 18:04:35 +00:00
|
|
|
struct Deps
|
|
|
|
|
{
|
2024-08-01 00:13:01 +00:00
|
|
|
virtual ~Deps() = default;
|
2024-10-17 18:04:35 +00:00
|
|
|
virtual QString hostname() const
|
|
|
|
|
{
|
|
|
|
|
return QHostInfo::localHostName();
|
|
|
|
|
}
|
2024-08-01 00:13:01 +00:00
|
|
|
};
|
2024-07-22 16:48:02 +00:00
|
|
|
|
2024-10-17 18:04:35 +00:00
|
|
|
explicit AppConfig(IConfigScopes &scopes, std::shared_ptr<Deps> deps = std::make_shared<Deps>());
|
2024-07-22 16:48:02 +00:00
|
|
|
|
2024-08-07 14:05:18 +00:00
|
|
|
void determineScope();
|
2024-08-03 00:17:29 +00:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @brief Commits the current settings to the active scope.
|
|
|
|
|
* This should only be called when the settings are about to be saved.
|
|
|
|
|
*/
|
|
|
|
|
void commit();
|
|
|
|
|
|
2024-08-08 17:41:59 +00:00
|
|
|
//
|
|
|
|
|
// Getters (overrides)
|
|
|
|
|
//
|
2024-08-09 08:08:29 +00:00
|
|
|
|
2024-08-12 15:39:18 +00:00
|
|
|
IConfigScopes &scopes() const override;
|
2024-08-07 14:05:18 +00:00
|
|
|
ProcessMode processMode() const override;
|
|
|
|
|
ElevateMode elevateMode() const override;
|
|
|
|
|
QString logLevelText() const override;
|
|
|
|
|
const QString &screenName() const override;
|
|
|
|
|
bool logToFile() const override;
|
|
|
|
|
const QString &logFilename() const override;
|
|
|
|
|
QString coreServerName() const override;
|
|
|
|
|
void persistLogDir() const override;
|
|
|
|
|
bool languageSync() const override;
|
|
|
|
|
bool invertScrollDirection() const override;
|
|
|
|
|
int port() const override;
|
|
|
|
|
bool useExternalConfig() const override;
|
|
|
|
|
const QString &configFile() const override;
|
|
|
|
|
const QString &networkInterface() const override;
|
2024-08-08 17:41:59 +00:00
|
|
|
const QString &serverHostname() const override;
|
2024-08-09 08:08:29 +00:00
|
|
|
bool isActiveScopeWritable() const override;
|
|
|
|
|
bool isActiveScopeSystem() const override;
|
|
|
|
|
int logLevel() const override;
|
|
|
|
|
bool enableService() const override;
|
|
|
|
|
bool clientGroupChecked() const override;
|
2024-08-08 17:41:59 +00:00
|
|
|
|
|
|
|
|
//
|
|
|
|
|
// Getters (new methods)
|
|
|
|
|
//
|
|
|
|
|
|
2024-07-22 16:48:02 +00:00
|
|
|
QString logDir() const;
|
|
|
|
|
bool serverGroupChecked() const;
|
|
|
|
|
bool useInternalConfig() const;
|
2024-07-26 22:53:52 +00:00
|
|
|
|
2024-08-08 17:41:59 +00:00
|
|
|
//
|
2024-08-09 08:08:29 +00:00
|
|
|
// Setters (overrides)
|
|
|
|
|
//
|
2024-08-12 15:39:18 +00:00
|
|
|
|
2024-08-09 08:08:29 +00:00
|
|
|
void setScreenName(const QString &s) override;
|
|
|
|
|
void setPort(int i) override;
|
|
|
|
|
void setNetworkInterface(const QString &s) override;
|
|
|
|
|
void setLogLevel(int i) override;
|
|
|
|
|
void setLogToFile(bool b) override;
|
|
|
|
|
void setLogFilename(const QString &s) override;
|
|
|
|
|
void setElevateMode(ElevateMode em) override;
|
|
|
|
|
void setInvertScrollDirection(bool b) override;
|
|
|
|
|
void setLanguageSync(bool b) override;
|
|
|
|
|
void setEnableService(bool enabled) override;
|
|
|
|
|
|
|
|
|
|
//
|
|
|
|
|
// Setters (new methods)
|
2024-08-08 17:41:59 +00:00
|
|
|
//
|
2024-07-22 16:48:02 +00:00
|
|
|
|
|
|
|
|
void setServerGroupChecked(bool);
|
|
|
|
|
void setUseExternalConfig(bool);
|
|
|
|
|
void setConfigFile(const QString &);
|
|
|
|
|
void setUseInternalConfig(bool);
|
|
|
|
|
void setClientGroupChecked(bool);
|
|
|
|
|
void setServerHostname(const QString &);
|
2024-07-02 19:07:06 +00:00
|
|
|
|
2024-07-22 16:48:02 +00:00
|
|
|
/// @brief Sets the user preference to load from SystemScope.
|
|
|
|
|
/// @param [in] value
|
|
|
|
|
/// True - This will set the variable and load the global scope
|
|
|
|
|
/// settings. False - This will set the variable and load the user
|
|
|
|
|
/// scope settings.
|
2024-08-09 08:08:29 +00:00
|
|
|
void setLoadFromSystemScope(bool value) override;
|
2024-07-02 19:07:06 +00:00
|
|
|
|
2024-08-01 00:13:01 +00:00
|
|
|
private:
|
|
|
|
|
static QString settingName(AppConfig::Setting name);
|
2024-08-03 00:17:29 +00:00
|
|
|
|
|
|
|
|
void recall();
|
|
|
|
|
void recallScreenName();
|
|
|
|
|
void recallElevateMode();
|
|
|
|
|
void recallFromAllScopes();
|
|
|
|
|
void recallFromCurrentScope();
|
2024-08-01 00:13:01 +00:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @brief Loads a setting if it exists, otherwise returns `std::nullopt`
|
|
|
|
|
*
|
|
|
|
|
* @param toType A function to convert the QVariant to the desired type.
|
|
|
|
|
*/
|
|
|
|
|
template <typename T>
|
2024-10-17 18:04:35 +00:00
|
|
|
std::optional<T> getFromCurrentScope(Setting name, std::function<T(const QVariant &)> toType) const;
|
2024-08-01 00:13:01 +00:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @brief Sets a setting if the value is not `std::nullopt`.
|
|
|
|
|
*/
|
2024-10-17 18:04:35 +00:00
|
|
|
template <typename T> void setInCurrentScope(Setting name, const std::optional<T> &value);
|
2024-07-02 19:07:06 +00:00
|
|
|
|
|
|
|
|
/// @brief Sets the value of a setting
|
|
|
|
|
/// @param [in] name The Setting to be saved
|
|
|
|
|
/// @param [in] value The Value to be saved
|
2024-10-17 18:04:35 +00:00
|
|
|
template <typename T> void setInCurrentScope(AppConfig::Setting name, T value);
|
2024-07-02 19:07:06 +00:00
|
|
|
|
|
|
|
|
/// @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
|
2024-08-03 00:17:29 +00:00
|
|
|
template <typename T> void saveToAllScopes(AppConfig::Setting name, T value);
|
|
|
|
|
|
2024-10-17 18:04:35 +00:00
|
|
|
QVariant getFromCurrentScope(AppConfig::Setting name, const QVariant &defaultValue = QVariant()) const;
|
2024-07-02 19:07:06 +00:00
|
|
|
|
2024-08-03 00:17:29 +00:00
|
|
|
/**
|
|
|
|
|
* @brief Finds a value by searching each scope starting with the current
|
|
|
|
|
* scope.
|
|
|
|
|
*/
|
2024-10-17 18:04:35 +00:00
|
|
|
QVariant findInAllScopes(AppConfig::Setting name, const QVariant &defaultValue = QVariant()) const;
|
2024-07-02 19:07:06 +00:00
|
|
|
|
|
|
|
|
/// @brief This method loads config from specified scope
|
|
|
|
|
/// @param [in] scope which should be loaded.
|
2024-08-07 14:05:18 +00:00
|
|
|
void loadScope(IConfigScopes::Scope scope);
|
2024-07-02 19:07:06 +00:00
|
|
|
|
2024-10-29 01:31:33 +00:00
|
|
|
// Used to make the server and client names on windows.
|
|
|
|
|
#ifdef Q_OS_WIN
|
|
|
|
|
inline static const auto s_winExeTemplate = QStringLiteral("%1.exe");
|
|
|
|
|
#endif
|
|
|
|
|
inline static const auto s_CoreServerName = QStringLiteral("deskflow-server");
|
2024-08-10 23:58:24 +00:00
|
|
|
static const char m_LogDir[];
|
|
|
|
|
|
|
|
|
|
/// @brief Contains the string values of the settings names that will be saved
|
|
|
|
|
static const char *const m_SettingsName[];
|
|
|
|
|
|
2024-10-28 19:20:35 +00:00
|
|
|
/// @brief Core config file extention (not the Qt settings filename)
|
|
|
|
|
#ifdef Q_OS_WIN
|
|
|
|
|
inline static const auto s_ConfigFileExt = QStringLiteral("sgc");
|
|
|
|
|
#else
|
|
|
|
|
inline static const auto s_ConfigFileExt = QStringLiteral("conf");
|
|
|
|
|
#endif
|
2024-08-10 23:58:24 +00:00
|
|
|
|
2024-07-22 16:48:02 +00:00
|
|
|
int m_Port = 24800;
|
|
|
|
|
QString m_Interface = "";
|
|
|
|
|
int m_LogLevel = 0;
|
|
|
|
|
bool m_LogToFile = false;
|
2024-09-17 19:00:25 +00:00
|
|
|
QString m_LogFilename = logDir() + deskflow::gui::kDefaultLogFile;
|
|
|
|
|
ElevateMode m_ElevateMode = deskflow::gui::kDefaultElevateMode;
|
2024-07-22 16:48:02 +00:00
|
|
|
bool m_InvertScrollDirection = false;
|
|
|
|
|
bool m_LanguageSync = true;
|
|
|
|
|
bool m_ServerGroupChecked = false;
|
|
|
|
|
bool m_UseExternalConfig = false;
|
2024-10-28 19:20:35 +00:00
|
|
|
QString m_ConfigFile = QStringLiteral("%1/%2.%3").arg(QDir::homePath(), kAppId, s_ConfigFileExt);
|
2024-07-22 16:48:02 +00:00
|
|
|
bool m_UseInternalConfig = false;
|
|
|
|
|
bool m_ClientGroupChecked = false;
|
|
|
|
|
QString m_ServerHostname = "";
|
2024-10-17 18:04:35 +00:00
|
|
|
bool m_EnableService = deskflow::gui::kDefaultProcessMode == ProcessMode::kService;
|
2024-08-03 00:17:29 +00:00
|
|
|
bool m_LoadFromSystemScope = false;
|
2024-07-22 16:48:02 +00:00
|
|
|
|
2024-09-17 19:00:25 +00:00
|
|
|
deskflow::gui::IConfigScopes &m_Scopes;
|
2024-08-10 23:58:24 +00:00
|
|
|
std::shared_ptr<Deps> m_pDeps;
|
|
|
|
|
QString m_ScreenName;
|
2024-07-22 16:48:02 +00:00
|
|
|
|
2024-07-02 19:07:06 +00:00
|
|
|
signals:
|
2024-07-26 22:53:52 +00:00
|
|
|
void screenNameChanged();
|
2025-02-07 17:02:20 +00:00
|
|
|
void logLevelChanged();
|
2012-06-10 16:50:54 +00:00
|
|
|
};
|