From 8cd5f9e6d1ef8af7f69fef924fcb45c726188045 Mon Sep 17 00:00:00 2001 From: Joel Torstensson Date: Thu, 12 Feb 2015 22:20:22 +0100 Subject: [PATCH] Implemented :download-delete. --- qutebrowser/browser/downloads.py | 34 +++++++++++++++++++++++++------- 1 file changed, 27 insertions(+), 7 deletions(-) diff --git a/qutebrowser/browser/downloads.py b/qutebrowser/browser/downloads.py index c8a714c37..17b661d51 100644 --- a/qutebrowser/browser/downloads.py +++ b/qutebrowser/browser/downloads.py @@ -358,16 +358,19 @@ class DownloadItem(QObject): self.reply = None if self.fileobj is not None: self.fileobj.close() - try: - if (self._filename is not None and os.path.exists(self._filename) - and remove_data): - os.remove(self._filename) - except OSError: - log.downloads.exception("Failed to remove partial file") + if remove_data: + self.delete() self.done = True self.finished.emit() self.data_changed.emit() + def delete(self): + try: + if (self._filename is not None and os.path.exists(self._filename)): + os.remove(self._filename) + except OSError: + log.downloads.exception("Failed to remove partial file") + def retry(self): """Retry a failed download.""" self.cancel() @@ -749,9 +752,26 @@ class DownloadManager(QAbstractListModel): except IndexError: raise cmdexc.CommandError("There's no download {}!".format(count)) if download.done: - raise cmdexc.CommandError("Download {} is already done!".format(count)) + raise cmdexc.CommandError("Download {} is already done!" + .format(count)) download.cancel() + @cmdutils.register(instance='download-manager', scope='window') + def download_delete(self, count: {'special': 'count'}=0): + """Delete the last/[count]th download from disk. + + Args: + count: The index of the download to cancel. + """ + try: + download = self.downloads[count - 1] + except IndexError: + raise cmdexc.CommandError("There's no download {}!".format(count)) + if not download.successful: + raise cmdexc.CommandError("Download {} is not done!".format(count)) + download.delete() + self.remove_item(download) + @cmdutils.register(instance='download-manager', scope='window', deprecated="Use :download instead.") def cancel_download(self, count: {'special': 'count'}=1):