comparison frontends/src/primitivus/contact_list.py @ 223:86d249b6d9b7

Files reorganisation
author Goffi <goffi@goffi.org>
date Wed, 29 Dec 2010 01:06:29 +0100
parents frontends/primitivus/contact_list.py@3198bfd66daa
children fd9b7834d98a
comparison
equal deleted inserted replaced
222:3198bfd66daa 223:86d249b6d9b7
1 #!/usr/bin/python
2 # -*- coding: utf-8 -*-
3
4 """
5 Primitivus: a SAT frontend
6 Copyright (C) 2009, 2010 Jérôme Poisson (goffi@goffi.org)
7
8 This program is free software: you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation, either version 3 of the License, or
11 (at your option) any later version.
12
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with this program. If not, see <http://www.gnu.org/licenses/>.
20 """
21
22 import urwid
23 from quick_frontend.quick_contact_list import QuickContactList
24 from tools.jid import JID
25 from urwid_satext import sat_widgets
26
27
28 class ContactList(urwid.WidgetWrap, QuickContactList):
29 signals = ['click','change']
30
31 def __init__(self, host, CM, on_click=None, on_change=None, user_data=None):
32 self.host = host
33 self.selected = None
34 self.groups={}
35 self.alert_jid=set()
36
37 #we now build the widget
38 self.frame = urwid.Frame(self.__buildList())
39 self.main_widget = sat_widgets.LabelLine(self.frame, sat_widgets.SurroundedText(_("Contacts")))
40 urwid.WidgetWrap.__init__(self, self.main_widget)
41 if on_click:
42 urwid.connect_signal(self, 'click', on_click, user_data)
43 if on_change:
44 urwid.connect_signal(self, 'change', on_change, user_data)
45 QuickContactList.__init__(self, CM)
46
47 def __contains__(self, jid):
48 for group in self.groups:
49 if jid.short in self.groups[group][1]:
50 return True
51 return False
52
53 def setFocus(self, name):
54 """give focus to the first group or contact with the given name"""
55 idx = 0
56 for widget in self.frame.body.body:
57 if widget.getValue() == name:
58 self.frame.body.set_focus(idx)
59 return
60 idx+=1
61
62 def putAlert(self, jid):
63 """Put an alert on the jid to get attention from user (e.g. for new message)"""
64 self.alert_jid.add(jid.short)
65 self.frame.body = self.__buildList()
66 self.host.redraw()
67
68 def __groupClicked(self, group_wid):
69 group = self.groups[group_wid.getValue()]
70 group[0] = not group[0]
71 self.frame.body = self.__buildList()
72 self.host.redraw()
73 self.setFocus(group_wid.getValue())
74
75 def __contactClicked(self, contact_wid, selected):
76 self.selected = contact_wid.data
77 for widget in self.frame.body.body:
78 if widget.__class__ == sat_widgets.SelectableText:
79 widget.setState(widget.data == self.selected, invisible=True)
80 if self.selected in self.alert_jid:
81 self.alert_jid.remove(self.selected)
82 self.frame.body = self.__buildList()
83 self.host.redraw()
84 self._emit('click')
85
86 def __buildContact(self, content, param_contacts):
87 """Add contact representation in widget list
88 @param content: widget list, e.g. SimpleListWalker
89 @param contacts: list of JID"""
90 contacts = list(param_contacts)
91 contacts.sort()
92 for contact in contacts:
93 jid=JID(contact)
94 name = self.CM.getAttr(jid,'name')
95 nick = self.CM.getAttr(jid,'nick')
96 display = nick or name or jid.node or jid.short
97 header = '(*) ' if contact in self.alert_jid else ''
98 widget = sat_widgets.SelectableText(('alert' if contact in self.alert_jid else 'default',display),
99 selected = contact==self.selected, header=header)
100 widget.data = contact
101 content.append(widget)
102 urwid.connect_signal(widget, 'change', self.__contactClicked)
103
104 def __buildList(self):
105 """Build the main contact list widget"""
106 content = urwid.SimpleListWalker([])
107 group_keys = self.groups.keys()
108 group_keys.sort()
109 for key in group_keys:
110 unfolded = self.groups[key][0]
111 if key!=None:
112 header = '[-]' if unfolded else '[+]'
113 widget = sat_widgets.ClickableText(key,header=header+' ')
114 content.append(widget)
115 urwid.connect_signal(widget, 'click', self.__groupClicked)
116 if unfolded:
117 self.__buildContact(content, self.groups[key][1])
118 return urwid.ListBox(content)
119
120 def unselectAll(self):
121 """Unselect all contacts"""
122 self.selected = None
123 for widget in self.frame.body.body:
124 if widget.__class__ == sat_widgets.SelectableText:
125 widget.setState(False, invisible=True)
126
127
128 def get_contact(self):
129 """Return contact currently selected"""
130 return self.selected
131
132 def clear_contacts(self):
133 """clear all the contact list"""
134 self.groups={}
135 self.selected = None
136 self.unselectAll()
137 self.frame.body = self.__buildList()
138 self.host.redraw()
139
140 def replace(self, jid, groups=[None]):
141 """add a contact to the list if doesn't exist, else update it"""
142 assert isinstance(groups, list)
143 assert isinstance(jid, JID)
144 if not groups:
145 groups=[None]
146 for group in groups:
147 if not self.groups.has_key(group):
148 self.groups[group] = [True,set()] #[unfold,list_of_contacts]
149 self.groups[group][1].add(jid.short)
150 self.frame.body = self.__buildList()
151 self.host.redraw()
152
153
154 """contacts = self.list_wid.getAllValues()
155 if jid.short not in contacts:
156 contacts.append(jid.short)
157 contacts.sort()
158 self.list_wid.changeValues(contacts)
159 self._emit('change')"""
160
161 def disconnect(self, jid):
162 """mark a contact disconnected"""
163 self.remove(jid.short)
164
165 def remove(self, param_jid):
166 """remove a contact from the list"""
167 groups_to_remove = []
168 jid = JID(param_jid)
169 for group in self.groups:
170 contacts = self.groups[group][1]
171 if jid.short in contacts:
172 contacts.remove(jid.short)
173 if not len(contacts):
174 groups_to_remove.append(group)
175 for group in groups_to_remove:
176 del self.groups[group]
177 self.frame.body = self.__buildList()
178 self.host.redraw()
179
180 def add(self, jid, param_groups=[None]):
181 """add a contact to the list"""
182 self.replace(jid,param_groups)
183