]> git.cworth.org Git - sup/blob - lib/sup/modes/contact-list-mode.rb
moved evertying to devel
[sup] / lib / sup / modes / contact-list-mode.rb
1 module Redwood
2
3 class ContactListMode < LineCursorMode
4   LOAD_MORE_CONTACTS_NUM = 10
5
6   register_keymap do |k|
7     k.add :load_more, "Load #{LOAD_MORE_CONTACTS_NUM} more contacts", 'M'
8     k.add :reload, "Reload contacts", 'R'
9     k.add :alias, "Edit alias for contact", 'a'
10     k.add :toggle_tagged, "Tag/untag current line", 't'
11     k.add :apply_to_tagged, "Apply next command to all tagged items", ';'
12     k.add :search, "Search for messages from particular people", 'S'
13   end
14
15   def initialize mode = :regular
16     @mode = mode
17     @tags = Tagger.new self
18     reload
19     super()
20   end
21
22   def lines; @text.length; end
23   def [] i; @text[i]; end
24
25   def toggle_tagged
26     p = @contacts[curpos] or return
27     @tags.toggle_tag_for p
28     update_text_for_line curpos
29     cursor_down
30   end
31
32   def multi_toggle_tagged threads
33     @tags.drop_all_tags
34     regen_text
35   end
36
37   def apply_to_tagged; @tags.apply_to_tagged; end
38
39   def load; regen_text; end
40   def load_more
41     @num += LOAD_MORE_CONTACTS_NUM
42     regen_text
43     BufferManager.flash "Loaded #{LOAD_MORE_CONTACTS_NUM} more contacts."
44   end
45
46   def multi_select people
47     case @mode
48     when :regular
49       mode = ComposeMode.new :to => people
50       BufferManager.spawn "new message", mode
51       mode.edit
52     end
53   end
54
55   def select
56     p = @contacts[curpos] or return
57     multi_select [p]
58   end
59
60   def multi_search people
61     mode = PersonSearchResultsMode.new people
62     BufferManager.spawn "personal search results", mode
63     mode.load_more_threads mode.buffer.content_height
64   end
65
66   def search
67     p = @contacts[curpos] or return
68     multi_search [p]
69   end    
70
71   def reload
72     @tags.drop_all_tags
73     @num = LOAD_MORE_CONTACTS_NUM
74     load
75   end
76
77   def alias
78     p = @contacts[curpos] or return
79     a = BufferManager.ask(:alias, "alias for #{p.longname}: ", @user_contacts[p]) or return
80     if a.empty?
81       ContactManager.drop_contact p
82     else
83       ContactManager.set_contact p, a
84       @user_contacts[p] = a
85       update_text_for_line curpos
86     end
87   end
88
89 protected
90
91   def update_text_for_line line
92     @text[line] = text_for_contact @contacts[line]
93     buffer.mark_dirty
94   end
95
96   def text_for_contact p
97     aalias = @user_contacts[p] || ""
98     [[:tagged_color, @tags.tagged?(p) ? ">" : " "],
99      [:none, sprintf("%-#{@awidth}s %-#{@nwidth}s %s", aalias, p.name, p.email)]]
100   end
101
102   def regen_text
103     @user_contacts = ContactManager.contacts.invert
104     recent = Index.load_contacts AccountManager.user_emails,
105                                  :num => @num
106     
107     @contacts = (@user_contacts.keys + recent.select { |p| !@user_contacts[p] }).sort_by { |p| p.sort_by_me + (p.name || "") + p.email }.remove_successive_dupes
108
109     @awidth, @nwidth = 0, 0
110     @contacts.each do |p|
111       aalias = @user_contacts[p]
112       @awidth = aalias.length if aalias && aalias.length > @awidth
113       @nwidth = p.name.length if p.name && p.name.length > @nwidth
114     end
115
116     @text = @contacts.map { |p| text_for_contact p }
117     buffer.mark_dirty if buffer
118   end
119 end
120
121 end