diff options
Diffstat (limited to 'src/core/Config.cpp')
-rw-r--r-- | src/core/Config.cpp | 91 |
1 files changed, 52 insertions, 39 deletions
diff --git a/src/core/Config.cpp b/src/core/Config.cpp index afb71f534..4c00dabbc 100644 --- a/src/core/Config.cpp +++ b/src/core/Config.cpp @@ -22,6 +22,7 @@ #include <QCoreApplication> #include <QDir> #include <QHash> +#include <QProcessEnvironment> #include <QSettings> #include <QSize> #include <QStandardPaths> @@ -98,6 +99,7 @@ static const QHash<Config::ConfigKey, ConfigDirective> configStrings = { {Config::GUI_MinimizeOnStartup, {QS("GUI/MinimizeOnStartup"), Roaming, false}}, {Config::GUI_MinimizeOnClose, {QS("GUI/MinimizeOnClose"), Roaming, false}}, {Config::GUI_HideUsernames, {QS("GUI/HideUsernames"), Roaming, false}}, + {Config::GUI_HidePasswords, {QS("GUI/HidePasswords"), Roaming, true}}, {Config::GUI_AdvancedSettings, {QS("GUI/AdvancedSettings"), Roaming, false}}, {Config::GUI_MonospaceNotes, {QS("GUI/MonospaceNotes"), Roaming, false}}, {Config::GUI_ApplicationTheme, {QS("GUI/ApplicationTheme"), Roaming, QS("auto")}}, @@ -418,49 +420,17 @@ void Config::migrate() sync(); } -Config::Config(const QString& fileName, QObject* parent) +Config::Config(const QString& configFileName, const QString& localConfigFileName, QObject* parent) : QObject(parent) { - init(fileName); + init(configFileName, localConfigFileName); } Config::Config(QObject* parent) : QObject(parent) { - // Check if we are running in portable mode, if so store the config files local to the app - auto portablePath = QCoreApplication::applicationDirPath().append("/%1"); - if (QFile::exists(portablePath.arg(".portable"))) { - init(portablePath.arg("config/keepassxc.ini"), portablePath.arg("config/keepassxc_local.ini")); - return; - } - - QString configPath; - QString localConfigPath; - -#if defined(Q_OS_WIN) - configPath = QStandardPaths::writableLocation(QStandardPaths::AppDataLocation); - localConfigPath = QStandardPaths::writableLocation(QStandardPaths::AppLocalDataLocation); -#elif defined(Q_OS_MACOS) - configPath = QStandardPaths::writableLocation(QStandardPaths::AppDataLocation); - localConfigPath = QStandardPaths::writableLocation(QStandardPaths::CacheLocation); -#else - // On case-sensitive Operating Systems, force use of lowercase app directories - configPath = QStandardPaths::writableLocation(QStandardPaths::GenericConfigLocation) + "/keepassxc"; - localConfigPath = QStandardPaths::writableLocation(QStandardPaths::GenericCacheLocation) + "/keepassxc"; -#endif - - configPath += "/keepassxc"; - localConfigPath += "/keepassxc"; - -#ifdef QT_DEBUG - configPath += "_debug"; - localConfigPath += "_debug"; -#endif - - configPath += ".ini"; - localConfigPath += ".ini"; - - init(QDir::toNativeSeparators(configPath), QDir::toNativeSeparators(localConfigPath)); + auto configFiles = defaultConfigFiles(); + init(configFiles.first, configFiles.second); } Config::~Config() @@ -488,6 +458,45 @@ void Config::init(const QString& configFileName, const QString& localConfigFileN connect(qApp, &QCoreApplication::aboutToQuit, this, &Config::sync); } +QPair<QString, QString> Config::defaultConfigFiles() +{ + // Check if we are running in portable mode, if so store the config files local to the app + auto portablePath = QCoreApplication::applicationDirPath().append("/%1"); + if (QFile::exists(portablePath.arg(".portable"))) { + return {portablePath.arg("config/keepassxc.ini"), portablePath.arg("config/keepassxc_local.ini")}; + } + + QString configPath; + QString localConfigPath; + +#if defined(Q_OS_WIN) + configPath = QStandardPaths::writableLocation(QStandardPaths::AppDataLocation); + localConfigPath = QStandardPaths::writableLocation(QStandardPaths::AppLocalDataLocation); +#elif defined(Q_OS_MACOS) + configPath = QStandardPaths::writableLocation(QStandardPaths::AppDataLocation); + localConfigPath = QStandardPaths::writableLocation(QStandardPaths::CacheLocation); +#else + // On case-sensitive Operating Systems, force use of lowercase app directories + configPath = QStandardPaths::writableLocation(QStandardPaths::GenericConfigLocation) + "/keepassxc"; + localConfigPath = QStandardPaths::writableLocation(QStandardPaths::GenericCacheLocation) + "/keepassxc"; +#endif + + QString suffix; +#ifdef QT_DEBUG + suffix = "_debug"; +#endif + + configPath += QString("/keepassxc%1.ini").arg(suffix); + localConfigPath += QString("/keepassxc%1.ini").arg(suffix); + + // Allow overriding the default location with env vars + const auto& env = QProcessEnvironment::systemEnvironment(); + configPath = env.value("KPXC_CONFIG", configPath); + localConfigPath = env.value("KPXC_CONFIG_LOCAL", localConfigPath); + + return {QDir::toNativeSeparators(configPath), QDir::toNativeSeparators(localConfigPath)}; +} + Config* Config::instance() { if (!m_instance) { @@ -497,12 +506,16 @@ Config* Config::instance() return m_instance; } -void Config::createConfigFromFile(const QString& file) +void Config::createConfigFromFile(const QString& configFileName, const QString& localConfigFileName) { if (m_instance) { delete m_instance; } - m_instance = new Config(file, qApp); + + auto defaultFiles = defaultConfigFiles(); + m_instance = new Config(configFileName.isEmpty() ? defaultFiles.first : configFileName, + localConfigFileName.isEmpty() ? defaultFiles.second : localConfigFileName, + qApp); } void Config::createTempFileInstance() @@ -514,7 +527,7 @@ void Config::createTempFileInstance() bool openResult = tmpFile->open(); Q_ASSERT(openResult); Q_UNUSED(openResult); - m_instance = new Config(tmpFile->fileName(), qApp); + m_instance = new Config(tmpFile->fileName(), "", qApp); tmpFile->setParent(m_instance); } |