comparison frontends/src/primitivus/contact_list.py @ 510:886754295efe

quick frontend, primitivus, wix: MUC private messages management /!\ not fully finished, backend part is not done yet /!\ - as resources are discarded to manage chat windows lists, a pretty dirty hack is done to work around this: full jid is escaped using a prefix (it becomes invalid and resource is preserved). - new quick_utils module, with helper methods. escapePrivate and unescapePrivate implementations - MUC private messages are not managed in Wix yet
author Goffi <goffi@goffi.org>
date Thu, 11 Oct 2012 00:48:35 +0200
parents f98bef71a918
children 62f7f2403093
comparison
equal deleted inserted replaced
509:64ff046dc201 510:886754295efe
20 """ 20 """
21 21
22 import urwid 22 import urwid
23 from urwid_satext import sat_widgets 23 from urwid_satext import sat_widgets
24 from sat_frontends.quick_frontend.quick_contact_list import QuickContactList 24 from sat_frontends.quick_frontend.quick_contact_list import QuickContactList
25 from sat_frontends.quick_frontend.quick_utils import escapePrivate, unescapePrivate
25 from sat.tools.jid import JID 26 from sat.tools.jid import JID
26 27
27 28
28 class ContactList(urwid.WidgetWrap, QuickContactList): 29 class ContactList(urwid.WidgetWrap, QuickContactList):
29 signals = ['click','change'] 30 signals = ['click','change']
30 31
31 def __init__(self, host, on_click=None, on_change=None, user_data=None): 32 def __init__(self, host, on_click=None, on_change=None, user_data=None):
33 QuickContactList.__init__(self)
32 self.host = host 34 self.host = host
33 self.selected = None 35 self.selected = None
34 self.groups={} 36 self.groups={}
35 self.special={}
36 self.alert_jid=set() 37 self.alert_jid=set()
37 self.show_status = False 38 self.show_status = False
38 self.show_disconnected = False 39 self.show_disconnected = False
39 40
40 #we now build the widget 41 #we now build the widget
43 urwid.WidgetWrap.__init__(self, self.main_widget) 44 urwid.WidgetWrap.__init__(self, self.main_widget)
44 if on_click: 45 if on_click:
45 urwid.connect_signal(self, 'click', on_click, user_data) 46 urwid.connect_signal(self, 'click', on_click, user_data)
46 if on_change: 47 if on_change:
47 urwid.connect_signal(self, 'change', on_change, user_data) 48 urwid.connect_signal(self, 'change', on_change, user_data)
48 QuickContactList.__init__(self)
49 49
50 def update(self): 50 def update(self):
51 """Update display, keep focus""" 51 """Update display, keep focus"""
52 widget, position = self.frame.body.get_focus() 52 widget, position = self.frame.body.get_focus()
53 self.frame.body = self.__buildList() 53 self.frame.body = self.__buildList()
113 contacts = list(param_contacts) 113 contacts = list(param_contacts)
114 114
115 widgets = [] #list of built widgets 115 widgets = [] #list of built widgets
116 116
117 for contact in contacts: 117 for contact in contacts:
118 jid=JID(contact) 118 if contact.startswith(const_PRIVATE_PREFIX):
119 name = self.getCache(jid, 'name') 119 contact_disp = ('alert' if contact in self.alert_jid else "show_normal", unescapePrivate(contact))
120 nick = self.getCache(jid, 'nick') 120 show_icon = ''
121 status = self.getCache(jid, 'status') 121 status = ''
122 show = self.getCache(jid, 'show') 122 else:
123 if show == None: 123 jid=JID(contact)
124 show = "unavailable" 124 name = self.getCache(jid, 'name')
125 if (not self.show_disconnected and show == "unavailable" 125 nick = self.getCache(jid, 'nick')
126 and not contact in self.alert_jid and contact != self.selected): 126 status = self.getCache(jid, 'status')
127 continue 127 show = self.getCache(jid, 'show')
128 show_icon, show_attr = const_SHOW_ICON.get(show,('','default')) 128 if show == None:
129 contact_disp = ('alert' if contact in self.alert_jid else show_attr, nick or name or jid.node or jid.short) 129 show = "unavailable"
130 if (not self.show_disconnected and show == "unavailable"
131 and not contact in self.alert_jid and contact != self.selected):
132 continue
133 show_icon, show_attr = const_SHOW_ICON.get(show,('','default'))
134 contact_disp = ('alert' if contact in self.alert_jid else show_attr, nick or name or jid.node or jid.short)
130 display = [ show_icon + " " , contact_disp] 135 display = [ show_icon + " " , contact_disp]
131 if self.show_status: 136 if self.show_status:
132 status_disp = ('status',"\n " + status) if status else "" 137 status_disp = ('status',"\n " + status) if status else ""
133 display.append(status_disp) 138 display.append(status_disp)
134 header = '(*) ' if contact in self.alert_jid else '' 139 header = '(*) ' if contact in self.alert_jid else ''
145 content.append(widget) 150 content.append(widget)
146 urwid.connect_signal(widget, 'change', self.__contactClicked) 151 urwid.connect_signal(widget, 'change', self.__contactClicked)
147 152
148 def __buildSpecials(self, content): 153 def __buildSpecials(self, content):
149 """Build the special entities""" 154 """Build the special entities"""
150 specials = self.special.keys() 155 specials = self.specials.keys()
151 specials.sort() 156 specials.sort()
152 for special in specials: 157 for special in specials:
153 jid=JID(special) 158 jid=JID(special)
154 name = self.getCache(jid, 'name') 159 name = self.getCache(jid, 'name')
155 nick = self.getCache(jid, 'nick') 160 nick = self.getCache(jid, 'nick')
166 def __buildList(self): 171 def __buildList(self):
167 """Build the main contact list widget""" 172 """Build the main contact list widget"""
168 content = urwid.SimpleListWalker([]) 173 content = urwid.SimpleListWalker([])
169 174
170 self.__buildSpecials(content) 175 self.__buildSpecials(content)
171 if self.special: 176 if self.specials:
172 content.append(urwid.Divider('=')) 177 content.append(urwid.Divider('='))
173 178
174 group_keys = self.groups.keys() 179 group_keys = self.groups.keys()
175 group_keys.sort(key = lambda x: x.lower() if x else x) 180 group_keys.sort(key = lambda x: x.lower() if x else x)
176 for key in group_keys: 181 for key in group_keys:
196 """Return contact currently selected""" 201 """Return contact currently selected"""
197 return self.selected 202 return self.selected
198 203
199 def clearContacts(self): 204 def clearContacts(self):
200 """clear all the contact list""" 205 """clear all the contact list"""
206 QuickContactList.clearContacts(self)
201 self.groups={} 207 self.groups={}
202 self.selected = None 208 self.selected = None
203 self.unselectAll() 209 self.unselectAll()
204 self.update() 210 self.update()
205 211
206 def replace(self, jid, groups=None, attributes=None): 212 def replace(self, jid, groups=None, attributes=None):
207 """add a contact to the list if doesn't exist, else update it""" 213 """add a contact to the list if doesn't exist, else update it"""
208 if jid.short in self.special: 214 QuickContactList.replace(self, jid, groups, attributes)
215 if jid.short in self.specials:
209 return 216 return
210 if not groups: 217 if not groups:
211 groups = [None] 218 groups = [None]
212 if not attributes: 219 if not attributes:
213 attributes={} 220 attributes={}
225 contacts.append(jid.short) 232 contacts.append(jid.short)
226 contacts.sort() 233 contacts.sort()
227 self.list_wid.changeValues(contacts) 234 self.list_wid.changeValues(contacts)
228 self._emit('change')""" 235 self._emit('change')"""
229 236
230 def remove(self, param_jid): 237 def remove(self, jid):
231 """remove a contact from the list""" 238 """remove a contact from the list"""
239 QuickContactList.remove(self, jid)
232 groups_to_remove = [] 240 groups_to_remove = []
233 jid = JID(param_jid)
234 for group in self.groups: 241 for group in self.groups:
235 contacts = self.groups[group][1] 242 contacts = self.groups[group][1]
236 if jid.short in contacts: 243 if jid.short in contacts:
237 contacts.remove(jid.short) 244 contacts.remove(jid.short)
238 if not len(contacts): 245 if not len(contacts):
239 groups_to_remove.append(group) 246 groups_to_remove.append(group)
240 for group in groups_to_remove: 247 for group in groups_to_remove:
241 del self.groups[group] 248 del self.groups[group]
242 try:
243 del self.special[jid.short]
244 except KeyError:
245 pass
246 self.update() 249 self.update()
247 250
248 def add(self, jid, param_groups=[None]): 251 def add(self, jid, param_groups=[None]):
249 """add a contact to the list""" 252 """add a contact to the list"""
250 self.replace(jid,param_groups) 253 self.replace(jid,param_groups)
252 def setSpecial(self, special_jid, special_type): 255 def setSpecial(self, special_jid, special_type):
253 """Set entity as a special 256 """Set entity as a special
254 @param jid: jid of the entity 257 @param jid: jid of the entity
255 @param _type: special type (e.g.: "MUC") 258 @param _type: special type (e.g.: "MUC")
256 """ 259 """
257 self.special[special_jid.short] = special_type 260 QuickContactList.setSpecial(self, special_jid, special_type)
258 if None in self.groups: 261 if None in self.groups:
259 folded,group_jids = self.groups[None] 262 folded,group_jids = self.groups[None]
260 for group_jid in group_jids: 263 for group_jid in group_jids:
261 if JID(group_jid).short == special_jid.short: 264 if JID(group_jid).short == special_jid.short:
262 group_jids.remove(group_jid) 265 group_jids.remove(group_jid)
263 break 266 break
264 self.update() 267 self.update()
265 268
266 def updatePresence(self, jid, show, priority, statuses): 269 def updatePresence(self, jid, show, priority, statuses):
267 #XXX: for the moment, we ignore presence updates for special entities 270 #XXX: for the moment, we ignore presence updates for special entities
268 if jid.short not in self.special: 271 if jid.short not in self.specials:
269 QuickContactList.updatePresence(self, jid, show, priority, statuses) 272 QuickContactList.updatePresence(self, jid, show, priority, statuses)