Remember the last used download directory.

Thanks to @Carpetsmoker for the original PR.

Closes #745.
Closes #37.
This commit is contained in:
Florian Bruhin 2015-08-01 14:19:06 +02:00
parent 68512ce2cd
commit d6585202fd
3 changed files with 30 additions and 3 deletions

View File

@ -123,6 +123,7 @@
|==============
|Setting|Description
|<<storage-download-directory,download-directory>>|The directory to save downloads to. An empty value selects a sensible os-specific default. Will expand environment variables.
|<<storage-remember-download-directory,remember-download-directory>>|Whether to remember the last used download directory.
|<<storage-maximum-pages-in-cache,maximum-pages-in-cache>>|The maximum number of pages to hold in the global memory page cache.
|<<storage-object-cache-capacities,object-cache-capacities>>|The capacities for the global memory cache for dead objects such as stylesheets or scripts. Syntax: cacheMinDeadCapacity, cacheMaxDead, totalCapacity.
|<<storage-offline-storage-default-quota,offline-storage-default-quota>>|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.

View File

@ -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

View File

@ -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']), ''),