From 0ac2b713043a772ec47f8b883c1e570299f5272f Mon Sep 17 00:00:00 2001 From: Florian Bruhin Date: Tue, 1 Nov 2016 17:24:54 +0100 Subject: [PATCH] Simplify how filename questions are handled --- qutebrowser/browser/commands.py | 10 ++-- qutebrowser/browser/downloads.py | 28 +++++++---- qutebrowser/browser/webkit/downloads.py | 63 +++++-------------------- 3 files changed, 36 insertions(+), 65 deletions(-) diff --git a/qutebrowser/browser/commands.py b/qutebrowser/browser/commands.py index 84285c9e1..1f2ab4ba4 100644 --- a/qutebrowser/browser/commands.py +++ b/qutebrowser/browser/commands.py @@ -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) diff --git a/qutebrowser/browser/downloads.py b/qutebrowser/browser/downloads.py index 37e23ecbb..7af3cfa07 100644 --- a/qutebrowser/browser/downloads.py +++ b/qutebrowser/browser/downloads.py @@ -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.""" diff --git a/qutebrowser/browser/webkit/downloads.py b/qutebrowser/browser/webkit/downloads.py index 91a038ffd..0855619b6 100644 --- a/qutebrowser/browser/webkit/downloads.py +++ b/qutebrowser/browser/webkit/downloads.py @@ -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