4 ## subclasses should implement load_threads
5 class ThreadIndexMode < LineCursorMode
6 DATE_WIDTH = Time::TO_NICE_S_MAX_LEN
8 LOAD_MORE_THREAD_NUM = 20
10 register_keymap do |k|
11 k.add :load_threads, "Load #{LOAD_MORE_THREAD_NUM} more threads", 'M'
12 k.add :reload, "Discard threads and reload", 'D'
13 k.add :toggle_archived, "Toggle archived status", 'a'
14 k.add :toggle_starred, "Star or unstar all messages in thread", '*'
15 k.add :toggle_new, "Toggle new/read status of all messages in thread", 'N'
16 k.add :edit_labels, "Edit or add labels for a thread", 'l'
17 k.add :edit_message, "Edit message (drafts only)", 'e'
18 k.add :mark_as_spam, "Mark thread as spam", 'S'
19 k.add :delete, "Mark thread for deletion", 'd'
20 k.add :kill, "Kill thread (never to be seen in inbox again)", '&'
21 k.add :save, "Save changes now", '$'
22 k.add :jump_to_next_new, "Jump to next new thread", :tab
23 k.add :reply, "Reply to a thread", 'r'
24 k.add :forward, "Forward a thread", 'f'
25 k.add :toggle_tagged, "Tag/untag current line", 't'
26 k.add :apply_to_tagged, "Apply next command to all tagged threads", ';'
29 def initialize required_labels=[], hidden_labels=[]
32 @required_labels = required_labels
33 @hidden_labels = hidden_labels + LabelManager::HIDDEN_LABELS
34 @date_width = DATE_WIDTH
35 @from_width = FROM_WIDTH
37 @last_load_more_size = nil
39 @tags = Tagger.new self
44 UpdateManager.register self
46 to_load_more do |size|
47 next if @last_load_more_size == 0
48 load_threads :num => size,
49 :when_done => lambda { |num| @last_load_more_size = num }
50 sleep 1.0 # give 'em a chance to load
54 def lines; @text.length; end
55 def [] i; @text[i]; end
56 def contains_thread? t; !@lines[t].nil?; end
60 BufferManager.draw_screen
61 load_threads :num => buffer.content_height
64 ## open up a thread view window
66 t ||= @threads[curpos] or return
68 ## TODO: don't regen text completely
69 Redwood::reporting_thread do
70 BufferManager.say("Loading message bodies...") do |sid|
71 t.each { |m, *o| m.load_from_source! if m }
73 mode = ThreadViewMode.new t, @hidden_labels
74 BufferManager.spawn t.subj, mode
75 BufferManager.draw_screen
76 mode.jump_to_first_open
77 BufferManager.draw_screen # lame TODO: make this unnecessary
78 ## the first draw_screen is needed before topline and botline
79 ## are set, and the second to show the cursor having moved
81 t.remove_label :unread
82 update_text_for_line curpos
83 UpdateManager.relay self, :read, t
87 def multi_select threads
88 threads.each { |t| select t }
91 def handle_starred_update sender, m
92 t = @ts.thread_for(m) or return
93 l = @lines[t] or return
94 update_text_for_line l
95 BufferManager.draw_screen
98 def handle_read_update sender, t
99 l = @lines[t] or return
100 update_text_for_line @lines[t]
101 BufferManager.draw_screen
104 def handle_archived_update *a; handle_read_update(*a); end
107 def is_relevant? m; false; end
109 def handle_add_update sender, m
110 if is_relevant?(m) || @ts.is_relevant?(m)
111 @ts.load_thread_for_message m
113 BufferManager.draw_screen
117 def handle_delete_update sender, mid
118 if @ts.contains_id? mid
121 BufferManager.draw_screen
126 ## let's see you do THIS in python
127 @threads = @ts.threads.select { |t| !@hidden_threads[t] }.sort_by { |t| t.date }.reverse
128 @size_width = (@threads.map { |t| t.size }.max || 0).num_digits
133 return unless(t = @threads[curpos])
134 message, *crap = t.find { |m, *o| m.has_label? :draft }
136 mode = ResumeMode.new message
137 BufferManager.spawn "Edit message", mode
139 BufferManager.flash "Not a draft message!"
143 def actually_toggle_starred t
144 if t.has_label? :starred # if ANY message has a star
145 t.remove_label :starred # remove from all
147 t.first.add_label :starred # add only to first
152 t = @threads[curpos] or return
153 actually_toggle_starred t
154 update_text_for_line curpos
158 def multi_toggle_starred threads
159 threads.each { |t| actually_toggle_starred t }
163 def actually_toggle_archived t
164 if t.has_label? :inbox
165 t.remove_label :inbox
166 UpdateManager.relay self, :archived, t
169 UpdateManager.relay self, :unarchived, t
174 t = @threads[curpos] or return
175 actually_toggle_archived t
176 update_text_for_line curpos
179 def multi_toggle_archived threads
180 threads.each { |t| actually_toggle_archived t }
185 t = @threads[curpos] or return
186 t.toggle_label :unread
187 update_text_for_line curpos
191 def multi_toggle_new threads
192 threads.each { |t| t.toggle_label :unread }
196 def multi_toggle_tagged threads
202 n = ((curpos + 1) ... lines).find { |i| @threads[i].has_label? :unread }
203 n = (0 ... curpos).find { |i| @threads[i].has_label? :unread } unless n
207 BufferManager.flash "No new messages"
212 t = @threads[curpos] or return
213 multi_mark_as_spam [t]
216 def multi_mark_as_spam threads
225 t = @threads[curpos] or return
229 def multi_delete threads
231 t.toggle_label :deleted
238 t = @threads[curpos] or return
242 def multi_kill threads
244 t.apply_label :killed
251 dirty_threads = (@threads + @hidden_threads.keys).select { |t| t.dirty? }
252 return if dirty_threads.empty?
254 BufferManager.say("Saving threads...") do |say_id|
255 dirty_threads.each_with_index do |t, i|
256 BufferManager.say "Saving modified thread #{i + 1} of #{dirty_threads.length}...", say_id
263 UpdateManager.unregister self
267 BufferManager.clear @mbid if @mbid
268 sleep 0.1 # TODO: necessary?
269 BufferManager.erase_flash
276 t = @threads[curpos] or return
277 @tags.toggle_tag_for t
278 update_text_for_line curpos
282 def apply_to_tagged; @tags.apply_to_tagged; end
285 thread = @threads[curpos] or return
286 speciall = (@hidden_labels + LabelManager::RESERVED_LABELS).uniq
287 keepl, modifyl = thread.labels.partition { |t| speciall.member? t }
288 label_string = modifyl.join(" ")
289 label_string += " " unless label_string.empty?
291 answer = BufferManager.ask :edit_labels, "edit labels: ", label_string
293 user_labels = answer.split(/\s+/).map { |l| l.intern }
295 hl = user_labels.select { |l| speciall.member? l }
297 thread.labels = keepl + user_labels
298 user_labels.each { |l| LabelManager << l }
300 BufferManager.flash "'#{hl}' is a reserved label!"
302 update_text_for_line curpos
305 def multi_edit_labels threads
306 answer = BufferManager.ask :add_labels, "add labels: "
308 user_labels = answer.split(/\s+/).map { |l| l.intern }
310 hl = user_labels.select { |l| @hidden_labels.member? l }
312 threads.each { |t| user_labels.each { |l| t.apply_label l } }
313 user_labels.each { |l| LabelManager << l }
315 BufferManager.flash "'#{hl}' is a reserved label!"
321 t = @threads[curpos] or return
323 return if m.nil? # probably won't happen
325 mode = ReplyMode.new m
326 BufferManager.spawn "Reply to #{m.subj}", mode
330 t = @threads[curpos] or return
332 return if m.nil? # probably won't happen
334 mode = ForwardMode.new m
335 BufferManager.spawn "Forward of #{m.subj}", mode
339 def load_n_threads_background n=LOAD_MORE_THREAD_NUM, opts={}
340 return if @load_thread # todo: wrap in mutex
341 @load_thread = Redwood::reporting_thread do
342 num = load_n_threads n, opts
343 opts[:when_done].call(num) if opts[:when_done]
348 def load_n_threads n=LOAD_MORE_THREAD_NUM, opts={}
349 @mbid = BufferManager.say "Searching for threads..."
351 last_update = Time.now - 9999 # oh yeah
352 @ts.load_n_threads(@ts.size + n, opts) do |i|
353 BufferManager.say "Loaded #{i} threads...", @mbid
354 if (Time.now - last_update) >= 0.25
356 BufferManager.draw_screen
357 last_update = Time.now
360 @ts.threads.each { |th| th.labels.each { |l| LabelManager << l } }
363 BufferManager.clear @mbid
365 BufferManager.draw_screen
373 "line #{curpos + 1} of #{l} #{dirty? ? '*modified*' : ''}"
379 def cursor_thread; @threads[curpos]; end
388 raise "already hidden" if @hidden_threads[t]
389 @hidden_threads[t] = true
395 if @hidden_threads[t]
396 @hidden_threads.delete t
403 def update_text_for_line l
404 return unless l # not sure why this happens, but it does, occasionally
405 @text[l] = text_for_thread @threads[l]
406 buffer.mark_dirty if buffer
410 @text = @threads.map_with_index { |t, i| text_for_thread t }
411 @lines = @threads.map_with_index { |t, i| [t, i] }.to_h
412 buffer.mark_dirty if buffer
415 def author_text_for_thread t
416 if t.authors.size == 1
417 t.authors.first.mediumname
419 t.authors.map { |p| AccountManager.is_account?(p) ? "me" : p.shortname }.join ", "
423 def text_for_thread t
424 date = t.date.to_nice_s(Time.now)
425 from = author_text_for_thread(t)
426 if from.length > @from_width
427 from = from[0 ... (@from_width - 1)]
428 from += "." unless from[-1] == ?\s
431 new = t.has_label?(:unread)
432 starred = t.has_label?(:starred)
434 dp = t.direct_participants.any? { |p| AccountManager.is_account? p }
435 p = dp || t.participants.any? { |p| AccountManager.is_account? p }
447 [:tagged_color, @tags.tagged?(t) ? ">" : " "],
448 [:none, sprintf("%#{@date_width}s", date)],
449 (starred ? [:starred_color, "*"] : [:none, " "]),
450 [base_color, sprintf("%-#{@from_width}s", from)],
451 [:none, t.size == 1 ? " " * (@size_width + 2) : sprintf("(%#{@size_width}d)", t.size)],
452 [:to_me_color, dp ? " >" : (p ? ' +' : " ")],
453 [base_color, t.subj + (t.subj.empty? ? "" : " ")],
455 (t.labels - @hidden_labels).map { |label| [:label_color, "+#{label} "] } +
456 [[:snippet_color, t.snippet]
460 def dirty?; (@hidden_threads.keys + @threads).any? { |t| t.dirty? }; end
464 def initialize_threads
465 @ts = ThreadSet.new Index.instance, $config[:thread_by_subject]
466 @ts_mutex = Mutex.new