]> git.cworth.org Git - sup/blob - lib/sup/modes/thread-index-mode.rb
8238534d7c0d65ee467d22447e93ab3391b4e668
[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", '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", ';'
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_starred_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   ## overwrite me!
109   def is_relevant? m; false; end
110
111   def handle_add_update sender, m
112     if is_relevant?(m) || @ts.is_relevant?(m)
113       @ts.load_thread_for_message m
114       update
115       BufferManager.draw_screen
116     end
117   end
118
119   def handle_delete_update sender, mid
120     if @ts.contains_id? mid
121       @ts.remove mid
122       update
123       BufferManager.draw_screen
124     end
125   end
126
127   def update
128     ## let's see you do THIS in python
129     @threads = @ts.threads.select { |t| !@hidden_threads[t] }.sort_by { |t| t.date }.reverse
130     @size_width = (@threads.max_of { |t| t.size } || 0).num_digits
131     regen_text
132   end
133
134   def edit_message
135     return unless(t = @threads[curpos])
136     message, *crap = t.find { |m, *o| m.has_label? :draft }
137     if message
138       mode = ResumeMode.new message
139       BufferManager.spawn "Edit message", mode
140     else
141       BufferManager.flash "Not a draft message!"
142     end
143   end
144
145   def actually_toggle_starred t
146     if t.has_label? :starred # if ANY message has a star
147       t.remove_label :starred # remove from all
148     else
149       t.first.add_label :starred # add only to first
150     end
151   end  
152
153   def toggle_starred 
154     t = @threads[curpos] or return
155     actually_toggle_starred t
156     update_text_for_line curpos
157     cursor_down
158   end
159
160   def multi_toggle_starred threads
161     threads.each { |t| actually_toggle_starred t }
162     regen_text
163   end
164
165   def actually_toggle_archived t
166     if t.has_label? :inbox
167       t.remove_label :inbox
168       UpdateManager.relay self, :archived, t
169     else
170       t.apply_label :inbox
171       UpdateManager.relay self, :unarchived, t
172     end
173   end
174
175   def toggle_archived 
176     t = @threads[curpos] or return
177     actually_toggle_archived t
178     update_text_for_line curpos
179   end
180
181   def multi_toggle_archived threads
182     threads.each { |t| actually_toggle_archived t }
183     regen_text
184   end
185
186   def toggle_new
187     t = @threads[curpos] or return
188     t.toggle_label :unread
189     update_text_for_line curpos
190     cursor_down
191   end
192
193   def multi_toggle_new threads
194     threads.each { |t| t.toggle_label :unread }
195     regen_text
196   end
197
198   def multi_toggle_tagged threads
199     @tags.drop_all_tags
200     regen_text
201   end
202
203   def jump_to_next_new
204     n = ((curpos + 1) ... lines).find { |i| @threads[i].has_label? :unread } || (0 ... curpos).find { |i| @threads[i].has_label? :unread }
205     if n
206       ## jump there if necessary
207       jump_to_line n unless n >= topline && n < botline
208       set_cursor_pos n
209     else
210       BufferManager.flash "No new messages"
211     end
212   end
213
214   def mark_as_spam
215     t = @threads[curpos] or return
216     multi_mark_as_spam [t]
217   end
218
219   def multi_mark_as_spam threads
220     threads.each do |t|
221       t.toggle_label :spam
222       hide_thread t
223     end
224     regen_text
225   end
226
227   def delete
228     t = @threads[curpos] or return
229     multi_delete [t]
230   end
231
232   def multi_delete threads
233     threads.each do |t|
234       t.toggle_label :deleted
235       hide_thread t
236     end
237     regen_text
238   end
239
240   def kill
241     t = @threads[curpos] or return
242     multi_kill [t]
243   end
244
245   def multi_kill threads
246     threads.each do |t|
247       t.apply_label :killed
248       hide_thread t
249     end
250     regen_text
251   end
252
253   def save
254     dirty_threads = (@threads + @hidden_threads.keys).select { |t| t.dirty? }
255     return if dirty_threads.empty?
256
257     BufferManager.say("Saving threads...") do |say_id|
258       dirty_threads.each_with_index do |t, i|
259         BufferManager.say "Saving modified thread #{i + 1} of #{dirty_threads.length}...", say_id
260         t.save Index
261       end
262     end
263   end
264
265   def cleanup
266     UpdateManager.unregister self
267
268     if @load_thread
269       @load_thread.kill 
270       BufferManager.clear @mbid if @mbid
271       sleep 0.1 # TODO: necessary?
272       BufferManager.erase_flash
273     end
274     save
275     super
276   end
277
278   def toggle_tagged
279     t = @threads[curpos] or return
280     @tags.toggle_tag_for t
281     update_text_for_line curpos
282     cursor_down
283   end
284
285   def apply_to_tagged; @tags.apply_to_tagged; end
286
287   def edit_labels
288     thread = @threads[curpos] or return
289     speciall = (@hidden_labels + LabelManager::RESERVED_LABELS).uniq
290     keepl, modifyl = thread.labels.partition { |t| speciall.member? t }
291     cur_label_string = modifyl.join(" ")
292     cur_label_string += " " unless cur_label_string.empty?
293
294     applyable_labels = (LabelManager.applyable_labels - @hidden_labels).map { |l| LabelManager.string_for l }.sort_by { |s| s.downcase }
295
296     answer = BufferManager.ask_many_with_completions :label, "Labels for thread: ", applyable_labels, cur_label_string
297
298     return unless answer
299     user_labels = answer.split(/\s+/).map { |l| l.intern }
300     
301     hl = user_labels.select { |l| speciall.member? l }
302     if hl.empty?
303       thread.labels = keepl + user_labels
304       user_labels.each { |l| LabelManager << l }
305     else
306       BufferManager.flash "'#{hl}' is a reserved label!"
307     end
308     update_text_for_line curpos
309   end
310
311   def multi_edit_labels threads
312     answer = BufferManager.ask :add_labels, "add labels: "
313     return unless answer
314     user_labels = answer.split(/\s+/).map { |l| l.intern }
315     
316     hl = user_labels.select { |l| @hidden_labels.member? l }
317     if hl.empty?
318       threads.each { |t| user_labels.each { |l| t.apply_label l } }
319       user_labels.each { |l| LabelManager << l }
320     else
321       BufferManager.flash "'#{hl}' is a reserved label!"
322     end
323     regen_text
324   end
325
326   def reply
327     t = @threads[curpos] or return
328     m = t.latest_message
329     return if m.nil? # probably won't happen
330     m.load_from_source!
331     mode = ReplyMode.new m
332     BufferManager.spawn "Reply to #{m.subj}", mode
333   end
334
335   def forward
336     t = @threads[curpos] or return
337     m = t.latest_message
338     return if m.nil? # probably won't happen
339     m.load_from_source!
340     mode = ForwardMode.new m
341     BufferManager.spawn "Forward of #{m.subj}", mode
342     mode.edit
343   end
344
345   def load_n_threads_background n=LOAD_MORE_THREAD_NUM, opts={}
346     return if @load_thread # todo: wrap in mutex
347     @load_thread = Redwood::reporting_thread do
348       num = load_n_threads n, opts
349       opts[:when_done].call(num) if opts[:when_done]
350       @load_thread = nil
351     end
352   end
353
354   def load_n_threads n=LOAD_MORE_THREAD_NUM, opts={}
355     @mbid = BufferManager.say "Searching for threads..."
356     orig_size = @ts.size
357     last_update = Time.now - 9999 # oh yeah
358     @ts.load_n_threads(@ts.size + n, opts) do |i|
359       BufferManager.say "Loaded #{i} threads...", @mbid
360       if (Time.now - last_update) >= 0.25
361         update
362         BufferManager.draw_screen
363         last_update = Time.now
364       end
365     end
366     @ts.threads.each { |th| th.labels.each { |l| LabelManager << l } }
367
368     update
369     BufferManager.clear @mbid
370     @mbid = nil
371     BufferManager.draw_screen
372     @ts.size - orig_size
373   end
374   synchronized :load_n_threads
375
376   def status
377     if (l = lines) == 0
378       "line 0 of 0"
379     else
380       "line #{curpos + 1} of #{l} #{dirty? ? '*modified*' : ''}"
381     end
382   end
383
384   def load_threads opts={}
385     n = opts[:num] || ThreadIndexMode::LOAD_MORE_THREAD_NUM
386
387     myopts = @load_thread_opts.merge({ :when_done => (lambda do |num|
388       opts[:when_done].call(num) if opts[:when_done]
389       if num > 0
390         BufferManager.flash "Found #{num} threads"
391       else
392         BufferManager.flash "No matches"
393       end
394     end)})
395
396     if opts[:background] || opts[:background].nil?
397       load_n_threads_background n, myopts
398     else
399       load_n_threads n, myopts
400     end
401   end
402
403 protected
404
405   def cursor_thread; @threads[curpos]; end
406
407   def drop_all_threads
408     @tags.drop_all_tags
409     initialize_threads
410     update
411   end
412
413   def hide_thread t
414     raise "already hidden" if @hidden_threads[t]
415     @hidden_threads[t] = true
416     @threads.delete t
417     @tags.drop_tag_for t
418   end
419
420   def show_thread t
421     if @hidden_threads[t]
422       @hidden_threads.delete t
423     else
424       @ts.add_thread t
425     end
426     update
427   end
428
429   def update_text_for_line l
430     return unless l # not sure why this happens, but it does, occasionally
431     @text[l] = text_for_thread @threads[l]
432     buffer.mark_dirty if buffer
433   end
434
435   def regen_text
436     @text = @threads.map_with_index { |t, i| text_for_thread t }
437     @lines = @threads.map_with_index { |t, i| [t, i] }.to_h
438     buffer.mark_dirty if buffer
439   end
440   
441   def author_text_for_thread t
442     t.authors.map do |p|
443       if AccountManager.is_account?(p)
444         "me"
445       elsif t.authors.size == 1
446         p.mediumname
447       else
448         p.shortname
449       end
450     end.uniq.join ","
451   end
452
453   def text_for_thread t
454     date = t.date.to_nice_s
455     from = author_text_for_thread t
456     if from.length > @from_width
457       from = from[0 ... (@from_width - 1)]
458       from += "." unless from[-1] == ?\s
459     end
460
461     new = t.has_label?(:unread)
462     starred = t.has_label?(:starred)
463
464     dp = t.direct_participants.any? { |p| AccountManager.is_account? p }
465     p = dp || t.participants.any? { |p| AccountManager.is_account? p }
466
467     base_color =
468       if new
469         :index_new_color
470       elsif starred
471         :index_starred_color
472       else 
473         :index_old_color
474       end
475
476     [ 
477       [:tagged_color, @tags.tagged?(t) ? ">" : " "],
478       [:none, sprintf("%#{@date_width}s", date)],
479       (starred ? [:starred_color, "*"] : [:none, " "]),
480       [base_color, sprintf("%-#{@from_width}s", from)],
481       [:none, t.size == 1 ? " " * (@size_width + 2) : sprintf("(%#{@size_width}d)", t.size)],
482       [:to_me_color, dp ? " >" : (p ? ' +' : "  ")],
483       [base_color, t.subj + (t.subj.empty? ? "" : " ")],
484     ] +
485       (t.labels - @hidden_labels).map { |label| [:label_color, "+#{label} "] } +
486       [[:snippet_color, t.snippet]
487     ]
488   end
489
490   def dirty?; (@hidden_threads.keys + @threads).any? { |t| t.dirty? }; end
491
492 private
493
494   def initialize_threads
495     @ts = ThreadSet.new Index.instance, $config[:thread_by_subject]
496     @ts_mutex = Mutex.new
497     @hidden_threads = {}
498   end
499 end
500
501 end