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", '@'
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_label_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
108 def handle_deleted_update sender, t
109 handle_read_update sender, t
115 def is_relevant? m; false; end
117 def handle_add_update sender, m
118 if is_relevant?(m) || @ts.is_relevant?(m)
119 @ts.load_thread_for_message m
121 BufferManager.draw_screen
125 def handle_delete_update sender, mid
126 if @ts.contains_id? mid
129 BufferManager.draw_screen
134 ## let's see you do THIS in python
135 @threads = @ts.threads.select { |t| !@hidden_threads[t] }.sort_by { |t| t.date }.reverse
136 @size_width = (@threads.max_of { |t| t.size } || 0).num_digits
141 return unless(t = @threads[curpos])
142 message, *crap = t.find { |m, *o| m.has_label? :draft }
144 mode = ResumeMode.new message
145 BufferManager.spawn "Edit message", mode
147 BufferManager.flash "Not a draft message!"
151 def actually_toggle_starred t
152 if t.has_label? :starred # if ANY message has a star
153 t.remove_label :starred # remove from all
155 t.first.add_label :starred # add only to first
160 t = @threads[curpos] or return
161 actually_toggle_starred t
162 update_text_for_line curpos
166 def multi_toggle_starred threads
167 threads.each { |t| actually_toggle_starred t }
171 def actually_toggle_archived t
172 if t.has_label? :inbox
173 t.remove_label :inbox
174 UpdateManager.relay self, :archived, t
177 UpdateManager.relay self, :unarchived, t
182 t = @threads[curpos] or return
183 actually_toggle_archived t
184 update_text_for_line curpos
187 def multi_toggle_archived threads
188 threads.each { |t| actually_toggle_archived t }
193 t = @threads[curpos] or return
194 t.toggle_label :unread
195 update_text_for_line curpos
199 def multi_toggle_new threads
200 threads.each { |t| t.toggle_label :unread }
204 def multi_toggle_tagged threads
210 n = ((curpos + 1) ... lines).find { |i| @threads[i].has_label? :unread } || (0 ... curpos).find { |i| @threads[i].has_label? :unread }
212 ## jump there if necessary
213 jump_to_line n unless n >= topline && n < botline
216 BufferManager.flash "No new messages"
221 t = @threads[curpos] or return
222 multi_mark_as_spam [t]
225 def multi_mark_as_spam threads
234 t = @threads[curpos] or return
238 def multi_delete threads
240 t.toggle_label :deleted
247 t = @threads[curpos] or return
251 def multi_kill threads
253 t.apply_label :killed
260 dirty_threads = (@threads + @hidden_threads.keys).select { |t| t.dirty? }
261 return if dirty_threads.empty?
263 BufferManager.say("Saving threads...") do |say_id|
264 dirty_threads.each_with_index do |t, i|
265 BufferManager.say "Saving modified thread #{i + 1} of #{dirty_threads.length}...", say_id
272 UpdateManager.unregister self
276 BufferManager.clear @mbid if @mbid
277 sleep 0.1 # TODO: necessary?
278 BufferManager.erase_flash
285 t = @threads[curpos] or return
286 @tags.toggle_tag_for t
287 update_text_for_line curpos
291 def apply_to_tagged; @tags.apply_to_tagged; end
294 thread = @threads[curpos] or return
295 speciall = (@hidden_labels + LabelManager::RESERVED_LABELS).uniq
296 keepl, modifyl = thread.labels.partition { |t| speciall.member? t }
298 user_labels = BufferManager.ask_for_labels :label, "Labels for thread: ", modifyl, @hidden_labels
300 return unless user_labels
301 thread.labels = keepl + user_labels
302 user_labels.each { |l| LabelManager << l }
303 update_text_for_line curpos
306 def multi_edit_labels threads
307 answer = BufferManager.ask :add_labels, "add labels: "
309 user_labels = answer.split(/\s+/).map { |l| l.intern }
311 hl = user_labels.select { |l| @hidden_labels.member? l }
313 threads.each { |t| user_labels.each { |l| t.apply_label l } }
314 user_labels.each { |l| LabelManager << l }
316 BufferManager.flash "'#{hl}' is a reserved label!"
322 t = @threads[curpos] or return
324 return if m.nil? # probably won't happen
326 mode = ReplyMode.new m
327 BufferManager.spawn "Reply to #{m.subj}", mode
331 t = @threads[curpos] or return
333 return if m.nil? # probably won't happen
335 mode = ForwardMode.new m
336 BufferManager.spawn "Forward of #{m.subj}", mode
340 def load_n_threads_background n=LOAD_MORE_THREAD_NUM, opts={}
341 return if @load_thread # todo: wrap in mutex
342 @load_thread = Redwood::reporting_thread do
343 num = load_n_threads n, opts
344 opts[:when_done].call(num) if opts[:when_done]
349 def load_n_threads n=LOAD_MORE_THREAD_NUM, opts={}
350 @mbid = BufferManager.say "Searching for threads..."
352 last_update = Time.now - 9999 # oh yeah
353 @ts.load_n_threads(@ts.size + n, opts) do |i|
354 BufferManager.say "Loaded #{i} threads...", @mbid
355 if (Time.now - last_update) >= 0.25
357 BufferManager.draw_screen
358 last_update = Time.now
361 @ts.threads.each { |th| th.labels.each { |l| LabelManager << l } }
364 BufferManager.clear @mbid
366 BufferManager.draw_screen
369 synchronized :load_n_threads
375 "line #{curpos + 1} of #{l} #{dirty? ? '*modified*' : ''}"
379 def load_threads opts={}
380 n = opts[:num] || ThreadIndexMode::LOAD_MORE_THREAD_NUM
382 myopts = @load_thread_opts.merge({ :when_done => (lambda do |num|
383 opts[:when_done].call(num) if opts[:when_done]
385 BufferManager.flash "Found #{num} threads"
387 BufferManager.flash "No matches"
391 if opts[:background] || opts[:background].nil?
392 load_n_threads_background n, myopts
394 load_n_threads n, myopts
400 def cursor_thread; @threads[curpos]; end
409 raise "already hidden" if @hidden_threads[t]
410 @hidden_threads[t] = true
416 if @hidden_threads[t]
417 @hidden_threads.delete t
424 def update_text_for_line l
425 return unless l # not sure why this happens, but it does, occasionally
426 @text[l] = text_for_thread @threads[l]
427 buffer.mark_dirty if buffer
431 @text = @threads.map_with_index { |t, i| text_for_thread t }
432 @lines = @threads.map_with_index { |t, i| [t, i] }.to_h
433 buffer.mark_dirty if buffer
436 def author_text_for_thread t
438 if AccountManager.is_account?(p)
440 elsif t.authors.size == 1
448 def text_for_thread t
449 date = t.date.to_nice_s
450 from = author_text_for_thread t
451 if from.length > @from_width
452 from = from[0 ... (@from_width - 1)]
453 from += "." unless from[-1] == ?\s
456 new = t.has_label?(:unread)
457 starred = t.has_label?(:starred)
459 dp = t.direct_participants.any? { |p| AccountManager.is_account? p }
460 p = dp || t.participants.any? { |p| AccountManager.is_account? p }
472 [:tagged_color, @tags.tagged?(t) ? ">" : " "],
473 [:none, sprintf("%#{@date_width}s", date)],
474 (starred ? [:starred_color, "*"] : [:none, " "]),
475 [base_color, sprintf("%-#{@from_width}s", from)],
476 [:none, t.size == 1 ? " " * (@size_width + 2) : sprintf("(%#{@size_width}d)", t.size)],
477 [:to_me_color, dp ? " >" : (p ? ' +' : " ")],
478 [base_color, t.subj + (t.subj.empty? ? "" : " ")],
480 (t.labels - @hidden_labels).map { |label| [:label_color, "+#{label} "] } +
481 [[:snippet_color, t.snippet]
485 def dirty?; (@hidden_threads.keys + @threads).any? { |t| t.dirty? }; end
489 def initialize_threads
490 @ts = ThreadSet.new Index.instance, $config[:thread_by_subject]
491 @ts_mutex = Mutex.new