Show error messages in downloads.

This commit is contained in:
Florian Bruhin 2014-10-15 21:22:53 +02:00
parent 971e4f4372
commit 7e820a0e82
2 changed files with 34 additions and 12 deletions

View File

@ -62,6 +62,7 @@ class DownloadItem(QObject):
_reply: The QNetworkReply associated with this download. _reply: The QNetworkReply associated with this download.
_last_done: The count of bytes which where downloaded when calculating _last_done: The count of bytes which where downloaded when calculating
the speed the last time. the speed the last time.
_error: The current error message, or None
Signals: Signals:
data_changed: The downloads metadata changed. data_changed: The downloads metadata changed.
@ -88,6 +89,7 @@ class DownloadItem(QObject):
self._reply = reply self._reply = reply
self._bytes_total = None self._bytes_total = None
self._speed = 0 self._speed = 0
self._error = None
self.basename = '???' self.basename = '???'
samples = int(self.SPEED_AVG_WINDOW * samples = int(self.SPEED_AVG_WINDOW *
(1000 / self.SPEED_REFRESH_INTERVAL)) (1000 / self.SPEED_REFRESH_INTERVAL))
@ -127,9 +129,13 @@ class DownloadItem(QObject):
down = utils.format_size(self._bytes_done, suffix='B') down = utils.format_size(self._bytes_done, suffix='B')
perc = self._percentage() perc = self._percentage()
remaining = self._remaining_time() remaining = self._remaining_time()
if self._error is None:
errmsg = ""
else:
errmsg = " - {}".format(self._error)
if all(e is None for e in (perc, remaining, self._bytes_total)): if all(e is None for e in (perc, remaining, self._bytes_total)):
return ('{name} [{speed:>10}|{down}]'.format( return ('{name} [{speed:>10}|{down}]{errmsg}'.format(
name=self.basename, speed=speed, down=down)) name=self.basename, speed=speed, down=down, errmsg=errmsg))
if perc is None: if perc is None:
perc = '??' perc = '??'
else: else:
@ -140,9 +146,9 @@ class DownloadItem(QObject):
remaining = utils.format_seconds(remaining) remaining = utils.format_seconds(remaining)
total = utils.format_size(self._bytes_total, suffix='B') total = utils.format_size(self._bytes_total, suffix='B')
return ('{name} [{speed:>10}|{remaining:>5}|{perc:>2}%|' return ('{name} [{speed:>10}|{remaining:>5}|{perc:>2}%|'
'{down}/{total}]'.format(name=self.basename, speed=speed, '{down}/{total}]{errmsg}'.format(
remaining=remaining, perc=perc, name=self.basename, speed=speed, remaining=remaining,
down=down, total=total)) perc=perc, down=down, total=total, errmsg=errmsg))
def _die(self, msg): def _die(self, msg):
"""Abort the download and emit an error.""" """Abort the download and emit an error."""
@ -150,17 +156,19 @@ class DownloadItem(QObject):
self._reply.finished.disconnect() self._reply.finished.disconnect()
self._reply.error.disconnect() self._reply.error.disconnect()
self._reply.readyRead.disconnect() self._reply.readyRead.disconnect()
self._error = msg
self._bytes_done = self._bytes_total self._bytes_done = self._bytes_total
self.timer.stop() self.timer.stop()
self.error.emit(msg) self.error.emit(msg)
self._reply.abort() self._reply.abort()
self._reply.deleteLater() self._reply.deleteLater()
self._reply = None
if self._fileobj is not None: if self._fileobj is not None:
try: try:
self._fileobj.close() self._fileobj.close()
except OSError as e: except OSError as e:
self.error.emit(e.strerror) self.error.emit(e.strerror)
self.finished.emit() self.data_changed.emit()
def _percentage(self): def _percentage(self):
"""The current download percentage, or None if unknown.""" """The current download percentage, or None if unknown."""
@ -187,7 +195,10 @@ class DownloadItem(QObject):
start = config.get('colors', 'downloads.bg.start') start = config.get('colors', 'downloads.bg.start')
stop = config.get('colors', 'downloads.bg.stop') stop = config.get('colors', 'downloads.bg.stop')
system = config.get('colors', 'downloads.bg.system') system = config.get('colors', 'downloads.bg.system')
if self._percentage() is None: error = config.get('colors', 'downloads.bg.error')
if self._error is not None:
return error
elif self._percentage() is None:
return start return start
else: else:
return utils.interpolate_color(start, stop, self._percentage(), return utils.interpolate_color(start, stop, self._percentage(),
@ -198,6 +209,7 @@ class DownloadItem(QObject):
log.downloads.debug("cancelled") log.downloads.debug("cancelled")
self.cancelled.emit() self.cancelled.emit()
self._is_cancelled = True self._is_cancelled = True
if self._reply is not None:
self._reply.abort() self._reply.abort()
self._reply.deleteLater() self._reply.deleteLater()
if self._fileobj is not None: if self._fileobj is not None:
@ -389,7 +401,8 @@ class DownloadManager(QAbstractListModel):
functools.partial(self.on_finished, download)) functools.partial(self.on_finished, download))
download.data_changed.connect( download.data_changed.connect(
functools.partial(self.on_data_changed, download)) functools.partial(self.on_data_changed, download))
download.error.connect(self.on_error) download.error.connect(
functools.partial(self.on_error, download))
download.basename = suggested_filename download.basename = suggested_filename
idx = len(self.downloads) + 1 idx = len(self.downloads) + 1
self.beginInsertRows(QModelIndex(), idx, idx) self.beginInsertRows(QModelIndex(), idx, idx)
@ -428,8 +441,8 @@ class DownloadManager(QAbstractListModel):
qtutils.ensure_valid(model_idx) qtutils.ensure_valid(model_idx)
self.dataChanged.emit(model_idx, model_idx) self.dataChanged.emit(model_idx, model_idx)
@pyqtSlot(str) @pyqtSlot(DownloadItem, str)
def on_error(self, msg): def on_error(self, download, msg):
"""Display error message on download errors.""" """Display error message on download errors."""
message.error('last-focused', "Download error: {}".format(msg)) message.error('last-focused', "Download error: {}".format(msg))
@ -465,6 +478,11 @@ class DownloadManager(QAbstractListModel):
data = item.bg_color() data = item.bg_color()
elif role == ModelRole.item: elif role == ModelRole.item:
data = item data = item
elif role == Qt.ToolTipRole:
if item._error is None:
data = QVariant()
else:
return item._error
else: else:
data = QVariant() data = QVariant()
return data return data

View File

@ -714,6 +714,10 @@ DATA = collections.OrderedDict([
('downloads.bg.system', ('downloads.bg.system',
SettingValue(typ.ColorSystem(), 'rgb'), SettingValue(typ.ColorSystem(), 'rgb'),
"Color gradient interpolation system for downloads."), "Color gradient interpolation system for downloads."),
('downloads.bg.error',
SettingValue(typ.QtColor(), 'red'),
"Background color for downloads with errors."),
)), )),
('fonts', sect.KeyValue( ('fonts', sect.KeyValue(