From f50e4e850b69528414981d9c06951fc5b0ee1770 Mon Sep 17 00:00:00 2001 From: sithlord48 Date: Wed, 12 Mar 2025 22:02:48 -0400 Subject: [PATCH] refactor: QSettingsProxy use Settings paths for user and system paths --- .../deskflow-gui/dialogs/SettingsDialog.cpp | 1 + src/lib/common/QSettingsProxy.cpp | 127 +----------------- 2 files changed, 5 insertions(+), 123 deletions(-) diff --git a/src/apps/deskflow-gui/dialogs/SettingsDialog.cpp b/src/apps/deskflow-gui/dialogs/SettingsDialog.cpp index 4e476f9f9..386365df2 100644 --- a/src/apps/deskflow-gui/dialogs/SettingsDialog.cpp +++ b/src/apps/deskflow-gui/dialogs/SettingsDialog.cpp @@ -57,6 +57,7 @@ SettingsDialog::SettingsDialog(QWidget *parent, const IServerConfig &serverConfi void SettingsDialog::initConnections() { connect(this, &SettingsDialog::shown, this, &SettingsDialog::showReadOnlyMessage, Qt::QueuedConnection); + connect(Settings::instance(), &Settings::writableChanged, this, &SettingsDialog::showReadOnlyMessage); connect(ui->buttonBox, &QDialogButtonBox::accepted, this, &SettingsDialog::accept); connect(ui->buttonBox, &QDialogButtonBox::rejected, this, &SettingsDialog::reject); diff --git a/src/lib/common/QSettingsProxy.cpp b/src/lib/common/QSettingsProxy.cpp index 0267db3ee..635b69829 100644 --- a/src/lib/common/QSettingsProxy.cpp +++ b/src/lib/common/QSettingsProxy.cpp @@ -6,24 +6,7 @@ #include "QSettingsProxy.h" -#include "common/constants.h" - -#include -#include -#include -#include -#include - -const auto kLegacyOrgDomain = "http-symless-com"; -const auto kLegacySystemConfigFilename = "SystemConfig.ini"; - -#if defined(Q_OS_UNIX) -const auto kUnixSystemConfigPath = "/usr/local/etc/"; -#endif - -// -// Free functions -// +#include "common/Settings.h" /** * @brief The base dir for the system settings file. @@ -33,79 +16,7 @@ const auto kUnixSystemConfigPath = "/usr/local/etc/"; */ QString getSystemSettingsBaseDir() { -#if defined(Q_OS_WIN) - return QCoreApplication::applicationDirPath(); -#elif defined(Q_OS_UNIX) - // Qt already adds application and filename to the end of the path. - // On macOS, it would be nice to use /Library dir, but qt has no elevate - // system. - return kUnixSystemConfigPath; -#else -#error "unsupported platform" -#endif -} - -void migrateLegacySystemSettings(QSettings &settings) -{ - if (QFile(settings.fileName()).exists()) { - qDebug("system settings already exist, skipping migration"); - return; - } - - QSettings::setPath(QSettings::IniFormat, QSettings::SystemScope, kLegacySystemConfigFilename); - QSettings oldSystemSettings( - QSettings::IniFormat, QSettings::SystemScope, QCoreApplication::organizationName(), - QCoreApplication::applicationName() - ); - - if (QFile(oldSystemSettings.fileName()).exists()) { - const auto keys = oldSystemSettings.allKeys(); - for (const auto &key : keys) { - settings.setValue(key, oldSystemSettings.value(key)); - } - } - - QSettings::setPath(QSettings::IniFormat, QSettings::SystemScope, getSystemSettingsBaseDir()); -} - -void migrateLegacyUserSettings(QSettings &newSettings) -{ - QString newPath = newSettings.fileName(); - QFile newFile(newPath); - if (newFile.exists()) { - qInfo("user settings already exist, skipping migration"); - return; - } - - QSettings oldSettings(kLegacyOrgDomain, kAppName); - QString oldPath = oldSettings.fileName(); - - if (oldPath.isEmpty()) { - qInfo("no legacy settings to migrate, filename empty"); - return; - } - - if (!QFile(oldPath).exists()) { - qInfo("no legacy settings to migrate, file does not exist"); - return; - } - - QFileInfo oldFileInfo(oldPath); - QFileInfo newFileInfo(newPath); - - qDebug( - "migrating legacy settings: '%s' -> '%s'", // - qPrintable(oldFileInfo.fileName()), qPrintable(newFileInfo.fileName()) - ); - - QStringList keys = oldSettings.allKeys(); - for (const QString &key : std::as_const(keys)) { - QVariant oldValue = oldSettings.value(key); - newSettings.setValue(key, oldValue); - qInfo().noquote() << QStringLiteral("migrating setting '%1' = '%2'").arg(key, oldValue.toString()); - } - - newSettings.sync(); + return Settings::SystemDir; } // @@ -114,42 +25,12 @@ void migrateLegacyUserSettings(QSettings &newSettings) void QSettingsProxy::loadUser() { - m_pSettings = std::make_unique(); - -#if defined(Q_OS_MAC) - // on mac, we used to save settings to "com.http-symless-com.Deskflow.plist" - // because `setOrganizationName` was historically called using a url instead - // of an actual domain (e.g. deskflow.org). - migrateLegacyUserSettings(*m_pSettings); -#endif // Q_OS_MAC + m_pSettings = std::make_unique(Settings::UserSettingFile, QSettings::IniFormat); } void QSettingsProxy::loadSystem() { - auto orgName = QCoreApplication::organizationName(); - if (orgName.isEmpty()) { - qFatal("unable to load config, organization name is empty"); - return; - } else { - qDebug() << "org name for config:" << orgName; - } - - auto appName = QCoreApplication::applicationName(); - if (appName.isEmpty()) { - qFatal("unable to load config, application name is empty"); - return; - } else { - qDebug() << "app name for config:" << appName; - } - - QSettings::setPath(QSettings::Format::IniFormat, QSettings::Scope::SystemScope, getSystemSettingsBaseDir()); - - m_pSettings = - std::make_unique(QSettings::Format::IniFormat, QSettings::Scope::SystemScope, orgName, appName); - -#if defined(Q_OS_WIN) - migrateLegacySystemSettings(*m_pSettings); -#endif // Q_OS_WIN + m_pSettings = std::make_unique(Settings::SystemSettingFile, QSettings::IniFormat); } int QSettingsProxy::beginReadArray(const QString &prefix)