From 371bf6414bb181a3856648d877b9f1980da1bfd7 Mon Sep 17 00:00:00 2001 From: Anton Khirnov Date: Wed, 13 Jan 2021 14:49:09 +0100 Subject: thread: allow switching between multipart/alternative parts --- alot/widgets/thread.py | 68 +++++++++++++++++++++++++++++++++++++------------- 1 file changed, 51 insertions(+), 17 deletions(-) (limited to 'alot/widgets') diff --git a/alot/widgets/thread.py b/alot/widgets/thread.py index 3be17976..3da660a5 100644 --- a/alot/widgets/thread.py +++ b/alot/widgets/thread.py @@ -82,6 +82,13 @@ class _MIMEPartWidget(urwid.WidgetWrap): self._fold_level = val_out + def cycle_alt(self): + if self._children is None: + return + + for c in self._children: + c.cycle_alt() + class _CryptPartWidget(_MIMEPartWidget): def __init__(self, mime_tree, alternative_pref): children = None @@ -136,6 +143,43 @@ class _MultiMixedWidget(_MIMEPartWidget): def __init__(self, children): super().__init__(urwid.Pile(children), children) +class _MultiAltWidget(_MIMEPartWidget): + _cur_idx = None + _child_parts = None + _sel_placeholder = None + + def __init__(self, child_widgets, child_parts, alternative_pref): + self._child_parts = child_parts + + child_idx = None + for idx, ch in enumerate(child_parts): + if ch.content_type == alternative_pref: + child_idx = idx + break + if child_idx is None: + child_idx = 0 + + self._sel_placeholder = urwid.WidgetPlaceholder(urwid.Text('')) + + body_wgt = urwid.LineBox(self._sel_placeholder) + + super().__init__(body_wgt, child_widgets) + + self._select_alt(child_idx) + + def _select_alt(self, idx): + child_part = self._child_parts[idx] + child_wgt = self._children[idx] + + self._w.set_title('Alternative MIME part %d/%d: %s' % + (idx + 1, len(self._children), child_part.content_type)) + self._sel_placeholder.original_widget = child_wgt + + self._cur_idx = idx + + def cycle_alt(self): + self._select_alt((self._cur_idx + 1) % len(self._children)) + class _Fold: level = None start = None @@ -320,7 +364,7 @@ class _EmptyMessageWidget(_MIMEPartWidget): body_wgt = urwid.Text('<<< No displayable content >>>', align = 'center') super().__init__(body_wgt) -def _handle_mixed(mime_tree, alternative_pref): +def _handle_multipart(mime_tree, alternative_pref): children = [] for child in mime_tree.children: ch = _render_mime_tree(child, alternative_pref) @@ -328,20 +372,10 @@ def _handle_mixed(mime_tree, alternative_pref): children.append(ch) if len(children) > 0: + if mime_tree.is_alternative: + return _MultiAltWidget(children, mime_tree.children, alternative_pref) return _MultiMixedWidget(children) -def _handle_alternative(mime_tree, alternative_pref): - # TODO: switching between alternatives - child = None - for ch in mime_tree.children: - if ch.content_type == alternative_pref: - child = ch - break - if child is None: - child = mime_tree.children[0] - - return _render_mime_tree(child, alternative_pref) - def _render_mime_tree(mime_tree, alternative_pref): # handle encrypted/signed parts if mime_tree.is_signed or mime_tree.is_encrypted: @@ -349,10 +383,7 @@ def _render_mime_tree(mime_tree, alternative_pref): if mime_tree.children is not None: # multipart MIME parts - if mime_tree.is_alternative: - return _handle_alternative(mime_tree, alternative_pref) - - return _handle_mixed(mime_tree, alternative_pref) + return _handle_multipart(mime_tree, alternative_pref) # no children - this is a leaf node # skip attachment parts @@ -580,6 +611,9 @@ class MessageWidget(urwid.WidgetWrap): def foldlevel(self, val): self._body_wgt.foldlevel = val + def cycle_alt(self): + self._body_wgt.cycle_alt() + def get_selected_attachment(self): """ If an AttachmentWidget is currently focused, return it. Otherwise return -- cgit v1.2.3