3 class ThreadViewMode < LineCursorMode
4 include CanSpawnComposeMode
6 ## this holds all info we need to lay out a message
8 attr_accessor :top, :bot, :prev, :next, :depth, :width, :state, :color, :star_color, :orig_new
15 DATE_FORMAT = "%B %e %Y %l:%M%P"
16 INDENT_SPACES = 2 # how many spaces to indent child messages
18 register_keymap do |k|
19 k.add :toggle_detailed_header, "Toggle detailed header", 'h'
20 k.add :show_header, "Show full message header", 'H'
21 k.add :activate_chunk, "Expand/collapse or activate item", :enter
22 k.add :expand_all_messages, "Expand/collapse all messages", 'E'
23 k.add :edit_draft, "Edit draft", 'e'
24 k.add :edit_labels, "Edit or add labels for a thread", 'l'
25 k.add :expand_all_quotes, "Expand/collapse all quotes in a message", 'o'
26 k.add :jump_to_next_open, "Jump to next open message", 'n'
27 k.add :jump_to_prev_open, "Jump to previous open message", 'p'
28 k.add :toggle_starred, "Star or unstar message", '*'
29 k.add :toggle_new, "Toggle new/read status of message", 'N'
30 # k.add :collapse_non_new_messages, "Collapse all but new messages", 'N'
31 k.add :reply, "Reply to a message", 'r'
32 k.add :forward, "Forward a message", 'f'
33 k.add :alias, "Edit alias/nickname for a person", 'i'
34 k.add :edit_as_new, "Edit message as new", 'D'
35 k.add :save_to_disk, "Save message/attachment to disk", 's'
36 k.add :search, "Search for messages from particular people", 'S'
37 k.add :compose, "Compose message to person", 'm'
38 k.add :archive_and_kill, "Archive thread and kill buffer", 'a'
39 k.add :delete_and_kill, "Delete thread and kill buffer", 'd'
42 ## there are a couple important instance variables we hold to format
43 ## the thread and to provide line-based functionality. @layout is a
44 ## map from Messages to MessageLayouts, and @chunk_layout from
45 ## Chunks to ChunkLayouts. @message_lines is a map from row #s to
46 ## Message objects. @chunk_lines is a map from row #s to Chunk
47 ## objects. @person_lines is a map from row #s to Person objects.
49 def initialize thread, hidden_labels=[]
52 @hidden_labels = hidden_labels
54 @layout = SavingHash.new { MessageLayout.new }
55 @chunk_layout = SavingHash.new { ChunkLayout.new }
56 earliest, latest = nil, nil
60 @thread.each do |m, d, p|
63 @layout[m].state = initial_state_for m
64 @layout[m].color = altcolor ? :alternate_patina_color : :message_patina_color
65 @layout[m].star_color = altcolor ? :alternate_starred_patina_color : :starred_patina_color
66 @layout[m].orig_new = m.has_label? :read
68 if latest_date.nil? || m.date > latest_date
74 @layout[latest].state = :open if @layout[latest].state == :closed
75 @layout[earliest].state = :detailed if earliest.has_label?(:unread) || @thread.size == 1
77 @thread.remove_label :unread
81 def draw_line ln, opts={}
83 super ln, :highlight => true
88 def lines; @text.length; end
89 def [] i; @text[i]; end
92 m = @message_lines[curpos] or return
93 BufferManager.spawn_unless_exists("Full header") do
94 TextMode.new m.raw_header
98 def toggle_detailed_header
99 m = @message_lines[curpos] or return
100 @layout[m].state = (@layout[m].state == :detailed ? :open : :detailed)
105 m = @message_lines[curpos] or return
106 mode = ReplyMode.new m
107 BufferManager.spawn "Reply to #{m.subj}", mode
111 m = @message_lines[curpos] or return
112 mode = ForwardMode.new m
113 BufferManager.spawn "Forward of #{m.subj}", mode
117 include CanAliasContacts
119 p = @person_lines[curpos] or return
125 p = @person_lines[curpos] or return
126 mode = PersonSearchResultsMode.new [p]
127 BufferManager.spawn "Search for #{p.name}", mode
128 mode.load_threads :num => mode.buffer.content_height
132 p = @person_lines[curpos]
134 spawn_compose_mode :to => [p]
141 reserved_labels = @thread.labels.select { |l| LabelManager::RESERVED_LABELS.include? l }
142 new_labels = BufferManager.ask_for_labels :label, "Labels for thread: ", @thread.labels
144 return unless new_labels
145 @thread.labels = (reserved_labels + new_labels).uniq
146 new_labels.each { |l| LabelManager << l }
148 UpdateManager.relay self, :label, m
152 m = @message_lines[curpos] or return
153 toggle_label m, :starred
157 m = @message_lines[curpos] or return
158 toggle_label m, :unread
161 def toggle_label m, label
162 if m.has_label? label
167 ## TODO: don't recalculate EVERYTHING just to add a stupid little
168 ## star to the display
170 UpdateManager.relay self, :label, m
173 ## called when someone presses enter when the cursor is highlighting
174 ## a chunk. for expandable chunks (including messages) we toggle
175 ## open/closed state; for viewable chunks (like attachments) we
178 chunk = @chunk_lines[curpos] or return
180 if chunk.is_a?(Message)
182 elsif chunk.expandable?
186 layout.state = (layout.state != :closed ? :closed : :open)
187 #cursor_down if layout.state == :closed # too annoying
189 elsif chunk.viewable?
195 m = @message_lines[curpos] or return
196 mode = ComposeMode.new(:body => m.basic_body_lines, :to => m.to, :cc => m.cc, :subj => m.subj, :bcc => m.bcc)
197 BufferManager.spawn "edit as new", mode
202 chunk = @chunk_lines[curpos] or return
204 when Chunk::Attachment
205 fn = BufferManager.ask_for_filename :filename, "Save attachment to file: ", chunk.filename
206 save_to_file(fn) { |f| f.print chunk.raw_content } if fn
208 m = @message_lines[curpos]
209 fn = BufferManager.ask_for_filename :filename, "Save message to file: "
211 save_to_file(fn) do |f|
212 m.each_raw_message_line { |l| f.print l }
218 m = @message_lines[curpos] or return
220 mode = ResumeMode.new m
221 BufferManager.spawn "Edit message", mode
222 BufferManager.kill_buffer self.buffer
225 BufferManager.flash "Not a draft message!"
229 def jump_to_first_open
230 m = @message_lines[0] or return
231 if @layout[m].state != :closed
238 def jump_to_next_open
239 m = @message_lines[curpos] or return
240 while nextm = @layout[m].next
241 break if @layout[nextm].state != :closed
244 jump_to_message nextm if nextm
247 def jump_to_prev_open
248 m = @message_lines[curpos] or return
249 ## jump to the top of the current message if we're in the body;
250 ## otherwise, to the previous message
254 while(prevm = @layout[m].prev)
255 break if @layout[prevm].state != :closed
258 jump_to_message prevm if prevm
264 def jump_to_message m
266 left = l.depth * INDENT_SPACES
267 right = left + l.width
269 ## jump to the top line unless both top and bottom fit in the current view
270 jump_to_line l.top unless l.top >= topline && l.top <= botline && l.bot >= topline && l.bot <= botline
272 ## jump to the left columns unless both left and right fit in the current view
273 jump_to_col left unless left >= leftcol && left <= rightcol && right >= leftcol && right <= rightcol
275 ## either way, move the cursor to the first line
279 def expand_all_messages
280 @global_message_state ||= :closed
281 @global_message_state = (@global_message_state == :closed ? :open : :closed)
282 @layout.each { |m, l| l.state = @global_message_state }
286 def collapse_non_new_messages
287 @layout.each { |m, l| l.state = l.orig_new ? :open : :closed }
291 def expand_all_quotes
292 if(m = @message_lines[curpos])
293 quotes = m.chunks.select { |c| (c.is_a?(Chunk::Quote) || c.is_a?(Chunk::Signature)) && c.lines.length > 1 }
294 numopen = quotes.inject(0) { |s, c| s + (@chunk_layout[c].state == :open ? 1 : 0) }
295 newstate = numopen > quotes.length / 2 ? :closed : :open
296 quotes.each { |c| @chunk_layout[c].state = newstate }
302 @layout = @chunk_layout = @text = nil # for good luck
306 @thread.remove_label :inbox
307 UpdateManager.relay self, :archived, @thread
308 BufferManager.kill_buffer_safely buffer
312 @thread.apply_label :deleted
313 UpdateManager.relay self, :deleted, @thread
314 BufferManager.kill_buffer_safely buffer
319 def initial_state_for m
320 if m.has_label?(:starred) || m.has_label?(:unread)
329 buffer.mark_dirty if buffer
332 ## here we generate the actual content lines. we accumulate
333 ## everything into @text, and we set @chunk_lines and
334 ## @message_lines, and we update @layout.
342 @thread.each do |m, depth, parent|
343 unless m.is_a? Message # handle nil and :fake_root
344 @text += chunk_to_lines m, nil, @text.length, depth, parent
349 ## is this still necessary?
350 next unless @layout[m].state # skip discarded drafts
353 text = chunk_to_lines m, l.state, @text.length, depth, parent, l.color, l.star_color
356 l.bot = @text.length + text.length # updated below
360 # l.state we preserve
361 l.width = 0 # updated below
362 @layout[l.prev].next = m if l.prev
364 (0 ... text.length).each do |i|
365 @chunk_lines[@text.length + i] = m
366 @message_lines[@text.length + i] = m
367 lw = text[i].flatten.select { |x| x.is_a? String }.map { |x| x.length }.sum
372 if l.state != :closed
374 cl = @chunk_layout[c]
376 ## set the default state for chunks
378 if c.expandable? && c.respond_to?(:initial_state)
384 text = chunk_to_lines c, cl.state, @text.length, depth
385 (0 ... text.length).each do |i|
386 @chunk_lines[@text.length + i] = c
387 @message_lines[@text.length + i] = m
388 lw = text[i].flatten.select { |x| x.is_a? String }.map { |x| x.length }.sum - (depth * INDENT_SPACES)
389 l.width = lw if lw > l.width
393 @layout[m].bot = @text.length
398 def message_patina_lines m, state, start, parent, prefix, color, star_color
399 prefix_widget = [color, prefix]
401 open_widget = [color, (state == :closed ? "+ " : "- ")]
402 new_widget = [color, (m.has_label?(:unread) ? "N" : " ")]
404 if m.has_label?(:starred)
412 @person_lines[start] = m.from
413 [[prefix_widget, open_widget, new_widget, starred_widget,
415 "#{m.from ? m.from.mediumname : '?'} to #{m.recipients.map { |l| l.shortname }.join(', ')} #{m.date.to_nice_s} (#{m.date.to_nice_distance_s})"]]]
418 @person_lines[start] = m.from
419 [[prefix_widget, open_widget, new_widget, starred_widget,
421 "#{m.from ? m.from.mediumname : '?'}, #{m.date.to_nice_s} (#{m.date.to_nice_distance_s}) #{m.snippet}"]]]
424 @person_lines[start] = m.from
425 from = [[prefix_widget, open_widget, new_widget, starred_widget,
426 [color, "From: #{m.from ? format_person(m.from) : '?'}"]]]
430 m.to.each_with_index { |p, i| @person_lines[start + rest.length + from.length + i] = p }
431 rest += format_person_list " To: ", m.to
434 m.cc.each_with_index { |p, i| @person_lines[start + rest.length + from.length + i] = p }
435 rest += format_person_list " Cc: ", m.cc
438 m.bcc.each_with_index { |p, i| @person_lines[start + rest.length + from.length + i] = p }
439 rest += format_person_list " Bcc: ", m.bcc
442 show_labels = @thread.labels - LabelManager::HIDDEN_RESERVED_LABELS
444 " Date: #{m.date.strftime DATE_FORMAT} (#{m.date.to_nice_distance_s})",
445 " Subject: #{m.subj}",
446 (parent ? " In reply to: #{parent.from.mediumname}'s message of #{parent.date.strftime DATE_FORMAT}" : nil),
447 show_labels.empty? ? nil : " Labels: #{show_labels.join(', ')}",
450 from + rest.map { |l| [[color, prefix + " " + l]] }
454 def format_person_list prefix, people
455 ptext = people.map { |p| format_person p }
456 pad = " " * prefix.length
457 [prefix + ptext.first + (ptext.length > 1 ? "," : "")] +
458 ptext[1 .. -1].map_with_index do |e, i|
459 pad + e + (i == ptext.length - 1 ? "" : ",")
464 p.longname + (ContactManager.is_contact?(p) ? " (#{ContactManager.alias_for p})" : "")
467 ## todo: check arguments on this overly complex function
468 def chunk_to_lines chunk, state, start, depth, parent=nil, color=nil, star_color=nil
469 prefix = " " * INDENT_SPACES * depth
472 [[[:missing_message_color, "#{prefix}<one or more unreceived messages>"]]]
474 [[[:missing_message_color, "#{prefix}<an unreceived message>"]]]
476 message_patina_lines(chunk, state, start, parent, prefix, color, star_color) +
477 (chunk.is_draft? ? [[[:draft_notification_color, prefix + " >>> This message is a draft. To edit, hit 'e'. <<<"]]] : [])
480 raise "Bad chunk: #{chunk.inspect}" unless chunk.respond_to?(:inlineable?) ## debugging
482 chunk.lines.map { |line| [[chunk.color, "#{prefix}#{line}"]] }
483 elsif chunk.expandable?
486 [[[chunk.patina_color, "#{prefix}+ #{chunk.patina_text}"]]]
488 [[[chunk.patina_color, "#{prefix}- #{chunk.patina_text}"]]] + chunk.lines.map { |line| [[chunk.color, "#{prefix}#{line}"]] }
491 [[[chunk.patina_color, "#{prefix}x #{chunk.patina_text}"]]]
497 BufferManager.flash "viewing #{chunk.content_type} attachment..."
498 success = chunk.view!
499 BufferManager.erase_flash
500 BufferManager.completely_redraw_screen
502 BufferManager.spawn "Attachment: #{chunk.filename}", TextMode.new(chunk.to_s)
503 BufferManager.flash "Couldn't execute view command, viewing as text."