Coverage for qutebrowser/mainwindow/prompt.py : 37%

Hot-keys on this page
r m x p toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
# vim: ft=python fileencoding=utf-8 sts=4 sw=4 et:
# Copyright 2016-2018 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/>.
QItemSelectionModel, QObject, QEventLoop) QLabel, QFileSystemModel, QTreeView, QSizePolicy, QSpacerItem)
class AuthInfo:
"""Authentication info returned by a prompt."""
"""Base class for errors in this module."""
"""Raised when the prompt class doesn't support the requested operation."""
"""Global manager and queue for upcoming prompts.
The way in which multiple questions are handled deserves some explanation.
If a question is blocking, we *need* to ask it immediately, and can't wait for previous questions to finish. We could theoretically ask a blocking question inside of another blocking one, so in ask_question we simply save the current question on the stack, let the user answer the *most recent* question, and then restore the previous state.
With a non-blocking question, things are a bit easier. We simply add it to self._queue if we're still busy handling another question, since it can be answered at any time.
In either case, as soon as we finished handling a question, we call _pop_later() which schedules a _pop to ask the next question in _queue. We schedule it rather than doing it immediately because then the order of how things happen is clear, e.g. on_mode_left can't happen after we already set up the *new* question.
Attributes: _shutting_down: Whether we're currently shutting down the prompter and should ignore future questions to avoid segfaults. _loops: A list of local EventLoops to spin in when blocking. _queue: A deque of waiting questions. _question: The current Question object if we're handling a question.
Signals: show_prompts: Emitted with a Question object when prompts should be shown. """
super().__init__(parent) self._question = None self._shutting_down = False self._loops = [] self._queue = collections.deque() message.global_bridge.mode_left.connect(self._on_mode_left)
def __repr__(self): return utils.get_repr(self, loops=len(self._loops), queue=len(self._queue), question=self._question)
"""Helper to call self._pop as soon as everything else is done.""" QTimer.singleShot(0, self._pop)
"""Pop a question from the queue and ask it, if there are any.""" log.prompt.debug("Popping from queue {}".format(self._queue)) if self._queue: question = self._queue.popleft() if not sip.isdeleted(question): # the question could already be deleted, e.g. by a cancelled # download. See # https://github.com/qutebrowser/qutebrowser/issues/415 self.ask_question(question, blocking=False)
"""Cancel all blocking questions.
Quits and removes all running event loops.
Return: True if loops needed to be aborted, False otherwise. """ log.prompt.debug("Shutting down with loops {}".format(self._loops)) self._shutting_down = True if self._loops: for loop in self._loops: loop.quit() loop.deleteLater() return True else: return False
def ask_question(self, question, blocking): """Display a prompt for a given question.
Args: question: The Question object to ask. blocking: If True, this function blocks and returns the result.
Return: The answer of the user when blocking=True. None if blocking=False. """ log.prompt.debug("Asking question {}, blocking {}, loops {}, queue " "{}".format(question, blocking, self._loops, self._queue))
if self._shutting_down: # If we're currently shutting down we have to ignore this question # to avoid segfaults - see # https://github.com/qutebrowser/qutebrowser/issues/95 log.prompt.debug("Ignoring question because we're shutting down.") question.abort() return None
if self._question is not None and not blocking: # We got an async question, but we're already busy with one, so we # just queue it up for later. log.prompt.debug("Adding {} to queue.".format(question)) self._queue.append(question) return None
if blocking: # If we're blocking we save the old question on the stack, so we # can restore it after exec, if exec gets called multiple times. log.prompt.debug("New question is blocking, saving {}".format( self._question)) old_question = self._question if old_question is not None: old_question.interrupted = True
self._question = question self.show_prompts.emit(question)
if blocking: loop = qtutils.EventLoop() self._loops.append(loop) loop.destroyed.connect(lambda: self._loops.remove(loop)) question.completed.connect(loop.quit) question.completed.connect(loop.deleteLater) log.prompt.debug("Starting loop.exec_() for {}".format(question)) loop.exec_(QEventLoop.ExcludeSocketNotifiers) log.prompt.debug("Ending loop.exec_() for {}".format(question))
log.prompt.debug("Restoring old question {}".format(old_question)) self._question = old_question self.show_prompts.emit(old_question) if old_question is None: # Nothing left to restore, so we can go back to popping async # questions. if self._queue: self._pop_later()
return question.answer else: question.completed.connect(self._pop_later) return None
def _on_mode_left(self, mode): """Abort question when a prompt mode was left.""" if mode not in [usertypes.KeyMode.prompt, usertypes.KeyMode.yesno]: return if self._question is None: return
log.prompt.debug("Left mode {}, hiding {}".format( mode, self._question)) self.show_prompts.emit(None) if self._question.answer is None and not self._question.is_aborted: log.prompt.debug("Cancelling {} because {} was left".format( self._question, mode)) self._question.cancel() self._question = None
"""Container for prompts to be shown above the statusbar.
This is a per-window object, however each window shows the same prompt.
Attributes: _layout: The layout used to show prompts in. _win_id: The window ID this object is associated with.
Signals: update_geometry: Emitted when the geometry should be updated. """
QWidget#PromptContainer { {% if conf.statusbar.position == 'top' %} border-bottom-left-radius: {{ conf.prompt.radius }}px; border-bottom-right-radius: {{ conf.prompt.radius }}px; {% else %} border-top-left-radius: {{ conf.prompt.radius }}px; border-top-right-radius: {{ conf.prompt.radius }}px; {% endif %} }
QWidget { font: {{ conf.fonts.prompts }}; color: {{ conf.colors.prompts.fg }}; background-color: {{ conf.colors.prompts.bg }}; }
QLineEdit { border: {{ conf.colors.prompts.border }}; }
QTreeView { selection-background-color: {{ conf.colors.prompts.selected.bg }}; border: {{ conf.colors.prompts.border }}; }
QTreeView::branch { background-color: {{ conf.colors.prompts.bg }}; }
QTreeView::item:selected, QTreeView::item:selected:hover, QTreeView::branch:selected { background-color: {{ conf.colors.prompts.selected.bg }}; } """
super().__init__(parent) self._layout = QVBoxLayout(self) self._layout.setContentsMargins(10, 10, 10, 10) self._win_id = win_id self._prompt = None
self.setObjectName('PromptContainer') self.setAttribute(Qt.WA_StyledBackground, True) config.set_register_stylesheet(self)
message.global_bridge.prompt_done.connect(self._on_prompt_done) prompt_queue.show_prompts.connect(self._on_show_prompts) message.global_bridge.mode_left.connect(self._on_global_mode_left)
def __repr__(self): return utils.get_repr(self, win_id=self._win_id)
def _on_show_prompts(self, question): """Show a prompt for the given question.
Args: question: A Question object or None. """ item = self._layout.takeAt(0) if item is not None: widget = item.widget() log.prompt.debug("Deleting old prompt {}".format(widget)) widget.hide() widget.deleteLater()
if question is None: log.prompt.debug("No prompts left, hiding prompt container.") self._prompt = None self.hide() return
classes = { usertypes.PromptMode.yesno: YesNoPrompt, usertypes.PromptMode.text: LineEditPrompt, usertypes.PromptMode.user_pwd: AuthenticationPrompt, usertypes.PromptMode.download: DownloadFilenamePrompt, usertypes.PromptMode.alert: AlertPrompt, } klass = classes[question.mode] prompt = klass(question)
log.prompt.debug("Displaying prompt {}".format(prompt)) self._prompt = prompt
if not question.interrupted: # If this question was interrupted, we already connected the signal question.aborted.connect( lambda: modeman.leave(self._win_id, prompt.KEY_MODE, 'aborted', maybe=True)) modeman.enter(self._win_id, prompt.KEY_MODE, 'question asked')
self.setSizePolicy(prompt.sizePolicy()) self._layout.addWidget(prompt) prompt.show() self.show() prompt.setFocus() self.update_geometry.emit()
def _on_prompt_done(self, key_mode): """Leave the prompt mode in this window if a question was answered.""" modeman.leave(self._win_id, key_mode, ':prompt-accept', maybe=True)
def _on_global_mode_left(self, mode): """Leave prompt/yesno mode in this window if it was left elsewhere.
This ensures no matter where a prompt was answered, we leave the prompt mode and dispose of the prompt object in every window. """ if mode not in [usertypes.KeyMode.prompt, usertypes.KeyMode.yesno]: return modeman.leave(self._win_id, mode, 'left in other window', maybe=True) item = self._layout.takeAt(0) if item is not None: widget = item.widget() log.prompt.debug("Deleting prompt {}".format(widget)) widget.hide() widget.deleteLater()
modes=[usertypes.KeyMode.prompt, usertypes.KeyMode.yesno]) """Accept the current prompt.
//
This executes the next action depending on the question mode, e.g. asks for the password or leaves the mode.
Args: value: If given, uses this value instead of the entered one. For boolean prompts, "yes"/"no" are accepted as value. """ question = self._prompt.question try: done = self._prompt.accept(value) except Error as e: raise cmdexc.CommandError(str(e)) if done: message.global_bridge.prompt_done.emit(self._prompt.KEY_MODE) question.done()
modes=[usertypes.KeyMode.prompt], maxsplit=0) """Immediately open a download.
If no specific command is given, this will use the system's default application to open the file.
Args: cmdline: The command which should be used to open the file. A `{}` is expanded to the temporary file name. If no `{}` is present, the filename is automatically appended to the cmdline. """ try: self._prompt.download_open(cmdline) except UnsupportedOperationError: pass
modes=[usertypes.KeyMode.prompt]) def prompt_item_focus(self, which): """Shift the focus of the prompt file completion menu to another item.
Args: which: 'next', 'prev' """ try: self._prompt.item_focus(which) except UnsupportedOperationError: pass
instance='prompt-container', scope='window', modes=[usertypes.KeyMode.prompt, usertypes.KeyMode.yesno]) """Yank URL to clipboard or primary selection.
Args: sel: Use the primary selection instead of the clipboard. """ question = self._prompt.question if question.url is None: message.error('No URL found.') return if sel and utils.supports_selection(): target = 'primary selection' else: sel = False target = 'clipboard' utils.set_clipboard(question.url, sel) message.info("Yanked to {}: {}".format(target, question.url))
"""A line edit used in prompts."""
QLineEdit { background-color: transparent; } """)
"""Override keyPressEvent to paste primary selection on Shift + Ins.""" try: text = utils.get_clipboard(selection=True, fallback=True) except utils.ClipboardError: # pragma: no cover e.ignore() else: e.accept() self.insert(text) return
def __repr__(self): return utils.get_repr(self)
"""Base class for all prompts."""
def __repr__(self): return utils.get_repr(self, question=self.question, constructor=True)
html.escape(question.title)) # Not doing any HTML escaping here as the text can be formatted text_label = QLabel(question.text) text_label.setTextInteractionFlags(Qt.TextSelectableByMouse) self._vbox.addWidget(text_label)
# The bindings are all in the 'prompt' mode, even for yesno prompts
binding = None preferred = ['<enter>', '<escape>'] for pref in preferred: if pref in bindings: binding = pref if binding is None: binding = bindings[0] key_label = QLabel('<b>{}</b>'.format(html.escape(binding))) text_label = QLabel(text) labels.append((key_label, text_label))
self._key_grid.addWidget(key_label, i, 0) self._key_grid.addWidget(text_label, i, 1)
raise NotImplementedError
"""Open the download directly if this is a download prompt.""" raise UnsupportedOperationError
"""Switch to next file item if this is a filename prompt..""" raise UnsupportedOperationError
"""Get the commands we could run as response to this message.""" raise NotImplementedError
"""A prompt for a single text value."""
super().__init__(question, parent) self._lineedit = LineEdit(self) self._init_texts(question) self._vbox.addWidget(self._lineedit) if question.default: self._lineedit.setText(question.default) self.setFocusProxy(self._lineedit) self._init_key_label()
text = value if value is not None else self._lineedit.text() self.question.answer = text return True
return [('prompt-accept', 'Accept'), ('leave-mode', 'Abort')]
"""A prompt for a filename."""
"""Set the root path for the file display.""" separators += os.altsep
pass # Input "/" -> don't strip anything pass # Input like /foo/bar/ -> show /foo/bar/ contents # Input like /foo/ba -> show /foo contents else: except OSError: log.prompt.exception("Failed to get directory information") return
"""Handle an element selection.
Args: index: The QModelIndex of the selected element. clicked: Whether the element was clicked. """ path += os.sep else: # On Windows, when we have C:\foo and tab over .., we get C:\
# Avoid having a ..-subtree highlighted self._file_view.setCurrentIndex(QModelIndex())
else: self._file_view.hide()
# Only show name # Nothing selected initially # The model needs to be sorted so we get the correct first/last index lambda: self._file_model.sort(0))
text = value if value is not None else self._lineedit.text() text = downloads.transform_path(text) if text is None: message.error("Invalid filename") return False self.question.answer = text return True
# This duplicates some completion code, but I don't see a nicer way...
# No entries return
# No item selected yet else:
# wrap around if we arrived at beginning/end idx = last_index if which == 'prev' else first_index
idx, QItemSelectionModel.ClearAndSelect | QItemSelectionModel.Rows)
return [('prompt-accept', 'Accept'), ('leave-mode', 'Abort')]
"""A prompt for a filename for downloads."""
done = super().accept(value) answer = self.question.answer if answer is not None: self.question.answer = downloads.FileDownloadTarget(answer) return done
self.question.answer = downloads.OpenFileDownloadTarget(cmdline) self.question.done() message.global_bridge.prompt_done.emit(self.KEY_MODE)
('prompt-accept', 'Accept'), ('leave-mode', 'Abort'), ('prompt-open-download', "Open download"), ('prompt-yank', "Yank URL"), ]
"""A prompt for username/password."""
super().__init__(question, parent) self._init_texts(question)
user_label = QLabel("Username:", self) self._user_lineedit = LineEdit(self)
password_label = QLabel("Password:", self) self._password_lineedit = LineEdit(self) self._password_lineedit.setEchoMode(QLineEdit.Password)
grid = QGridLayout() grid.addWidget(user_label, 1, 0) grid.addWidget(self._user_lineedit, 1, 1) grid.addWidget(password_label, 2, 0) grid.addWidget(self._password_lineedit, 2, 1) self._vbox.addLayout(grid) self._init_key_label()
assert not question.default, question.default self.setFocusProxy(self._user_lineedit)
if value is not None: if ':' not in value: raise Error("Value needs to be in the format " "username:password, but {} was given".format( value)) username, password = value.split(':', maxsplit=1) self.question.answer = AuthInfo(username, password) return True elif self._user_lineedit.hasFocus(): # Earlier, tab was bound to :prompt-accept, so to still support # that we simply switch the focus when tab was pressed. self._password_lineedit.setFocus() return False else: self.question.answer = AuthInfo(self._user_lineedit.text(), self._password_lineedit.text()) return True
"""Support switching between fields with tab.""" assert which in ['prev', 'next'], which if which == 'next' and self._user_lineedit.hasFocus(): self._password_lineedit.setFocus() elif which == 'prev' and self._password_lineedit.hasFocus(): self._user_lineedit.setFocus()
return [('prompt-accept', "Accept"), ('leave-mode', "Abort")]
"""A prompt with yes/no answers."""
super().__init__(question, parent) self._init_texts(question) self._init_key_label()
if value is None: if self.question.default is None: raise Error("No default value was set for this question!") self.question.answer = self.question.default elif value == 'yes': self.question.answer = True elif value == 'no': self.question.answer = False else: raise Error("Invalid value {} - expected yes/no!".format(value)) return True
cmds = [ ('prompt-accept yes', "Yes"), ('prompt-accept no', "No"), ('prompt-yank', "Yank URL"), ]
if self.question.default is not None: assert self.question.default in [True, False] default = 'yes' if self.question.default else 'no' cmds.append(('prompt-accept', "Use default ({})".format(default)))
cmds.append(('leave-mode', "Abort")) return cmds
"""A prompt without any answer possibility."""
super().__init__(question, parent) self._init_texts(question) self._init_key_label()
if value is not None: raise Error("No value is permitted with alert prompts!") # Simply mark prompt as done without setting self.question.answer return True
return [('prompt-accept', "Hide")]
"""Initialize global prompt objects.""" global prompt_queue prompt_queue = PromptQueue() objreg.register('prompt-queue', prompt_queue) # for commands message.global_bridge.ask_question.connect( prompt_queue.ask_question, Qt.DirectConnection) |