refactor: QSettingsProxy use Settings paths for user and system paths
This commit is contained in:
parent
3cd3d7b1ff
commit
f50e4e850b
2 changed files with 5 additions and 123 deletions
|
|
@ -57,6 +57,7 @@ SettingsDialog::SettingsDialog(QWidget *parent, const IServerConfig &serverConfi
|
||||||
void SettingsDialog::initConnections()
|
void SettingsDialog::initConnections()
|
||||||
{
|
{
|
||||||
connect(this, &SettingsDialog::shown, this, &SettingsDialog::showReadOnlyMessage, Qt::QueuedConnection);
|
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::accepted, this, &SettingsDialog::accept);
|
||||||
connect(ui->buttonBox, &QDialogButtonBox::rejected, this, &SettingsDialog::reject);
|
connect(ui->buttonBox, &QDialogButtonBox::rejected, this, &SettingsDialog::reject);
|
||||||
|
|
|
||||||
|
|
@ -6,24 +6,7 @@
|
||||||
|
|
||||||
#include "QSettingsProxy.h"
|
#include "QSettingsProxy.h"
|
||||||
|
|
||||||
#include "common/constants.h"
|
#include "common/Settings.h"
|
||||||
|
|
||||||
#include <QCoreApplication>
|
|
||||||
#include <QDir>
|
|
||||||
#include <QFile>
|
|
||||||
#include <QSettings>
|
|
||||||
#include <memory>
|
|
||||||
|
|
||||||
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
|
|
||||||
//
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief The base dir for the system settings file.
|
* @brief The base dir for the system settings file.
|
||||||
|
|
@ -33,79 +16,7 @@ const auto kUnixSystemConfigPath = "/usr/local/etc/";
|
||||||
*/
|
*/
|
||||||
QString getSystemSettingsBaseDir()
|
QString getSystemSettingsBaseDir()
|
||||||
{
|
{
|
||||||
#if defined(Q_OS_WIN)
|
return Settings::SystemDir;
|
||||||
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();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
//
|
//
|
||||||
|
|
@ -114,42 +25,12 @@ void migrateLegacyUserSettings(QSettings &newSettings)
|
||||||
|
|
||||||
void QSettingsProxy::loadUser()
|
void QSettingsProxy::loadUser()
|
||||||
{
|
{
|
||||||
m_pSettings = std::make_unique<QSettings>();
|
m_pSettings = std::make_unique<QSettings>(Settings::UserSettingFile, QSettings::IniFormat);
|
||||||
|
|
||||||
#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
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void QSettingsProxy::loadSystem()
|
void QSettingsProxy::loadSystem()
|
||||||
{
|
{
|
||||||
auto orgName = QCoreApplication::organizationName();
|
m_pSettings = std::make_unique<QSettings>(Settings::SystemSettingFile, QSettings::IniFormat);
|
||||||
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>(QSettings::Format::IniFormat, QSettings::Scope::SystemScope, orgName, appName);
|
|
||||||
|
|
||||||
#if defined(Q_OS_WIN)
|
|
||||||
migrateLegacySystemSettings(*m_pSettings);
|
|
||||||
#endif // Q_OS_WIN
|
|
||||||
}
|
}
|
||||||
|
|
||||||
int QSettingsProxy::beginReadArray(const QString &prefix)
|
int QSettingsProxy::beginReadArray(const QString &prefix)
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue