From cf819e0a3f5664fb0e1705217dbebbdf704bdc34 Mon Sep 17 00:00:00 2001 From: ShellCode33 Date: Sat, 4 Mar 2023 13:39:05 +0100 Subject: [PATCH] Allow specifying initial directory via the KPXC_INITIAL_DIR environment variable --- docs/topics/UserInterface.adoc | 1 + src/gui/DatabaseWidget.cpp | 6 +++--- src/gui/FileDialog.cpp | 20 +++++++++++++++----- src/gui/FileDialog.h | 2 +- 4 files changed, 20 insertions(+), 9 deletions(-) diff --git a/docs/topics/UserInterface.adoc b/docs/topics/UserInterface.adoc index dc7908e56..b60b28a66 100644 --- a/docs/topics/UserInterface.adoc +++ b/docs/topics/UserInterface.adoc @@ -85,6 +85,7 @@ Additionally, the following environment variables may be useful when running the |KPXC_CONFIG | Override default path to roaming configuration file |KPXC_CONFIG_LOCAL | Override default path to local configuration file +|KPXC_INITIAL_DIR | Override initial location picking for databases |SSH_AUTH_SOCKET | Path of the unix file socket that the agent uses for communication with other processes (SSH Agent) |QT_SCALE_FACTOR [numeric] | Defines a global scale factor for the whole application, including point-sized fonts. |QT_SCREEN_SCALE_FACTORS [list] | Specifies scale factors for each screen. See https://doc.qt.io/qt-5/highdpi.html#high-dpi-support-in-qt diff --git a/src/gui/DatabaseWidget.cpp b/src/gui/DatabaseWidget.cpp index 9bb0ac475..16392b9f4 100644 --- a/src/gui/DatabaseWidget.cpp +++ b/src/gui/DatabaseWidget.cpp @@ -2031,7 +2031,7 @@ bool DatabaseWidget::saveAs() if (!QFileInfo::exists(oldFilePath)) { QString defaultFileName = config()->get(Config::DefaultDatabaseFileName).toString(); oldFilePath = - QDir::toNativeSeparators(config()->get(Config::LastDir).toString() + "/" + QDir::toNativeSeparators(FileDialog::getLastDir("db") + "/" + (defaultFileName.isEmpty() ? tr("Passwords").append(".kdbx") : defaultFileName)); } const QString newFilePath = fileDialog()->getSaveFileName( @@ -2121,13 +2121,13 @@ bool DatabaseWidget::saveBackup() if (!QFileInfo::exists(oldFilePath)) { QString defaultFileName = config()->get(Config::DefaultDatabaseFileName).toString(); oldFilePath = QDir::toNativeSeparators( - config()->get(Config::LastDir).toString() + "/" + FileDialog::getLastDir("db") + "/" + (defaultFileName.isEmpty() ? tr("Passwords").append(".kdbx") : defaultFileName)); } const QString newFilePath = fileDialog()->getSaveFileName(this, tr("Save database backup"), - FileDialog::getLastDir("backup"), + FileDialog::getLastDir("backup", oldFilePath), tr("KeePass 2 Database").append(" (*.kdbx)"), nullptr, nullptr); diff --git a/src/gui/FileDialog.cpp b/src/gui/FileDialog.cpp index 84b19e260..96ee2b206 100644 --- a/src/gui/FileDialog.cpp +++ b/src/gui/FileDialog.cpp @@ -19,6 +19,8 @@ #include "core/Config.h" +#include + FileDialog* FileDialog::m_instance(nullptr); FileDialog::FileDialog() = default; @@ -35,7 +37,7 @@ QString FileDialog::getOpenFileName(QWidget* parent, m_nextFileName.clear(); return result; } else { - const auto& workingDir = dir.isEmpty() ? config()->get(Config::LastDir).toString() : dir; + const auto& workingDir = dir.isEmpty() ? getLastDir("default") : dir; const auto result = QDir::toNativeSeparators( QFileDialog::getOpenFileName(parent, caption, workingDir, filter, selectedFilter, options)); @@ -61,7 +63,7 @@ QStringList FileDialog::getOpenFileNames(QWidget* parent, m_nextFileNames.clear(); return results; } else { - const auto& workingDir = dir.isEmpty() ? config()->get(Config::LastDir).toString() : dir; + const auto& workingDir = dir.isEmpty() ? getLastDir("default") : dir; auto results = QFileDialog::getOpenFileNames(parent, caption, workingDir, filter, selectedFilter, options); for (auto& path : results) { @@ -90,7 +92,7 @@ QString FileDialog::getSaveFileName(QWidget* parent, m_nextFileName.clear(); return result; } else { - const auto& workingDir = dir.isEmpty() ? config()->get(Config::LastDir).toString() : dir; + const auto& workingDir = dir.isEmpty() ? getLastDir("default") : dir; const auto result = QDir::toNativeSeparators( QFileDialog::getSaveFileName(parent, caption, workingDir, filter, selectedFilter, options)); @@ -114,7 +116,7 @@ QString FileDialog::getExistingDirectory(QWidget* parent, m_nextDirName.clear(); return result; } else { - const auto& workingDir = dir.isEmpty() ? config()->get(Config::LastDir).toString() : dir; + const auto& workingDir = dir.isEmpty() ? getLastDir("default") : dir; const auto result = QDir::toNativeSeparators(QFileDialog::getExistingDirectory(parent, caption, workingDir, options)); @@ -158,7 +160,15 @@ void FileDialog::saveLastDir(const QString& role, const QString& path, bool sens QString FileDialog::getLastDir(const QString& role, const QString& defaultDir) { auto lastDirs = config()->get(Config::LastDir).toHash(); - return lastDirs.value(role, defaultDir).toString(); + auto fallbackDir = defaultDir; + + if (fallbackDir.isEmpty()) { + // Fallback to the environment variable, if it exists, otherwise use the home directory + const auto& env = QProcessEnvironment::systemEnvironment(); + fallbackDir = env.value("KPXC_INITIAL_DIR", QDir::homePath()); + } + + return lastDirs.value(role, fallbackDir).toString(); } FileDialog* FileDialog::instance() diff --git a/src/gui/FileDialog.h b/src/gui/FileDialog.h index fa36a7d3a..14913e429 100644 --- a/src/gui/FileDialog.h +++ b/src/gui/FileDialog.h @@ -56,7 +56,7 @@ public: void setNextDirectory(const QString& path); static void saveLastDir(const QString& role, const QString& path, bool sensitive = false); - static QString getLastDir(const QString& role, const QString& defaultDir = QDir::homePath()); + static QString getLastDir(const QString& role, const QString& defaultDir = QString()); static FileDialog* instance();