Add support for portable config settings (#645)

* Add support for portable config settings

* Use applicationDirPath instead of currentPath
This commit is contained in:
Jonathan White 2017-06-19 10:49:03 -04:00 committed by louib
parent b75b9fb7d6
commit 8d70167acf

View File

@ -60,39 +60,43 @@ Config::Config(const QString& fileName, QObject* parent)
Config::Config(QObject* parent) Config::Config(QObject* parent)
: QObject(parent) : QObject(parent)
{ {
// 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 userPath;
QString homePath = QDir::homePath(); QString homePath = QDir::homePath();
#if defined(Q_OS_UNIX) && !defined(Q_OS_MAC) #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 // we can't use QStandardPaths on X11 as it uses XDG_DATA_HOME instead of XDG_CONFIG_HOME
QByteArray env = qgetenv("XDG_CONFIG_HOME"); QByteArray env = qgetenv("XDG_CONFIG_HOME");
if (env.isEmpty()) { if (env.isEmpty()) {
userPath = homePath; userPath = homePath;
userPath += "/.config"; userPath += "/.config";
} } else if (env[0] == '/') {
else if (env[0] == '/') {
userPath = QFile::decodeName(env); userPath = QFile::decodeName(env);
} } else {
else {
userPath = homePath; userPath = homePath;
userPath += '/'; userPath += '/';
userPath += QFile::decodeName(env); userPath += QFile::decodeName(env);
} }
userPath += "/keepassxc/"; userPath += "/keepassxc/";
#else #else
userPath = QDir::fromNativeSeparators(QStandardPaths::writableLocation(QStandardPaths::DataLocation)); userPath = QDir::fromNativeSeparators(QStandardPaths::writableLocation(QStandardPaths::DataLocation));
// storageLocation() appends the application name ("/keepassxc") to the end // storageLocation() appends the application name ("/keepassxc") to the end
userPath += "/"; userPath += "/";
#endif #endif
#ifdef QT_DEBUG #ifdef QT_DEBUG
userPath += "keepassxc_debug.ini"; userPath += "keepassxc_debug.ini";
#else #else
userPath += "keepassxc.ini"; userPath += "keepassxc.ini";
#endif #endif
init(userPath); init(userPath);
}
} }
Config::~Config() Config::~Config()