]> git.cworth.org Git - sup/blob - lib/sup/modes/contact-list-mode.rb
bugfix to # of loaded entries
[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     @num = 0
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 num=LOAD_MORE_CONTACTS_NUM
41     @num += num
42     regen_text
43     BufferManager.flash "Added #{num} 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_threads :num => 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     load
74   end
75
76   def alias
77     p = @contacts[curpos] or return
78     a = BufferManager.ask(:alias, "alias for #{p.longname}: ", @user_contacts[p]) or return
79     if a.empty?
80       ContactManager.drop_contact p
81     else
82       ContactManager.set_contact p, a
83       @user_contacts[p] = a
84       update_text_for_line curpos
85     end
86   end
87
88 protected
89
90   def update_text_for_line line
91     @text[line] = text_for_contact @contacts[line]
92     buffer.mark_dirty
93   end
94
95   def text_for_contact p
96     aalias = @user_contacts[p] || ""
97     [[:tagged_color, @tags.tagged?(p) ? ">" : " "],
98      [:none, sprintf("%-#{@awidth}s %-#{@nwidth}s %s", aalias, p.name, p.email)]]
99   end
100
101   def regen_text
102     @user_contacts = ContactManager.contacts.invert
103     recent = Index.load_contacts AccountManager.user_emails, :num => [@num - @user_contacts.length, 0].max
104     
105     @contacts = (@user_contacts.keys + recent.select { |p| !@user_contacts[p] }).sort_by { |p| p.sort_by_me + (p.name || "") + p.email }.remove_successive_dupes
106
107     @awidth, @nwidth = 0, 0
108     @contacts.each do |p|
109       aalias = @user_contacts[p]
110       @awidth = aalias.length if aalias && aalias.length > @awidth
111       @nwidth = p.name.length if p.name && p.name.length > @nwidth
112     end
113
114     @text = @contacts.map { |p| text_for_contact p }
115     buffer.mark_dirty if buffer
116   end
117 end
118
119 end