browser.downloads: Download with default name if path is given.

This commit is contained in:
Florian Bruhin 2014-09-01 17:51:49 +02:00
parent 0a646b110b
commit 6d97da7bcc
2 changed files with 18 additions and 10 deletions

View File

@ -48,7 +48,6 @@ New big features
Downloads Downloads
========= =========
- Download to default filename if only path is given
- Remember last path for prompt, if explicitely set. - Remember last path for prompt, if explicitely set.
- re-ask instead of killing download if path is invalid - re-ask instead of killing download if path is invalid
- open files - open files

View File

@ -198,22 +198,31 @@ class DownloadItem(QObject):
filename: The full filename to save the download to. filename: The full filename to save the download to.
None: special value to stop the download. None: special value to stop the download.
""" """
if os.path.isabs(filename): if self.filename is not None:
target = os.path.expanduser(filename) raise ValueError("Filename was already set! filename: {}, "
"existing: {}".format(filename, self.filename))
filename = os.path.expanduser(filename)
if os.path.isabs(filename) and os.path.isdir(filename):
# We got an absolute directory from the user, so we save it under
# the default filename in that directory.
self.filename = os.path.join(filename, self.basename)
elif os.path.isabs(filename):
# We got an absolute filename from the user, so we save it under
# that filename.
self.filename = filename
self.basename = os.path.basename(self.filename)
else: else:
# We only got a filename (without directory) from the user, so we
# save it under that filename in the default directory.
download_dir = config.get('storage', 'download-directory') download_dir = config.get('storage', 'download-directory')
if download_dir is None: if download_dir is None:
download_dir = utils.get_standard_dir( download_dir = utils.get_standard_dir(
QStandardPaths.DownloadLocation) QStandardPaths.DownloadLocation)
target = os.path.join(download_dir, filename) self.filename = os.path.join(download_dir, filename)
self.basename = filename
log.downloads.debug("Setting filename to {}".format(filename)) log.downloads.debug("Setting filename to {}".format(filename))
if self.filename is not None:
raise ValueError("Filename was already set! filename: {}, "
"existing: {}".format(filename, self.filename))
self.filename = target
self.basename = os.path.basename(target)
try: try:
self.fileobj = open(target, 'wb') self.fileobj = open(self.filename, 'wb')
if self._do_delayed_write: if self._do_delayed_write:
# Downloading to the buffer in RAM has already finished so we # Downloading to the buffer in RAM has already finished so we
# write out the data and clean up now. # write out the data and clean up now.