Always use a global QNAM for downloads

This makes a lot of code eaiser, and we don't have per-tab settings yet
anyways. Also, with QtWebEngine, we can't honour any per-tab settings
for downloads...
This commit is contained in:
Florian Bruhin 2016-11-07 13:33:21 +01:00
parent bc1e4385e0
commit 53e360ec4b
5 changed files with 7 additions and 93 deletions

View File

@ -1335,20 +1335,11 @@ class CommandDispatcher:
else:
tab = self._current_widget()
# FIXME:qtwebengine have a proper API for this
# pylint: disable=protected-access
try:
qnam = tab._widget.page().networkAccessManager()
except AttributeError:
# QtWebEngine
qnam = None
# pylint: enable=protected-access
if dest is None:
target = None
else:
target = usertypes.FileDownloadTarget(dest)
download_manager.get(self._current_url(), qnam=qnam, target=target)
download_manager.get(self._current_url(), target=target)
def _download_mhtml(self, dest=None):
"""Download the current page as an MHTML file, including all assets.

View File

@ -283,19 +283,10 @@ class HintActions:
else:
prompt = None
# FIXME:qtwebengine get a proper API for this
# pylint: disable=protected-access
try:
qnam = elem._elem.webFrame().page().networkAccessManager()
except AttributeError:
# QtWebEngine
qnam = None
# pylint: enable=protected-access
# FIXME:qtwebengine do this with QtWebEngine downloads?
download_manager = objreg.get('qtnetwork-download-manager',
scope='window', window=self._win_id)
download_manager.get(url, qnam=qnam, prompt_download_directory=prompt)
download_manager.get(url, prompt_download_directory=prompt)
def call_userscript(self, elem, context):
"""Call a userscript from a hint.

View File

@ -66,15 +66,9 @@ class DownloadItem(downloads.AbstractDownloadItem):
periodically.
_manager: The DownloadManager which started this download
_reply: The QNetworkReply associated with this download.
Signals:
adopt_download: Emitted when a download is retried and should be
adopted by the QNAM if needed.
arg 0: The new DownloadItem
"""
_MAX_REDIRECTS = 10
adopt_download = pyqtSignal(object) # DownloadItem
def __init__(self, reply, manager):
"""Constructor.
@ -167,9 +161,7 @@ class DownloadItem(downloads.AbstractDownloadItem):
assert self.done
assert not self.successful
new_reply = self._retry_info.manager.get(self._retry_info.request)
new_download = self._manager.fetch(new_reply,
suggested_filename=self.basename)
self.adopt_download.emit(new_download)
self._manager.fetch(new_reply, suggested_filename=self.basename)
self.cancel()
def _get_open_filename(self):
@ -338,14 +330,6 @@ class DownloadItem(downloads.AbstractDownloadItem):
old_reply.deleteLater()
return True
def _uses_nam(self, nam):
"""Check if this download uses the given QNetworkAccessManager."""
running_nam = self._reply is not None and self._reply.manager() is nam
# user could request retry after tab is closed.
retry_nam = (self.done and (not self.successful) and
self._retry_info.manager is nam)
return running_nam or retry_nam
def set_target(self, target):
if isinstance(target, usertypes.FileObjDownloadTarget):
self._set_fileobj(target.fileobj)
@ -427,20 +411,17 @@ class DownloadManager(downloads.AbstractDownloadManager):
suggested_filename=suggested_fn,
**kwargs)
def _fetch_request(self, request, *, qnam=None, **kwargs):
def _fetch_request(self, request, **kwargs):
"""Download a QNetworkRequest to disk.
Args:
request: The QNetworkRequest to download.
qnam: The QNetworkAccessManager to use.
**kwargs: passed to fetch().
Return:
The created DownloadItem.
"""
if qnam is None:
qnam = self._networkmanager
reply = qnam.get(request)
reply = self._networkmanager.get(request)
return self.fetch(reply, **kwargs)
@pyqtSlot('QNetworkReply')
@ -491,18 +472,3 @@ class DownloadManager(downloads.AbstractDownloadManager):
message.global_bridge.ask(question, blocking=False)
return download
def has_downloads_with_nam(self, nam):
"""Check if the DownloadManager has any downloads with the given QNAM.
Args:
nam: The QNetworkAccessManager to check.
Return:
A boolean.
"""
assert nam.adopted_downloads == 0
for download in self.downloads:
if download._uses_nam(nam): # pylint: disable=protected-access
nam.adopt_download(download)
return nam.adopted_downloads

View File

@ -135,11 +135,6 @@ class NetworkManager(QNetworkAccessManager):
"""Our own QNetworkAccessManager.
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.
_scheme_handlers: A dictionary (scheme -> handler) of supported custom
schemes.
@ -161,7 +156,6 @@ class NetworkManager(QNetworkAccessManager):
# http://www.riverbankcomputing.com/pipermail/pyqt/2014-November/035045.html
super().__init__(parent)
log.init.debug("NetworkManager init done")
self.adopted_downloads = 0
self._win_id = win_id
self._tab_id = tab_id
self._requests = []
@ -394,28 +388,6 @@ class NetworkManager(QNetworkAccessManager):
# switched from private mode to normal mode
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()
@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.adopt_download.connect(self.adopt_download)
def set_referer(self, req, current_url):
"""Set the referer header."""
referer_header_conf = config.get('network', 'referer-header')

View File

@ -219,13 +219,7 @@ class BrowserPage(QWebPage):
"""Prepare the web page for being deleted."""
self._is_shutting_down = True
self.shutting_down.emit()
download_manager = objreg.get('qtnetwork-download-manager',
scope='window', window=self._win_id)
nam = self.networkAccessManager()
if download_manager.has_downloads_with_nam(nam):
nam.setParent(download_manager)
else:
nam.shutdown()
self.networkAccessManager().shutdown()
def display_content(self, reply, mimetype):
"""Display a QNetworkReply with an explicitly set mimetype."""
@ -254,7 +248,7 @@ class BrowserPage(QWebPage):
req = QNetworkRequest(request)
download_manager = objreg.get('qtnetwork-download-manager',
scope='window', window=self._win_id)
download_manager.get_request(req, qnam=self.networkAccessManager())
download_manager.get_request(req)
@pyqtSlot('QNetworkReply*')
def on_unsupported_content(self, reply):