Allow specifying initial directory via the KPXC_INITIAL_DIR environment variable

This commit is contained in:
ShellCode33 2023-03-04 13:39:05 +01:00 committed by Jonathan White
parent 6c9078c870
commit 64281b508b
No known key found for this signature in database
GPG Key ID: 440FC65F2E0C6E01
4 changed files with 20 additions and 9 deletions

View File

@ -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 | Override default path to roaming configuration file
|KPXC_CONFIG_LOCAL | Override default path to local 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) |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_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 |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

View File

@ -2031,7 +2031,7 @@ bool DatabaseWidget::saveAs()
if (!QFileInfo::exists(oldFilePath)) { if (!QFileInfo::exists(oldFilePath)) {
QString defaultFileName = config()->get(Config::DefaultDatabaseFileName).toString(); QString defaultFileName = config()->get(Config::DefaultDatabaseFileName).toString();
oldFilePath = oldFilePath =
QDir::toNativeSeparators(config()->get(Config::LastDir).toString() + "/" QDir::toNativeSeparators(FileDialog::getLastDir("db") + "/"
+ (defaultFileName.isEmpty() ? tr("Passwords").append(".kdbx") : defaultFileName)); + (defaultFileName.isEmpty() ? tr("Passwords").append(".kdbx") : defaultFileName));
} }
const QString newFilePath = fileDialog()->getSaveFileName( const QString newFilePath = fileDialog()->getSaveFileName(
@ -2121,13 +2121,13 @@ bool DatabaseWidget::saveBackup()
if (!QFileInfo::exists(oldFilePath)) { if (!QFileInfo::exists(oldFilePath)) {
QString defaultFileName = config()->get(Config::DefaultDatabaseFileName).toString(); QString defaultFileName = config()->get(Config::DefaultDatabaseFileName).toString();
oldFilePath = QDir::toNativeSeparators( oldFilePath = QDir::toNativeSeparators(
config()->get(Config::LastDir).toString() + "/" FileDialog::getLastDir("db") + "/"
+ (defaultFileName.isEmpty() ? tr("Passwords").append(".kdbx") : defaultFileName)); + (defaultFileName.isEmpty() ? tr("Passwords").append(".kdbx") : defaultFileName));
} }
const QString newFilePath = fileDialog()->getSaveFileName(this, const QString newFilePath = fileDialog()->getSaveFileName(this,
tr("Save database backup"), tr("Save database backup"),
FileDialog::getLastDir("backup"), FileDialog::getLastDir("backup", oldFilePath),
tr("KeePass 2 Database").append(" (*.kdbx)"), tr("KeePass 2 Database").append(" (*.kdbx)"),
nullptr, nullptr,
nullptr); nullptr);

View File

@ -19,6 +19,8 @@
#include "core/Config.h" #include "core/Config.h"
#include <QProcessEnvironment>
FileDialog* FileDialog::m_instance(nullptr); FileDialog* FileDialog::m_instance(nullptr);
FileDialog::FileDialog() FileDialog::FileDialog()
@ -37,7 +39,7 @@ QString FileDialog::getOpenFileName(QWidget* parent,
m_nextFileName.clear(); m_nextFileName.clear();
return result; return result;
} else { } 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( const auto result = QDir::toNativeSeparators(
QFileDialog::getOpenFileName(parent, caption, workingDir, filter, selectedFilter, options)); QFileDialog::getOpenFileName(parent, caption, workingDir, filter, selectedFilter, options));
@ -63,7 +65,7 @@ QStringList FileDialog::getOpenFileNames(QWidget* parent,
m_nextFileNames.clear(); m_nextFileNames.clear();
return results; return results;
} else { } 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); auto results = QFileDialog::getOpenFileNames(parent, caption, workingDir, filter, selectedFilter, options);
for (auto& path : results) { for (auto& path : results) {
@ -92,7 +94,7 @@ QString FileDialog::getSaveFileName(QWidget* parent,
m_nextFileName.clear(); m_nextFileName.clear();
return result; return result;
} else { } 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( const auto result = QDir::toNativeSeparators(
QFileDialog::getSaveFileName(parent, caption, workingDir, filter, selectedFilter, options)); QFileDialog::getSaveFileName(parent, caption, workingDir, filter, selectedFilter, options));
@ -116,7 +118,7 @@ QString FileDialog::getExistingDirectory(QWidget* parent,
m_nextDirName.clear(); m_nextDirName.clear();
return result; return result;
} else { } else {
const auto& workingDir = dir.isEmpty() ? config()->get(Config::LastDir).toString() : dir; const auto& workingDir = dir.isEmpty() ? getLastDir("default") : dir;
const auto result = const auto result =
QDir::toNativeSeparators(QFileDialog::getExistingDirectory(parent, caption, workingDir, options)); QDir::toNativeSeparators(QFileDialog::getExistingDirectory(parent, caption, workingDir, options));
@ -160,7 +162,15 @@ void FileDialog::saveLastDir(const QString& role, const QString& path, bool sens
QString FileDialog::getLastDir(const QString& role, const QString& defaultDir) QString FileDialog::getLastDir(const QString& role, const QString& defaultDir)
{ {
auto lastDirs = config()->get(Config::LastDir).toHash(); 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() FileDialog* FileDialog::instance()

View File

@ -56,7 +56,7 @@ public:
void setNextDirectory(const QString& path); void setNextDirectory(const QString& path);
static void saveLastDir(const QString& role, const QString& path, bool sensitive = false); 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(); static FileDialog* instance();