Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Add support for portable config settings #645

Merged
merged 3 commits into from
Jun 19, 2017
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
68 changes: 36 additions & 32 deletions src/core/Config.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,39 +60,43 @@ Config::Config(const QString& fileName, QObject* parent)
Config::Config(QObject* parent)
: QObject(parent)
{
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";
// 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";
} 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()
Expand Down