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 :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
38 @tags = Tagger.new self
43 UpdateManager.register self
46 def lines; @text.length; end
47 def [] i; @text[i]; end
51 BufferManager.draw_screen
52 load_threads :num => buffer.content_height
55 ## open up a thread view window
57 t ||= @threads[curpos]
59 ## TODO: don't regen text completely
60 Redwood::reporting_thread do
61 mode = ThreadViewMode.new t, @hidden_labels
62 BufferManager.spawn t.subj, mode
63 BufferManager.draw_screen
67 def multi_select threads
68 threads.each { |t| select t }
71 def handle_starred_update m
72 return unless(t = @ts.thread_for m)
73 update_text_for_line @lines[t]
76 def handle_read_update m
77 return unless(t = @ts.thread_for m)
78 update_text_for_line @lines[t]
82 def is_relevant? m; false; end
84 def handle_add_update m
85 if is_relevant?(m) || @ts.is_relevant?(m)
86 @ts.load_thread_for_message m
91 def handle_delete_update mid
92 if @ts.contains_id? mid
99 ## let's see you do THIS in python
100 @threads = @ts.threads.select { |t| !@hidden_threads[t] }.sort_by { |t| t.date }.reverse
101 @size_width = (@threads.map { |t| t.size }.max || 0).num_digits
106 return unless(t = @threads[curpos])
107 message, *crap = t.find { |m, *o| m.has_label? :draft }
109 mode = ResumeMode.new message
110 BufferManager.spawn "Edit message", mode
112 BufferManager.flash "Not a draft message!"
116 ## i always hate people who name things like this...
117 def actually_toggle_starred t=@threads[curpos]
118 if t.has_label? :starred # if ANY message has a star
119 t.remove_label :starred # remove from all
121 t.first.add_label :starred # add only to first
126 actually_toggle_starred
127 update_text_for_line curpos
131 def multi_toggle_starred threads
132 threads.each { |t| actually_toggle_starred t }
137 t = @threads[curpos] or return
138 t.toggle_label :inbox
139 update_text_for_line curpos
143 def multi_toggle_archived threads
144 threads.each { |t| t.toggle_label :inbox }
149 t = @threads[curpos] or return
150 t.toggle_label :unread
151 update_text_for_line curpos
155 def multi_toggle_new threads
156 threads.each { |t| t.toggle_label :unread }
160 def multi_toggle_tagged threads
166 t = @threads[curpos] or return
167 n = ((curpos + 1) ... lines).find { |i| @threads[i].has_label? :unread }
168 n = (0 ... curpos).find { |i| @threads[i].has_label? :unread } unless n
172 BufferManager.flash "No new messages"
177 t = @threads[curpos] or return
178 multi_mark_as_spam [t]
181 def multi_mark_as_spam threads
190 t = @threads[curpos] or return
194 def multi_kill threads
196 t.apply_label :killed
203 threads = @threads + @hidden_threads.keys
205 BufferManager.say("Saving threads...") do |say_id|
206 threads.each_with_index do |t, i|
208 BufferManager.say "Saving thread #{i +1} of #{threads.length}...", say_id
215 UpdateManager.unregister self
219 BufferManager.clear @mbid if @mbid
220 sleep 0.1 # TODO: necessary?
221 BufferManager.erase_flash
228 t = @threads[curpos] or return
229 @tags.toggle_tag_for t
230 update_text_for_line curpos
234 def apply_to_tagged; @tags.apply_to_tagged; end
237 thread = @threads[curpos]
238 speciall = (@hidden_labels + LabelManager::RESERVED_LABELS).uniq
239 keepl, modifyl = thread.labels.partition { |t| speciall.member? t }
240 label_string = modifyl.join(" ")
242 answer = BufferManager.ask :edit_labels, "edit labels: ", label_string
244 user_labels = answer.split(/\s+/).map { |l| l.intern }
246 hl = user_labels.select { |l| speciall.member? l }
248 thread.labels = keepl + user_labels
249 user_labels.each { |l| LabelManager << l }
251 BufferManager.flash "'#{hl}' is a reserved label!"
253 update_text_for_line curpos
256 def multi_edit_labels threads
257 answer = BufferManager.ask :add_labels, "add labels: "
259 user_labels = answer.split(/\s+/).map { |l| l.intern }
261 hl = user_labels.select { |l| @hidden_labels.member? l }
263 threads.each { |t| user_labels.each { |l| t.apply_label l } }
264 user_labels.each { |l| LabelManager << l }
266 BufferManager.flash "'#{hl}' is a reserved label!"
272 t = @threads[curpos] or return
274 return if m.nil? # probably won't happen
275 mode = ReplyMode.new m
276 BufferManager.spawn "Reply to #{m.subj}", mode
280 t = @threads[curpos] or return
282 return if m.nil? # probably won't happen
283 mode = ForwardMode.new m
284 BufferManager.spawn "Forward of #{m.subj}", mode
288 def load_n_threads_background n=LOAD_MORE_THREAD_NUM, opts={}
289 return if @load_thread
290 @load_thread = Redwood::reporting_thread do
291 num = load_n_threads n, opts
292 opts[:when_done].call(num) if opts[:when_done]
297 def load_n_threads n=LOAD_MORE_THREAD_NUM, opts={}
298 @mbid = BufferManager.say "Searching for threads..."
300 @ts.load_n_threads(@ts.size + n, opts) do |i|
301 BufferManager.say "Loaded #{i} threads...", @mbid
304 BufferManager.draw_screen
307 @ts.threads.each { |th| th.labels.each { |l| LabelManager << l } }
310 BufferManager.clear @mbid
312 BufferManager.draw_screen
320 "line #{curpos + 1} of #{l} #{dirty? ? '*modified*' : ''}"
326 def cursor_thread; @threads[curpos]; end
334 def remove_label_and_hide_thread t, label
340 raise "already hidden" if @hidden_threads[t]
341 @hidden_threads[t] = true
346 def update_text_for_line l
347 return unless l # not sure why this happens, but it does, occasionally
348 @text[l] = text_for_thread @threads[l]
349 buffer.mark_dirty if buffer
353 @text = @threads.map_with_index { |t, i| text_for_thread t }
354 @lines = @threads.map_with_index { |t, i| [t, i] }.to_h
355 buffer.mark_dirty if buffer
358 def author_text_for_thread t
359 if t.authors.size == 1
360 t.authors.first.mediumname
362 t.authors.map { |p| AccountManager.is_account?(p) ? "me" : p.shortname }.join ", "
366 def text_for_thread t
367 date = t.date.to_nice_s(Time.now)
368 from = author_text_for_thread(t)
369 if from.length > @from_width
370 from = from[0 ... (@from_width - 1)]
371 from += "." unless from[-1] == ?\s
374 new = t.has_label?(:unread)
375 starred = t.has_label?(:starred)
377 dp = t.direct_participants.any? { |p| AccountManager.is_account? p }
378 p = dp || t.participants.any? { |p| AccountManager.is_account? p }
380 base_color = (new ? :index_new_color : :index_old_color)
382 [:tagged_color, @tags.tagged?(t) ? ">" : " "],
383 [:none, sprintf("%#{@date_width}s ", date)],
384 [base_color, sprintf("%-#{@from_width}s", from)],
385 [:starred_color, starred ? "*" : " "],
386 [:none, t.size == 1 ? " " * (@size_width + 2) : sprintf("(%#{@size_width}d)", t.size)],
387 [:to_me_color, dp ? " >" : (p ? ' +' : " ")],
388 [base_color, t.subj + (t.subj.empty? ? "" : " ")],
390 (t.labels - @hidden_labels).map { |label| [:label_color, "+#{label} "] } +
391 [[:snippet_color, t.snippet]
395 def dirty?; (@hidden_threads.keys + @threads).any? { |t| t.dirty? }; end
399 def initialize_threads
400 @ts = ThreadSet.new Index.instance
401 @ts_mutex = Mutex.new