summaryrefslogtreecommitdiff
path: root/alot/widgets.py
blob: a95ad1da330b70fa9769f9844c14e6bb1b786700 (plain)
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
"""
This file is part of alot.

Alot 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.

Notmuch 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 notmuch.  If not, see <http://www.gnu.org/licenses/>.

Copyright (C) 2011 Patrick Totzke <patricktotzke@gmail.com>
"""
import email
import urwid
import tempfile
import os
import re
from datetime import datetime

from settings import config
from settings import get_mime_handler
from helper import shorten
from helper import pretty_datetime
from helper import cmd_output


class ThreadlineWidget(urwid.AttrMap):
#TODO: receive a thread here. needs change in calling walker
    def __init__(self, tid, dbman):
        self.dbman = dbman
        self.thread = dbman.get_thread(tid)
        self.rebuild()
        urwid.AttrMap.__init__(self, self.columns,
                               'threadline', 'threadline_focus')

    def rebuild(self):
        cols = []
        datestring = pretty_datetime(self.thread.get_newest_date()).rjust(10)
        self.date_w = urwid.AttrMap(urwid.Text(datestring), 'threadline_date')
        cols.append(('fixed', len(datestring), self.date_w))

        mailcountstring = "(%d)" % self.thread.get_total_messages()
        self.mailcount_w = urwid.AttrMap(urwid.Text(mailcountstring),
                                   'threadline_mailcount')
        cols.append(('fixed', len(mailcountstring), self.mailcount_w))

        tagsstring = " ".join(self.thread.get_tags())
        self.tags_w = urwid.AttrMap(urwid.Text(tagsstring), 'threadline_tags')
        if tagsstring:
            cols.append(('fixed', len(tagsstring), self.tags_w))

        authors = self.thread.get_authors() or '(None)'
        maxlength = config.getint('general', 'authors_maxlength')
        authorsstring = shorten(authors, maxlength)
        self.authors_w = urwid.AttrMap(urwid.Text(authorsstring),
                                       'threadline_authors')
        cols.append(('fixed', len(authorsstring), self.authors_w))

        subjectstring = self.thread.get_subject()
        self.subject_w = urwid.AttrMap(urwid.Text(subjectstring, wrap='clip'),
                                 'threadline_subject')
        if subjectstring:
            cols.append(self.subject_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: 'threadline_date_focus'})
            self.mailcount_w.set_attr_map({None:
                                           'threadline_mailcount_focus'})
            self.tags_w.set_attr_map({None: 'threadline_tags_focus'})
            self.authors_w.set_attr_map({None: 'threadline_authors_focus'})
            self.subject_w.set_attr_map({None: 'threadline_subject_focus'})
        else:
            self.date_w.set_attr_map({None: 'threadline_date'})
            self.mailcount_w.set_attr_map({None: 'threadline_mailcount'})
            self.tags_w.set_attr_map({None: 'threadline_tags'})
            self.authors_w.set_attr_map({None: 'threadline_authors'})
            self.subject_w.set_attr_map({None: 'threadline_subject'})
        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


class BufferlineWidget(urwid.Text):
    def __init__(self, buffer):
        self.buffer = buffer
        line = '[' + buffer.typename + '] ' + unicode(buffer)
        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.Text):
    def __init__(self, tag):
        self.tag = tag
        urwid.Text.__init__(self, tag, wrap='clip')

    def selectable(self):
        return True

    def keypress(self, size, key):
        return key

    def get_tag(self):
        return self.tag


class PromptWidget(urwid.AttrMap):
    def __init__(self, prefix, text='', completer=None):
        self.completer = completer
        leftpart = urwid.Text(prefix, align='left')
        self.editpart = urwid.Edit(edit_text=text)
        self.start_completion_pos = len(text)
        self.completion_results = None
        both = urwid.Columns(
            [
                ('fixed', len(prefix) + 1, leftpart),
                ('weight', 1, self.editpart),
            ])
        urwid.AttrMap.__init__(self, both, 'prompt', 'prompt')

    def set_input(self, txt):
        return self.editpart.set_edit_text(txt)

    def get_input(self):
        return self.editpart.get_edit_text()

    def keypress(self, size, key):
        if key in ['tab', 'shift tab']:
            if self.completer:
                pos = self.start_completion_pos
                original = self.editpart.edit_text[:pos]
                if not self.completion_results:  # not in completion mode
                    self.completion_results = [''] + \
                        self.completer.complete(original)
                    self.focus_in_clist = 1
                else:
                    if key == 'tab':
                        self.focus_in_clist += 1
                    else:
                        self.focus_in_clist -= 1
                if len(self.completion_results) > 1:
                    suffix = self.completion_results[self.focus_in_clist %
                                              len(self.completion_results)]
                    self.editpart.set_edit_text(original + suffix)
                    self.editpart.edit_pos += len(suffix)
                else:
                    self.editpart.set_edit_text(original + ' ')
                    self.editpart.edit_pos += 1
                    self.start_completion_pos = self.editpart.edit_pos
                    self.completion_results = None
        else:
            result = self.editpart.keypress(size, key)
            self.start_completion_pos = self.editpart.edit_pos
            self.completion_results = None
            return result


class MessageWidget(urwid.WidgetWrap):
    """flow widget that displays a single message"""
    def __init__(self, message, even=False, unfold_body=False,
                 unfold_attachments=False,
                 unfold_header=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: boolean
        :param unfold_body: initially show message body
        :type unfold_body: boolean
        :param unfold_header: initially show message headers
        :type unfold_header: boolean
        :param depth: number of characters to shift content to the right
        :type depth: int
        :param bars_at: list of positions smaller than depth where horizontal
        ars are used instead of spaces.
        :type bars_at: list(int)
        """
        self.message = message
        self.depth = depth
        self.bars_at = bars_at
        self.even = even

        # build the summary line, header and body will be created on demand
        self.sumline = self._build_sum_line()
        self.headerw = None
        self.attachmentw = None
        self.bodyw = None
        self.displayed_list = [self.sumline]
        if unfold_header:
            self.displayed_list.append(self.get_header_widget())
        if unfold_body:
            self.displayed_list.append(self.get_body_widget())

        if unfold_attachments:
            self.displayed_list.append(self.get_attachment_widget())
        #build pile and call super constructor
        self.pile = urwid.Pile(self.displayed_list)
        urwid.WidgetWrap.__init__(self, self.pile)

        # in case the message is yet unread, remove this tag
        if 'unread' in message.get_tags():
            message.remove_tags(['unread'])

    def rebuild(self):
        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)
        if self.even:
            attr = 'messagesummary_even'
        else:
            attr = 'messagesummary_odd'
        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.AttrMap(urwid.Columns(cols, box_columns=bc),
                             attr, 'messagesummary_focus')
        return line

    def _get_header_widget(self):
        """creates/returns the widget that displays the mail header"""
        if not self.headerw:
            displayed = config.getstringlist('general', 'displayed_headers')
            cols = [MessageHeaderWidget(self.message.get_email(), displayed)]
            bc = list()
            if self.depth:
                cols.insert(0, self._get_spacer(self.bars_at[1:]))
                bc.append(0)
            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)
                lines.append(urwid.Columns(cols, box_columns=bc))
            self.attachmentw = urwid.Pile(lines)
        return self.attachmentw

        attachments = message.get_attachments()

    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)
            self.bodyw = urwid.Columns(cols, box_columns=bc)
        return self.bodyw

    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 toggle_attachments(self):
        """toggles if message headers are shown"""
        hw = self._get_attachment_widget()
        if hw:
            if hw in self.displayed_list:
                self.displayed_list.remove(hw)
            else:
                self.displayed_list.insert(1, hw)
            self.rebuild()

    def toggle_header(self):
        """toggles if message headers are shown"""
        hw = self._get_header_widget()
        if hw in self.displayed_list:
            self.displayed_list.remove(hw)
        else:
            self.displayed_list.insert(1, hw)
        self.rebuild()

    def toggle_full_header(self):
        """toggles if message headers are shown"""
        hw = self._get_header_widget().widget_list[-1]
        hw.toggle_all()

    def toggle_body(self):
        """toggles if message body is shown"""
        bw = self._get_body_widget()
        if bw in self.displayed_list:
            self.displayed_list.remove(bw)
        else:
            self.displayed_list.append(bw)
        self.sumw.toggle_folded()
        self.rebuild()

    def selectable(self):
        return True

    #TODO: this needs to go in favour of a binding in the buffer!
    def keypress(self, size, key):
        if key == 'h':
            self.toggle_header()
        elif key == 'enter':
            self.toggle_attachments()
            self.toggle_header()
            self.toggle_body()
        elif key == 'H':
            self.toggle_full_header()
        else:
            return self.pile.keypress(size, key)

    def get_message(self):
        """get contained message
        returns: alot.db.Message"""
        return self.message

    def get_email(self):
        """get contained email
        returns: email.Message"""
        return self.message.get_email()


class MessageSummaryWidget(urwid.WidgetWrap):
    """a one line summary of a message"""

    def __init__(self, message, folded=True):
        """
        :param message: the message to summarize
        :type message: alot.db.Message
        """
        self.message = message
        self.folded = folded
        sumstr = self.__str__()
        urwid.WidgetWrap.__init__(self, urwid.Text(sumstr))

    def __str__(self):
        prefix = "-  "
        if self.folded:
            prefix = '+  '
        return u"%s%s" % (prefix, unicode(self.message))

    def toggle_folded(self):
        self.folded = not self.folded
        self._w = urwid.Text(self.__str__())

    def selectable(self):
        return True

    def keypress(self, size, key):
        return key


class MessageHeaderWidget(urwid.AttrMap):
    """
    displays a "key:value\n" list of email headers.
    RFC 2822 style encoded values are decoded into utf8 first.
    """

    def __init__(self, eml, displayed_headers=None):
        """
        :param eml: the email
        :type eml: email.Message
        :param displayed_headers: a whitelist of header fields to display
        :type state: list(str)
        """
        self.eml = eml
        self.display_all = False
        self.displayed_headers = displayed_headers
        headerlines = self._build_lines(displayed_headers)
        urwid.AttrMap.__init__(self, urwid.Pile(headerlines), 'message_header')

    def toggle_all(self):
        if self.display_all:
            self.display_all = False
            headerlines = self._build_lines(self.displayed_headers)
        else:
            self.display_all = True
            headerlines = self._build_lines(None)
        self._w = urwid.Pile(headerlines)

    def _build_lines(self, displayed):
        max_key_len = 1
        headerlines = []
        if not displayed:
            displayed = self.eml.keys()
        for key in displayed:
            if key in self.eml:
                if len(key) > max_key_len:
                    max_key_len = len(key)
        for key in displayed:
            #todo: parse from,cc,bcc seperately into name-addr-widgets
            if key in self.eml:
                valuelist = email.header.decode_header(self.eml[key])
                value = ''
                for v, enc in valuelist:
                    if enc:
                        value = value + v.decode(enc)
                    else:
                        value = value + v
                #sanitize it a bit:
                value = value.replace('\t', '')
                value = value.replace('\r', '')
                keyw = ('fixed', max_key_len + 1,
                        urwid.Text(('message_header_key', key)))
                valuew = urwid.Text(('message_header_value', value))
                line = urwid.Columns([keyw, valuew])
                headerlines.append(line)
        return headerlines

    def selectable(self):
        return True

    def keypress(self, size, key):
        return key


class MessageBodyWidget(urwid.AttrMap):
    """displays printable parts of an email"""

    def __init__(self, eml):
        """
        :param eml: the email
        :type eml: email.Message
        """
        self.eml = eml
        bodytxt = ''
        for part in self.eml.walk():
            ctype = part.get_content_type()
            enc = part.get_content_charset()
            raw_payload = part.get_payload(decode=True)
            if part.get_content_maintype() == 'text':
                if enc:
                    raw_payload = unicode(raw_payload, enc)
                else:
                    raw_payload = unicode(raw_payload, errors='replace')
            if ctype == 'text/plain':
                bodytxt += raw_payload
            else:
                #get mime handler
                handler = get_mime_handler(ctype, key='view',
                                           interactive=False)
                if handler:
                    #open tempfile. Not all handlers accept stuff from stdin
                    tmpfile = tempfile.NamedTemporaryFile(delete=False,
                                                          suffix='.html')
                    #write payload to tmpfile
                    if part.get_content_maintype() == 'text':
                        tmpfile.write(raw_payload.encode('utf8'))
                    else:
                        tmpfile.write(raw_payload)
                    #create and call external command
                    cmd = handler % tmpfile.name
                    rendered_payload = cmd_output(cmd)
                    #remove tempfile
                    tmpfile.close()
                    os.unlink(tmpfile.name)
                    if rendered_payload:  # handler had output
                        bodytxt += unicode(rendered_payload.strip(),
                                           encoding='utf8', errors='replace')
                    elif part.get_content_maintype() == 'text':
                        bodytxt += raw_payload
                    # else drop
        urwid.AttrMap.__init__(self, urwid.Text(bodytxt), 'message_body')

    def selectable(self):
        return True

    def keypress(self, size, key):
        return key


class AttachmentWidget(urwid.WidgetWrap):
    def __init__(self, attachment):
        self.attachment = attachment
        widget = urwid.AttrMap(urwid.Text(unicode(attachment)),
                               'message_attachment')
        urwid.WidgetWrap.__init__(self, widget)

    def selectable(self):
        return True

    def keypress(self, size, key):
        return key