Simplify how filename questions are handled
This commit is contained in:
parent
3b51548d3a
commit
0ac2b71304
@ -1355,14 +1355,16 @@ class CommandDispatcher:
|
||||
if dest is None:
|
||||
suggested_fn = self._current_title() + ".mht"
|
||||
suggested_fn = utils.sanitize_filename(suggested_fn)
|
||||
filename, q = downloads.ask_for_filename_async(
|
||||
suggested_fn, parent=tab, url=tab.url())
|
||||
|
||||
filename = downloads.immediate_download_path()
|
||||
if filename is not None:
|
||||
mhtml.start_download_checked(filename, tab=tab)
|
||||
else:
|
||||
q.answered.connect(functools.partial(
|
||||
question = downloads.get_filename_question(
|
||||
suggested_filename=suggested_fn, url=tab.url(), parent=tab)
|
||||
question.answered.connect(functools.partial(
|
||||
mhtml.start_download_checked, tab=tab))
|
||||
q.ask()
|
||||
message.global_bridge.ask(question, blocking=False)
|
||||
else:
|
||||
mhtml.start_download_checked(dest, tab=tab)
|
||||
|
||||
|
@ -73,6 +73,24 @@ def download_dir():
|
||||
return directory
|
||||
|
||||
|
||||
def immediate_download_path(prompt_download_directory=None):
|
||||
"""Try to get an immediate download path without asking the user.
|
||||
|
||||
If that's possible, we return a path immediately. If not, None is returned.
|
||||
|
||||
Args:
|
||||
prompt_download_directory: If this is something else than None, it
|
||||
will overwrite the
|
||||
storage->prompt-download-directory setting.
|
||||
"""
|
||||
if prompt_download_directory is None:
|
||||
prompt_download_directory = config.get('storage',
|
||||
'prompt-download-directory')
|
||||
|
||||
if not prompt_download_directory:
|
||||
return download_dir()
|
||||
|
||||
|
||||
def _path_suggestion(filename):
|
||||
"""Get the suggested file path.
|
||||
|
||||
@ -525,7 +543,6 @@ class AbstractDownloadManager(QObject):
|
||||
|
||||
Attributes:
|
||||
downloads: A list of active DownloadItems.
|
||||
questions: A list of Question objects to not GC them.
|
||||
_networkmanager: A NetworkManager for generic downloads.
|
||||
|
||||
Signals:
|
||||
@ -548,7 +565,6 @@ class AbstractDownloadManager(QObject):
|
||||
def __init__(self, parent=None):
|
||||
super().__init__(parent)
|
||||
self.downloads = []
|
||||
self.questions = []
|
||||
self._update_timer = usertypes.Timer(self, 'download-update')
|
||||
self._update_timer.timeout.connect(self._update_gui)
|
||||
self._update_timer.setInterval(_REFRESH_INTERVAL)
|
||||
@ -556,14 +572,6 @@ class AbstractDownloadManager(QObject):
|
||||
def __repr__(self):
|
||||
return utils.get_repr(self, downloads=len(self.downloads))
|
||||
|
||||
def _postprocess_question(self, q):
|
||||
"""Postprocess a Question object that is asked."""
|
||||
q.destroyed.connect(functools.partial(self.questions.remove, q))
|
||||
# We set the mode here so that other code that uses ask_for_filename
|
||||
# doesn't need to handle the special download mode.
|
||||
q.mode = usertypes.PromptMode.download
|
||||
self.questions.append(q)
|
||||
|
||||
@pyqtSlot()
|
||||
def _update_gui(self):
|
||||
"""Periodical GUI update of all items."""
|
||||
|
@ -46,43 +46,6 @@ from qutebrowser.browser.webkit.network import networkmanager
|
||||
_RetryInfo = collections.namedtuple('_RetryInfo', ['request', 'manager'])
|
||||
|
||||
|
||||
_DownloadPath = collections.namedtuple('_DownloadPath', ['filename',
|
||||
'question'])
|
||||
|
||||
|
||||
def ask_for_filename_async(suggested_filename, *, url, parent=None,
|
||||
prompt_download_directory=None):
|
||||
"""Prepare a question for a download-path.
|
||||
|
||||
If a filename can be determined directly, it is returned instead.
|
||||
|
||||
Returns a (filename, question)-namedtuple, in which one component is
|
||||
None. filename is a string, question is a usertypes.Question. The
|
||||
question has a special .ask() method that takes no arguments for
|
||||
convenience, as this function does not yet ask the question, it
|
||||
only prepares it.
|
||||
|
||||
Args:
|
||||
suggested_filename: The "default"-name that is pre-entered as path.
|
||||
url: The URL the download originated from.
|
||||
parent: The parent of the question (a QObject).
|
||||
prompt_download_directory: If this is something else than None, it
|
||||
will overwrite the
|
||||
storage->prompt-download-directory setting.
|
||||
"""
|
||||
if prompt_download_directory is None:
|
||||
prompt_download_directory = config.get('storage',
|
||||
'prompt-download-directory')
|
||||
|
||||
if not prompt_download_directory:
|
||||
return _DownloadPath(filename=downloads.download_dir(), question=None)
|
||||
|
||||
q = downloads.get_filename_question(
|
||||
suggested_filename=suggested_filename, url=url, parent=parent)
|
||||
q.ask = lambda: message.global_bridge.ask(q, blocking=False)
|
||||
return _DownloadPath(filename=None, question=q)
|
||||
|
||||
|
||||
class DownloadItem(downloads.AbstractDownloadItem):
|
||||
|
||||
"""A single download currently running.
|
||||
@ -540,27 +503,25 @@ class DownloadManager(downloads.AbstractDownloadManager):
|
||||
download.set_target(target)
|
||||
return download
|
||||
|
||||
# Neither filename nor fileobj were given, prepare a question
|
||||
filename, q = ask_for_filename_async(
|
||||
suggested_filename, parent=self,
|
||||
prompt_download_directory=prompt_download_directory,
|
||||
url=reply.url())
|
||||
# Neither filename nor fileobj were given
|
||||
|
||||
# User doesn't want to be asked, so just use the download_dir
|
||||
filename = downloads.immediate_download_path(prompt_download_directory)
|
||||
if filename is not None:
|
||||
# User doesn't want to be asked, so just use the download_dir
|
||||
target = usertypes.FileDownloadTarget(filename)
|
||||
download.set_target(target)
|
||||
return download
|
||||
|
||||
## FIXME
|
||||
|
||||
# Ask the user for a filename
|
||||
self._postprocess_question(q)
|
||||
q.answered.connect(download.set_target)
|
||||
q.cancelled.connect(download.cancel)
|
||||
download.cancelled.connect(q.abort)
|
||||
download.error.connect(q.abort)
|
||||
q.ask()
|
||||
question = downloads.get_filename_question(
|
||||
suggested_filename=suggested_filename, url=reply.url(),
|
||||
parent=self)
|
||||
question.mode = usertypes.PromptMode.download
|
||||
question.answered.connect(download.set_target)
|
||||
question.cancelled.connect(download.cancel)
|
||||
download.cancelled.connect(question.abort)
|
||||
download.error.connect(question.abort)
|
||||
message.global_bridge.ask(question, blocking=False)
|
||||
|
||||
return download
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user