Downloads now shows path in question.

This commit is contained in:
Joel Torstensson 2015-02-19 10:51:06 +01:00
parent 40af99bacc
commit 82deaeed2e
2 changed files with 25 additions and 34 deletions

View File

@ -38,7 +38,6 @@ from qutebrowser.config import config
from qutebrowser.commands import cmdexc, cmdutils from qutebrowser.commands import cmdexc, cmdutils
from qutebrowser.utils import (message, usertypes, log, utils, urlutils, from qutebrowser.utils import (message, usertypes, log, utils, urlutils,
objreg, standarddir, qtutils) objreg, standarddir, qtutils)
from qutebrowser.browser import http
from qutebrowser.browser.network import networkmanager from qutebrowser.browser.network import networkmanager
@ -374,6 +373,13 @@ class DownloadItem(QObject):
log.downloads.exception("Failed to remove partial file") log.downloads.exception("Failed to remove partial file")
@pyqtSlot() @pyqtSlot()
def download_dir(self):
"""Get the download directory to use."""
download_dir = config.get('storage', 'download-directory')
if download_dir is None:
download_dir = standarddir.get(QStandardPaths.DownloadLocation)
return download_dir + os.sep
def retry(self): def retry(self):
"""Retry a failed download.""" """Retry a failed download."""
self.cancel() self.cancel()
@ -415,10 +421,7 @@ class DownloadItem(QObject):
else: else:
# We only got a filename (without directory) from the user, so we # We only got a filename (without directory) from the user, so we
# save it under that filename in the default directory. # save it under that filename in the default directory.
download_dir = config.get('storage', 'download-directory') self._filename = os.path.join(self.download_dir(), filename)
if download_dir is None:
download_dir = standarddir.download()
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))
if os.path.isfile(self._filename): if os.path.isfile(self._filename):
@ -604,8 +607,7 @@ class DownloadManager(QAbstractListModel):
ui -> remove-finished-downloads is set to false. ui -> remove-finished-downloads is set to false.
Return: Return:
If the download could start immediately, (fileobj/filename given), If the url is valid, the created DownloadItem.
the created DownloadItem.
If not, None. If not, None.
""" """
@ -630,10 +632,7 @@ class DownloadManager(QAbstractListModel):
ui -> remove-finished-downloads is set to false. ui -> remove-finished-downloads is set to false.
Return: Return:
If the download could start immediately, (fileobj/filename given), The created DownloadItem.
the created DownloadItem.
If not, None.
""" """
if fileobj is not None and filename is not None: if fileobj is not None and filename is not None:
raise TypeError("Only one of fileobj/filename may be given!") raise TypeError("Only one of fileobj/filename may be given!")
@ -644,21 +643,14 @@ class DownloadManager(QAbstractListModel):
if fileobj is not None or filename is not None: if fileobj is not None or filename is not None:
return self.fetch_request(request, page, fileobj, filename, return self.fetch_request(request, page, fileobj, filename,
auto_remove) auto_remove)
q = self._prepare_question() suggested_filename = urlutils.filename_from_url(request.url())
filename = urlutils.filename_from_url(request.url())
encoding = sys.getfilesystemencoding() encoding = sys.getfilesystemencoding()
filename = utils.force_encoding(filename, encoding) suggested_filename = utils.force_encoding(suggested_filename, encoding)
q.default = filename return self.fetch_request(request, page, fileobj, filename,
message_bridge = objreg.get('message-bridge', scope='window', auto_remove, suggested_filename)
window=self._win_id)
q.answered.connect(
lambda fn: self.fetch_request(request, filename=fn, page=page,
auto_remove=auto_remove))
message_bridge.ask(q, blocking=False)
return None
def fetch_request(self, request, page=None, fileobj=None, filename=None, def fetch_request(self, request, page=None, fileobj=None, filename=None,
auto_remove=False): auto_remove=False, suggested_filename=None):
"""Download a QNetworkRequest to disk. """Download a QNetworkRequest to disk.
Args: Args:
@ -677,10 +669,12 @@ class DownloadManager(QAbstractListModel):
else: else:
nam = page.networkAccessManager() nam = page.networkAccessManager()
reply = nam.get(request) reply = nam.get(request)
return self.fetch(reply, fileobj, filename, auto_remove) return self.fetch(reply, fileobj, filename, auto_remove,
suggested_filename)
@pyqtSlot('QNetworkReply') @pyqtSlot('QNetworkReply')
def fetch(self, reply, fileobj=None, filename=None, auto_remove=False): def fetch(self, reply, fileobj=None, filename=None, auto_remove=False,
suggested_filename=None):
"""Download a QNetworkReply to disk. """Download a QNetworkReply to disk.
Args: Args:
@ -699,8 +693,6 @@ class DownloadManager(QAbstractListModel):
suggested_filename = os.path.basename(filename) suggested_filename = os.path.basename(filename)
elif fileobj is not None and getattr(fileobj, 'name', None): elif fileobj is not None and getattr(fileobj, 'name', None):
suggested_filename = fileobj.name suggested_filename = fileobj.name
else:
_inline, suggested_filename = http.parse_content_disposition(reply)
log.downloads.debug("fetch: {} -> {}".format(reply.url(), log.downloads.debug("fetch: {} -> {}".format(reply.url(),
suggested_filename)) suggested_filename))
download = DownloadItem(reply, self._win_id, self) download = DownloadItem(reply, self._win_id, self)
@ -729,10 +721,7 @@ class DownloadManager(QAbstractListModel):
download.autoclose = False download.autoclose = False
else: else:
q = self._prepare_question() q = self._prepare_question()
encoding = sys.getfilesystemencoding() q.default = download.download_dir()
suggested_filename = utils.force_encoding(suggested_filename,
encoding)
q.default = suggested_filename
q.answered.connect(download.set_filename) q.answered.connect(download.set_filename)
q.cancelled.connect(download.cancel) q.cancelled.connect(download.cancel)
download.cancelled.connect(q.abort) download.cancelled.connect(q.abort)

View File

@ -280,12 +280,13 @@ class BrowserPage(QWebPage):
At some point we might want to implement the MIME Sniffing standard At some point we might want to implement the MIME Sniffing standard
here: http://mimesniff.spec.whatwg.org/ here: http://mimesniff.spec.whatwg.org/
""" """
inline, _suggested_filename = http.parse_content_disposition(reply) inline, suggested_filename = http.parse_content_disposition(reply)
download_manager = objreg.get('download-manager', scope='window', download_manager = objreg.get('download-manager', scope='window',
window=self._win_id) window=self._win_id)
if not inline: if not inline:
# Content-Disposition: attachment -> force download # Content-Disposition: attachment -> force download
download_manager.fetch(reply) download_manager.fetch(reply,
suggested_filename=suggested_filename)
return return
mimetype, _rest = http.parse_content_type(reply) mimetype, _rest = http.parse_content_type(reply)
if mimetype == 'image/jpg': if mimetype == 'image/jpg':
@ -300,7 +301,8 @@ class BrowserPage(QWebPage):
self.display_content, reply, 'image/jpeg')) self.display_content, reply, 'image/jpeg'))
else: else:
# Unknown mimetype, so download anyways. # Unknown mimetype, so download anyways.
download_manager.fetch(reply) download_manager.fetch(reply,
suggested_filename=suggested_filename)
@pyqtSlot() @pyqtSlot()
def on_load_started(self): def on_load_started(self):