Skip to content

Commit

Permalink
Allow specifying initial directory via the KPXC_INITIAL_DIR environme…
Browse files Browse the repository at this point in the history
…nt variable
  • Loading branch information
ShellCode33 committed Mar 4, 2023
1 parent 5b31288 commit 46a242a
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 6 deletions.
1 change: 1 addition & 0 deletions docs/topics/UserInterface.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 3 additions & 3 deletions src/gui/DatabaseWidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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);
Expand Down
14 changes: 13 additions & 1 deletion src/gui/FileDialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@

#include "core/Config.h"

#include <qprocess.h>

FileDialog* FileDialog::m_instance(nullptr);

FileDialog::FileDialog() = default;
Expand Down Expand Up @@ -158,7 +160,17 @@ 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()) {
// If defaultDir is not explicitly given a as parameter,
// fallback to the environment variable if it exists,
// use the home directory otherwise.
const auto& env = QProcessEnvironment::systemEnvironment();
fallbackDir = env.value("KPXC_INITIAL_DIR", QDir::homePath());
}

return lastDirs.value(role, fallbackDir).toString();
}

FileDialog* FileDialog::instance()
Expand Down
2 changes: 1 addition & 1 deletion src/gui/FileDialog.h
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ class FileDialog
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();

Expand Down
2 changes: 1 addition & 1 deletion src/gui/styles/base/BaseStyle.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1936,7 +1936,7 @@ void BaseStyle::drawPrimitive(PrimitiveElement elem,
auto fropt = qstyleoption_cast<const QStyleOptionFocusRect*>(option);
if (!fropt)
break;
//### check for d->alt_down
// ### check for d->alt_down
if (!(fropt->state & State_KeyboardFocusChange))
return;
if (fropt->state & State_Item) {
Expand Down

0 comments on commit 46a242a

Please sign in to comment.