]> git.cworth.org Git - sup/blob - lib/sup/modes/thread-index-mode.rb
fc86be7bbc388d41ff9cf263dea0f8fa15e50449
[sup] / lib / sup / modes / thread-index-mode.rb
1 module Redwood
2
3 ## subclasses should implement:
4 ## - is_relevant?
5
6 class ThreadIndexMode < LineCursorMode
7   DATE_WIDTH = Time::TO_NICE_S_MAX_LEN
8   FROM_WIDTH = 15
9   LOAD_MORE_THREAD_NUM = 20
10
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", ';'
28   end
29
30   def initialize hidden_labels=[], load_thread_opts={}
31     super()
32     @mutex = Mutex.new
33     @load_thread = nil
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
38     @size_width = nil
39     
40     @tags = Tagger.new self
41     
42     initialize_threads
43     update
44
45     UpdateManager.register self
46
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 }
53     end
54   end
55
56   def lines; @text.length; end
57   def [] i; @text[i]; end
58   def contains_thread? t; !@lines[t].nil?; end
59
60   def reload
61     drop_all_threads
62     BufferManager.draw_screen
63     load_threads :num => buffer.content_height
64   end
65
66   ## open up a thread view window
67   def select t=nil
68     t ||= @threads[curpos] or return
69
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 }
74       end
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
82
83       t.remove_label :unread
84       update_text_for_line curpos
85       UpdateManager.relay self, :read, t
86     end
87   end
88
89   def multi_select threads
90     threads.each { |t| select t }
91   end
92   
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
98   end
99
100   def handle_read_update sender, t
101     l = @lines[t] or return
102     update_text_for_line @lines[t]
103     BufferManager.draw_screen
104   end
105
106   def handle_archived_update *a; handle_read_update(*a); end
107
108   def handle_deleted_update sender, t
109     handle_read_update sender, t
110     hide_thread t
111     regen_text
112   end
113
114   ## overwrite me!
115   def is_relevant? m; false; end
116
117   def handle_add_update sender, m
118     if is_relevant?(m) || @ts.is_relevant?(m)
119       @ts.load_thread_for_message m
120       update
121       BufferManager.draw_screen
122     end
123   end
124
125   def handle_delete_update sender, mid
126     if @ts.contains_id? mid
127       @ts.remove mid
128       update
129       BufferManager.draw_screen
130     end
131   end
132
133   def update
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
137     regen_text
138   end
139
140   def edit_message
141     return unless(t = @threads[curpos])
142     message, *crap = t.find { |m, *o| m.has_label? :draft }
143     if message
144       mode = ResumeMode.new message
145       BufferManager.spawn "Edit message", mode
146     else
147       BufferManager.flash "Not a draft message!"
148     end
149   end
150
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
154     else
155       t.first.add_label :starred # add only to first
156     end
157   end  
158
159   def toggle_starred 
160     t = @threads[curpos] or return
161     actually_toggle_starred t
162     update_text_for_line curpos
163     cursor_down
164   end
165
166   def multi_toggle_starred threads
167     threads.each { |t| actually_toggle_starred t }
168     regen_text
169   end
170
171   def actually_toggle_archived t
172     if t.has_label? :inbox
173       t.remove_label :inbox
174       UpdateManager.relay self, :archived, t
175     else
176       t.apply_label :inbox
177       UpdateManager.relay self, :unarchived, t
178     end
179   end
180
181   def toggle_archived 
182     t = @threads[curpos] or return
183     actually_toggle_archived t
184     update_text_for_line curpos
185   end
186
187   def multi_toggle_archived threads
188     threads.each { |t| actually_toggle_archived t }
189     regen_text
190   end
191
192   def toggle_new
193     t = @threads[curpos] or return
194     t.toggle_label :unread
195     update_text_for_line curpos
196     cursor_down
197   end
198
199   def multi_toggle_new threads
200     threads.each { |t| t.toggle_label :unread }
201     regen_text
202   end
203
204   def multi_toggle_tagged threads
205     @tags.drop_all_tags
206     regen_text
207   end
208
209   def jump_to_next_new
210     n = ((curpos + 1) ... lines).find { |i| @threads[i].has_label? :unread } || (0 ... curpos).find { |i| @threads[i].has_label? :unread }
211     if n
212       ## jump there if necessary
213       jump_to_line n unless n >= topline && n < botline
214       set_cursor_pos n
215     else
216       BufferManager.flash "No new messages"
217     end
218   end
219
220   def mark_as_spam
221     t = @threads[curpos] or return
222     multi_mark_as_spam [t]
223   end
224
225   def multi_mark_as_spam threads
226     threads.each do |t|
227       t.toggle_label :spam
228       hide_thread t
229     end
230     regen_text
231   end
232
233   def delete
234     t = @threads[curpos] or return
235     multi_delete [t]
236   end
237
238   def multi_delete threads
239     threads.each do |t|
240       t.toggle_label :deleted
241       hide_thread t
242     end
243     regen_text
244   end
245
246   def kill
247     t = @threads[curpos] or return
248     multi_kill [t]
249   end
250
251   def multi_kill threads
252     threads.each do |t|
253       t.apply_label :killed
254       hide_thread t
255     end
256     regen_text
257   end
258
259   def save
260     dirty_threads = (@threads + @hidden_threads.keys).select { |t| t.dirty? }
261     return if dirty_threads.empty?
262
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
266         t.save Index
267       end
268     end
269   end
270
271   def cleanup
272     UpdateManager.unregister self
273
274     if @load_thread
275       @load_thread.kill 
276       BufferManager.clear @mbid if @mbid
277       sleep 0.1 # TODO: necessary?
278       BufferManager.erase_flash
279     end
280     save
281     super
282   end
283
284   def toggle_tagged
285     t = @threads[curpos] or return
286     @tags.toggle_tag_for t
287     update_text_for_line curpos
288     cursor_down
289   end
290
291   def apply_to_tagged; @tags.apply_to_tagged; end
292
293   def edit_labels
294     thread = @threads[curpos] or return
295     speciall = (@hidden_labels + LabelManager::RESERVED_LABELS).uniq
296     keepl, modifyl = thread.labels.partition { |t| speciall.member? t }
297
298     user_labels = BufferManager.ask_for_labels :label, "Labels for thread: ", modifyl, @hidden_labels
299
300     return unless user_labels
301     thread.labels = keepl + user_labels
302     user_labels.each { |l| LabelManager << l }
303     update_text_for_line curpos
304   end
305
306   def multi_edit_labels threads
307     answer = BufferManager.ask :add_labels, "add labels: "
308     return unless answer
309     user_labels = answer.split(/\s+/).map { |l| l.intern }
310     
311     hl = user_labels.select { |l| @hidden_labels.member? l }
312     if hl.empty?
313       threads.each { |t| user_labels.each { |l| t.apply_label l } }
314       user_labels.each { |l| LabelManager << l }
315     else
316       BufferManager.flash "'#{hl}' is a reserved label!"
317     end
318     regen_text
319   end
320
321   def reply
322     t = @threads[curpos] or return
323     m = t.latest_message
324     return if m.nil? # probably won't happen
325     m.load_from_source!
326     mode = ReplyMode.new m
327     BufferManager.spawn "Reply to #{m.subj}", mode
328   end
329
330   def forward
331     t = @threads[curpos] or return
332     m = t.latest_message
333     return if m.nil? # probably won't happen
334     m.load_from_source!
335     mode = ForwardMode.new m
336     BufferManager.spawn "Forward of #{m.subj}", mode
337     mode.edit_message
338   end
339
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]
345       @load_thread = nil
346     end
347   end
348
349   def load_n_threads n=LOAD_MORE_THREAD_NUM, opts={}
350     @mbid = BufferManager.say "Searching for threads..."
351     orig_size = @ts.size
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
356         update
357         BufferManager.draw_screen
358         last_update = Time.now
359       end
360     end
361     @ts.threads.each { |th| th.labels.each { |l| LabelManager << l } }
362
363     update
364     BufferManager.clear @mbid
365     @mbid = nil
366     BufferManager.draw_screen
367     @ts.size - orig_size
368   end
369   synchronized :load_n_threads
370
371   def status
372     if (l = lines) == 0
373       "line 0 of 0"
374     else
375       "line #{curpos + 1} of #{l} #{dirty? ? '*modified*' : ''}"
376     end
377   end
378
379   def load_threads opts={}
380     n = opts[:num] || ThreadIndexMode::LOAD_MORE_THREAD_NUM
381
382     myopts = @load_thread_opts.merge({ :when_done => (lambda do |num|
383       opts[:when_done].call(num) if opts[:when_done]
384       if num > 0
385         BufferManager.flash "Found #{num} threads"
386       else
387         BufferManager.flash "No matches"
388       end
389     end)})
390
391     if opts[:background] || opts[:background].nil?
392       load_n_threads_background n, myopts
393     else
394       load_n_threads n, myopts
395     end
396   end
397
398 protected
399
400   def cursor_thread; @threads[curpos]; end
401
402   def drop_all_threads
403     @tags.drop_all_tags
404     initialize_threads
405     update
406   end
407
408   def hide_thread t
409     raise "already hidden" if @hidden_threads[t]
410     @hidden_threads[t] = true
411     @threads.delete t
412     @tags.drop_tag_for t
413   end
414
415   def show_thread t
416     if @hidden_threads[t]
417       @hidden_threads.delete t
418     else
419       @ts.add_thread t
420     end
421     update
422   end
423
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
428   end
429
430   def regen_text
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
434   end
435   
436   def author_text_for_thread t
437     t.authors.map do |p|
438       if AccountManager.is_account?(p)
439         "me"
440       elsif t.authors.size == 1
441         p.mediumname
442       else
443         p.shortname
444       end
445     end.uniq.join ","
446   end
447
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
454     end
455
456     new = t.has_label?(:unread)
457     starred = t.has_label?(:starred)
458
459     dp = t.direct_participants.any? { |p| AccountManager.is_account? p }
460     p = dp || t.participants.any? { |p| AccountManager.is_account? p }
461
462     base_color =
463       if new
464         :index_new_color
465       elsif starred
466         :index_starred_color
467       else 
468         :index_old_color
469       end
470
471     [ 
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? ? "" : " ")],
479     ] +
480       (t.labels - @hidden_labels).map { |label| [:label_color, "+#{label} "] } +
481       [[:snippet_color, t.snippet]
482     ]
483   end
484
485   def dirty?; (@hidden_threads.keys + @threads).any? { |t| t.dirty? }; end
486
487 private
488
489   def initialize_threads
490     @ts = ThreadSet.new Index.instance, $config[:thread_by_subject]
491     @ts_mutex = Mutex.new
492     @hidden_threads = {}
493   end
494 end
495
496 end