4 ## subclasses should implement load_threads
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 required_labels=[], hidden_labels=[]
33 @required_labels = required_labels
34 @hidden_labels = hidden_labels + LabelManager::HIDDEN_LABELS
35 @date_width = DATE_WIDTH
36 @from_width = FROM_WIDTH
39 @tags = Tagger.new self
44 UpdateManager.register self
47 def lines; @text.length; end
48 def [] i; @text[i]; end
49 def contains_thread? t; !@lines[t].nil?; end
53 BufferManager.draw_screen
54 load_threads :num => buffer.content_height
57 ## open up a thread view window
59 t ||= @threads[curpos]
61 ## this isn't working entirely. TODO:figure out why
62 # t = t.clone # required so that messages added later on don't completely
65 ## TODO: don't regen text completely
66 Redwood::reporting_thread do
67 BufferManager.say("Loading message bodies...") do |sid|
68 t.each { |m, *o| m.load_from_source! if m }
70 mode = ThreadViewMode.new t, @hidden_labels
71 BufferManager.spawn t.subj, mode
72 BufferManager.draw_screen
73 mode.jump_to_first_open
74 BufferManager.draw_screen # lame TODO: make this unnecessary
75 ## the first draw_screen is needed before topline and botline
76 ## are set, and the second to show the cursor having moved
80 def multi_select threads
81 threads.each { |t| select t }
84 def handle_starred_update sender, m
85 return unless(t = @ts.thread_for m)
86 update_text_for_line @lines[t]
87 BufferManager.draw_screen
90 def handle_read_update sender, t
91 return unless @lines[t]
92 update_text_for_line @lines[t]
93 BufferManager.draw_screen
96 def handle_archived_update *a; handle_read_update *a; end
99 def is_relevant? m; false; end
101 def handle_add_update sender, m
102 if is_relevant?(m) || @ts.is_relevant?(m)
103 @ts.load_thread_for_message m
105 BufferManager.draw_screen
109 def handle_delete_update sender, mid
110 if @ts.contains_id? mid
113 BufferManager.draw_screen
118 ## let's see you do THIS in python
119 @threads = @ts.threads.select { |t| !@hidden_threads[t] && !t.has_label?(:killed) }.sort_by { |t| t.date }.reverse
120 @size_width = (@threads.map { |t| t.size }.max || 0).num_digits
125 return unless(t = @threads[curpos])
126 message, *crap = t.find { |m, *o| m.has_label? :draft }
128 mode = ResumeMode.new message
129 BufferManager.spawn "Edit message", mode
131 BufferManager.flash "Not a draft message!"
135 def actually_toggle_starred t
136 if t.has_label? :starred # if ANY message has a star
137 t.remove_label :starred # remove from all
139 t.first.add_label :starred # add only to first
144 t = @threads[curpos] or return
145 actually_toggle_starred t
146 update_text_for_line curpos
150 def multi_toggle_starred threads
151 threads.each { |t| actually_toggle_starred t }
155 def actually_toggle_archived t
156 if t.has_label? :inbox
157 t.remove_label :inbox
158 UpdateManager.relay self, :unarchived, t
161 UpdateManager.relay self, :archived, t
166 t = @threads[curpos] or return
167 actually_toggle_archived t
168 update_text_for_line curpos
171 def multi_toggle_archived threads
172 threads.each { |t| actually_toggle_archived t }
177 t = @threads[curpos] or return
178 t.toggle_label :unread
179 update_text_for_line curpos
183 def multi_toggle_new threads
184 threads.each { |t| t.toggle_label :unread }
188 def multi_toggle_tagged threads
194 n = ((curpos + 1) ... lines).find { |i| @threads[i].has_label? :unread }
195 n = (0 ... curpos).find { |i| @threads[i].has_label? :unread } unless n
199 BufferManager.flash "No new messages"
204 t = @threads[curpos] or return
205 multi_mark_as_spam [t]
208 def multi_mark_as_spam threads
217 t = @threads[curpos] or return
221 def multi_delete threads
223 t.toggle_label :deleted
230 t = @threads[curpos] or return
234 def multi_kill threads
236 t.apply_label :killed
243 threads = @threads + @hidden_threads.keys
245 BufferManager.say("Saving threads...") do |say_id|
246 threads.each_with_index do |t, i|
248 BufferManager.say "Saving thread #{i +1} of #{threads.length}...", say_id
255 UpdateManager.unregister self
259 BufferManager.clear @mbid if @mbid
260 sleep 0.1 # TODO: necessary?
261 BufferManager.erase_flash
268 t = @threads[curpos] or return
269 @tags.toggle_tag_for t
270 update_text_for_line curpos
274 def apply_to_tagged; @tags.apply_to_tagged; end
277 thread = @threads[curpos]
278 speciall = (@hidden_labels + LabelManager::RESERVED_LABELS).uniq
279 keepl, modifyl = thread.labels.partition { |t| speciall.member? t }
280 label_string = modifyl.join(" ")
281 label_string += " " unless label_string.empty?
283 answer = BufferManager.ask :edit_labels, "edit labels: ", label_string
285 user_labels = answer.split(/\s+/).map { |l| l.intern }
287 hl = user_labels.select { |l| speciall.member? l }
289 thread.labels = keepl + user_labels
290 user_labels.each { |l| LabelManager << l }
292 BufferManager.flash "'#{hl}' is a reserved label!"
294 update_text_for_line curpos
297 def multi_edit_labels threads
298 answer = BufferManager.ask :add_labels, "add labels: "
300 user_labels = answer.split(/\s+/).map { |l| l.intern }
302 hl = user_labels.select { |l| @hidden_labels.member? l }
304 threads.each { |t| user_labels.each { |l| t.apply_label l } }
305 user_labels.each { |l| LabelManager << l }
307 BufferManager.flash "'#{hl}' is a reserved label!"
313 t = @threads[curpos] or return
315 return if m.nil? # probably won't happen
317 mode = ReplyMode.new m
318 BufferManager.spawn "Reply to #{m.subj}", mode
322 t = @threads[curpos] or return
324 return if m.nil? # probably won't happen
326 mode = ForwardMode.new m
327 BufferManager.spawn "Forward of #{m.subj}", mode
331 def load_n_threads_background n=LOAD_MORE_THREAD_NUM, opts={}
332 return if @load_thread
333 @load_thread = Redwood::reporting_thread do
334 num = load_n_threads n, opts
335 opts[:when_done].call(num) if opts[:when_done]
340 def load_n_threads n=LOAD_MORE_THREAD_NUM, opts={}
341 @mbid = BufferManager.say "Searching for threads..."
343 last_update = Time.now - 9999 # oh yeah
344 @ts.load_n_threads(@ts.size + n, opts) do |i|
345 BufferManager.say "Loaded #{i} threads...", @mbid
346 if (Time.now - last_update) >= 0.25
348 BufferManager.draw_screen
349 last_update = Time.now
352 @ts.threads.each { |th| th.labels.each { |l| LabelManager << l } }
355 BufferManager.clear @mbid
357 BufferManager.draw_screen
365 "line #{curpos + 1} of #{l} #{dirty? ? '*modified*' : ''}"
371 def cursor_thread; @threads[curpos]; end
380 raise "already hidden" if @hidden_threads[t]
381 @hidden_threads[t] = true
386 def update_text_for_line l
387 return unless l # not sure why this happens, but it does, occasionally
388 @text[l] = text_for_thread @threads[l]
389 buffer.mark_dirty if buffer
393 @text = @threads.map_with_index { |t, i| text_for_thread t }
394 @lines = @threads.map_with_index { |t, i| [t, i] }.to_h
395 buffer.mark_dirty if buffer
398 def author_text_for_thread t
399 if t.authors.size == 1
400 t.authors.first.mediumname
402 t.authors.map { |p| AccountManager.is_account?(p) ? "me" : p.shortname }.join ", "
406 def text_for_thread t
407 date = t.date.to_nice_s(Time.now)
408 from = author_text_for_thread(t)
409 if from.length > @from_width
410 from = from[0 ... (@from_width - 1)]
411 from += "." unless from[-1] == ?\s
414 new = t.has_label?(:unread)
415 starred = t.has_label?(:starred)
417 dp = t.direct_participants.any? { |p| AccountManager.is_account? p }
418 p = dp || t.participants.any? { |p| AccountManager.is_account? p }
420 base_color = (new ? :index_new_color : :index_old_color)
422 [:tagged_color, @tags.tagged?(t) ? ">" : " "],
423 [:none, sprintf("%#{@date_width}s ", date)],
424 [base_color, sprintf("%-#{@from_width}s", from)],
425 [:starred_color, starred ? "*" : " "],
426 [:none, t.size == 1 ? " " * (@size_width + 2) : sprintf("(%#{@size_width}d)", t.size)],
427 [:to_me_color, dp ? " >" : (p ? ' +' : " ")],
428 [base_color, t.subj + (t.subj.empty? ? "" : " ")],
430 (t.labels - @hidden_labels).map { |label| [:label_color, "+#{label} "] } +
431 [[:snippet_color, t.snippet]
435 def dirty?; (@hidden_threads.keys + @threads).any? { |t| t.dirty? }; end
439 def initialize_threads
440 @ts = ThreadSet.new Index.instance
441 @ts_mutex = Mutex.new