From 8d70167acfb62b3a88701b73b2de7b439fe5c4e1 Mon Sep 17 00:00:00 2001 From: Jonathan White Date: Mon, 19 Jun 2017 10:49:03 -0400 Subject: [PATCH] Add support for portable config settings (#645) * Add support for portable config settings * Use applicationDirPath instead of currentPath --- src/core/Config.cpp | 66 ++++++++++++++++++++++++--------------------- 1 file changed, 35 insertions(+), 31 deletions(-) diff --git a/src/core/Config.cpp b/src/core/Config.cpp index c8d23b361..013d148d7 100644 --- a/src/core/Config.cpp +++ b/src/core/Config.cpp @@ -60,39 +60,43 @@ Config::Config(const QString& fileName, QObject* parent) Config::Config(QObject* parent) : QObject(parent) { - QString userPath; - QString homePath = QDir::homePath(); + // Check if portable config is present. If not, find it in user's directory + QString portablePath = QCoreApplication::applicationDirPath() + "/keepassxc.ini"; + if (QFile::exists(portablePath)) { + init(portablePath); + } else { + QString userPath; + QString homePath = QDir::homePath(); -#if defined(Q_OS_UNIX) && !defined(Q_OS_MAC) - // we can't use QStandardPaths on X11 as it uses XDG_DATA_HOME instead of XDG_CONFIG_HOME - QByteArray env = qgetenv("XDG_CONFIG_HOME"); - if (env.isEmpty()) { - userPath = homePath; - userPath += "/.config"; + #if defined(Q_OS_UNIX) && !defined(Q_OS_MAC) + // we can't use QStandardPaths on X11 as it uses XDG_DATA_HOME instead of XDG_CONFIG_HOME + QByteArray env = qgetenv("XDG_CONFIG_HOME"); + if (env.isEmpty()) { + userPath = homePath; + userPath += "/.config"; + } else if (env[0] == '/') { + userPath = QFile::decodeName(env); + } else { + userPath = homePath; + userPath += '/'; + userPath += QFile::decodeName(env); + } + + userPath += "/keepassxc/"; + #else + userPath = QDir::fromNativeSeparators(QStandardPaths::writableLocation(QStandardPaths::DataLocation)); + // storageLocation() appends the application name ("/keepassxc") to the end + userPath += "/"; + #endif + + #ifdef QT_DEBUG + userPath += "keepassxc_debug.ini"; + #else + userPath += "keepassxc.ini"; + #endif + + init(userPath); } - else if (env[0] == '/') { - userPath = QFile::decodeName(env); - } - else { - userPath = homePath; - userPath += '/'; - userPath += QFile::decodeName(env); - } - - userPath += "/keepassxc/"; -#else - userPath = QDir::fromNativeSeparators(QStandardPaths::writableLocation(QStandardPaths::DataLocation)); - // storageLocation() appends the application name ("/keepassxc") to the end - userPath += "/"; -#endif - -#ifdef QT_DEBUG - userPath += "keepassxc_debug.ini"; -#else - userPath += "keepassxc.ini"; -#endif - - init(userPath); } Config::~Config()