From 965a1256a334b071a9cfce2511e50238a401bc00 Mon Sep 17 00:00:00 2001 From: Florian Bruhin Date: Tue, 16 Dec 2014 14:30:47 +0100 Subject: [PATCH] Add "Remove finished" to the download context menu Closes #344. --- qutebrowser/browser/downloads.py | 39 +++++++++++++++++++++++++++++ qutebrowser/browser/downloadview.py | 3 +++ 2 files changed, 42 insertions(+) diff --git a/qutebrowser/browser/downloads.py b/qutebrowser/browser/downloads.py index f2ad75119..c2aef5258 100644 --- a/qutebrowser/browser/downloads.py +++ b/qutebrowser/browser/downloads.py @@ -761,6 +761,17 @@ class DownloadManager(QAbstractListModel): return True return False + def can_clear(self): + """Check if there are finished downloads to clear.""" + if self.downloads: + return any(download.done for download in self.downloads) + else: + return False + + def clear(self): + """Remove all finished downloads.""" + self.remove_items(d for d in self.downloads if d.done) + def last_index(self): """Get the last index in the model. @@ -782,6 +793,34 @@ class DownloadManager(QAbstractListModel): self.endRemoveRows() download.deleteLater() + def remove_items(self, downloads): + """Remove an iterable of downloads.""" + # On the first pass, we only generate the indices so we get the + # first/last one for beginRemoveRows. + indices = [] + # We need to iterate over downloads twice, which won't work if it's a + # generator. + downloads = list(downloads) + for download in downloads: + try: + indices.append(self.downloads.index(download)) + except ValueError: + # already removed + pass + if not indices: + return + indices.sort() + self.beginRemoveRows(QModelIndex(), indices[0], indices[-1]) + for download in downloads: + try: + self.downloads.remove(download) + except ValueError: + # already removed + pass + else: + download.deleteLater() + self.endRemoveRows() + def headerData(self, section, orientation, role): """Simple constant header.""" if (section == 0 and orientation == Qt.Horizontal and diff --git a/qutebrowser/browser/downloadview.py b/qutebrowser/browser/downloadview.py index 9ef7f1ec5..4744de74d 100644 --- a/qutebrowser/browser/downloadview.py +++ b/qutebrowser/browser/downloadview.py @@ -140,6 +140,9 @@ class DownloadView(QListView): functools.partial(self.model().remove_item, item))) else: actions.append(("Cancel", item.cancel)) + if self.model().can_clear(): + actions.append((None, None)) + actions.append(("Remove all finished", self.model().clear)) return actions @pyqtSlot('QPoint')