3 ## subclasses should implement:
6 class ThreadIndexMode < LineCursorMode
7 DATE_WIDTH = Time::TO_NICE_S_MAX_LEN
9 LOAD_MORE_THREAD_NUM = 20
11 register_keymap do |k|
12 k.add :load_threads, "Load #{LOAD_MORE_THREAD_NUM} more threads", 'M'
13 k.add :reload, "Discard threads and reload", 'D'
14 k.add :toggle_archived, "Toggle archived status", 'a'
15 k.add :toggle_starred, "Star or unstar all messages in thread", '*'
16 k.add :toggle_new, "Toggle new/read status of all messages in thread", 'N'
17 k.add :edit_labels, "Edit or add labels for a thread", 'l'
18 k.add :edit_message, "Edit message (drafts only)", 'e'
19 k.add :mark_as_spam, "Mark thread as spam", 'S'
20 k.add :delete, "Mark thread for deletion", 'd'
21 k.add :kill, "Kill thread (never to be seen in inbox again)", '&'
22 k.add :save, "Save changes now", '$'
23 k.add :jump_to_next_new, "Jump to next new thread", :tab
24 k.add :reply, "Reply to a thread", 'r'
25 k.add :forward, "Forward a thread", 'f'
26 k.add :toggle_tagged, "Tag/untag current line", 't'
27 k.add :apply_to_tagged, "Apply next command to all tagged threads", ';'
30 def initialize hidden_labels=[], load_thread_opts={}
34 @load_thread_opts = load_thread_opts
35 @hidden_labels = hidden_labels + LabelManager::HIDDEN_RESERVED_LABELS
36 @date_width = DATE_WIDTH
37 @from_width = FROM_WIDTH
40 @tags = Tagger.new self
45 UpdateManager.register self
47 @last_load_more_size = nil
48 to_load_more do |size|
49 next if @last_load_more_size == 0
50 load_threads :num => 1, :background => false
51 load_threads :num => (size - 1),
52 :when_done => lambda { |num| @last_load_more_size = num }
56 def lines; @text.length; end
57 def [] i; @text[i]; end
58 def contains_thread? t; !@lines[t].nil?; end
62 BufferManager.draw_screen
63 load_threads :num => buffer.content_height
66 ## open up a thread view window
68 t ||= @threads[curpos] or return
70 ## TODO: don't regen text completely
71 Redwood::reporting_thread do
72 BufferManager.say("Loading message bodies...") do |sid|
73 t.each { |m, *o| m.load_from_source! if m }
75 mode = ThreadViewMode.new t, @hidden_labels
76 BufferManager.spawn t.subj, mode
77 BufferManager.draw_screen
78 mode.jump_to_first_open
79 BufferManager.draw_screen # lame TODO: make this unnecessary
80 ## the first draw_screen is needed before topline and botline
81 ## are set, and the second to show the cursor having moved
83 t.remove_label :unread
84 update_text_for_line curpos
85 UpdateManager.relay self, :read, t
89 def multi_select threads
90 threads.each { |t| select t }
93 def handle_starred_update sender, m
94 t = @ts.thread_for(m) or return
95 l = @lines[t] or return
96 update_text_for_line l
97 BufferManager.draw_screen
100 def handle_read_update sender, t
101 l = @lines[t] or return
102 update_text_for_line @lines[t]
103 BufferManager.draw_screen
106 def handle_archived_update *a; handle_read_update(*a); end
109 def is_relevant? m; false; end
111 def handle_add_update sender, m
112 if is_relevant?(m) || @ts.is_relevant?(m)
113 @ts.load_thread_for_message m
115 BufferManager.draw_screen
119 def handle_delete_update sender, mid
120 if @ts.contains_id? mid
123 BufferManager.draw_screen
128 ## let's see you do THIS in python
129 @threads = @ts.threads.select { |t| !@hidden_threads[t] }.sort_by { |t| t.date }.reverse
130 @size_width = (@threads.max_of { |t| t.size } || 0).num_digits
135 return unless(t = @threads[curpos])
136 message, *crap = t.find { |m, *o| m.has_label? :draft }
138 mode = ResumeMode.new message
139 BufferManager.spawn "Edit message", mode
141 BufferManager.flash "Not a draft message!"
145 def actually_toggle_starred t
146 if t.has_label? :starred # if ANY message has a star
147 t.remove_label :starred # remove from all
149 t.first.add_label :starred # add only to first
154 t = @threads[curpos] or return
155 actually_toggle_starred t
156 update_text_for_line curpos
160 def multi_toggle_starred threads
161 threads.each { |t| actually_toggle_starred t }
165 def actually_toggle_archived t
166 if t.has_label? :inbox
167 t.remove_label :inbox
168 UpdateManager.relay self, :archived, t
171 UpdateManager.relay self, :unarchived, t
176 t = @threads[curpos] or return
177 actually_toggle_archived t
178 update_text_for_line curpos
181 def multi_toggle_archived threads
182 threads.each { |t| actually_toggle_archived t }
187 t = @threads[curpos] or return
188 t.toggle_label :unread
189 update_text_for_line curpos
193 def multi_toggle_new threads
194 threads.each { |t| t.toggle_label :unread }
198 def multi_toggle_tagged threads
204 n = ((curpos + 1) ... lines).find { |i| @threads[i].has_label? :unread } || (0 ... curpos).find { |i| @threads[i].has_label? :unread }
206 ## jump there if necessary
207 jump_to_line n unless n >= topline && n < botline
210 BufferManager.flash "No new messages"
215 t = @threads[curpos] or return
216 multi_mark_as_spam [t]
219 def multi_mark_as_spam threads
228 t = @threads[curpos] or return
232 def multi_delete threads
234 t.toggle_label :deleted
241 t = @threads[curpos] or return
245 def multi_kill threads
247 t.apply_label :killed
254 dirty_threads = (@threads + @hidden_threads.keys).select { |t| t.dirty? }
255 return if dirty_threads.empty?
257 BufferManager.say("Saving threads...") do |say_id|
258 dirty_threads.each_with_index do |t, i|
259 BufferManager.say "Saving modified thread #{i + 1} of #{dirty_threads.length}...", say_id
266 UpdateManager.unregister self
270 BufferManager.clear @mbid if @mbid
271 sleep 0.1 # TODO: necessary?
272 BufferManager.erase_flash
279 t = @threads[curpos] or return
280 @tags.toggle_tag_for t
281 update_text_for_line curpos
285 def apply_to_tagged; @tags.apply_to_tagged; end
288 thread = @threads[curpos] or return
289 speciall = (@hidden_labels + LabelManager::RESERVED_LABELS).uniq
290 keepl, modifyl = thread.labels.partition { |t| speciall.member? t }
291 cur_label_string = modifyl.join(" ")
292 cur_label_string += " " unless cur_label_string.empty?
294 applyable_labels = (LabelManager.applyable_labels - @hidden_labels).map { |l| LabelManager.string_for l }.sort_by { |s| s.downcase }
296 answer = BufferManager.ask_many_with_completions :label, "Labels for thread: ", applyable_labels, cur_label_string
299 user_labels = answer.split(/\s+/).map { |l| l.intern }
301 hl = user_labels.select { |l| speciall.member? l }
303 thread.labels = keepl + user_labels
304 user_labels.each { |l| LabelManager << l }
306 BufferManager.flash "'#{hl}' is a reserved label!"
308 update_text_for_line curpos
311 def multi_edit_labels threads
312 answer = BufferManager.ask :add_labels, "add labels: "
314 user_labels = answer.split(/\s+/).map { |l| l.intern }
316 hl = user_labels.select { |l| @hidden_labels.member? l }
318 threads.each { |t| user_labels.each { |l| t.apply_label l } }
319 user_labels.each { |l| LabelManager << l }
321 BufferManager.flash "'#{hl}' is a reserved label!"
327 t = @threads[curpos] or return
329 return if m.nil? # probably won't happen
331 mode = ReplyMode.new m
332 BufferManager.spawn "Reply to #{m.subj}", mode
336 t = @threads[curpos] or return
338 return if m.nil? # probably won't happen
340 mode = ForwardMode.new m
341 BufferManager.spawn "Forward of #{m.subj}", mode
345 def load_n_threads_background n=LOAD_MORE_THREAD_NUM, opts={}
346 return if @load_thread # todo: wrap in mutex
347 @load_thread = Redwood::reporting_thread do
348 num = load_n_threads n, opts
349 opts[:when_done].call(num) if opts[:when_done]
354 def load_n_threads n=LOAD_MORE_THREAD_NUM, opts={}
355 @mbid = BufferManager.say "Searching for threads..."
357 last_update = Time.now - 9999 # oh yeah
358 @ts.load_n_threads(@ts.size + n, opts) do |i|
359 BufferManager.say "Loaded #{i} threads...", @mbid
360 if (Time.now - last_update) >= 0.25
362 BufferManager.draw_screen
363 last_update = Time.now
366 @ts.threads.each { |th| th.labels.each { |l| LabelManager << l } }
369 BufferManager.clear @mbid
371 BufferManager.draw_screen
374 synchronized :load_n_threads
380 "line #{curpos + 1} of #{l} #{dirty? ? '*modified*' : ''}"
384 def load_threads opts={}
385 n = opts[:num] || ThreadIndexMode::LOAD_MORE_THREAD_NUM
387 myopts = @load_thread_opts.merge({ :when_done => (lambda do |num|
388 opts[:when_done].call(num) if opts[:when_done]
390 BufferManager.flash "Found #{num} threads"
392 BufferManager.flash "No matches"
396 if opts[:background] || opts[:background].nil?
397 load_n_threads_background n, myopts
399 load_n_threads n, myopts
405 def cursor_thread; @threads[curpos]; end
414 raise "already hidden" if @hidden_threads[t]
415 @hidden_threads[t] = true
421 if @hidden_threads[t]
422 @hidden_threads.delete t
429 def update_text_for_line l
430 return unless l # not sure why this happens, but it does, occasionally
431 @text[l] = text_for_thread @threads[l]
432 buffer.mark_dirty if buffer
436 @text = @threads.map_with_index { |t, i| text_for_thread t }
437 @lines = @threads.map_with_index { |t, i| [t, i] }.to_h
438 buffer.mark_dirty if buffer
441 def author_text_for_thread t
443 if AccountManager.is_account?(p)
445 elsif t.authors.size == 1
453 def text_for_thread t
454 date = t.date.to_nice_s
455 from = author_text_for_thread t
456 if from.length > @from_width
457 from = from[0 ... (@from_width - 1)]
458 from += "." unless from[-1] == ?\s
461 new = t.has_label?(:unread)
462 starred = t.has_label?(:starred)
464 dp = t.direct_participants.any? { |p| AccountManager.is_account? p }
465 p = dp || t.participants.any? { |p| AccountManager.is_account? p }
477 [:tagged_color, @tags.tagged?(t) ? ">" : " "],
478 [:none, sprintf("%#{@date_width}s", date)],
479 (starred ? [:starred_color, "*"] : [:none, " "]),
480 [base_color, sprintf("%-#{@from_width}s", from)],
481 [:none, t.size == 1 ? " " * (@size_width + 2) : sprintf("(%#{@size_width}d)", t.size)],
482 [:to_me_color, dp ? " >" : (p ? ' +' : " ")],
483 [base_color, t.subj + (t.subj.empty? ? "" : " ")],
485 (t.labels - @hidden_labels).map { |label| [:label_color, "+#{label} "] } +
486 [[:snippet_color, t.snippet]
490 def dirty?; (@hidden_threads.keys + @threads).any? { |t| t.dirty? }; end
494 def initialize_threads
495 @ts = ThreadSet.new Index.instance, $config[:thread_by_subject]
496 @ts_mutex = Mutex.new