From 3d4da9e4ea5291658c528c53bc2ab236b00b9e83 Mon Sep 17 00:00:00 2001 From: nowrep Date: Wed, 25 Dec 2013 19:06:58 +0100 Subject: [PATCH] [FileDialogs] Remember last path in all dialogs Closes #1075 --- src/lib/app/qupzilla.cpp | 2 +- .../bookmarksimport/bookmarksimportdialog.cpp | 5 +- src/lib/other/iconchooser.cpp | 3 +- src/lib/other/sourceviewer.cpp | 2 +- .../qtwebkit/spellcheck/spellcheckdialog.cpp | 4 +- src/lib/plugins/speeddial.cpp | 3 +- src/lib/preferences/preferences.cpp | 6 +- src/lib/preferences/sslmanager.cpp | 4 +- src/lib/tools/qztools.cpp | 84 ++++++++++++++++++- src/lib/tools/qztools.h | 10 +++ src/lib/webview/webpage.cpp | 4 +- 11 files changed, 111 insertions(+), 16 deletions(-) diff --git a/src/lib/app/qupzilla.cpp b/src/lib/app/qupzilla.cpp index 940523e5a..5655727bf 100644 --- a/src/lib/app/qupzilla.cpp +++ b/src/lib/app/qupzilla.cpp @@ -1821,7 +1821,7 @@ void QupZilla::openFile() "%3(*.txt);;" "%4(*.*)").arg(tr("HTML files"), tr("Image files"), tr("Text files"), tr("All files")); - const QString &filePath = QFileDialog::getOpenFileName(this, tr("Open file..."), QDir::homePath(), fileTypes); + const QString &filePath = QzTools::getOpenFileName("MainWindow-openFile", this, tr("Open file..."), QDir::homePath(), fileTypes); if (!filePath.isEmpty()) { loadAddress(QUrl::fromLocalFile(filePath)); diff --git a/src/lib/bookmarksimport/bookmarksimportdialog.cpp b/src/lib/bookmarksimport/bookmarksimportdialog.cpp index bf051d646..0adbfed10 100644 --- a/src/lib/bookmarksimport/bookmarksimportdialog.cpp +++ b/src/lib/bookmarksimport/bookmarksimportdialog.cpp @@ -26,6 +26,7 @@ #include "bookmarksimporticonfetcher.h" #include "iconprovider.h" #include "networkmanager.h" +#include "qztools.h" #include #include @@ -263,7 +264,7 @@ void BookmarksImportDialog::setFile() { #ifdef Q_OS_WIN if (m_browser == IE) { - QString path = QFileDialog::getExistingDirectory(this, tr("Choose directory...")); + QString path = QzTools::getExistingDirectory("BookmarksImport-Directory", this, tr("Choose directory...")); if (!path.isEmpty()) { ui->fileLine->setText(path); } @@ -271,7 +272,7 @@ void BookmarksImportDialog::setFile() else #endif { - QString path = QFileDialog::getOpenFileName(this, tr("Choose file..."), QDir::homePath(), m_browserBookmarkFile); + QString path = QzTools::getOpenFileName("BookmarksImport-File", this, tr("Choose file..."), QDir::homePath(), m_browserBookmarkFile); if (!path.isEmpty()) { ui->fileLine->setText(path); } diff --git a/src/lib/other/iconchooser.cpp b/src/lib/other/iconchooser.cpp index 522a3ca99..bdc1b4f44 100644 --- a/src/lib/other/iconchooser.cpp +++ b/src/lib/other/iconchooser.cpp @@ -17,6 +17,7 @@ * ============================================================ */ #include "iconchooser.h" #include "ui_iconchooser.h" +#include "qztools.h" #include #include @@ -36,7 +37,7 @@ IconChooser::IconChooser(QWidget* parent) void IconChooser::chooseFile() { const QString &fileTypes = QString("%3(*.png *.jpg *.jpeg *.gif)").arg(tr("Image files")); - const QString &path = QFileDialog::getOpenFileName(this, tr("Choose icon..."), QDir::homePath(), fileTypes); + const QString &path = QzTools::getOpenFileName("IconChooser-ChangeIcon", this, tr("Choose icon..."), QDir::homePath(), fileTypes); if (path.isEmpty()) { return; diff --git a/src/lib/other/sourceviewer.cpp b/src/lib/other/sourceviewer.cpp index 4f8642c8a..3e4bea0f3 100644 --- a/src/lib/other/sourceviewer.cpp +++ b/src/lib/other/sourceviewer.cpp @@ -165,7 +165,7 @@ void SourceViewer::loadSource() void SourceViewer::save() { - QString filePath = QFileDialog::getSaveFileName(this, tr("Save file..."), QDir::homePath() + "/source_code.html"); + QString filePath = QzTools::getSaveFileName("SourceViewer-Save", this, tr("Save file..."), QDir::homePath() + "/source_code.html"); if (filePath.isEmpty()) { return; } diff --git a/src/lib/plugins/qtwebkit/spellcheck/spellcheckdialog.cpp b/src/lib/plugins/qtwebkit/spellcheck/spellcheckdialog.cpp index 2e8e8ace0..576c0c181 100644 --- a/src/lib/plugins/qtwebkit/spellcheck/spellcheckdialog.cpp +++ b/src/lib/plugins/qtwebkit/spellcheck/spellcheckdialog.cpp @@ -19,6 +19,7 @@ #include "ui_spellcheckdialog.h" #include "settings.h" #include "speller.h" +#include "qztools.h" #include "mainapplication.h" #include @@ -67,8 +68,7 @@ SpellCheckDialog::SpellCheckDialog(QWidget* parent) void SpellCheckDialog::changeDictionaryPath() { - const QString &path = QFileDialog::getExistingDirectory(this, tr("Choose dictionary path..."), - ui->dictPath->text()); + const QString &path = QzTools::getExistingDirectory("SpellCheckDialog-Directory", this, tr("Choose dictionary path..."), ui->dictPath->text()); if (!path.isEmpty()) { ui->dictPath->setText(path); diff --git a/src/lib/plugins/speeddial.cpp b/src/lib/plugins/speeddial.cpp index 7c399b778..c8a8f58d1 100644 --- a/src/lib/plugins/speeddial.cpp +++ b/src/lib/plugins/speeddial.cpp @@ -19,6 +19,7 @@ #include "mainapplication.h" #include "pagethumbnailer.h" #include "settings.h" +#include "qztools.h" #include #include @@ -285,7 +286,7 @@ void SpeedDial::removeImageForUrl(const QString &url) QString SpeedDial::getOpenFileName() { const QString &fileTypes = QString("%3(*.png *.jpg *.jpeg *.bmp *.gif *.svg *.tiff)").arg(tr("Image files")); - const QString &image = QFileDialog::getOpenFileName(0, tr("Select image..."), QDir::homePath(), fileTypes); + const QString &image = QzTools::getOpenFileName("SpeedDial-GetOpenFileName", 0, tr("Select image..."), QDir::homePath(), fileTypes); if (image.isEmpty()) { return image; diff --git a/src/lib/preferences/preferences.cpp b/src/lib/preferences/preferences.cpp index 56c68bbff..595fa92c9 100644 --- a/src/lib/preferences/preferences.cpp +++ b/src/lib/preferences/preferences.cpp @@ -567,7 +567,7 @@ void Preferences::useActualNewTab() void Preferences::chooseDownPath() { - QString userFileName = QFileDialog::getExistingDirectory(this, tr("Choose download location..."), QDir::homePath()); + QString userFileName = QzTools::getExistingDirectory("Preferences-ChooseDownPath", this, tr("Choose download location..."), QDir::homePath()); if (userFileName.isEmpty()) { return; } @@ -598,7 +598,7 @@ void Preferences::deleteHtml5storage() void Preferences::chooseExternalDownloadManager() { - QString path = QFileDialog::getOpenFileName(this, tr("Choose executable location..."), QDir::homePath()); + QString path = QzTools::getOpenFileName("Preferences-ExternalDownloadManager", this, tr("Choose executable location..."), QDir::homePath()); if (path.isEmpty()) { return; } @@ -722,7 +722,7 @@ void Preferences::showTabPreviewsChanged(bool state) void Preferences::changeCachePathClicked() { - QString path = QFileDialog::getExistingDirectory(this, tr("Choose cache path..."), ui->cachePath->text()); + QString path = QzTools::getExistingDirectory("Preferences-CachePath", this, tr("Choose cache path..."), ui->cachePath->text()); if (path.isEmpty()) { return; } diff --git a/src/lib/preferences/sslmanager.cpp b/src/lib/preferences/sslmanager.cpp index 5b2b3ea0a..93b5df507 100644 --- a/src/lib/preferences/sslmanager.cpp +++ b/src/lib/preferences/sslmanager.cpp @@ -58,7 +58,7 @@ SSLManager::SSLManager(QWidget* parent) void SSLManager::addPath() { - QString path = QFileDialog::getExistingDirectory(this, tr("Choose path...")); + QString path = QzTools::getExistingDirectory("SSLManager-AddPath", this, tr("Choose path...")); if (path.isEmpty()) { return; } @@ -129,7 +129,7 @@ void SSLManager::showCaCertInfo() void SSLManager::addLocalCertificate() { - const QString &path = QFileDialog::getOpenFileName(this, tr("Import certificate..."), QDir::homePath(), "*.crt"); + const QString &path = QzTools::getOpenFileName("SSLManager-AddLocalCert", this, tr("Import certificate..."), QDir::homePath(), "*.crt"); if (path.isEmpty()) { return; diff --git a/src/lib/tools/qztools.cpp b/src/lib/tools/qztools.cpp index 338af0faa..41195e165 100644 --- a/src/lib/tools/qztools.cpp +++ b/src/lib/tools/qztools.cpp @@ -17,6 +17,7 @@ * ============================================================ */ #include "qztools.h" #include "mainapplication.h" +#include "settings.h" #include #include @@ -252,6 +253,28 @@ QString QzTools::filterCharsFromFilename(const QString &name) return value; } +QString QzTools::lastPathForFileDialog(const QString &dialogName, const QString &fallbackPath) +{ + Settings settings; + settings.beginGroup("LastFileDialogsPaths"); + QString path = settings.value("FileDialogs/" + dialogName).toString(); + settings.endGroup(); + + return path.isEmpty() ? fallbackPath : path; +} + +void QzTools::saveLastPathForFileDialog(const QString &dialogName, const QString &path) +{ + if (path.isEmpty()) { + return; + } + + Settings settings; + settings.beginGroup("LastFileDialogsPaths"); + settings.setValue(dialogName, path); + settings.endGroup(); +} + QString QzTools::alignTextToWidth(const QString &string, const QString &text, const QFontMetrics &metrics, int width) { int pos = 0; @@ -503,6 +526,66 @@ bool QzTools::isUtf8(const char* string) return true; } +QString QzTools::getExistingDirectory(const QString &name, QWidget* parent, const QString &caption, const QString &dir, QFileDialog::Options options) +{ + Settings settings; + settings.beginGroup("FileDialogPaths"); + QString lastDir = settings.value(name, dir).toString(); + + QString path = QFileDialog::getExistingDirectory(parent, caption, lastDir, options); + + if (!path.isEmpty()) { + settings.setValue(name, QFileInfo(path).absolutePath()); + } + + return path; +} + +QString QzTools::getOpenFileName(const QString &name, QWidget* parent, const QString &caption, const QString &dir, const QString &filter, QString* selectedFilter, QFileDialog::Options options) +{ + Settings settings; + settings.beginGroup("FileDialogPaths"); + QString lastDir = settings.value(name, dir).toString(); + + QString path = QFileDialog::getOpenFileName(parent, caption, lastDir, filter, selectedFilter, options); + + if (!path.isEmpty()) { + settings.setValue(name, QFileInfo(path).absolutePath()); + } + + return path; +} + +QStringList QzTools::getOpenFileNames(const QString &name, QWidget* parent, const QString &caption, const QString &dir, const QString &filter, QString* selectedFilter, QFileDialog::Options options) +{ + Settings settings; + settings.beginGroup("FileDialogPaths"); + QString lastDir = settings.value(name, dir).toString(); + + QStringList paths = QFileDialog::getOpenFileNames(parent, caption, lastDir, filter, selectedFilter, options); + + if (!paths.isEmpty()) { + settings.setValue(name, QFileInfo(paths.first()).absolutePath()); + } + + return paths; +} + +QString QzTools::getSaveFileName(const QString &name, QWidget* parent, const QString &caption, const QString &dir, const QString &filter, QString* selectedFilter, QFileDialog::Options options) +{ + Settings settings; + settings.beginGroup("FileDialogPaths"); + QString lastDir = settings.value(name, dir).toString(); + + QString path = QFileDialog::getSaveFileName(parent, caption, lastDir, filter, selectedFilter, options); + + if (!path.isEmpty()) { + settings.setValue(name, QFileInfo(path).absolutePath()); + } + + return path; +} + // Matches domain (assumes both pattern and domain not starting with dot) // pattern = domain to be matched // domain = site domain @@ -776,4 +859,3 @@ QString QzTools::operatingSystem() return str; #endif } - diff --git a/src/lib/tools/qztools.h b/src/lib/tools/qztools.h index 131fef9e1..76ee61f63 100644 --- a/src/lib/tools/qztools.h +++ b/src/lib/tools/qztools.h @@ -21,6 +21,7 @@ #include #include #include +#include #include "qz_namespace.h" @@ -51,6 +52,9 @@ QString QT_QUPZILLA_EXPORT ensureUniqueFilename(const QString &name, const QStri QString QT_QUPZILLA_EXPORT getFileNameFromUrl(const QUrl &url); QString QT_QUPZILLA_EXPORT filterCharsFromFilename(const QString &name); +QString QT_QUPZILLA_EXPORT lastPathForFileDialog(const QString &dialogName, const QString &fallbackPath); +void QT_QUPZILLA_EXPORT saveLastPathForFileDialog(const QString &dialogName, const QString &path); + QString QT_QUPZILLA_EXPORT alignTextToWidth(const QString &string, const QString &text, const QFontMetrics &metrics, int width); QString QT_QUPZILLA_EXPORT fileSizeToString(qint64 size); @@ -65,6 +69,12 @@ QRegion QT_QUPZILLA_EXPORT roundedRect(const QRect &rect, int radius); QIcon QT_QUPZILLA_EXPORT iconFromFileName(const QString &fileName); bool QT_QUPZILLA_EXPORT isUtf8(const char* string); +// QFileDialog static functions that remembers last used directory +QString QT_QUPZILLA_EXPORT getExistingDirectory(const QString &name, QWidget* parent = 0, const QString &caption = QString(), const QString &dir = QString(), QFileDialog::Options options = QFileDialog::ShowDirsOnly); +QString QT_QUPZILLA_EXPORT getOpenFileName(const QString &name, QWidget* parent = 0, const QString &caption = QString(), const QString &dir = QString(), const QString &filter = QString(), QString* selectedFilter = 0, QFileDialog::Options options = 0); +QStringList QT_QUPZILLA_EXPORT getOpenFileNames(const QString &name, QWidget* parent = 0, const QString &caption = QString(), const QString &dir = QString(), const QString &filter = QString(), QString* selectedFilter = 0, QFileDialog::Options options = 0); +QString QT_QUPZILLA_EXPORT getSaveFileName(const QString &name, QWidget* parent = 0, const QString &caption = QString(), const QString &dir = QString(), const QString &filter = QString(), QString* selectedFilter = 0, QFileDialog::Options options = 0); + bool QT_QUPZILLA_EXPORT matchDomain(const QString &pattern, const QString &domain); QString QT_QUPZILLA_EXPORT operatingSystem(); diff --git a/src/lib/webview/webpage.cpp b/src/lib/webview/webpage.cpp index aebdeca4d..2b25b678a 100644 --- a/src/lib/webview/webpage.cpp +++ b/src/lib/webview/webpage.cpp @@ -697,7 +697,7 @@ bool WebPage::extension(Extension extension, const ExtensionOption* option, Exte suggestedFileName = exOption->suggestedFileNames.at(0); } - exReturn->fileNames = QFileDialog::getOpenFileNames(0, tr("Select files to upload..."), suggestedFileName); + exReturn->fileNames = QzTools::getOpenFileNames("WebPage-UploadFiles", 0, tr("Select files to upload..."), suggestedFileName); return true; } @@ -998,7 +998,7 @@ QString WebPage::chooseFile(QWebFrame* originatingFrame, const QString &oldFile) suggFileName = oldFile; } - const QString &fileName = QFileDialog::getOpenFileName(originatingFrame->page()->view(), tr("Choose file..."), suggFileName); + const QString &fileName = QzTools::getOpenFileName("WebPage-ChooseFile", originatingFrame->page()->view(), tr("Choose file..."), suggFileName); if (!fileName.isEmpty()) { s_lastUploadLocation = fileName;