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
9 DATE_FORMAT = "%B %e %Y %l:%M%P"
10 INDENT_SPACES = 2 # how many spaces to indent child messages
12 register_keymap do |k|
13 k.add :toggle_detailed_header, "Toggle detailed header", 'd'
14 k.add :show_header, "Show full message header", 'H'
15 k.add :toggle_expanded, "Expand/collapse item", :enter
16 k.add :expand_all_messages, "Expand/collapse all messages", 'E'
17 k.add :edit_message, "Edit message (drafts only)", 'e'
18 k.add :expand_all_quotes, "Expand/collapse all quotes in a message", 'o'
19 k.add :jump_to_next_open, "Jump to next open message", 'n'
20 k.add :jump_to_prev_open, "Jump to previous open message", 'p'
21 k.add :toggle_starred, "Star or unstar message", '*'
22 k.add :collapse_non_new_messages, "Collapse all but new messages", 'N'
23 k.add :reply, "Reply to a message", 'r'
24 k.add :forward, "Forward a message", 'f'
25 k.add :alias, "Edit alias/nickname for a person", 'a'
26 k.add :edit_as_new, "Edit message as new", 'D'
27 k.add :save_to_disk, "Save message/attachment to disk", 's'
28 k.add :search, "Search for messages from particular people", 'S'
31 ## there are a couple important instance variables we hold to lay
32 ## out the thread and to provide line-based functionality. @layout
33 ## is a map from Message and Chunk objects to Layout objects. (for
34 ## chunks, we only use the state field right now.) @message_lines is
35 ## a map from row #s to Message objects. @chunk_lines is a map from
36 ## row #s to Chunk objects. @person_lines is a map from row #s to
39 def initialize thread, hidden_labels=[]
42 @hidden_labels = hidden_labels
45 earliest, latest = nil, nil
48 @thread.each do |m, d, p|
51 @layout[m] = Layout.new
52 @layout[m].state = initial_state_for m
53 @layout[m].color = altcolor ? :alternate_patina_color : :message_patina_color
55 if latest_date.nil? || m.date > latest_date
61 @layout[latest].state = :open if @layout[latest].state == :closed
62 @layout[earliest].state = :detailed if earliest.has_label?(:unread) || @thread.size == 1
64 BufferManager.say("Loading message bodies...") { regen_text }
67 def draw_line ln, opts={}
69 super ln, :highlight => true
74 def lines; @text.length; end
75 def [] i; @text[i]; end
78 m = @message_lines[curpos] or return
79 BufferManager.spawn_unless_exists("Full header") do
80 TextMode.new m.raw_header
84 def toggle_detailed_header
85 m = @message_lines[curpos] or return
86 @layout[m].state = (@layout[m].state == :detailed ? :open : :detailed)
91 m = @message_lines[curpos] or return
92 mode = ReplyMode.new m
93 BufferManager.spawn "Reply to #{m.subj}", mode
97 m = @message_lines[curpos] or return
98 mode = ForwardMode.new m
99 BufferManager.spawn "Forward of #{m.subj}", mode
103 include CanAliasContacts
105 p = @person_lines[curpos] or return
111 p = @person_lines[curpos] or return
112 mode = PersonSearchResultsMode.new [p]
113 BufferManager.spawn "search for #{p.name}", mode
114 mode.load_threads :num => mode.buffer.content_height
118 m = @message_lines[curpos] or return
119 if m.has_label? :starred
120 m.remove_label :starred
124 ## TODO: don't recalculate EVERYTHING just to add a stupid little
125 ## star to the display
127 UpdateManager.relay :starred, m
131 chunk = @chunk_lines[curpos] or return
133 when Message, Message::Quote, Message::Signature
134 return if chunk.lines.length == 1 unless chunk.is_a? Message # too small to expand/close
136 l.state = (l.state != :closed ? :closed : :open)
137 cursor_down if l.state == :closed
138 when Message::Attachment
139 view_attachment chunk
145 m = @message_lines[curpos] or return
146 mode = ComposeMode.new(:body => m.basic_body_lines, :to => m.to, :cc => m.cc, :subj => m.subj, :bcc => m.bcc)
147 BufferManager.spawn "edit as new", mode
152 chunk = @chunk_lines[curpos] or return
154 when Message::Attachment
155 fn = BufferManager.ask :filename, "Save attachment to file: ", chunk.filename
156 save_to_file(fn) { |f| f.print chunk } if fn
158 m = @message_lines[curpos]
159 fn = BufferManager.ask :filename, "Save message to file: "
160 save_to_file(fn) { |f| f.print m.raw_full_message } if fn
165 m = @message_lines[curpos] or return
167 mode = ResumeMode.new m
168 BufferManager.spawn "Edit message", mode
171 BufferManager.flash "Not a draft message!"
175 def jump_to_first_open
176 m = @message_lines[0] or return
177 if @layout[m].state != :closed
184 def jump_to_next_open
185 m = @message_lines[curpos] or return
186 while nextm = @layout[m].next
187 break if @layout[nextm].state != :closed
190 jump_to_message nextm if nextm
193 def jump_to_prev_open
194 m = @message_lines[curpos] or return
195 ## jump to the top of the current message if we're in the body;
196 ## otherwise, to the previous message
200 while(prevm = @layout[m].prev)
201 break if @layout[prevm].state != :closed
204 jump_to_message prevm if prevm
210 def jump_to_message m
212 left = l.depth * INDENT_SPACES
213 right = left + l.width
215 ## jump to the top line unless both top and bottom fit in the current view
216 jump_to_line l.top unless l.top >= topline && l.top <= botline && l.bot >= topline && l.bot <= botline
218 ## jump to the left columns unless both left and right fit in the current view
219 jump_to_col left unless left >= leftcol && left <= rightcol && right >= leftcol && right <= rightcol
221 ## either way, move the cursor to the first line
225 def expand_all_messages
226 @global_message_state ||= :closed
227 @global_message_state = (@global_message_state == :closed ? :open : :closed)
228 @layout.each { |m, l| l.state = @global_message_state if m.is_a? Message }
232 def collapse_non_new_messages
233 @layout.each { |m, l| l.state = m.has_label?(:unread) ? :open : :closed }
237 def expand_all_quotes
238 if(m = @message_lines[curpos])
239 quotes = m.chunks.select { |c| (c.is_a?(Message::Quote) || c.is_a?(Message::Signature)) && c.lines.length > 1 }
240 numopen = quotes.inject(0) { |s, c| s + (@layout[c].state == :open ? 1 : 0) }
241 newstate = numopen > quotes.length / 2 ? :closed : :open
242 quotes.each { |c| @layout[c].state = newstate }
247 ## kinda slow for large threads. TODO: fasterify
249 BufferManager.say "Marking messages as read..." do
250 @thread.each do |m, d, p|
251 if m && m.has_label?(:unread)
252 m.remove_label :unread
253 UpdateManager.relay :read, m
257 @layout = @text = nil
262 def initial_state_for m
263 if m.has_label?(:starred) || m.has_label?(:unread)
272 buffer.mark_dirty if buffer
275 ## here we generate the actual content lines. we accumulate
276 ## everything into @text, and we set @chunk_lines and
277 ## @message_lines, and we update @layout.
285 @thread.each do |m, depth, parent|
286 unless m.is_a? Message # handle nil and :fake_root
287 @text += chunk_to_lines m, nil, @text.length, depth, parent
291 ## we're occasionally called on @threads that have had messages
292 ## added to them since initialization. luckily we regen_text on
293 ## the entire thread every time the user does anything besides
294 ## scrolling (basically), so we can just slap this on here.
296 ## to pick nits, the niceness that i do in the constructor with
297 ## 'latest' etc. (for automatically opening just the latest
298 ## message if everything's been read) will not be valid, but
299 ## that's just a nicety and hopefully this won't happen too
302 unless @layout.member? m
303 l = @layout[m] = Layout.new
304 l.state = initial_state_for m
305 l.color = prevm && @layout[prevm].color == :message_patina_color ? :alternate_patina_color : :message_patina_color
310 text = chunk_to_lines m, l.state, @text.length, depth, parent, @layout[m].color
313 l.bot = @text.length + text.length # updated below
317 # l.state we preserve
318 l.width = 0 # updated below
319 @layout[l.prev].next = m if l.prev
321 (0 ... text.length).each do |i|
322 @chunk_lines[@text.length + i] = m
323 @message_lines[@text.length + i] = m
324 lw = text[i].flatten.select { |x| x.is_a? String }.map { |x| x.length }.sum
329 if @layout[m].state != :closed
331 cl = (@layout[c] ||= Layout.new)
333 text = chunk_to_lines c, cl.state, @text.length, depth
334 (0 ... text.length).each do |i|
335 @chunk_lines[@text.length + i] = c
336 @message_lines[@text.length + i] = m
337 lw = text[i].flatten.select { |x| x.is_a? String }.map { |x| x.length }.sum - (depth * INDENT_SPACES)
338 l.width = lw if lw > l.width
342 @layout[m].bot = @text.length
347 def message_patina_lines m, state, start, parent, prefix, color
348 prefix_widget = [color, prefix]
353 when :open, :detailed
357 if m.has_label?(:starred)
358 [:starred_patina_color, "* "]
365 @person_lines[start] = m.from
366 [[prefix_widget, widget, imp_widget,
368 "#{m.from ? m.from.mediumname : '?'} to #{m.recipients.map { |l| l.shortname }.join(', ')} #{m.date.to_nice_s} (#{m.date.to_nice_distance_s})"]]]
371 @person_lines[start] = m.from
372 [[prefix_widget, widget, imp_widget,
374 "#{m.from ? m.from.mediumname : '?'}, #{m.date.to_nice_s} (#{m.date.to_nice_distance_s}) #{m.snippet}"]]]
377 @person_lines[start] = m.from
378 from = [[prefix_widget, widget, imp_widget, [color, "From: #{m.from ? format_person(m.from) : '?'}"]]]
382 m.to.each_with_index { |p, i| @person_lines[start + rest.length + from.length + i] = p }
383 rest += format_person_list " To: ", m.to
386 m.cc.each_with_index { |p, i| @person_lines[start + rest.length + from.length + i] = p }
387 rest += format_person_list " Cc: ", m.cc
390 m.bcc.each_with_index { |p, i| @person_lines[start + rest.length + from.length + i] = p }
391 rest += format_person_list " Bcc: ", m.bcc
395 " Date: #{m.date.strftime DATE_FORMAT} (#{m.date.to_nice_distance_s})",
396 " Subject: #{m.subj}",
397 (parent ? " In reply to: #{parent.from.mediumname}'s message of #{parent.date.strftime DATE_FORMAT}" : nil),
398 m.labels.empty? ? nil : " Labels: #{m.labels.join(', ')}",
401 from + rest.map { |l| [[color, prefix + " " + l]] }
405 def format_person_list prefix, people
406 ptext = people.map { |p| format_person p }
407 pad = " " * prefix.length
408 [prefix + ptext.first + (ptext.length > 1 ? "," : "")] +
409 ptext[1 .. -1].map_with_index do |e, i|
410 pad + e + (i == ptext.length - 1 ? "" : ",")
415 p.longname + (ContactManager.is_contact?(p) ? " (#{ContactManager.alias_for p})" : "")
418 def chunk_to_lines chunk, state, start, depth, parent=nil, color=nil
419 prefix = " " * INDENT_SPACES * depth
422 [[[:missing_message_color, "#{prefix}<one or more unreceived messages>"]]]
424 [[[:missing_message_color, "#{prefix}<an unreceived message>"]]]
426 message_patina_lines(chunk, state, start, parent, prefix, color) +
427 (chunk.is_draft? ? [[[:draft_notification_color, prefix + " >>> This message is a draft. To edit, hit 'e'. <<<"]]] : [])
428 when Message::Attachment
429 [[[:mime_color, "#{prefix}+ MIME attachment #{chunk.content_type}#{chunk.desc ? ' (' + chunk.desc + ')': ''}"]]]
432 if t.last =~ /^\s*$/ && t.length > 1
433 t.pop while t[-2] =~ /^\s*$/ # pop until only one file empty line
435 t.map { |line| [[:none, "#{prefix}#{line}"]] }
437 return [[[:quote_color, "#{prefix}#{chunk.lines.first}"]]] if chunk.lines.length == 1
440 [[[:quote_patina_color, "#{prefix}+ (#{chunk.lines.length} quoted lines)"]]]
442 [[[:quote_patina_color, "#{prefix}- (#{chunk.lines.length} quoted lines)"]]] + chunk.lines.map { |line| [[:quote_color, "#{prefix}#{line}"]] }
444 when Message::Signature
445 return [[[:sig_patina_color, "#{prefix}#{chunk.lines.first}"]]] if chunk.lines.length == 1
448 [[[:sig_patina_color, "#{prefix}+ (#{chunk.lines.length}-line signature)"]]]
450 [[[:sig_patina_color, "#{prefix}- (#{chunk.lines.length}-line signature)"]]] + chunk.lines.map { |line| [[:sig_color, "#{prefix}#{line}"]] }
453 raise "unknown chunk type #{chunk.class.name}"
457 def view_attachment a
458 BufferManager.flash "viewing #{a.content_type} attachment..."
460 BufferManager.erase_flash
461 BufferManager.completely_redraw_screen