GUI: ensure initial directory for file dialog exists

Older Windows versions show an error message if the initial directory
doesn't exist. Fixes #1438.
This commit is contained in:
Moritz Bunkus 2015-10-13 21:58:33 +02:00
parent 0ffb2b592c
commit 2af6d4aa5e
13 changed files with 129 additions and 53 deletions

View File

@ -1,3 +1,10 @@
2015-10-13 Moritz Bunkus <moritz@bunkus.org>
* MKVToolNix GUI: enhancement: if the last directory opened
doesn't exist anymore then default to one that does in order to
prevent an error message from older Windows versions about a
location not being available. Fixes #1438.
2015-10-10 Moritz Bunkus <moritz@bunkus.org>
* MKVToolNix GUI: enhancement: the context menu for the status bar

View File

@ -1,6 +1,5 @@
#include "common/common_pch.h"
#include <QFileDialog>
#include <QFileInfo>
#include <QMenu>
#include <QMessageBox>
@ -31,6 +30,7 @@
#include "mkvtoolnix-gui/main_window/main_window.h"
#include "mkvtoolnix-gui/main_window/select_character_set_dialog.h"
#include "mkvtoolnix-gui/util/file.h"
#include "mkvtoolnix-gui/util/file_dialog.h"
#include "mkvtoolnix-gui/util/header_view_manager.h"
#include "mkvtoolnix-gui/util/message_box.h"
#include "mkvtoolnix-gui/util/model.h"
@ -486,7 +486,7 @@ Tab::saveAsXmlImpl(bool requireNewFileName) {
saveAsImpl(requireNewFileName, [this](bool doRequireNewFileName, QString &newFileName) -> bool {
if (doRequireNewFileName) {
auto defaultFilePath = !m_fileName.isEmpty() ? QFileInfo{m_fileName}.path() : Util::Settings::get().m_lastOpenDir.path();
newFileName = QFileDialog::getSaveFileName(this, QY("Save chapters as XML"), defaultFilePath, QY("XML chapter files") + Q(" (*.xml);;") + QY("All files") + Q(" (*)"), nullptr, QFileDialog::DontUseCustomDirectoryIcons);
newFileName = Util::getSaveFileName(this, QY("Save chapters as XML"), defaultFilePath, QY("XML chapter files") + Q(" (*.xml);;") + QY("All files") + Q(" (*)"));
if (newFileName.isEmpty())
return false;
@ -523,8 +523,7 @@ Tab::saveToMatroskaImpl(bool requireNewFileName) {
if (doRequireNewFileName) {
auto defaultFilePath = !m_fileName.isEmpty() ? QFileInfo{m_fileName}.path() : Util::Settings::get().m_lastOpenDir.path();
newFileName = QFileDialog::getOpenFileName(this, QY("Save chapters to Matroska file"), defaultFilePath, QY("Matroska files") + Q(" (*.mkv *.mka *.mks *.mk3d);;") + QY("All files") + Q(" (*)"),
nullptr, QFileDialog::DontUseCustomDirectoryIcons);
newFileName = Util::getOpenFileName(this, QY("Save chapters to Matroska file"), defaultFilePath, QY("Matroska files") + Q(" (*.mkv *.mka *.mks *.mk3d);;") + QY("All files") + Q(" (*)"));
if (newFileName.isEmpty())
return false;

View File

@ -2,7 +2,6 @@
#include <QDragEnterEvent>
#include <QDropEvent>
#include <QFileDialog>
#include <QFileInfo>
#include <QMenu>
#include <QMessageBox>
@ -17,6 +16,7 @@
#include "mkvtoolnix-gui/chapter_editor/tool.h"
#include "mkvtoolnix-gui/main_window/main_window.h"
#include "mkvtoolnix-gui/merge/mux_config.h"
#include "mkvtoolnix-gui/util/file_dialog.h"
#include "mkvtoolnix-gui/util/message_box.h"
#include "mkvtoolnix-gui/util/settings.h"
#include "mkvtoolnix-gui/util/widget.h"
@ -165,14 +165,13 @@ Tool::openFilesFromCommandLine(QStringList const &fileNames) {
void
Tool::selectFileToOpen() {
auto fileNames = QFileDialog::getOpenFileNames(this, QY("Open files in chapter editor"), Util::Settings::get().m_lastOpenDir.path(),
QY("Supported file types") + Q(" (*.mpls *.mkv *.mka *.mks *.mk3d *.txt *.xml);;") +
QY("Matroska files") + Q(" (*.mkv *.mka *.mks *.mk3d);;") +
QY("Blu-ray playlist files") + Q(" (*.mpls);;") +
QY("XML chapter files") + Q(" (*.xml);;") +
QY("Simple OGM-style chapter files") + Q(" (*.txt);;") +
QY("All files") + Q(" (*)"),
nullptr, QFileDialog::DontUseCustomDirectoryIcons);
auto fileNames = Util::getOpenFileNames(this, QY("Open files in chapter editor"), Util::Settings::get().m_lastOpenDir.path(),
QY("Supported file types") + Q(" (*.mpls *.mkv *.mka *.mks *.mk3d *.txt *.xml);;") +
QY("Matroska files") + Q(" (*.mkv *.mka *.mks *.mk3d);;") +
QY("Blu-ray playlist files") + Q(" (*.mpls);;") +
QY("XML chapter files") + Q(" (*.xml);;") +
QY("Simple OGM-style chapter files") + Q(" (*.txt);;") +
QY("All files") + Q(" (*)"));
if (fileNames.isEmpty())
return;

View File

@ -16,6 +16,7 @@
#include "mkvtoolnix-gui/header_editor/tool.h"
#include "mkvtoolnix-gui/main_window/main_window.h"
#include "mkvtoolnix-gui/merge/mux_config.h"
#include "mkvtoolnix-gui/util/file_dialog.h"
#include "mkvtoolnix-gui/util/message_box.h"
#include "mkvtoolnix-gui/util/settings.h"
#include "mkvtoolnix-gui/util/widget.h"
@ -139,9 +140,8 @@ Tool::openFile(QString const &fileName) {
void
Tool::selectFileToOpen() {
auto fileNames = QFileDialog::getOpenFileNames(this, QY("Open files in header editor"), Util::Settings::get().m_lastOpenDir.path(),
QY("Matroska and WebM files") + Q(" (*.mkv *.mka *.mks *.mk3d *.webm);;") + QY("All files") + Q(" (*)"),
nullptr, QFileDialog::DontUseCustomDirectoryIcons);
auto fileNames = Util::getOpenFileNames(this, QY("Open files in header editor"), Util::Settings::get().m_lastOpenDir.path(),
QY("Matroska and WebM files") + Q(" (*.mkv *.mka *.mks *.mk3d *.webm);;") + QY("All files") + Q(" (*)"));
if (fileNames.isEmpty())
return;

View File

@ -11,6 +11,7 @@
#include "mkvtoolnix-gui/forms/main_window/preferences_dialog.h"
#include "mkvtoolnix-gui/main_window/preferences_dialog.h"
#include "mkvtoolnix-gui/merge/additional_command_line_options_dialog.h"
#include "mkvtoolnix-gui/util/file_dialog.h"
#include "mkvtoolnix-gui/util/widget.h"
namespace mtx { namespace gui {
@ -484,7 +485,7 @@ PreferencesDialog::enableOutputFileNameControls() {
void
PreferencesDialog::browseFixedOutputDirectory() {
auto dir = QFileDialog::getExistingDirectory(this, QY("Select output directory"), ui->leMAutoSetFixedDirectory->text(), QFileDialog::ShowDirsOnly | QFileDialog::DontUseCustomDirectoryIcons);
auto dir = Util::getExistingDirectory(this, QY("Select output directory"), ui->leMAutoSetFixedDirectory->text());
if (!dir.isEmpty())
ui->leMAutoSetFixedDirectory->setText(dir);
}

View File

@ -1,12 +1,12 @@
#include "common/common_pch.h"
#include <QFileDialog>
#include <QMenu>
#include "common/extern_data.h"
#include "common/qt.h"
#include "mkvtoolnix-gui/merge/tab.h"
#include "mkvtoolnix-gui/forms/merge/tab.h"
#include "mkvtoolnix-gui/util/file_dialog.h"
#include "mkvtoolnix-gui/util/files_drag_drop_widget.h"
#include "mkvtoolnix-gui/util/header_view_manager.h"
#include "mkvtoolnix-gui/util/model.h"
@ -141,19 +141,12 @@ Tab::onAddAttachments() {
QStringList
Tab::selectAttachmentsToAdd() {
QFileDialog dlg{this};
dlg.setNameFilter(QY("All files") + Q(" (*)"));
dlg.setFileMode(QFileDialog::ExistingFiles);
dlg.setOptions(QFileDialog::DontUseCustomDirectoryIcons);
dlg.setDirectory(Util::Settings::get().m_lastOpenDir);
dlg.setWindowTitle(QY("Add attachments"));
auto fileNames = Util::getOpenFileNames(this, QY("Add attachments"), Util::Settings::get().m_lastOpenDir.path(), QY("All files") + Q(" (*)"));
if (!dlg.exec())
return QStringList{};
if (!fileNames.isEmpty())
Util::Settings::get().m_lastOpenDir = QFileInfo{fileNames[0]}.path();
Util::Settings::get().m_lastOpenDir = dlg.directory();
return dlg.selectedFiles();
return fileNames;
}
void

View File

@ -1,12 +1,12 @@
#include "common/common_pch.h"
#include <QDialog>
#include <QFileDialog>
#include <QPushButton>
#include "common/qt.h"
#include "mkvtoolnix-gui/forms/merge/executable_location_dialog.h"
#include "mkvtoolnix-gui/merge/executable_location_dialog.h"
#include "mkvtoolnix-gui/util/file_dialog.h"
namespace mtx { namespace gui { namespace Merge {
@ -62,7 +62,7 @@ ExecutableLocationDialog::browse() {
#endif
filters << QY("All files") + Q(" (*)");
auto fileName = QFileDialog::getOpenFileName(this, QY("Select executable"), m_ui->leExecutable->text(), filters.join(Q(";;")), nullptr, QFileDialog::DontUseCustomDirectoryIcons);
auto fileName = Util::getOpenFileName(this, QY("Select executable"), m_ui->leExecutable->text(), filters.join(Q(";;")));
if (!fileName.isEmpty())
m_ui->leExecutable->setText(fileName);
}

View File

@ -3,7 +3,6 @@
#include <QDir>
#include <QDragEnterEvent>
#include <QDropEvent>
#include <QFileDialog>
#include <QFileInfo>
#include <QList>
#include <QMenu>
@ -29,6 +28,7 @@
#include "mkvtoolnix-gui/merge/tool.h"
#include "mkvtoolnix-gui/merge/playlist_scanner.h"
#include "mkvtoolnix-gui/util/file_identifier.h"
#include "mkvtoolnix-gui/util/file_dialog.h"
#include "mkvtoolnix-gui/util/file_type_filter.h"
#include "mkvtoolnix-gui/util/header_view_manager.h"
#include "mkvtoolnix-gui/util/message_box.h"
@ -1018,19 +1018,12 @@ Tab::setDefaultsFromSettingsForAddedFiles(QList<SourceFilePtr> const &files) {
QStringList
Tab::selectFilesToAdd(QString const &title) {
QFileDialog dlg{this};
dlg.setNameFilters(Util::FileTypeFilter::get());
dlg.setFileMode(QFileDialog::ExistingFiles);
dlg.setDirectory(Util::Settings::get().m_lastOpenDir);
dlg.setWindowTitle(title);
dlg.setOptions(QFileDialog::HideNameFilterDetails | QFileDialog::DontUseCustomDirectoryIcons);
auto fileNames = Util::getOpenFileNames(this, title, Util::Settings::get().m_lastOpenDir.path(), Util::FileTypeFilter::get().join(Q(";;")), nullptr, QFileDialog::HideNameFilterDetails);
if (!dlg.exec())
return QStringList{};
if (!fileNames.isEmpty())
Util::Settings::get().m_lastOpenDir = QFileInfo{fileNames[0]}.path();
Util::Settings::get().m_lastOpenDir = dlg.directory();
return dlg.selectedFiles();
return fileNames;
}
void

View File

@ -12,6 +12,7 @@
#include "mkvtoolnix-gui/merge/tool.h"
#include "mkvtoolnix-gui/forms/main_window/main_window.h"
#include "mkvtoolnix-gui/forms/merge/tab.h"
#include "mkvtoolnix-gui/util/file_dialog.h"
#include "mkvtoolnix-gui/util/message_box.h"
#include "mkvtoolnix-gui/util/option_file.h"
#include "mkvtoolnix-gui/util/settings.h"
@ -22,7 +23,6 @@
#include <QDir>
#include <QMenu>
#include <QTreeView>
#include <QFileDialog>
#include <QInputDialog>
#include <QMessageBox>
#include <QString>
@ -163,7 +163,7 @@ Tab::onSaveConfig() {
void
Tab::onSaveOptionFile() {
auto &settings = Util::Settings::get();
auto fileName = QFileDialog::getSaveFileName(this, QY("Save option file"), settings.m_lastConfigDir.path(), QY("All files") + Q(" (*)"), nullptr, QFileDialog::DontUseCustomDirectoryIcons);
auto fileName = Util::getSaveFileName(this, QY("Save option file"), settings.m_lastConfigDir.path(), QY("All files") + Q(" (*)"));
if (fileName.isEmpty())
return;
@ -177,8 +177,7 @@ Tab::onSaveOptionFile() {
void
Tab::onSaveConfigAs() {
auto &settings = Util::Settings::get();
auto fileName = QFileDialog::getSaveFileName(this, QY("Save settings file as"), settings.m_lastConfigDir.path(), QY("MKVToolnix GUI config files") + Q(" (*.mtxcfg);;") + QY("All files") + Q(" (*)"),
nullptr, QFileDialog::DontUseCustomDirectoryIcons);
auto fileName = Util::getSaveFileName(this, QY("Save settings file as"), settings.m_lastConfigDir.path(), QY("MKVToolnix GUI config files") + Q(" (*.mtxcfg);;") + QY("All files") + Q(" (*)"));
if (fileName.isEmpty())
return;
@ -231,7 +230,7 @@ Tab::getOpenFileName(QString const &title,
auto &settings = Util::Settings::get();
auto dir = determineInitialDir(lineEdit, initialDirMode);
auto fileName = QFileDialog::getOpenFileName(this, title, dir, fullFilter, nullptr, QFileDialog::DontUseCustomDirectoryIcons);
auto fileName = Util::getOpenFileName(this, title, dir, fullFilter);
if (fileName.isEmpty())
return fileName;
@ -258,7 +257,7 @@ Tab::getSaveFileName(QString const &title,
auto dir = !lineEdit->text().isEmpty() ? lineEdit->text()
: !settings.m_lastOutputDir.path().isEmpty() && (settings.m_lastOutputDir.path() != Q(".")) ? settings.m_lastOutputDir.path()
: settings.m_lastOpenDir.path();
auto fileName = QFileDialog::getSaveFileName(this, title, dir, fullFilter, nullptr, QFileDialog::DontUseCustomDirectoryIcons);
auto fileName = Util::getSaveFileName(this, title, dir, fullFilter);
if (fileName.isEmpty())
return fileName;

View File

@ -2,7 +2,6 @@
#include <QDragEnterEvent>
#include <QDropEvent>
#include <QFileDialog>
#include <QFileInfo>
#include <QMenu>
#include <QMessageBox>
@ -14,6 +13,7 @@
#include "mkvtoolnix-gui/merge/tab.h"
#include "mkvtoolnix-gui/merge/tool.h"
#include "mkvtoolnix-gui/main_window/main_window.h"
#include "mkvtoolnix-gui/util/file_dialog.h"
#include "mkvtoolnix-gui/util/message_box.h"
#include "mkvtoolnix-gui/util/settings.h"
#include "mkvtoolnix-gui/util/widget.h"
@ -135,8 +135,7 @@ Tool::newConfig() {
void
Tool::openConfig() {
auto &settings = Util::Settings::get();
auto fileName = QFileDialog::getOpenFileName(this, QY("Open settings file"), settings.m_lastConfigDir.path(), QY("MKVToolnix GUI config files") + Q(" (*.mtxcfg);;") + QY("All files") + Q(" (*)"),
nullptr, QFileDialog::DontUseCustomDirectoryIcons);
auto fileName = Util::getOpenFileName(this, QY("Open settings file"), settings.m_lastConfigDir.path(), QY("MKVToolnix GUI config files") + Q(" (*.mtxcfg);;") + QY("All files") + Q(" (*)"));
if (fileName.isEmpty())
return;

View File

@ -0,0 +1,62 @@
#include "common/common_pch.h"
#include <QStandardPaths>
#include <QString>
#include "common/qt.h"
#include "mkvtoolnix-gui/util/file_dialog.h"
namespace mtx { namespace gui { namespace Util {
QString
sanitizeDirectory(QString const &directory) {
auto dir = to_utf8(directory.isEmpty() || (directory == Q(".")) ? QStandardPaths::writableLocation(QStandardPaths::MoviesLocation) : directory);
auto path = bfs::absolute(bfs::path{dir});
auto ec = boost::system::error_code{};
while ( !(bfs::exists(path, ec) && bfs::is_directory(path, ec))
&& !path.parent_path().empty())
path = path.parent_path();
return Q(path.string());
}
QString
getOpenFileName(QWidget *parent,
QString const &caption,
QString const &dir,
QString const &filter,
QString *selectedFilter,
QFileDialog::Options options) {
return QFileDialog::getOpenFileName(parent, caption, sanitizeDirectory(dir), filter, selectedFilter, options & QFileDialog::DontUseCustomDirectoryIcons);
}
QStringList
getOpenFileNames(QWidget *parent,
QString const &caption,
QString const &dir,
QString const &filter,
QString *selectedFilter,
QFileDialog::Options options) {
return QFileDialog::getOpenFileNames(parent, caption, sanitizeDirectory(dir), filter, selectedFilter, options & QFileDialog::DontUseCustomDirectoryIcons);
}
QString
getSaveFileName(QWidget *parent,
QString const &caption,
QString const &dir,
QString const &filter,
QString *selectedFilter,
QFileDialog::Options options) {
return QFileDialog::getSaveFileName(parent, caption, sanitizeDirectory(dir), filter, selectedFilter, options & QFileDialog::DontUseCustomDirectoryIcons);
}
QString
getExistingDirectory(QWidget *parent,
QString const &caption,
QString const &dir,
QFileDialog::Options options) {
return QFileDialog::getExistingDirectory(parent, caption, sanitizeDirectory(dir), options & QFileDialog::DontUseCustomDirectoryIcons);
}
}}}

View File

@ -0,0 +1,23 @@
#ifndef MTX_MKVTOOLNIX_GUI_UTIL_FILE_DIALOG_H
#define MTX_MKVTOOLNIX_GUI_UTIL_FILE_DIALOG_H
#include "common/common_pch.h"
#include <QFileDialog>
class QString;
namespace mtx { namespace gui { namespace Util {
QString sanitizeDirectory(QString const &directory);
QString getOpenFileName(QWidget *parent = nullptr, QString const &caption = QString{}, QString const &dir = QString{}, QString const &filter = QString{},
QString *selectedFilter = nullptr, QFileDialog::Options options = QFileDialog::Options{});
QStringList getOpenFileNames(QWidget *parent = nullptr, QString const &caption = QString{}, QString const &dir = QString{}, QString const &filter = QString{},
QString *selectedFilter = nullptr, QFileDialog::Options options = QFileDialog::Options{});
QString getSaveFileName(QWidget *parent = nullptr, QString const &caption = QString{}, QString const &dir = QString{}, QString const &filter = QString{},
QString *selectedFilter = nullptr, QFileDialog::Options options = QFileDialog::Options{});
QString getExistingDirectory(QWidget *parent = nullptr, QString const &caption = QString{}, QString const &dir = QString{}, QFileDialog::Options options = QFileDialog::ShowDirsOnly);
}}}
#endif // MTX_MKVTOOLNIX_GUI_UTIL_FILE_DIALOG_H

View File

@ -14,6 +14,7 @@
#include "mkvtoolnix-gui/jobs/mux_job.h"
#include "mkvtoolnix-gui/jobs/tool.h"
#include "mkvtoolnix-gui/main_window/main_window.h"
#include "mkvtoolnix-gui/util/file_dialog.h"
#include "mkvtoolnix-gui/util/message_box.h"
#include "mkvtoolnix-gui/util/settings.h"
#include "mkvtoolnix-gui/util/string.h"
@ -352,7 +353,7 @@ Tab::onSaveOutput() {
Q_D(Tab);
auto &cfg = Util::Settings::get();
auto fileName = QFileDialog::getSaveFileName(this, QY("Save job output"), cfg.m_lastOpenDir.path(), QY("Text files") + Q(" (*.txt);;") + QY("All files") + Q(" (*)"), nullptr, QFileDialog::DontUseCustomDirectoryIcons);
auto fileName = Util::getSaveFileName(this, QY("Save job output"), cfg.m_lastOpenDir.path(), QY("Text files") + Q(" (*.txt);;") + QY("All files") + Q(" (*)"));
if (fileName.isEmpty())
return;