Clean up NetworkManager after downloads finished.

Fixes #490.
This commit is contained in:
Florian Bruhin 2015-01-28 22:52:24 +01:00
parent 66ec4f0599
commit b721a0e992
2 changed files with 31 additions and 7 deletions

View File

@ -797,14 +797,19 @@ class DownloadManager(QAbstractListModel):
Return: Return:
A boolean. A boolean.
""" """
assert nam.adopted_downloads == 0
for download in self.downloads: for download in self.downloads:
if download.reply is not None and download.reply.manager() is nam: running_download = (download.reply is not None and
return True download.reply.manager() is nam)
if (download.done and (not download.successful) and # user could request retry after tab is closed.
download.retry_info.manager is nam): failed_download = (download.done and (not download.successful) and
# user could request retry after tab is closed. download.retry_info.manager is nam)
return True if running_download or failed_download:
return False log.downloads.debug("Found running/failed downloads, "
"adopting the NAM.")
nam.adopted_downloads += 1
download.destroyed.connect(nam.on_adopted_download_destroyed)
return nam.adopted_downloads
def can_clear(self): def can_clear(self):
"""Check if there are finished downloads to clear.""" """Check if there are finished downloads to clear."""

View File

@ -43,6 +43,11 @@ class NetworkManager(QNetworkAccessManager):
"""Our own QNetworkAccessManager. """Our own QNetworkAccessManager.
Attributes: Attributes:
adopted_downloads: If downloads are running with this QNAM but the
associated tab gets closed already, the NAM gets
reparented to the DownloadManager. This counts the
still running downloads, so the QNAM can clean
itself up when this reaches zero again.
_requests: Pending requests. _requests: Pending requests.
_scheme_handlers: A dictionary (scheme -> handler) of supported custom _scheme_handlers: A dictionary (scheme -> handler) of supported custom
schemes. schemes.
@ -62,6 +67,7 @@ class NetworkManager(QNetworkAccessManager):
# http://www.riverbankcomputing.com/pipermail/pyqt/2014-November/035045.html # http://www.riverbankcomputing.com/pipermail/pyqt/2014-November/035045.html
super().__init__(parent) super().__init__(parent)
log.init.debug("NetworkManager init done") log.init.debug("NetworkManager init done")
self.adopted_downloads = 0
self._win_id = win_id self._win_id = win_id
self._tab_id = tab_id self._tab_id = tab_id
self._requests = [] self._requests = []
@ -207,6 +213,19 @@ class NetworkManager(QNetworkAccessManager):
# switched from private mode to normal mode # switched from private mode to normal mode
self._set_cookiejar() self._set_cookiejar()
@pyqtSlot()
def on_adopted_download_destroyed(self):
"""Check if we can clean up if an adopted download was destroyed.
See the description for adopted_downloads for details.
"""
self.adopted_downloads -= 1
log.downloads.debug("Adopted download destroyed, {} left.".format(
self.adopted_downloads))
assert self.adopted_downloads >= 0
if self.adopted_downloads == 0:
self.deleteLater()
# WORKAROUND for: # WORKAROUND for:
# http://www.riverbankcomputing.com/pipermail/pyqt/2014-September/034806.html # http://www.riverbankcomputing.com/pipermail/pyqt/2014-September/034806.html
# #