]> git.cworth.org Git - sup/blob - lib/sup/modes/inbox-mode.rb
Merge branch 'dont-canonicalize-email-addresses' into next
[sup] / lib / sup / modes / inbox-mode.rb
1 require 'thread'
2
3 module Redwood
4
5 class InboxMode < ThreadIndexMode
6   register_keymap do |k|
7     ## overwrite toggle_archived with archive
8     k.add :archive, "Archive thread (remove from inbox)", 'a'
9     k.add :read_and_archive, "Archive thread (remove from inbox) and mark read", 'A'
10   end
11
12   def initialize
13     super [:inbox, :sent, :draft], { :label => :inbox, :skip_killed => true }
14     raise "can't have more than one!" if defined? @@instance
15     @@instance = self
16   end
17
18   def is_relevant? m
19     m.has_label?(:inbox) && ([:spam, :deleted, :killed] & m.labels).empty?
20   end
21
22   ## label-list-mode wants to be able to raise us if the user selects
23   ## the "inbox" label, so we need to keep our singletonness around
24   def self.instance; @@instance; end
25   def killable?; false; end
26
27   def archive
28     return unless cursor_thread
29     thread = cursor_thread # to make sure lambda only knows about 'old' cursor_thread
30
31     undo = lambda {
32       thread.apply_label :inbox
33       add_or_unhide thread.first
34     }
35     UndoManager.register("archiving thread #{thread.first.id}", undo)
36
37     cursor_thread.remove_label :inbox
38     hide_thread cursor_thread
39     regen_text
40   end
41
42   def multi_archive threads
43     undo = threads.map {|t|
44              lambda{
45                t.apply_label :inbox
46                add_or_unhide t.first
47              }}
48     UndoManager.register("archiving #{threads.size} #{threads.size.pluralize 'thread'}",
49                          undo << lambda {regen_text} )
50
51     threads.each do |t|
52       t.remove_label :inbox
53       hide_thread t
54     end
55     regen_text
56   end
57
58   def read_and_archive
59     return unless cursor_thread
60     thread = cursor_thread # to make sure lambda only knows about 'old' cursor_thread
61
62     undo = lambda {
63       thread.apply_label :inbox
64       thread.apply_label :unread
65       add_or_unhide thread.first
66     }
67     UndoManager.register("reading and archiving thread ", undo)
68
69     cursor_thread.remove_label :unread
70     cursor_thread.remove_label :inbox
71     hide_thread cursor_thread
72     regen_text
73   end
74
75   def multi_read_and_archive threads
76     undo = threads.map {|t|
77       lambda {
78         t.apply_label :inbox
79         t.apply_label :unread
80         add_or_unhide t.first
81       }
82     }
83     UndoManager.register("reading and archiving #{threads.size} #{threads.size.pluralize 'thread'}",
84                          undo << lambda {regen_text})
85
86     threads.each do |t|
87       t.remove_label :unread
88       t.remove_label :inbox
89       hide_thread t
90     end
91     regen_text
92   end
93
94   def handle_unarchived_update sender, m
95     add_or_unhide m
96   end
97
98   def handle_archived_update sender, m
99     t = thread_containing(m) or return
100     hide_thread t
101     regen_text
102   end
103
104   def status
105     super + "    #{Index.size} messages in index"
106   end
107 end
108
109 end