From d6585202fd9cf1f49a1f29b3ceb34ae6041bb65e Mon Sep 17 00:00:00 2001 From: Florian Bruhin Date: Sat, 1 Aug 2015 14:19:06 +0200 Subject: [PATCH] Remember the last used download directory. Thanks to @Carpetsmoker for the original PR. Closes #745. Closes #37. --- doc/help/settings.asciidoc | 12 ++++++++++++ qutebrowser/browser/downloads.py | 17 ++++++++++++++--- qutebrowser/config/configdata.py | 4 ++++ 3 files changed, 30 insertions(+), 3 deletions(-) diff --git a/doc/help/settings.asciidoc b/doc/help/settings.asciidoc index 1236a4e22..62593eacd 100644 --- a/doc/help/settings.asciidoc +++ b/doc/help/settings.asciidoc @@ -123,6 +123,7 @@ |============== |Setting|Description |<>|The directory to save downloads to. An empty value selects a sensible os-specific default. Will expand environment variables. +|<>|Whether to remember the last used download directory. |<>|The maximum number of pages to hold in the global memory page cache. |<>|The capacities for the global memory cache for dead objects such as stylesheets or scripts. Syntax: cacheMinDeadCapacity, cacheMaxDead, totalCapacity. |<>|Default quota for new offline storage databases. @@ -1108,6 +1109,17 @@ The directory to save downloads to. An empty value selects a sensible os-specifi Default: empty +[[storage-remember-download-directory]] +=== remember-download-directory +Whether to remember the last used download directory. + +Valid values: + + * +true+ + * +false+ + +Default: +pass:[true]+ + [[storage-maximum-pages-in-cache]] === maximum-pages-in-cache The maximum number of pages to hold in the global memory page cache. diff --git a/qutebrowser/browser/downloads.py b/qutebrowser/browser/downloads.py index 2a1ba6aef..9a818ade7 100644 --- a/qutebrowser/browser/downloads.py +++ b/qutebrowser/browser/downloads.py @@ -48,13 +48,21 @@ ModelRole = usertypes.enum('ModelRole', ['item'], start=Qt.UserRole, RetryInfo = collections.namedtuple('RetryInfo', ['request', 'manager']) +# Remember the last used directory +_last_used_directory = None + def _download_dir(): """Get the download directory to use.""" directory = config.get('storage', 'download-directory') - if directory is None: - directory = standarddir.download() - return directory + remember_dir = config.get('storage', 'remember-download-directory') + + if remember_dir and _last_used_directory is not None: + return _last_used_directory + elif directory is None: + return standarddir.download() + else: + return directory def _path_suggestion(filename): @@ -433,6 +441,7 @@ class DownloadItem(QObject): filename: The full filename to save the download to. None: special value to stop the download. """ + global _last_used_directory if self.fileobj is not None: raise ValueError("fileobj was already set! filename: {}, " "existing: {}, fileobj {}".format( @@ -448,6 +457,8 @@ class DownloadItem(QObject): # try again. self._create_full_filename(os.path.join(_download_dir(), filename)) + _last_used_directory = os.path.dirname(self._filename) + log.downloads.debug("Setting filename to {}".format(filename)) if os.path.isfile(self._filename): # The file already exists, so ask the user if it should be diff --git a/qutebrowser/config/configdata.py b/qutebrowser/config/configdata.py index 7371394ca..5968731f3 100644 --- a/qutebrowser/config/configdata.py +++ b/qutebrowser/config/configdata.py @@ -557,6 +557,10 @@ def data(readonly=False): "sensible os-specific default. Will expand environment " "variables."), + ('remember-download-directory', + SettingValue(typ.Bool(), 'true'), + "Whether to remember the last used download directory."), + ('maximum-pages-in-cache', SettingValue( typ.Int(none_ok=True, minval=0, maxval=MAXVALS['int']), ''),