parent
d1e0de236d
commit
f0779f8cc0
@ -171,6 +171,7 @@ class DownloadItem(QObject):
|
|||||||
_read_timer: A QTimer which reads the QNetworkReply into self._buffer
|
_read_timer: A QTimer which reads the QNetworkReply into self._buffer
|
||||||
periodically.
|
periodically.
|
||||||
_retry_info: A RetryInfo instance.
|
_retry_info: A RetryInfo instance.
|
||||||
|
_win_id: The window ID the DownloadItem runs in.
|
||||||
|
|
||||||
Signals:
|
Signals:
|
||||||
data_changed: The downloads metadata changed.
|
data_changed: The downloads metadata changed.
|
||||||
@ -217,6 +218,7 @@ class DownloadItem(QObject):
|
|||||||
self.fileobj = None
|
self.fileobj = None
|
||||||
self._filename = None
|
self._filename = None
|
||||||
self.init_reply(reply)
|
self.init_reply(reply)
|
||||||
|
self._win_id = parent._win_id
|
||||||
|
|
||||||
def __repr__(self):
|
def __repr__(self):
|
||||||
return utils.get_repr(self, basename=self.basename)
|
return utils.get_repr(self, basename=self.basename)
|
||||||
@ -256,6 +258,25 @@ class DownloadItem(QObject):
|
|||||||
name=self.basename, speed=speed, remaining=remaining,
|
name=self.basename, speed=speed, remaining=remaining,
|
||||||
perc=perc, down=down, total=total, errmsg=errmsg))
|
perc=perc, down=down, total=total, errmsg=errmsg))
|
||||||
|
|
||||||
|
def _create_fileobj(self):
|
||||||
|
"""Creates a file object using the internal filename."""
|
||||||
|
try:
|
||||||
|
fileobj = open(self._filename, 'wb')
|
||||||
|
except OSError as e:
|
||||||
|
self._die(e.strerror)
|
||||||
|
else:
|
||||||
|
self.set_fileobj(fileobj)
|
||||||
|
|
||||||
|
def _create_overwrite_question(self):
|
||||||
|
"""Create a Question object to be asked."""
|
||||||
|
q = usertypes.Question(self)
|
||||||
|
q.text = " already exists. Overwrite? (y/n)"
|
||||||
|
q.mode = usertypes.PromptMode.yesno
|
||||||
|
q.answered_yes.connect(self._create_fileobj)
|
||||||
|
q.answered_no.connect(functools.partial(self.cancel, False))
|
||||||
|
q.cancelled.connect(functools.partial(self.cancel, False))
|
||||||
|
return q
|
||||||
|
|
||||||
def _die(self, msg):
|
def _die(self, msg):
|
||||||
"""Abort the download and emit an error."""
|
"""Abort the download and emit an error."""
|
||||||
assert not self.successful
|
assert not self.successful
|
||||||
@ -312,8 +333,12 @@ class DownloadItem(QObject):
|
|||||||
return utils.interpolate_color(
|
return utils.interpolate_color(
|
||||||
start, stop, self.stats.percentage(), system)
|
start, stop, self.stats.percentage(), system)
|
||||||
|
|
||||||
def cancel(self):
|
def cancel(self, remove_data=True):
|
||||||
"""Cancel the download."""
|
"""Cancel the download.
|
||||||
|
|
||||||
|
Args:
|
||||||
|
remove_data: Whether to remove the downloaded data.
|
||||||
|
"""
|
||||||
log.downloads.debug("cancelled")
|
log.downloads.debug("cancelled")
|
||||||
self._read_timer.stop()
|
self._read_timer.stop()
|
||||||
self.cancelled.emit()
|
self.cancelled.emit()
|
||||||
@ -325,7 +350,8 @@ class DownloadItem(QObject):
|
|||||||
if self.fileobj is not None:
|
if self.fileobj is not None:
|
||||||
self.fileobj.close()
|
self.fileobj.close()
|
||||||
try:
|
try:
|
||||||
if self._filename is not None and os.path.exists(self._filename):
|
if (self._filename is not None and os.path.exists(self._filename)
|
||||||
|
and remove_data):
|
||||||
os.remove(self._filename)
|
os.remove(self._filename)
|
||||||
except OSError:
|
except OSError:
|
||||||
log.downloads.exception("Failed to remove partial file")
|
log.downloads.exception("Failed to remove partial file")
|
||||||
@ -376,12 +402,16 @@ class DownloadItem(QObject):
|
|||||||
self._filename = os.path.join(download_dir, filename)
|
self._filename = os.path.join(download_dir, filename)
|
||||||
self.basename = filename
|
self.basename = filename
|
||||||
log.downloads.debug("Setting filename to {}".format(filename))
|
log.downloads.debug("Setting filename to {}".format(filename))
|
||||||
try:
|
if os.path.isfile(self._filename):
|
||||||
fileobj = open(self._filename, 'wb')
|
# The file already exists, so ask the user if it should be
|
||||||
except OSError as e:
|
# overwritten.
|
||||||
self._die(e.strerror)
|
q = self._create_overwrite_question()
|
||||||
|
q.text = self._filename + q.text
|
||||||
|
message_bridge = objreg.get('message-bridge', scope='window',
|
||||||
|
window=self._win_id)
|
||||||
|
message_bridge.ask(q, blocking=False)
|
||||||
else:
|
else:
|
||||||
self.set_fileobj(fileobj)
|
self._create_fileobj()
|
||||||
|
|
||||||
def set_fileobj(self, fileobj):
|
def set_fileobj(self, fileobj):
|
||||||
""""Set the file object to write the download to.
|
""""Set the file object to write the download to.
|
||||||
|
Loading…
Reference in New Issue
Block a user