Fix checking for errors when a page is loaded.

Because of the error page, we got loadFinished with ok=True even with errors.

Fixes #84.
This commit is contained in:
Florian Bruhin 2014-12-10 16:38:09 +01:00
parent 06cc982ab5
commit 206b5f548e
3 changed files with 34 additions and 8 deletions

View File

@ -39,9 +39,11 @@ class BrowserPage(QWebPage):
"""Our own QWebPage with advanced features.
Attributes:
error_occured: Whether an error occured while loading.
_extension_handlers: Mapping of QWebPage extensions to their handlers.
_networkmnager: The NetworkManager used.
_win_id: The window ID this BrowserPage is associated with.
_ignore_load_started: Whether to ignore the next loadStarted signal.
"""
def __init__(self, win_id, parent=None):
@ -51,12 +53,15 @@ class BrowserPage(QWebPage):
QWebPage.ErrorPageExtension: self._handle_errorpage,
QWebPage.ChooseMultipleFilesExtension: self._handle_multiple_files,
}
self._ignore_load_started = False
self.error_occured = False
self._networkmanager = networkmanager.NetworkManager(win_id, self)
self.setNetworkAccessManager(self._networkmanager)
self.setForwardUnsupportedContent(True)
self.printRequested.connect(self.on_print_requested)
self.downloadRequested.connect(self.on_download_requested)
self.unsupportedContent.connect(self.on_unsupported_content)
self.loadStarted.connect(self.on_load_started)
if PYQT_VERSION > 0x050300:
# WORKAROUND (remove this when we bump the requirements to 5.3.1)
@ -112,6 +117,8 @@ class BrowserPage(QWebPage):
info.error))
return False
else:
self._ignore_load_started = True
self.error_occured = True
log.webview.error("Error while loading {}: {}".format(
urlstr, info.errorString))
log.webview.debug("Error domain: {}, error code: {}".format(
@ -205,6 +212,14 @@ class BrowserPage(QWebPage):
# Unknown mimetype, so download anyways.
download_manager.fetch(reply)
@pyqtSlot()
def on_load_started(self):
"""Reset error_occured when loading of a new page started."""
if self._ignore_load_started:
self._ignore_load_started = False
else:
self.error_occured = False
def userAgentForUrl(self, url):
"""Override QWebPage::userAgentForUrl to customize the user agent."""
ua = config.get('network', 'user-agent')

View File

@ -553,20 +553,25 @@ class TabbedBrowser(tabwidget.TabWidget):
color = utils.interpolate_color(start, stop, perc, system)
self.tabBar().set_tab_indicator_color(idx, color)
def on_load_finished(self, tab, ok):
"""Adjust tab indicator when loading finished."""
def on_load_finished(self, tab):
"""Adjust tab indicator when loading finished.
We don't take loadFinished's ok argument here as it always seems to be
true when the QWebPage has an ErrorPageExtension implemented.
See https://github.com/The-Compiler/qutebrowser/issues/84
"""
try:
idx = self.indexOf(tab)
except RuntimeError:
# We can get signals for tabs we already deleted...
return
if ok:
if tab.page().error_occured:
color = config.get('colors', 'tabs.indicator.error')
else:
start = config.get('colors', 'tabs.indicator.start')
stop = config.get('colors', 'tabs.indicator.stop')
system = config.get('colors', 'tabs.indicator.system')
color = utils.interpolate_color(start, stop, 100, system)
else:
color = config.get('colors', 'tabs.indicator.error')
self.tabBar().set_tab_indicator_color(idx, color)
def resizeEvent(self, e):

View File

@ -366,9 +366,15 @@ class WebView(QWebView):
self._has_ssl_errors = False
self._set_load_status(LoadStatus.loading)
@pyqtSlot(bool)
def on_load_finished(self, ok):
"""Handle auto-insert-mode after loading finished."""
@pyqtSlot()
def on_load_finished(self):
"""Handle auto-insert-mode after loading finished.
We don't take loadFinished's ok argument here as it always seems to be
true when the QWebPage has an ErrorPageExtension implemented.
See https://github.com/The-Compiler/qutebrowser/issues/84
"""
ok = not self.page().error_occured
if ok and not self._has_ssl_errors:
self._set_load_status(LoadStatus.success)
elif ok: