3 class ThreadViewMode < LineCursorMode
4 ## this holds all info we need to lay out a message
6 attr_accessor :top, :bot, :prev, :next, :depth, :width, :state, :color, :star_color, :orig_new
13 DATE_FORMAT = "%B %e %Y %l:%M%P"
14 INDENT_SPACES = 2 # how many spaces to indent child messages
16 register_keymap do |k|
17 k.add :toggle_detailed_header, "Toggle detailed header", 'h'
18 k.add :show_header, "Show full message header", 'H'
19 k.add :toggle_expanded, "Expand/collapse item", :enter
20 k.add :expand_all_messages, "Expand/collapse all messages", 'E'
21 k.add :edit_draft, "Edit draft", 'e'
22 k.add :edit_labels, "Edit or add labels for a thread", 'l'
23 k.add :expand_all_quotes, "Expand/collapse all quotes in a message", 'o'
24 k.add :jump_to_next_open, "Jump to next open message", 'n'
25 k.add :jump_to_prev_open, "Jump to previous open message", 'p'
26 k.add :toggle_starred, "Star or unstar message", '*'
27 k.add :toggle_new, "Toggle new/read status of message", 'N'
28 # k.add :collapse_non_new_messages, "Collapse all but new messages", 'N'
29 k.add :reply, "Reply to a message", 'r'
30 k.add :forward, "Forward a message", 'f'
31 k.add :alias, "Edit alias/nickname for a person", 'i'
32 k.add :edit_as_new, "Edit message as new", 'D'
33 k.add :save_to_disk, "Save message/attachment to disk", 's'
34 k.add :search, "Search for messages from particular people", 'S'
35 k.add :compose, "Compose message to person", 'm'
36 k.add :archive_and_kill, "Archive thread and kill buffer", 'a'
37 k.add :delete_and_kill, "Delete thread and kill buffer", 'd'
40 ## there are a couple important instance variables we hold to format
41 ## the thread and to provide line-based functionality. @layout is a
42 ## map from Messages to MessageLayouts, and @chunk_layout from
43 ## Chunks to ChunkLayouts. @message_lines is a map from row #s to
44 ## Message objects. @chunk_lines is a map from row #s to Chunk
45 ## objects. @person_lines is a map from row #s to Person objects.
47 def initialize thread, hidden_labels=[]
50 @hidden_labels = hidden_labels
52 @layout = SavingHash.new { MessageLayout.new }
53 @chunk_layout = SavingHash.new { ChunkLayout.new }
54 earliest, latest = nil, nil
58 @thread.each do |m, d, p|
61 @layout[m].state = initial_state_for m
62 @layout[m].color = altcolor ? :alternate_patina_color : :message_patina_color
63 @layout[m].star_color = altcolor ? :alternate_starred_patina_color : :starred_patina_color
64 @layout[m].orig_new = m.has_label? :read
66 if latest_date.nil? || m.date > latest_date
72 @layout[latest].state = :open if @layout[latest].state == :closed
73 @layout[earliest].state = :detailed if earliest.has_label?(:unread) || @thread.size == 1
75 @thread.remove_label :unread
79 def draw_line ln, opts={}
81 super ln, :highlight => true
86 def lines; @text.length; end
87 def [] i; @text[i]; end
90 m = @message_lines[curpos] or return
91 BufferManager.spawn_unless_exists("Full header") do
92 TextMode.new m.raw_header
96 def toggle_detailed_header
97 m = @message_lines[curpos] or return
98 @layout[m].state = (@layout[m].state == :detailed ? :open : :detailed)
103 m = @message_lines[curpos] or return
104 mode = ReplyMode.new m
105 BufferManager.spawn "Reply to #{m.subj}", mode
109 m = @message_lines[curpos] or return
110 mode = ForwardMode.new m
111 BufferManager.spawn "Forward of #{m.subj}", mode
115 include CanAliasContacts
117 p = @person_lines[curpos] or return
123 p = @person_lines[curpos] or return
124 mode = PersonSearchResultsMode.new [p]
125 BufferManager.spawn "Search for #{p.name}", mode
126 mode.load_threads :num => mode.buffer.content_height
130 p = @person_lines[curpos]
133 ComposeMode.new :to => [p]
137 BufferManager.spawn "Compose message", mode
142 m = @message_lines[curpos] or return
143 new_labels = BufferManager.ask_for_labels :label, "Labels for message: ", m.labels
145 return unless new_labels
146 m.labels = new_labels
147 new_labels.each { |l| LabelManager << l }
148 ## TODO: don't recalculate EVERYTHING
150 UpdateManager.relay self, :label, m
154 m = @message_lines[curpos] or return
155 toggle_label m, :starred
159 m = @message_lines[curpos] or return
160 toggle_label m, :unread
163 def toggle_label m, label
164 if m.has_label? label
169 ## TODO: don't recalculate EVERYTHING just to add a stupid little
170 ## star to the display
172 UpdateManager.relay self, :label, m
176 chunk = @chunk_lines[curpos] or return
180 l.state = (l.state != :closed ? :closed : :open)
181 cursor_down if l.state == :closed
182 when Message::Quote, Message::Signature, Message::CryptoSignature
183 return if chunk.lines.length == 1
184 toggle_chunk_expansion chunk
185 when Message::Attachment
187 toggle_chunk_expansion chunk
189 view_attachment chunk
196 m = @message_lines[curpos] or return
197 mode = ComposeMode.new(:body => m.basic_body_lines, :to => m.to, :cc => m.cc, :subj => m.subj, :bcc => m.bcc)
198 BufferManager.spawn "edit as new", mode
203 chunk = @chunk_lines[curpos] or return
205 when Message::Attachment
206 fn = BufferManager.ask_for_filename :filename, "Save attachment to file: ", chunk.filename
207 save_to_file(fn) { |f| f.print chunk.raw_content } if fn
209 m = @message_lines[curpos]
210 fn = BufferManager.ask_for_filename :filename, "Save message to file: "
212 save_to_file(fn) do |f|
213 m.each_raw_full_message_line { |l| f.print l }
219 m = @message_lines[curpos] or return
221 mode = ResumeMode.new m
222 BufferManager.spawn "Edit message", mode
223 BufferManager.kill_buffer self.buffer
226 BufferManager.flash "Not a draft message!"
230 def jump_to_first_open
231 m = @message_lines[0] or return
232 if @layout[m].state != :closed
239 def jump_to_next_open
240 m = @message_lines[curpos] or return
241 while nextm = @layout[m].next
242 break if @layout[nextm].state != :closed
245 jump_to_message nextm if nextm
248 def jump_to_prev_open
249 m = @message_lines[curpos] or return
250 ## jump to the top of the current message if we're in the body;
251 ## otherwise, to the previous message
255 while(prevm = @layout[m].prev)
256 break if @layout[prevm].state != :closed
259 jump_to_message prevm if prevm
265 def jump_to_message m
267 left = l.depth * INDENT_SPACES
268 right = left + l.width
270 ## jump to the top line unless both top and bottom fit in the current view
271 jump_to_line l.top unless l.top >= topline && l.top <= botline && l.bot >= topline && l.bot <= botline
273 ## jump to the left columns unless both left and right fit in the current view
274 jump_to_col left unless left >= leftcol && left <= rightcol && right >= leftcol && right <= rightcol
276 ## either way, move the cursor to the first line
280 def expand_all_messages
281 @global_message_state ||= :closed
282 @global_message_state = (@global_message_state == :closed ? :open : :closed)
283 @layout.each { |m, l| l.state = @global_message_state }
287 def collapse_non_new_messages
288 @layout.each { |m, l| l.state = l.orig_new ? :open : :closed }
292 def expand_all_quotes
293 if(m = @message_lines[curpos])
294 quotes = m.chunks.select { |c| (c.is_a?(Message::Quote) || c.is_a?(Message::Signature)) && c.lines.length > 1 }
295 numopen = quotes.inject(0) { |s, c| s + (@chunk_layout[c].state == :open ? 1 : 0) }
296 newstate = numopen > quotes.length / 2 ? :closed : :open
297 quotes.each { |c| @chunk_layout[c].state = newstate }
303 @layout = @chunk_layout = @text = nil # for good luck
307 @thread.remove_label :inbox
308 UpdateManager.relay self, :archived, @thread
309 BufferManager.kill_buffer_safely buffer
313 @thread.apply_label :deleted
314 UpdateManager.relay self, :deleted, @thread
315 BufferManager.kill_buffer_safely buffer
320 def toggle_chunk_expansion chunk
321 l = @chunk_layout[chunk]
322 l.state = (l.state != :closed ? :closed : :open)
323 cursor_down if l.state == :closed
326 def initial_state_for m
327 if m.has_label?(:starred) || m.has_label?(:unread)
336 buffer.mark_dirty if buffer
339 ## here we generate the actual content lines. we accumulate
340 ## everything into @text, and we set @chunk_lines and
341 ## @message_lines, and we update @layout.
349 @thread.each do |m, depth, parent|
350 unless m.is_a? Message # handle nil and :fake_root
351 @text += chunk_to_lines m, nil, @text.length, depth, parent
356 ## is this still necessary?
357 next unless @layout[m].state # skip discarded drafts
360 text = chunk_to_lines m, l.state, @text.length, depth, parent, l.color, l.star_color
363 l.bot = @text.length + text.length # updated below
367 # l.state we preserve
368 l.width = 0 # updated below
369 @layout[l.prev].next = m if l.prev
371 (0 ... text.length).each do |i|
372 @chunk_lines[@text.length + i] = m
373 @message_lines[@text.length + i] = m
374 lw = text[i].flatten.select { |x| x.is_a? String }.map { |x| x.length }.sum
379 if l.state != :closed
381 cl = @chunk_layout[c]
383 ## set the default state for chunks
385 if c.is_a?(Message::Attachment) && c.inlineable?
391 text = chunk_to_lines c, cl.state, @text.length, depth
392 (0 ... text.length).each do |i|
393 @chunk_lines[@text.length + i] = c
394 @message_lines[@text.length + i] = m
395 lw = text[i].flatten.select { |x| x.is_a? String }.map { |x| x.length }.sum - (depth * INDENT_SPACES)
396 l.width = lw if lw > l.width
400 @layout[m].bot = @text.length
405 def message_patina_lines m, state, start, parent, prefix, color, star_color
406 prefix_widget = [color, prefix]
408 open_widget = [color, (state == :closed ? "+ " : "- ")]
409 new_widget = [color, (m.has_label?(:unread) ? "N" : " ")]
411 if m.has_label?(:starred)
419 @person_lines[start] = m.from
420 [[prefix_widget, open_widget, new_widget, starred_widget,
422 "#{m.from ? m.from.mediumname : '?'} to #{m.recipients.map { |l| l.shortname }.join(', ')} #{m.date.to_nice_s} (#{m.date.to_nice_distance_s})"]]]
425 @person_lines[start] = m.from
426 [[prefix_widget, open_widget, new_widget, starred_widget,
428 "#{m.from ? m.from.mediumname : '?'}, #{m.date.to_nice_s} (#{m.date.to_nice_distance_s}) #{m.snippet}"]]]
431 @person_lines[start] = m.from
432 from = [[prefix_widget, open_widget, new_widget, starred_widget,
433 [color, "From: #{m.from ? format_person(m.from) : '?'}"]]]
437 m.to.each_with_index { |p, i| @person_lines[start + rest.length + from.length + i] = p }
438 rest += format_person_list " To: ", m.to
441 m.cc.each_with_index { |p, i| @person_lines[start + rest.length + from.length + i] = p }
442 rest += format_person_list " Cc: ", m.cc
445 m.bcc.each_with_index { |p, i| @person_lines[start + rest.length + from.length + i] = p }
446 rest += format_person_list " Bcc: ", m.bcc
450 " Date: #{m.date.strftime DATE_FORMAT} (#{m.date.to_nice_distance_s})",
451 " Subject: #{m.subj}",
452 (parent ? " In reply to: #{parent.from.mediumname}'s message of #{parent.date.strftime DATE_FORMAT}" : nil),
453 m.labels.empty? ? nil : " Labels: #{m.labels.join(', ')}",
456 from + rest.map { |l| [[color, prefix + " " + l]] }
460 def format_person_list prefix, people
461 ptext = people.map { |p| format_person p }
462 pad = " " * prefix.length
463 [prefix + ptext.first + (ptext.length > 1 ? "," : "")] +
464 ptext[1 .. -1].map_with_index do |e, i|
465 pad + e + (i == ptext.length - 1 ? "" : ",")
470 p.longname + (ContactManager.is_contact?(p) ? " (#{ContactManager.alias_for p})" : "")
473 def chunk_to_lines chunk, state, start, depth, parent=nil, color=nil, star_color=nil
474 prefix = " " * INDENT_SPACES * depth
477 [[[:missing_message_color, "#{prefix}<one or more unreceived messages>"]]]
479 [[[:missing_message_color, "#{prefix}<an unreceived message>"]]]
481 message_patina_lines(chunk, state, start, parent, prefix, color, star_color) +
482 (chunk.is_draft? ? [[[:draft_notification_color, prefix + " >>> This message is a draft. To edit, hit 'e'. <<<"]]] : [])
483 when Message::Attachment
484 return [[[:attachment_color, "#{prefix}x Attachment: #{chunk.filename} (#{chunk.content_type})"]]] unless chunk.inlineable?
487 [[[:attachment_color, "#{prefix}+ Attachment: #{chunk.filename} (#{chunk.lines.length} lines)"]]]
489 [[[:attachment_color, "#{prefix}- Attachment: #{chunk.filename} (#{chunk.lines.length} lines)"]]] + chunk.lines.map { |line| [[:none, "#{prefix}#{line}"]] }
493 if t.last =~ /^\s*$/ && t.length > 1
494 t.pop while t[-2] =~ /^\s*$/ # pop until only one file empty line
496 t.map { |line| [[:none, "#{prefix}#{line}"]] }
498 return [[[:quote_color, "#{prefix}#{chunk.lines.first}"]]] if chunk.lines.length == 1
501 [[[:quote_patina_color, "#{prefix}+ (#{chunk.lines.length} quoted lines)"]]]
503 [[[:quote_patina_color, "#{prefix}- (#{chunk.lines.length} quoted lines)"]]] + chunk.lines.map { |line| [[:quote_color, "#{prefix}#{line}"]] }
505 when Message::Signature
506 return [[[:sig_patina_color, "#{prefix}#{chunk.lines.first}"]]] if chunk.lines.length == 1
509 [[[:sig_patina_color, "#{prefix}+ (#{chunk.lines.length}-line signature)"]]]
511 [[[:sig_patina_color, "#{prefix}- (#{chunk.lines.length}-line signature)"]]] + chunk.lines.map { |line| [[:sig_color, "#{prefix}#{line}"]] }
513 when Message::CryptoSignature
516 when :valid: :cryptosig_valid_color
517 when :invalid: :cryptosig_invalid_color
518 else :cryptosig_unknown_color
522 [[[color, "#{prefix}+ #{chunk.description}"]]]
524 [[[color, "#{prefix}- #{chunk.description}"]]] +
525 chunk.lines.map { |line| [[color, "#{prefix}#{line}"]] }
528 raise "unknown chunk type #{chunk.class.name}"
532 def view_attachment a
533 BufferManager.flash "viewing #{a.content_type} attachment..."
535 BufferManager.erase_flash
536 BufferManager.completely_redraw_screen
538 BufferManager.spawn "Attachment: #{a.filename}", TextMode.new(a.to_s)
539 BufferManager.flash "Couldn't execute view command, viewing as text."