# Copyright (C) 2011-2018 Patrick Totzke # Copyright © 2018 Dylan Baker # This file is released under the GNU GPL, version 3 or a later revision. # For further details see the COPYING file import asyncio import urwid import logging from .buffer import Buffer from ..settings.const import settings from ..widgets.thread import MessageWidget, ThreadNode from .. import commands from ..db.errors import NonexistantObjectError class ThreadBuffer(Buffer): """displays a thread as a tree of messages.""" modename = 'thread' _msg_widgets = None def __init__(self, ui, thread): """ :param ui: main UI :type ui: :class:`~alot.ui.UI` :param thread: thread to display :type thread: :class:`~alot.db.Thread` """ self.thread = thread self._indent_width = settings.get('thread_indent_replies') self.rebuild() super().__init__(ui, self.body) def __str__(self): return '[thread] %s (%d message%s)' % (self.thread.subject, self.thread.total_messages, 's' * (self.thread.total_messages > 1)) def translated_tags_str(self, intersection=False): tags = self.thread.get_tags(intersection=intersection) trans = [settings.get_tagstring_representation(tag)['translated'] for tag in tags] return ' '.join(trans) def get_info(self): info = {} info['subject'] = self.thread.subject info['authors'] = self.thread.get_authors_string() info['tid'] = self.thread.id info['message_count'] = self.thread.total_messages info['thread_tags'] = self.translated_tags_str() info['intersection_tags'] = self.translated_tags_str(intersection=True) return info def rebuild(self): try: self.thread.refresh() except NonexistantObjectError: self.body = urwid.SolidFill() return self._msg_widgets = [] body_walker = urwid.SimpleFocusListWalker([]) for pos, msg in enumerate(self.thread.message_list): msg_wgt = MessageWidget(msg, pos & 1) wgt = ThreadNode(msg_wgt, self.thread, pos, self._indent_width) self._msg_widgets.append(msg_wgt) body_walker.append(wgt) self.body = urwid.ListBox(body_walker) def get_selected_message_position(self): """Return position of focussed message in the thread tree.""" return self.body.focus_position def get_selected_message_widget(self): """Return currently focused :class:`MessageWidget`.""" pos = self.get_selected_message_position() return self._msg_widgets[pos] def get_selected_message(self): """Return focussed :class:`~alot.db.message.Message`.""" return self.get_selected_message_widget().get_message() def message_widgets(self): """ Iterate over all the message widgets in this buffer """ for w in self._msg_widgets: yield w # needed for ui.get_deep_focus.. def get_focus(self): "Get the focus from the underlying body widget." return self.body.focus def set_focus(self, pos): "Set the focus in the underlying body widget." logging.debug('setting focus to %s ', pos) self.body.set_focus(pos) def focus_first(self): """set focus to first message of thread""" self.set_focus(0) def focus_last(self): self.set_focus(max(self.thread.total_messages - 1, 0)) def focus_selected_message(self): """focus the summary line of currently focused message""" self.set_focus(self.get_selected_message_position()) def focus_parent(self): """move focus to parent of currently focused message""" msg = self.get_selected_message() if msg.parent: self.set_focus(self.thread.message_list.index(msg.parent)) def focus_first_reply(self): """move focus to first reply to currently focused message""" msg = self.get_selected_message() if len(msg.replies) > 0: new_focus = self.thread.message_list.index(msg.replies[0]) self.set_focus(new_focus) def focus_last_reply(self): """move focus to last reply to currently focused message""" msg = self.get_selected_message() if len(msg.replies) > 0: new_focus = self.thread.message_list.index(msg.replies[-1]) self.set_focus(new_focus) def focus_next_sibling(self): """focus next sibling of currently focussed message in thread tree""" pos_next = self.get_selected_message_position() + 1 depth = self.get_selected_message().depth if (pos_next < self.thread.total_messages and self.thread.message_list[pos_next].depth == depth): self.set_focus(pos_next) def focus_prev_sibling(self): """ focus previous sibling of currently focussed message in thread tree """ pos_next = self.get_selected_message_position() - 1 depth = self.get_selected_message().depth if (pos_next < self.thread.total_messages and self.thread.message_list[pos_next].depth == depth): self.set_focus(pos_next) def focus_next(self): """focus next message in depth first order""" next_focus = self.get_selected_message_position() + 1 if next_focus >= 0 and next_focus < self.thread.total_messages: self.set_focus(next_focus) def focus_prev(self): """focus previous message in depth first order""" next_focus = self.get_selected_message_position() - 1 if next_focus >= 0 and next_focus < self.thread.total_messages: self.set_focus(next_focus) def _focus_property(self, prop, direction): """does a walk in the given direction and focuses the first message that matches the given property""" cur_pos = self.get_selected_message_position() if direction > 0: walk = range(cur_pos + 1, self.thread.total_messages) else: walk = reversed(range(0, cur_pos)) for pos in walk: if prop(self._msg_widgets[pos]): self.set_focus(pos) break def focus_next_matching(self, querystring): """focus next matching message in depth first order""" self._focus_property(lambda x: x.get_message().matches(querystring), 1) def focus_prev_matching(self, querystring): """focus previous matching message in depth first order""" self._focus_property(lambda x: x.get_message().matches(querystring), -1) def focus_next_unfolded(self): """focus next unfolded message in depth first order""" self._focus_property(lambda x: x.display_content, 1) def focus_prev_unfolded(self): """focus previous unfolded message in depth first order""" self._focus_property(lambda x: x.display_content, -1) def expand(self, msgpos): """expand message at given position""" self.body.body[msgpos].expand() def expand_all(self): """expand all messages in thread""" for msg in self.message_widgets(): msg.expand() def collapse(self, msgpos): """collapse message at given position""" self.body.body[msgpos].collapse() self.focus_selected_message() def collapse_all(self): """collapse all messages in thread""" for msg in self.message_widgets(): msg.collapse() self.focus_selected_message() def unfold_matching(self, querystring, focus_first=True): """ expand all messages that match a given querystring. :param querystring: query to match :type querystring: str :param focus_first: set the focus to the first matching message :type focus_first: bool """ focus_set = False first = None for pos, msg_wgt in enumerate(self.message_widgets()): msg = msg_wgt.get_message() if msg.matches(querystring): msg_wgt.expand() if focus_first and not focus_set: self.set_focus(pos) focus_set = True else: msg_wgt.collapse()