Skip to content

Commit

Permalink
Improve File Dialog
Browse files Browse the repository at this point in the history
* QFileDialog returns UNIX paths, even on Windows. This patch converts what QFileDialog returns to the native path format.

* Improve const correctness

* Avoid imposing file extension on Linux

* This patch improves things like unneeded passes by values, missing const qualifiers, ugly copies because of variable reuse and consistency in variable names.
  • Loading branch information
Gianluca Recchia authored and droidmonkey committed Aug 31, 2019
1 parent c12fd36 commit fccbb98
Show file tree
Hide file tree
Showing 6 changed files with 70 additions and 159 deletions.
13 changes: 4 additions & 9 deletions src/gui/DatabaseTabWidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -394,8 +394,8 @@ void DatabaseTabWidget::exportToCsv()
return;
}

QString fileName = fileDialog()->getSaveFileName(
this, tr("Export database to CSV file"), QString(), tr("CSV file").append(" (*.csv)"), nullptr, nullptr, "csv");
const QString fileName = fileDialog()->getSaveFileName(
this, tr("Export database to CSV file"), QString(), tr("CSV file").append(" (*.csv)"), nullptr, nullptr);
if (fileName.isEmpty()) {
return;
}
Expand All @@ -419,13 +419,8 @@ void DatabaseTabWidget::exportToHtml()
return;
}

QString fileName = fileDialog()->getSaveFileName(this,
tr("Export database to HTML file"),
QString(),
tr("HTML file").append(" (*.html)"),
nullptr,
nullptr,
"html");
const QString fileName = fileDialog()->getSaveFileName(
this, tr("Export database to HTML file"), QString(), tr("HTML file").append(" (*.html)"), nullptr, nullptr);
if (fileName.isEmpty()) {
return;
}
Expand Down
9 changes: 2 additions & 7 deletions src/gui/DatabaseWidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1656,13 +1656,8 @@ bool DatabaseWidget::saveAs()
oldFilePath = QDir::toNativeSeparators(config()->get("LastDir", QDir::homePath()).toString() + "/"
+ tr("Passwords").append(".kdbx"));
}
QString newFilePath = fileDialog()->getSaveFileName(this,
tr("Save database as"),
oldFilePath,
tr("KeePass 2 Database").append(" (*.kdbx)"),
nullptr,
nullptr,
"kdbx");
const QString newFilePath = fileDialog()->getSaveFileName(
this, tr("Save database as"), oldFilePath, tr("KeePass 2 Database").append(" (*.kdbx)"), nullptr, nullptr);

if (!newFilePath.isEmpty()) {
// Ensure we don't recurse back into this function
Expand Down
151 changes: 47 additions & 104 deletions src/gui/FileDialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,59 +19,61 @@

#include "core/Config.h"

#include <QDir>

FileDialog* FileDialog::m_instance(nullptr);

QString FileDialog::getOpenFileName(QWidget* parent,
const QString& caption,
QString dir,
const QString& dir,
const QString& filter,
QString* selectedFilter,
QFileDialog::Options options)
const QFileDialog::Options options)
{
if (!m_nextFileName.isEmpty()) {
QString result = m_nextFileName;
const QString result = m_nextFileName;
m_nextFileName.clear();
return result;
} else {
if (dir.isEmpty()) {
dir = config()->get("LastDir").toString();
}

QString result = QFileDialog::getOpenFileName(parent, caption, dir, filter, selectedFilter, options);
const auto& workingDir = dir.isEmpty() ? config()->get("LastDir").toString() : dir;
const auto result = QDir::toNativeSeparators(
QFileDialog::getOpenFileName(parent, caption, workingDir, filter, selectedFilter, options));

#ifdef Q_OS_MACOS
// on Mac OS X the focus is lost after closing the native dialog
if (parent) {
parent->activateWindow();
}

#endif
saveLastDir(result);
return result;
}
}

QStringList FileDialog::getOpenFileNames(QWidget* parent,
const QString& caption,
QString dir,
const QString& dir,
const QString& filter,
QString* selectedFilter,
QFileDialog::Options options)
const QFileDialog::Options options)
{
if (!m_nextFileNames.isEmpty()) {
QStringList results = m_nextFileNames;
const QStringList results = m_nextFileNames;
m_nextFileNames.clear();
return results;
} else {
if (dir.isEmpty()) {
dir = config()->get("LastDir").toString();
}
const auto& workingDir = dir.isEmpty() ? config()->get("LastDir").toString() : dir;
auto results = QFileDialog::getOpenFileNames(parent, caption, workingDir, filter, selectedFilter, options);

QStringList results = QFileDialog::getOpenFileNames(parent, caption, dir, filter, selectedFilter, options);
for (auto& path : results)
path = QDir::toNativeSeparators(path);

#ifdef Q_OS_MACOS
// on Mac OS X the focus is lost after closing the native dialog
if (parent) {
parent->activateWindow();
}

#endif
if (!results.isEmpty()) {
saveLastDir(results[0]);
}
Expand All @@ -81,139 +83,80 @@ QStringList FileDialog::getOpenFileNames(QWidget* parent,

QString FileDialog::getFileName(QWidget* parent,
const QString& caption,
QString dir,
const QString& dir,
const QString& filter,
QString* selectedFilter,
QFileDialog::Options options,
const QString& defaultExtension,
const QString& defaultName)
const QFileDialog::Options options)
{
if (!m_nextFileName.isEmpty()) {
QString result = m_nextFileName;
const QString result = m_nextFileName;
m_nextFileName.clear();
return result;
} else {
if (dir.isEmpty()) {
dir = config()->get("LastDir").toString();
}

QString result;
#if defined(Q_OS_MAC) || defined(Q_OS_WIN)
Q_UNUSED(defaultName);
Q_UNUSED(defaultExtension);
// the native dialogs on these platforms already append the file extension
result = QFileDialog::getSaveFileName(parent, caption, dir, filter, selectedFilter, options);
#else
QFileDialog dialog(parent, caption, dir, filter);
dialog.setFileMode(QFileDialog::AnyFile);
dialog.setAcceptMode(QFileDialog::AcceptSave);
if (selectedFilter) {
dialog.selectNameFilter(*selectedFilter);
}
if (!defaultName.isEmpty()) {
dialog.selectFile(defaultName);
}
dialog.setOptions(options);
if (!defaultExtension.isEmpty()) {
dialog.setDefaultSuffix(defaultExtension);
}
dialog.setLabelText(QFileDialog::Accept, QFileDialog::tr("Select"));
QStringList results;
if (dialog.exec()) {
results = dialog.selectedFiles();
if (!results.isEmpty()) {
result = results[0];
}
}
#endif
const auto& workingDir = dir.isEmpty() ? config()->get("LastDir").toString() : dir;
const auto result = QDir::toNativeSeparators(
QFileDialog::getSaveFileName(parent, caption, workingDir, filter, selectedFilter, options));

#ifdef Q_OS_MACOS
// on Mac OS X the focus is lost after closing the native dialog
if (parent) {
parent->activateWindow();
}

#endif
saveLastDir(result);
return result;
}
}

QString FileDialog::getSaveFileName(QWidget* parent,
const QString& caption,
QString dir,
const QString& dir,
const QString& filter,
QString* selectedFilter,
QFileDialog::Options options,
const QString& defaultExtension,
const QString& defaultName)
const QFileDialog::Options options)
{
if (!m_nextFileName.isEmpty()) {
QString result = m_nextFileName;
const QString result = m_nextFileName;
m_nextFileName.clear();
return result;
} else {
if (dir.isEmpty()) {
dir = config()->get("LastDir").toString();
}

QString result;
#if defined(Q_OS_MACOS) || defined(Q_OS_WIN)
Q_UNUSED(defaultName);
Q_UNUSED(defaultExtension);
// the native dialogs on these platforms already append the file extension
result = QFileDialog::getSaveFileName(parent, caption, dir, filter, selectedFilter, options);
#else
QFileDialog dialog(parent, caption, dir, filter);
dialog.setAcceptMode(QFileDialog::AcceptSave);
dialog.setFileMode(QFileDialog::AnyFile);
if (selectedFilter) {
dialog.selectNameFilter(*selectedFilter);
}
if (!defaultName.isEmpty()) {
dialog.selectFile(defaultName);
}
dialog.setOptions(options);
dialog.setDefaultSuffix(defaultExtension);

QStringList results;
if (dialog.exec()) {
results = dialog.selectedFiles();
if (!results.isEmpty()) {
result = results[0];
}
}
#endif
const auto& workingDir = dir.isEmpty() ? config()->get("LastDir").toString() : dir;
const auto result = QDir::toNativeSeparators(
QFileDialog::getSaveFileName(parent, caption, workingDir, filter, selectedFilter, options));

#ifdef Q_OS_MACOS
// on Mac OS X the focus is lost after closing the native dialog
if (parent) {
parent->activateWindow();
}

#endif
saveLastDir(result);
return result;
}
}

QString
FileDialog::getExistingDirectory(QWidget* parent, const QString& caption, QString dir, QFileDialog::Options options)
QString FileDialog::getExistingDirectory(QWidget* parent,
const QString& caption,
const QString& dir,
const QFileDialog::Options options)
{
if (!m_nextDirName.isEmpty()) {
QString result = m_nextDirName;
const QString result = m_nextDirName;
m_nextDirName.clear();
return result;
} else {
if (dir.isEmpty()) {
dir = config()->get("LastDir").toString();
}

dir = QFileDialog::getExistingDirectory(parent, caption, dir, options);
const auto& workingDir = dir.isEmpty() ? config()->get("LastDir").toString() : dir;
const auto result =
QDir::toNativeSeparators(QFileDialog::getExistingDirectory(parent, caption, workingDir, options));

#ifdef Q_OS_MACOS
// on Mac OS X the focus is lost after closing the native dialog
if (parent) {
parent->activateWindow();
}

saveLastDir(dir);
return dir;
#endif
saveLastDir(result);
return result;
}
}

Expand Down
24 changes: 10 additions & 14 deletions src/gui/FileDialog.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,40 +25,36 @@ class FileDialog
public:
QString getOpenFileName(QWidget* parent = nullptr,
const QString& caption = QString(),
QString dir = QString(),
const QString& dir = QString(),
const QString& filter = QString(),
QString* selectedFilter = nullptr,
QFileDialog::Options options = 0);
const QFileDialog::Options options = {});

QStringList getOpenFileNames(QWidget* parent = nullptr,
const QString& caption = QString(),
QString dir = QString(),
const QString& dir = QString(),
const QString& filter = QString(),
QString* selectedFilter = nullptr,
QFileDialog::Options options = 0);
const QFileDialog::Options options = {});

QString getFileName(QWidget* parent = nullptr,
const QString& caption = QString(),
QString dir = QString(),
const QString& dir = QString(),
const QString& filter = QString(),
QString* selectedFilter = nullptr,
QFileDialog::Options options = 0,
const QString& defaultExtension = QString(),
const QString& defaultName = QString());
const QFileDialog::Options options = {});

QString getSaveFileName(QWidget* parent = nullptr,
const QString& caption = QString(),
QString dir = QString(),
const QString& dir = QString(),
const QString& filter = QString(),
QString* selectedFilter = nullptr,
QFileDialog::Options options = 0,
const QString& defaultExtension = QString(),
const QString& defaultName = QString());
const QFileDialog::Options options = {});

QString getExistingDirectory(QWidget* parent = nullptr,
const QString& caption = QString(),
QString dir = QString(),
QFileDialog::Options options = QFileDialog::ShowDirsOnly);
const QString& dir = QString(),
const QFileDialog::Options options = QFileDialog::ShowDirsOnly);

void setNextForgetDialog();
/**
Expand Down
2 changes: 1 addition & 1 deletion src/keeshare/SettingsWidgetKeeShare.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ void SettingsWidgetKeeShare::exportCertificate()
const auto filters = QString("%1 (*." + filetype + ");;%2 (*)").arg(tr("KeeShare key file"), tr("All files"));
QString filename = QString("%1.%2").arg(m_own.certificate.signer).arg(filetype);
filename = fileDialog()->getSaveFileName(
this, tr("Select path"), defaultDirPath, filters, nullptr, QFileDialog::Options(0), filetype, filename);
this, tr("Select path"), defaultDirPath, filters, nullptr, QFileDialog::Options(0));
if (filename.isEmpty()) {
return;
}
Expand Down
30 changes: 6 additions & 24 deletions src/keeshare/group/EditGroupWidgetKeeShare.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -284,35 +284,17 @@ void EditGroupWidgetKeeShare::launchPathSelectionDialog()
}
switch (reference.type) {
case KeeShareSettings::ImportFrom:
filename = fileDialog()->getFileName(this,
tr("Select import source"),
defaultDirPath,
filters,
nullptr,
QFileDialog::DontConfirmOverwrite,
defaultFiletype,
filename);
filename = fileDialog()->getFileName(
this, tr("Select import source"), defaultDirPath, filters, nullptr, QFileDialog::DontConfirmOverwrite);
break;
case KeeShareSettings::ExportTo:
filename = fileDialog()->getFileName(this,
tr("Select export target"),
defaultDirPath,
filters,
nullptr,
QFileDialog::Option(0),
defaultFiletype,
filename);
filename = fileDialog()->getFileName(
this, tr("Select export target"), defaultDirPath, filters, nullptr, QFileDialog::Option(0));
break;
case KeeShareSettings::SynchronizeWith:
case KeeShareSettings::Inactive:
filename = fileDialog()->getFileName(this,
tr("Select import/export file"),
defaultDirPath,
filters,
nullptr,
QFileDialog::Option(0),
defaultFiletype,
filename);
filename = fileDialog()->getFileName(
this, tr("Select import/export file"), defaultDirPath, filters, nullptr, QFileDialog::Option(0));
break;
}

Expand Down

0 comments on commit fccbb98

Please sign in to comment.