From 6d97da7bccee318194cba7c52d703ab489422c48 Mon Sep 17 00:00:00 2001 From: Florian Bruhin Date: Mon, 1 Sep 2014 17:51:49 +0200 Subject: [PATCH] browser.downloads: Download with default name if path is given. --- doc/TODO | 1 - qutebrowser/browser/downloads.py | 27 ++++++++++++++++++--------- 2 files changed, 18 insertions(+), 10 deletions(-) diff --git a/doc/TODO b/doc/TODO index aa9869159..be3a93c70 100644 --- a/doc/TODO +++ b/doc/TODO @@ -48,7 +48,6 @@ New big features Downloads ========= -- Download to default filename if only path is given - Remember last path for prompt, if explicitely set. - re-ask instead of killing download if path is invalid - open files diff --git a/qutebrowser/browser/downloads.py b/qutebrowser/browser/downloads.py index 7d2d51b77..198996007 100644 --- a/qutebrowser/browser/downloads.py +++ b/qutebrowser/browser/downloads.py @@ -198,22 +198,31 @@ class DownloadItem(QObject): filename: The full filename to save the download to. None: special value to stop the download. """ - if os.path.isabs(filename): - target = os.path.expanduser(filename) + if self.filename is not None: + 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: + # 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') if download_dir is None: download_dir = utils.get_standard_dir( 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)) - 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: - self.fileobj = open(target, 'wb') + self.fileobj = open(self.filename, 'wb') if self._do_delayed_write: # Downloading to the buffer in RAM has already finished so we # write out the data and clean up now.