qutebrowser/qutebrowser/browser/downloads.py

352 lines
13 KiB
Python
Raw Normal View History

# Copyright 2014 Florian Bruhin (The Compiler) <mail@qutebrowser.org>
#
# This file is part of qutebrowser.
#
# qutebrowser is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# qutebrowser is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with qutebrowser. If not, see <http://www.gnu.org/licenses/>.
"""Download manager."""
import os.path
2014-06-11 22:33:40 +02:00
from PyQt5.QtCore import pyqtSlot, pyqtSignal, QObject, QTimer
from PyQt5.QtNetwork import QNetworkReply
2014-06-11 21:55:23 +02:00
import qutebrowser.config.config as config
import qutebrowser.utils.message as message
from qutebrowser.utils.log import downloads as logger
2014-06-11 21:55:23 +02:00
from qutebrowser.utils.usertypes import PromptMode
from qutebrowser.utils.misc import (interpolate_color, format_seconds,
format_size)
2014-06-11 17:27:39 +02:00
class DownloadItem(QObject):
"""A single download currently running.
2014-06-11 21:55:23 +02:00
Class attributes:
REFRESH_INTERVAL: How often to refresh the speed, in msec.
2014-06-11 17:27:39 +02:00
Attributes:
reply: The QNetworkReply associated with this download.
percentage: How many percent were downloaded successfully.
2014-06-11 21:55:23 +02:00
bytes_done: How many bytes there are already downloaded.
bytes_total: The total count of bytes.
speed: The current download speed, in bytes per second.
fileobj: The file object to download the file to.
_last_done: The count of bytes which where downloaded when calculating
the speed the last time.
2014-06-12 23:29:34 +02:00
_last_percentage: The remembered percentage for data_changed.
2014-06-11 22:33:40 +02:00
Signals:
2014-06-12 23:29:34 +02:00
data_changed: The downloads metadata changed.
2014-06-12 08:02:44 +02:00
finished: The download was finished.
error: An error with the download occured.
arg: The error message as string.
2014-06-11 17:27:39 +02:00
"""
REFRESH_INTERVAL = 200
2014-06-12 23:29:34 +02:00
data_changed = pyqtSignal()
2014-06-12 08:02:44 +02:00
finished = pyqtSignal()
error = pyqtSignal(str)
2014-06-11 21:55:23 +02:00
def __init__(self, reply, parent=None):
2014-06-11 21:55:23 +02:00
"""Constructor.
Args:
reply: The QNetworkReply to download.
"""
2014-06-11 17:27:39 +02:00
super().__init__(parent)
self.reply = reply
2014-06-11 21:55:23 +02:00
self.bytes_done = None
self.bytes_total = None
self.speed = None
2014-06-12 23:29:34 +02:00
self.basename = '???'
self.fileobj = None
self._do_delayed_write = False
2014-06-11 21:55:23 +02:00
self._last_done = None
2014-06-11 22:33:40 +02:00
self._last_percentage = None
reply.setReadBufferSize(16 * 1024 * 1024)
2014-06-11 21:55:23 +02:00
reply.downloadProgress.connect(self.on_download_progress)
reply.finished.connect(self.on_reply_finished)
reply.error.connect(self.on_reply_error)
2014-06-12 10:18:02 +02:00
reply.readyRead.connect(self.on_ready_read)
2014-06-11 21:55:23 +02:00
self.timer = QTimer()
self.timer.timeout.connect(self.update_speed)
self.timer.setInterval(self.REFRESH_INTERVAL)
self.timer.start()
2014-06-12 23:29:34 +02:00
def __str__(self):
"""Get the download as a string.
Example: foo.pdf [699.2kB/s|0.34|16%|4.253/25.124]
2014-06-12 23:29:34 +02:00
"""
perc = 0 if self.percentage is None else round(self.percentage)
remaining = (format_seconds(self.remaining_time)
if self.remaining_time is not None else '?')
speed = format_size(self.speed, suffix='B/s')
down = format_size(self.bytes_done, suffix='B')
total = format_size(self.bytes_total, suffix='B')
return ('{name} [{speed:>10}|{remaining:>5}|{perc:>2}%|'
'{down}/{total}]'.format(name=self.basename, speed=speed,
remaining=remaining, perc=perc,
down=down, total=total))
2014-06-12 23:29:34 +02:00
def _die(self, msg):
"""Abort the download and emit an error."""
self.error.emit(msg)
self.reply.abort()
self.reply.deleteLater()
if self.fileobj is not None:
try:
self.fileobj.close()
except OSError as e:
self.error.emit(e.strerror)
self.finished.emit()
2014-06-11 21:55:23 +02:00
@property
def percentage(self):
2014-06-11 21:58:06 +02:00
"""Property to get the current download percentage."""
2014-06-11 21:55:23 +02:00
if self.bytes_total == -1:
return -1
elif self.bytes_total == 0:
return 0
elif self.bytes_done is None or self.bytes_total is None:
return None
else:
return 100 * self.bytes_done / self.bytes_total
2014-06-11 17:27:39 +02:00
2014-06-12 23:29:34 +02:00
@property
def remaining_time(self):
"""Property to get the remaining download time in seconds."""
if (self.bytes_total is None or self.bytes_total <= 0 or
self.speed is None or self.speed == 0):
return None
return (self.bytes_total - self.bytes_done) / self.speed
2014-06-12 21:43:30 +02:00
def bg_color(self):
"""Background color to be shown."""
2014-06-13 07:41:51 +02:00
start = config.get('colors', 'downloads.bg.start')
stop = config.get('colors', 'downloads.bg.stop')
system = config.get('colors', 'downloads.bg.system')
2014-06-12 21:43:30 +02:00
if self.percentage is None:
return start
else:
return interpolate_color(start, stop, self.percentage, system)
def cancel(self):
"""Cancel the download."""
logger.debug("cancelled")
self.reply.abort()
self.reply.deleteLater()
self.finished.emit()
def set_filename(self, filename):
"""Set the filename to save the download to.
Args:
filename: The full filename to save the download to.
None: special value to stop the download.
"""
if self.fileobj is not None:
raise ValueError("Filename was already set! filename: {}, "
"existing: {}".format(filename, self.fileobj))
2014-06-12 23:29:34 +02:00
self.basename = os.path.basename(filename)
try:
self.fileobj = open(filename, 'wb')
if self._do_delayed_write:
# Downloading to the buffer in RAM has already finished so we
# write out the data and clean up now.
self.delayed_write()
else:
# Since the buffer already might be full, on_ready_read might
# not be called at all anymore, so we force it here to flush
# the buffer and continue receiving new data.
self.on_ready_read()
except OSError as e:
self._die(e.strerror)
def delayed_write(self):
"""Write buffered data to disk and finish the QNetworkReply."""
logger.debug("Doing delayed write...")
self._do_delayed_write = False
self.fileobj.write(self.reply.readAll())
self.fileobj.close()
self.reply.close()
self.reply.deleteLater()
self.finished.emit()
logger.debug("Download finished")
2014-06-11 17:27:39 +02:00
@pyqtSlot(int, int)
2014-06-11 21:55:23 +02:00
def on_download_progress(self, bytes_done, bytes_total):
2014-06-11 21:58:06 +02:00
"""Upload local variables when the download progress changed.
Args:
bytes_done: How many bytes are downloaded.
bytes_total: How many bytes there are to download in total.
"""
2014-06-11 21:55:23 +02:00
self.bytes_done = bytes_done
self.bytes_total = bytes_total
2014-06-12 23:29:34 +02:00
self.data_changed.emit()
2014-06-11 21:55:23 +02:00
@pyqtSlot()
def on_reply_finished(self):
"""Clean up when the download was finished.
Note when this gets called, only the QNetworkReply has finished. This
doesn't mean the download (i.e. writing data to the disk) is finished
as well. Therefore, we can't close() the QNetworkReply in here yet.
"""
2014-06-11 21:55:23 +02:00
self.bytes_done = self.bytes_total
self.timer.stop()
logger.debug("Reply finished, fileobj {}".format(self.fileobj))
if self.fileobj is None:
# We'll handle emptying the buffer and cleaning up as soon as the
# filename is set.
self._do_delayed_write = True
else:
# We can do a "delayed" write immediately to empty the buffer and
# clean up.
self.delayed_write()
2014-06-11 21:55:23 +02:00
@pyqtSlot()
def on_ready_read(self):
"""Read available data and save file when ready to read."""
if self.fileobj is None:
# No filename has been set yet, so we don't empty the buffer.
return
try:
self.fileobj.write(self.reply.readAll())
except OSError as e:
self._die(e.strerror)
@pyqtSlot(int)
def on_reply_error(self, code):
"""Handle QNetworkReply errors."""
if code == QNetworkReply.OperationCanceledError:
return
else:
self.error.emit(self.reply.errorString())
2014-06-11 21:55:23 +02:00
@pyqtSlot()
def update_speed(self):
"""Recalculate the current download speed."""
if self._last_done is None:
if self.bytes_done is None:
self.speed = 0
else:
delta = self.bytes_done
self.speed = delta * 1000 / self.REFRESH_INTERVAL
2014-06-11 17:27:39 +02:00
else:
2014-06-11 21:55:23 +02:00
delta = self.bytes_done - self._last_done
self.speed = delta * 1000 / self.REFRESH_INTERVAL
2014-06-11 21:55:23 +02:00
logger.debug("Download speed: {} bytes/sec".format(self.speed))
self._last_done = self.bytes_done
2014-06-12 23:29:34 +02:00
self.data_changed.emit()
2014-06-11 17:27:39 +02:00
class DownloadManager(QObject):
2014-06-12 08:02:44 +02:00
"""Manager for running downloads.
Signals:
download_about_to_be_added: A new download will be added.
arg: The index of the new download.
download_added: A new download was added.
download_about_to_be_finished: A download will be finished and removed.
arg: The index of the new download.
download_finished: A download was finished and removed.
data_changed: The data to be displayed in a model changed.
arg: The index of the download which changed.
2014-06-12 08:02:44 +02:00
"""
download_about_to_be_added = pyqtSignal(int)
download_added = pyqtSignal()
download_about_to_be_finished = pyqtSignal(int)
download_finished = pyqtSignal()
data_changed = pyqtSignal(int)
def __init__(self, parent=None):
super().__init__(parent)
self.downloads = []
def _get_filename(self, reply):
"""Get a suitable filename to download a file to.
Args:
2014-06-11 21:58:06 +02:00
reply: The QNetworkReply to get a filename for.
"""
filename = None
# First check if the Content-Disposition header has a filename
# attribute.
if reply.hasRawHeader('Content-Disposition'):
header = reply.rawHeader('Content-Disposition')
data = header.split(':', maxsplit=1)[1].strip()
for pair in data.split(';'):
if '=' in pair:
key, value = pair.split('=')
if key == 'filename':
filename = value.strip('"')
break
# Then try to get filename from url
if not filename:
filename = reply.url().path()
# If that fails as well, use a fallback
if not filename:
filename = 'qutebrowser-download'
return os.path.basename(filename)
@pyqtSlot('QNetworkReply')
def fetch(self, reply):
"""Download a QNetworkReply to disk.
Args:
reply: The QNetworkReply to download.
"""
2014-06-11 21:55:23 +02:00
suggested_filename = self._get_filename(reply)
download_location = config.get('storage', 'download-directory')
2014-06-12 23:29:34 +02:00
suggested_filepath = os.path.join(download_location,
2014-06-11 21:55:23 +02:00
suggested_filename)
2014-06-12 23:29:34 +02:00
logger.debug("fetch: {} -> {}".format(reply.url(), suggested_filepath))
download = DownloadItem(reply)
download.finished.connect(self.on_finished)
2014-06-12 23:29:34 +02:00
download.data_changed.connect(self.on_data_changed)
download.error.connect(self.on_error)
2014-06-12 23:29:34 +02:00
download.basename = suggested_filename
self.download_about_to_be_added.emit(len(self.downloads) + 1)
self.downloads.append(download)
self.download_added.emit()
message.question("Save file to:", mode=PromptMode.text,
handler=download.set_filename,
cancelled_handler=download.cancel,
2014-06-12 23:29:34 +02:00
default=suggested_filepath)
2014-06-12 08:02:44 +02:00
@pyqtSlot()
def on_finished(self):
2014-06-12 17:56:28 +02:00
"""Remove finished download."""
2014-06-12 08:02:44 +02:00
idx = self.downloads.index(self.sender())
self.download_about_to_be_finished.emit(idx)
del self.downloads[idx]
self.download_finished.emit()
@pyqtSlot()
def on_data_changed(self):
2014-06-12 17:56:28 +02:00
"""Emit data_changed signal when download data changed."""
idx = self.downloads.index(self.sender())
self.data_changed.emit(idx)
@pyqtSlot(str)
def on_error(self, msg):
2014-06-12 17:56:28 +02:00
"""Display error message on download errors."""
message.error("Download error: {}".format(msg), queue=True)