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 }
292 user_labels = BufferManager.ask_for_labels :label, "Labels for thread: ", modifyl, @hidden_labels
294 return unless user_labels
295 thread.labels = keepl + user_labels
296 user_labels.each { |l| LabelManager << l }
297 update_text_for_line curpos
300 def multi_edit_labels threads
301 answer = BufferManager.ask :add_labels, "add labels: "
303 user_labels = answer.split(/\s+/).map { |l| l.intern }
305 hl = user_labels.select { |l| @hidden_labels.member? l }
307 threads.each { |t| user_labels.each { |l| t.apply_label l } }
308 user_labels.each { |l| LabelManager << l }
310 BufferManager.flash "'#{hl}' is a reserved label!"
316 t = @threads[curpos] or return
318 return if m.nil? # probably won't happen
320 mode = ReplyMode.new m
321 BufferManager.spawn "Reply to #{m.subj}", mode
325 t = @threads[curpos] or return
327 return if m.nil? # probably won't happen
329 mode = ForwardMode.new m
330 BufferManager.spawn "Forward of #{m.subj}", mode
334 def load_n_threads_background n=LOAD_MORE_THREAD_NUM, opts={}
335 return if @load_thread # todo: wrap in mutex
336 @load_thread = Redwood::reporting_thread do
337 num = load_n_threads n, opts
338 opts[:when_done].call(num) if opts[:when_done]
343 def load_n_threads n=LOAD_MORE_THREAD_NUM, opts={}
344 @mbid = BufferManager.say "Searching for threads..."
346 last_update = Time.now - 9999 # oh yeah
347 @ts.load_n_threads(@ts.size + n, opts) do |i|
348 BufferManager.say "Loaded #{i} threads...", @mbid
349 if (Time.now - last_update) >= 0.25
351 BufferManager.draw_screen
352 last_update = Time.now
355 @ts.threads.each { |th| th.labels.each { |l| LabelManager << l } }
358 BufferManager.clear @mbid
360 BufferManager.draw_screen
363 synchronized :load_n_threads
369 "line #{curpos + 1} of #{l} #{dirty? ? '*modified*' : ''}"
373 def load_threads opts={}
374 n = opts[:num] || ThreadIndexMode::LOAD_MORE_THREAD_NUM
376 myopts = @load_thread_opts.merge({ :when_done => (lambda do |num|
377 opts[:when_done].call(num) if opts[:when_done]
379 BufferManager.flash "Found #{num} threads"
381 BufferManager.flash "No matches"
385 if opts[:background] || opts[:background].nil?
386 load_n_threads_background n, myopts
388 load_n_threads n, myopts
394 def cursor_thread; @threads[curpos]; end
403 raise "already hidden" if @hidden_threads[t]
404 @hidden_threads[t] = true
410 if @hidden_threads[t]
411 @hidden_threads.delete t
418 def update_text_for_line l
419 return unless l # not sure why this happens, but it does, occasionally
420 @text[l] = text_for_thread @threads[l]
421 buffer.mark_dirty if buffer
425 @text = @threads.map_with_index { |t, i| text_for_thread t }
426 @lines = @threads.map_with_index { |t, i| [t, i] }.to_h
427 buffer.mark_dirty if buffer
430 def author_text_for_thread t
432 if AccountManager.is_account?(p)
434 elsif t.authors.size == 1
442 def text_for_thread t
443 date = t.date.to_nice_s
444 from = author_text_for_thread t
445 if from.length > @from_width
446 from = from[0 ... (@from_width - 1)]
447 from += "." unless from[-1] == ?\s
450 new = t.has_label?(:unread)
451 starred = t.has_label?(:starred)
453 dp = t.direct_participants.any? { |p| AccountManager.is_account? p }
454 p = dp || t.participants.any? { |p| AccountManager.is_account? p }
466 [:tagged_color, @tags.tagged?(t) ? ">" : " "],
467 [:none, sprintf("%#{@date_width}s", date)],
468 (starred ? [:starred_color, "*"] : [:none, " "]),
469 [base_color, sprintf("%-#{@from_width}s", from)],
470 [:none, t.size == 1 ? " " * (@size_width + 2) : sprintf("(%#{@size_width}d)", t.size)],
471 [:to_me_color, dp ? " >" : (p ? ' +' : " ")],
472 [base_color, t.subj + (t.subj.empty? ? "" : " ")],
474 (t.labels - @hidden_labels).map { |label| [:label_color, "+#{label} "] } +
475 [[:snippet_color, t.snippet]
479 def dirty?; (@hidden_threads.keys + @threads).any? { |t| t.dirty? }; end
483 def initialize_threads
484 @ts = ThreadSet.new Index.instance, $config[:thread_by_subject]
485 @ts_mutex = Mutex.new