Fix retrying of downloads from closed tabs.

Fixes #502.
This commit is contained in:
Florian Bruhin 2015-03-09 07:39:40 +01:00
parent bfc114ae35
commit 6a16875f50
2 changed files with 19 additions and 10 deletions

View File

@ -209,8 +209,8 @@ class DownloadItem(QObject):
redirected: Signal emitted when a download was redirected.
arg 0: The new QNetworkRequest.
arg 1: The old QNetworkReply.
do_retry: Emitted when a request should be re-tried.
arg: The QNetworkRequest to download.
do_retry: Emitted when a download is retried.
arg 0: The new DownloadItem
"""
MAX_REDIRECTS = 10
@ -219,7 +219,7 @@ class DownloadItem(QObject):
error = pyqtSignal(str)
cancelled = pyqtSignal()
redirected = pyqtSignal(QNetworkRequest, QNetworkReply)
do_retry = pyqtSignal('QNetworkReply')
do_retry = pyqtSignal(object) # DownloadItem
def __init__(self, reply, win_id, parent=None):
"""Constructor.
@ -401,9 +401,13 @@ class DownloadItem(QObject):
@pyqtSlot()
def retry(self):
"""Retry a failed download."""
self.cancel()
download_manager = objreg.get('download-manager', scope='window',
window=self._win_id)
new_reply = self.retry_info.manager.get(self.retry_info.request)
self.do_retry.emit(new_reply)
new_download = download_manager.fetch(
new_reply, suggested_filename=self.basename)
self.do_retry.emit(new_download)
self.cancel()
@pyqtSlot()
def open_file(self):
@ -751,7 +755,6 @@ class DownloadManager(QAbstractListModel):
download.error.connect(self.on_error)
download.redirected.connect(
functools.partial(self.on_redirect, download))
download.do_retry.connect(self.fetch)
download.basename = suggested_filename
idx = len(self.downloads) + 1
download.index = idx
@ -898,10 +901,7 @@ class DownloadManager(QAbstractListModel):
failed_download = (download.done and (not download.successful) and
download.retry_info.manager is nam)
if running_download or failed_download:
log.downloads.debug("Found running/failed downloads, "
"adopting the NAM.")
nam.adopted_downloads += 1
download.destroyed.connect(nam.on_adopted_download_destroyed)
nam.adopt_download(download)
return nam.adopted_downloads
def can_clear(self):

View File

@ -237,6 +237,15 @@ class NetworkManager(QNetworkAccessManager):
if self.adopted_downloads == 0:
self.deleteLater()
@pyqtSlot(object) # DownloadItem
def adopt_download(self, download):
"""Adopt a new DownloadItem."""
self.adopted_downloads += 1
log.downloads.debug("Adopted download, {} adopted.".format(
self.adopted_downloads))
download.destroyed.connect(self.on_adopted_download_destroyed)
download.do_retry.connect(self.adopt_download)
# WORKAROUND for:
# http://www.riverbankcomputing.com/pipermail/pyqt/2014-September/034806.html
#