1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
|
import urwid
import logging
from settings import settings
from helper import shorten_author_string
from helper import pretty_datetime
from helper import tag_cmp
from helper import string_decode
import message
import time
class DialogBox(urwid.WidgetWrap):
def __init__(self, body, title, bodyattr=None, titleattr=None):
self.body = urwid.LineBox(body)
self.title = urwid.Text(title)
if titleattr is not None:
self.title = urwid.AttrMap(self.title, titleattr)
if bodyattr is not None:
self.body = urwid.AttrMap(self.body, bodyattr)
box = urwid.Overlay(self.title, self.body,
align='center',
valign='top',
width=len(title),
height=None,
)
urwid.WidgetWrap.__init__(self, box)
def selectable(self):
return self.body.selectable()
def keypress(self, size, key):
return self.body.keypress(size, key)
class CatchKeyWidgetWrap(urwid.WidgetWrap):
def __init__(self, widget, key, on_catch, relay_rest=True):
urwid.WidgetWrap.__init__(self, widget)
self.key = key
self.relay = relay_rest
self.on_catch = on_catch
def selectable(self):
return True
def keypress(self, size, key):
logging.debug('CATCH KEY: %s' % key)
logging.debug('relay: %s' % self.relay)
if key == self.key:
self.on_catch()
elif self._w.selectable() and self.relay:
return self._w.keypress(size, key)
class ThreadlineWidget(urwid.AttrMap):
"""
selectable line widget that represents a :class:`~alot.db.Thread`
in the :class:`~alot.buffers.SearchBuffer`.
Respected settings:
* `general.display_content_in_threadline`
* `general.timestamp_format`
* `general.authors_maxlength`
Theme settings:
* `search_thread, search_thread_focus`
* `search_thread_date, search_thread_date_focus`
* `search_thread_mailcount, search_thread_mailcount_focus`
* `search_thread_authors, search_thread_authors_focus`
* `search_thread_subject, search_thread_subject_focus`
* `search_thread_content, search_thread_content_focus`
"""
# The pretty_datetime needs 9 characters, but only 8 if locale
# doesn't use am/pm (in which case "jan 2012" is the longest)
pretty_datetime_len = 8 if len(time.strftime("%P")) == 0 else 9
def __init__(self, tid, dbman):
self.dbman = dbman
#logging.debug('tid: %s' % tid)
self.thread = dbman.get_thread(tid)
#logging.debug('tid: %s' % self.thread)
self.tag_widgets = []
self.display_content = settings.get('display_content_in_threadline')
self.rebuild()
normal = settings.get_theming_attribute('search', 'thread')
focussed = settings.get_theming_attribute('search', 'thread_focus')
urwid.AttrMap.__init__(self, self.columns, normal, focussed)
def rebuild(self):
cols = []
if self.thread:
newest = self.thread.get_newest_date()
else:
newest = None
if newest == None:
datestring = ''
else:
formatstring = settings.get('timestamp_format')
if formatstring is None:
datestring = pretty_datetime(newest)
else:
datestring = newest.strftime(formatstring)
datestring = datestring.rjust(self.pretty_datetime_len)
self.date_w = urwid.AttrMap(urwid.Text(datestring),
self._get_theme('date'))
cols.append(('fixed', len(datestring), self.date_w))
if self.thread:
mailcountstring = "(%d)" % self.thread.get_total_messages()
else:
mailcountstring = "(?)"
self.mailcount_w = urwid.AttrMap(urwid.Text(mailcountstring),
self._get_theme('mailcount'))
cols.append(('fixed', len(mailcountstring), self.mailcount_w))
if self.thread:
self.tag_widgets = [TagWidget(t)
for t in self.thread.get_tags()]
else:
self.tag_widgets = []
self.tag_widgets.sort(tag_cmp,
lambda tag_widget: tag_widget.translated)
for tag_widget in self.tag_widgets:
if not tag_widget.hidden:
cols.append(('fixed', tag_widget.width(), tag_widget))
if self.thread:
authors = self.thread.get_authors_string() or '(None)'
else:
authors = '(None)'
maxlength = settings.get('authors_maxlength')
authorsstring = shorten_author_string(authors, maxlength)
self.authors_w = urwid.AttrMap(urwid.Text(authorsstring),
self._get_theme('authors'))
cols.append(('fixed', len(authorsstring), self.authors_w))
if self.thread:
subjectstring = self.thread.get_subject() or ''
else:
subjectstring = ''
# sanitize subject string:
subjectstring = subjectstring.replace('\n', ' ')
subjectstring = subjectstring.replace('\r', '')
subjectstring = subjectstring.strip()
self.subject_w = urwid.AttrMap(urwid.Text(subjectstring, wrap='clip'),
self._get_theme('subject'))
if subjectstring:
cols.append(('weight', 2, self.subject_w))
if self.display_content:
if self.thread:
msgs = self.thread.get_messages().keys()
else:
msgs = []
msgs.sort()
lastcontent = ' '.join([m.get_text_content() for m in msgs])
contentstring = lastcontent.replace('\n', ' ').strip()
self.content_w = urwid.AttrMap(urwid.Text(
contentstring,
wrap='clip'),
self._get_theme('content'))
cols.append(self.content_w)
self.columns = urwid.Columns(cols, dividechars=1)
self.original_widget = self.columns
def render(self, size, focus=False):
if focus:
self.date_w.set_attr_map({None: self._get_theme('date', focus)})
self.mailcount_w.set_attr_map({None:
self._get_theme('mailcount', focus)})
for tw in self.tag_widgets:
tw.set_focussed()
self.authors_w.set_attr_map({None: self._get_theme('authors',
focus)})
self.subject_w.set_attr_map({None: self._get_theme('subject',
focus)})
if self.display_content:
self.content_w.set_attr_map(
{None: self._get_theme('content', focus=True)})
else:
self.date_w.set_attr_map({None: self._get_theme('date')})
self.mailcount_w.set_attr_map({None: self._get_theme('mailcount')})
for tw in self.tag_widgets:
tw.set_unfocussed()
self.authors_w.set_attr_map({None: self._get_theme('authors')})
self.subject_w.set_attr_map({None: self._get_theme('subject')})
if self.display_content:
self.content_w.set_attr_map({None: self._get_theme('content')})
return urwid.AttrMap.render(self, size, focus)
def selectable(self):
return True
def keypress(self, size, key):
return key
def get_thread(self):
return self.thread
def _get_theme(self, component, focus=False):
attr_key = 'thread_{0}'.format(component)
if focus:
attr_key += '_focus'
return settings.get_theming_attribute('search', attr_key)
class BufferlineWidget(urwid.Text):
"""
selectable text widget that represents a :class:`~alot.buffers.Buffer`
in the :class:`~alot.buffers.BufferlistBuffer`.
"""
def __init__(self, buffer):
self.buffer = buffer
line = buffer.__str__()
urwid.Text.__init__(self, line, wrap='clip')
def selectable(self):
return True
def keypress(self, size, key):
return key
def get_buffer(self):
return self.buffer
class TagWidget(urwid.AttrMap):
"""
text widget that renders a tagstring.
It looks up the string it displays in the `tag-translate` section
of the config as well as custom theme settings for its tag. The
tag may also be configured as hidden, which users of this widget
should honour.
"""
def __init__(self, tag):
self.tag = tag
representation = settings.get_tagstring_representation(tag)
self.hidden = representation['hidden']
self.translated = representation['translated']
self.txt = urwid.Text(self.translated, wrap='clip')
self.normal_att = representation['normal']
self.focus_att = representation['focussed']
urwid.AttrMap.__init__(self, self.txt, self.normal_att, self.focus_att)
def width(self):
# evil voodoo hotfix for double width chars that may
# lead e.g. to strings with length 1 that need width 2
return self.txt.pack()[0]
def selectable(self):
return True
def keypress(self, size, key):
return key
def get_tag(self):
return self.tag
def set_focussed(self):
self.set_attr_map({None: self.focus_att})
def set_unfocussed(self):
self.set_attr_map({None: self.normal_att})
class ChoiceWidget(urwid.Text):
def __init__(self, choices, callback, cancel=None, select=None):
self.choices = choices
self.callback = callback
self.cancel = cancel
self.select = select
items = []
for k, v in choices.items():
if v == select and select != None:
items.append('[%s]:%s' % (k, v))
else:
items.append('(%s):%s' % (k, v))
urwid.Text.__init__(self, ' '.join(items))
def selectable(self):
return True
def keypress(self, size, key):
if key == 'select' and self.select != None:
self.callback(self.select)
elif key == 'cancel' and self.cancel != None:
self.callback(self.cancel)
elif key in self.choices:
self.callback(self.choices[key])
else:
return key
class CompleteEdit(urwid.Edit):
def __init__(self, completer, on_exit, edit_text=u'',
history=None, **kwargs):
self.completer = completer
self.on_exit = on_exit
self.history = list(history) # we temporarily add stuff here
self.historypos = None
if not isinstance(edit_text, unicode):
edit_text = string_decode(edit_text)
self.start_completion_pos = len(edit_text)
self.completions = None
urwid.Edit.__init__(self, edit_text=edit_text, **kwargs)
def keypress(self, size, key):
# if we tabcomplete
if key in ['tab', 'shift tab'] and self.completer:
# if not already in completion mode
if not self.completions:
self.completions = [(self.edit_text, self.edit_pos)] + \
self.completer.complete(self.edit_text, self.edit_pos)
self.focus_in_clist = 1
else: # otherwise tab through results
if key == 'tab':
self.focus_in_clist += 1
else:
self.focus_in_clist -= 1
if len(self.completions) > 1:
ctext, cpos = self.completions[self.focus_in_clist %
len(self.completions)]
self.set_edit_text(ctext)
self.set_edit_pos(cpos)
else:
self.edit_pos += 1
if self.edit_pos >= len(self.edit_text):
self.edit_text += ' '
self.completions = None
elif key in ['up', 'down']:
if self.history:
if self.historypos == None:
self.history.append(self.edit_text)
self.historypos = len(self.history) - 1
if key == 'cursor up':
self.historypos = (self.historypos + 1) % len(self.history)
else:
self.historypos = (self.historypos - 1) % len(self.history)
self.set_edit_text(self.history[self.historypos])
elif key == 'select':
self.on_exit(self.edit_text)
elif key == 'cancel':
self.on_exit(None)
else:
result = urwid.Edit.keypress(self, size, key)
self.completions = None
return result
class MessageWidget(urwid.WidgetWrap):
"""
Flow widget that renders a :class:`~alot.message.Message`.
Respected settings:
* `general.displayed_headers`
"""
#TODO: atm this is heavily bent to work nicely with ThreadBuffer to display
#a tree structure. A better way would be to keep this widget simple
#(subclass urwid.Pile) and use urwids new Tree widgets
def __init__(self, message, even=False, folded=True, raw=False,
all_headers=False, depth=0, bars_at=[]):
"""
:param message: the message to display
:type message: alot.db.Message
:param even: use messagesummary_even theme for summary
:type even: bool
:param folded: fold message initially
:type folded: bool
:param raw: show message source initially
:type raw: bool
:param all_headers: show all headers initially
:type all_headers: bool
:param depth: number of characters to shift content to the right
:type depth: int
:param bars_at: defines for each column of the indentation whether to
use a vertical bar instead of a space.
:type bars_at: list(bool)
"""
self.message = message
self.mail = self.message.get_email()
self.depth = depth
self.bars_at = bars_at
self.even = even
self.folded = folded
self.show_raw = raw
self.show_all_headers = all_headers
# define subwidgets that will be created on demand
self.sumline = None
self.headerw = None
self.attachmentw = None
self.bodyw = None
self.sourcew = None
# set available and to be displayed headers
self._all_headers = self.mail.keys()
displayed = settings.get('displayed_headers')
self._filtered_headers = [k for k in displayed if k in self.mail]
self._displayed_headers = None
self.rebuild() # this will build self.pile
urwid.WidgetWrap.__init__(self, self.pile)
def get_focus(self):
return self.pile.get_focus()
def rebuild(self):
self.sumline = self._build_sum_line()
if not self.folded: # only if already unfolded
self.displayed_list = [self.sumline]
if self.show_raw:
srcw = self._get_source_widget()
self.displayed_list.append(srcw)
else:
hw = self._get_header_widget()
aw = self._get_attachment_widget()
bw = self._get_body_widget()
if hw:
self.displayed_list.append(hw)
if aw:
self.displayed_list.append(aw)
self.displayed_list.append(bw)
else:
self.displayed_list = [self.sumline]
self.pile = urwid.Pile(self.displayed_list)
self._w = self.pile
def _build_sum_line(self):
"""creates/returns the widget that displays the summary line."""
self.sumw = MessageSummaryWidget(self.message, even=self.even)
cols = []
bc = list() # box_columns
if self.depth > 1:
bc.append(0)
cols.append(self._get_spacer(self.bars_at[1:-1]))
if self.depth > 0:
if self.bars_at[-1]:
arrowhead = u'\u251c\u25b6'
else:
arrowhead = u'\u2514\u25b6'
cols.append(('fixed', 2, urwid.Text(arrowhead)))
cols.append(self.sumw)
line = urwid.Columns(cols, box_columns=bc)
return line
def _get_header_widget(self):
"""creates/returns the widget that displays the mail header"""
all_shown = (self._all_headers == self._displayed_headers)
if self.headerw and (self.show_all_headers == all_shown):
return self.headerw
if self.show_all_headers:
self._displayed_headers = self._all_headers
else:
self._displayed_headers = self._filtered_headers
mail = self.message.get_email()
# normalize values if only filtered list is shown
norm = not (self._displayed_headers == self._all_headers)
lowercase_keys = [k.lower() for k in self._displayed_headers]
#build lines
lines = []
for k, v in mail.items():
if k.lower() in lowercase_keys:
lines.append((k, message.decode_header(v, normalize=norm)))
cols = [HeadersList(lines)]
bc = list()
if self.depth:
cols.insert(0, self._get_spacer(self.bars_at[1:]))
bc.append(0)
cols.insert(1, self._get_arrowhead_aligner())
bc.append(1)
self.headerw = urwid.Columns(cols, box_columns=bc)
return self.headerw
def _get_attachment_widget(self):
if self.message.get_attachments() and not self.attachmentw:
lines = []
for a in self.message.get_attachments():
cols = [AttachmentWidget(a)]
bc = list()
if self.depth:
cols.insert(0, self._get_spacer(self.bars_at[1:]))
bc.append(0)
cols.insert(1, self._get_arrowhead_aligner())
bc.append(1)
lines.append(urwid.Columns(cols, box_columns=bc))
self.attachmentw = urwid.Pile(lines)
return self.attachmentw
def _get_body_widget(self):
"""creates/returns the widget that displays the mail body"""
if not self.bodyw:
cols = [MessageBodyWidget(self.message.get_email())]
bc = list()
if self.depth:
cols.insert(0, self._get_spacer(self.bars_at[1:]))
bc.append(0)
cols.insert(1, self._get_arrowhead_aligner())
bc.append(1)
self.bodyw = urwid.Columns(cols, box_columns=bc)
return self.bodyw
def _get_source_widget(self):
"""creates/returns the widget that displays the mail body"""
if not self.sourcew:
cols = [urwid.Text(self.message.get_email().as_string())]
bc = list()
if self.depth:
cols.insert(0, self._get_spacer(self.bars_at[1:]))
bc.append(0)
cols.insert(1, self._get_arrowhead_aligner())
bc.append(1)
self.sourcew = urwid.Columns(cols, box_columns=bc)
return self.sourcew
def _get_spacer(self, bars_at):
prefixchars = []
length = len(bars_at)
for b in bars_at:
if b:
c = u'\u2502'
else:
c = ' '
prefixchars.append(('fixed', 1, urwid.SolidFill(c)))
spacer = urwid.Columns(prefixchars, box_columns=range(length))
return ('fixed', length, spacer)
def _get_arrowhead_aligner(self):
if self.message.has_replies():
aligner = u'\u2502'
else:
aligner = ' '
return ('fixed', 1, urwid.SolidFill(aligner))
def selectable(self):
return True
def keypress(self, size, key):
return self.pile.keypress(size, key)
def get_message(self):
"""get contained :class`~alot.message.Message`"""
return self.message
def get_email(self):
"""get contained :class:`email <email.Message>`"""
return self.message.get_email()
class MessageSummaryWidget(urwid.WidgetWrap):
"""
one line summary of a :class:`~alot.message.Message`.
Theme settings:
* `thread_summary_even`
* `thread_summary_odd`
* `thread_summary_focus`
"""
def __init__(self, message, even=True):
"""
:param message: a message
:type message: alot.db.Message
:param even: even entry in a pile of messages? Used for theming.
:type even: bool
"""
self.message = message
self.even = even
if even:
attr = settings.get_theming_attribute('thread', 'summary_even')
else:
attr = settings.get_theming_attribute('thread', 'summary_odd')
cols = []
sumstr = self.__str__()
txt = urwid.Text(sumstr)
cols.append(txt)
thread_tags = message.get_thread().get_tags(intersection=True)
outstanding_tags = set(message.get_tags()).difference(thread_tags)
tag_widgets = [TagWidget(t) for t in outstanding_tags]
tag_widgets.sort(tag_cmp, lambda tag_widget: tag_widget.translated)
for tag_widget in tag_widgets:
if not tag_widget.hidden:
cols.append(('fixed', tag_widget.width(), tag_widget))
focus_att = settings.get_theming_attribute('thread', 'summary_focus')
line = urwid.AttrMap(urwid.Columns(cols, dividechars=1), attr,
focus_att)
urwid.WidgetWrap.__init__(self, line)
def __str__(self):
author, address = self.message.get_author()
date = self.message.get_datestring()
rep = author if author != '' else address
if date != None:
rep += " (%s)" % date
return rep
def selectable(self):
return True
def keypress(self, size, key):
return key
class HeadersList(urwid.WidgetWrap):
"""
renders a pile of header values as key/value list
Theme settings:
* `thread_header`
* `thread_header_key`
* `thread_header_value`
"""
def __init__(self, headerslist):
self.headers = headerslist
pile = urwid.Pile(self._build_lines(headerslist))
att = settings.get_theming_attribute('thread', 'header')
pile = urwid.AttrMap(pile, att)
urwid.WidgetWrap.__init__(self, pile)
def __str__(self):
return str(self.headers)
def _build_lines(self, lines):
max_key_len = 1
headerlines = []
key_att = settings.get_theming_attribute('thread', 'header_key')
value_att = settings.get_theming_attribute('thread', 'header_value')
#calc max length of key-string
for key, value in lines:
if len(key) > max_key_len:
max_key_len = len(key)
for key, value in lines:
##todo : even/odd
keyw = ('fixed', max_key_len + 1,
urwid.Text((key_att, key)))
valuew = urwid.Text((value_att, value))
line = urwid.Columns([keyw, valuew])
headerlines.append(line)
return headerlines
class MessageBodyWidget(urwid.AttrMap):
"""
displays printable parts of an email
Theme settings:
* `thread_body`
"""
def __init__(self, msg):
bodytxt = message.extract_body(msg)
att = settings.get_theming_attribute('thread', 'body')
urwid.AttrMap.__init__(self, urwid.Text(bodytxt), att)
class AttachmentWidget(urwid.WidgetWrap):
"""
one-line summary of an :class:`~alot.message.Attachment`.
Theme settings:
* `thread_attachment`
* `thread_attachment_focus`
"""
def __init__(self, attachment, selectable=True):
self._selectable = selectable
self.attachment = attachment
if not isinstance(attachment, message.Attachment):
self.attachment = message.Attachment(self.attachment)
att = settings.get_theming_attribute('thread', 'attachment')
focus_att = settings.get_theming_attribute('thread',
'attachment_focus')
widget = urwid.AttrMap(urwid.Text(self.attachment.__str__()),
att, focus_att)
urwid.WidgetWrap.__init__(self, widget)
def get_attachment(self):
return self.attachment
def selectable(self):
return self._selectable
def keypress(self, size, key):
return key
|