Mercurial > libervia-web
comparison src/browser/sat_browser/contact_panel.py @ 684:e876f493dccc
browser_side: follow changes made on quick_frontend for chat states and MUC symbols + minor fixes following the refactorisation:
- some MUC handlers are no more needed, the presence handler is enough
- move the chat states logic to quick_frontend
- display MUC games symbols
- remove classes contact_list.ContactsPanel, contact_panel.Occupant and contact_panel.OccupantsList
- move buildPresenceStyle and setPresenceStyle to html_tools
- fixes games menu callback
author | souliane <souliane@mailoo.org> |
---|---|
date | Wed, 18 Mar 2015 10:17:04 +0100 |
parents | 849ffb24d5bf |
children | 9877607c719a |
comparison
equal
deleted
inserted
replaced
683:801eb94aa869 | 684:e876f493dccc |
---|---|
22 import pyjd # this is dummy in pyjs | 22 import pyjd # this is dummy in pyjs |
23 from sat.core.log import getLogger | 23 from sat.core.log import getLogger |
24 log = getLogger(__name__) | 24 log = getLogger(__name__) |
25 from sat_frontends.tools import jid | 25 from sat_frontends.tools import jid |
26 | 26 |
27 from pyjamas.ui.AbsolutePanel import AbsolutePanel | |
28 from pyjamas.ui.VerticalPanel import VerticalPanel | 27 from pyjamas.ui.VerticalPanel import VerticalPanel |
29 from pyjamas.ui.HTML import HTML | |
30 | 28 |
31 import html_tools | 29 import html_tools |
32 import contact_widget | 30 import contact_widget |
33 from constants import Const as C | 31 from constants import Const as C |
34 | |
35 | |
36 # FIXME: must be removed | |
37 class Occupant(HTML): | |
38 """Occupant of a MUC room""" | |
39 | |
40 def __init__(self, nick, state=None, special=""): | |
41 """ | |
42 @param nick: the user nickname | |
43 @param state: the user chate state (XEP-0085) | |
44 @param special: a string of symbols (e.g: for activities) | |
45 """ | |
46 HTML.__init__(self, StyleName="occupant") | |
47 self.nick = nick | |
48 self._state = state | |
49 self.special = special | |
50 self._refresh() | |
51 | |
52 def __str__(self): | |
53 return self.nick | |
54 | |
55 def setState(self, state): | |
56 self._state = state | |
57 self._refresh() | |
58 | |
59 def addSpecial(self, special): | |
60 """@param special: unicode""" | |
61 if special not in self.special: | |
62 self.special += special | |
63 self._refresh() | |
64 | |
65 def removeSpecials(self, special): | |
66 """@param special: unicode or list""" | |
67 if not isinstance(special, list): | |
68 special = [special] | |
69 for symbol in special: | |
70 self.special = self.special.replace(symbol, "") | |
71 self._refresh() | |
72 | |
73 def _refresh(self): | |
74 state = (' %s' % C.MUC_USER_STATES[self._state]) if self._state else '' | |
75 special = "" if len(self.special) == 0 else " %s" % self.special | |
76 self.setHTML("%s%s%s" % (html_tools.html_sanitize(self.nick), special, state)) | |
77 | 32 |
78 | 33 |
79 class ContactsPanel(VerticalPanel): | 34 class ContactsPanel(VerticalPanel): |
80 """ContactList graphic representation | 35 """ContactList graphic representation |
81 | 36 |
115 | 70 |
116 @param contact_jid (jid.JID): contact JID | 71 @param contact_jid (jid.JID): contact JID |
117 @return: jid.JID | 72 @return: jid.JID |
118 """ | 73 """ |
119 return contact_jid.bare if self.merge_resources else contact_jid | 74 return contact_jid.bare if self.merge_resources else contact_jid |
75 | |
76 def clear(self): | |
77 """Clear all contacts.""" | |
78 self._contacts.clear() | |
79 VerticalPanel.clear(self) | |
120 | 80 |
121 def setList(self, jids): | 81 def setList(self, jids): |
122 """set all contacts in the list in one shot. | 82 """set all contacts in the list in one shot. |
123 | 83 |
124 @param jids (list[jid.JID]): jids to display (the order is kept) | 84 @param jids (list[jid.JID]): jids to display (the order is kept) |
171 """Update the avatar of the given contact | 131 """Update the avatar of the given contact |
172 | 132 |
173 @param contact_jid (jid.JID): contact JID | 133 @param contact_jid (jid.JID): contact JID |
174 @param url (unicode): image url | 134 @param url (unicode): image url |
175 """ | 135 """ |
176 try: | 136 self.getContactBox(contact_jid).updateAvatar(url) |
177 self.getContactBox(contact_jid).updateAvatar(url) | |
178 except TypeError: | |
179 pass | |
180 | 137 |
181 def updateNick(self, contact_jid, new_nick): | 138 def updateNick(self, contact_jid, new_nick): |
182 """Update the avatar of the given contact. | 139 """Update the avatar of the given contact. |
183 | 140 |
184 @param contact_jid (jid.JID): contact JID | 141 @param contact_jid (jid.JID): contact JID |
185 @param new_nick (unicode): new nick of the contact | 142 @param new_nick (unicode): new nick of the contact |
186 """ | 143 """ |
187 try: | 144 self.getContactBox(contact_jid).updateNick(new_nick) |
188 self.getContactBox(contact_jid).updateNick(new_nick) | |
189 except TypeError: | |
190 pass | |
191 | 145 |
146 def setPresence(self, entity, show): | |
147 """Update entity's presence. | |
192 | 148 |
193 | 149 @param entity(jid.JID): entity updated |
194 # FIXME: must be removed and ContactsPanel must be used instead | 150 @param show: availability |
195 class OccupantsList(AbsolutePanel): | |
196 """Panel user to show occupants of a room""" | |
197 | |
198 def __init__(self): | |
199 AbsolutePanel.__init__(self) | |
200 self.occupants_list = {} | |
201 self.setStyleName('occupantsList') | |
202 | |
203 def addOccupant(self, nick): | |
204 if nick in self.occupants_list: | |
205 return | |
206 _occupant = Occupant(nick) | |
207 self.occupants_list[nick] = _occupant | |
208 self.add(_occupant) | |
209 | |
210 def removeOccupant(self, nick): | |
211 try: | |
212 self.remove(self.occupants_list[nick]) | |
213 except KeyError: | |
214 log.error("trying to remove an unexisting nick") | |
215 | |
216 def getOccupantBox(self, nick): | |
217 """Get the widget element of the given nick. | |
218 | |
219 @return: Occupant | |
220 """ | 151 """ |
221 try: | 152 if self.merge_resources: # we use cache to have the show information of main resource only |
222 return self.occupants_list[nick] | 153 clist = self.host.contact_list |
223 except KeyError: | 154 show = clist.getCache(entity.bare, C.PRESENCE_SHOW) |
224 return None | 155 if show is None: |
225 | 156 show = C.PRESENCE_UNAVAILABLE |
226 def clear(self): | 157 html_tools.setPresenceStyle(self.getContactBox(entity).label, show) |
227 self.occupants_list.clear() | |
228 AbsolutePanel.clear(self) | |
229 | |
230 def updateSpecials(self, occupants=[], html=""): | |
231 """Set the specified html "symbol" to the listed occupants, | |
232 and eventually remove it from the others (if they got it). | |
233 This is used for example to visualize who is playing a game. | |
234 @param occupants: list of the occupants that need the symbol | |
235 @param html: unicode symbol (actually one character or more) | |
236 or a list to assign different symbols of the same family. | |
237 """ | |
238 index = 0 | |
239 special = html | |
240 for occupant in self.occupants_list.keys(): | |
241 if occupant in occupants: | |
242 if isinstance(html, list): | |
243 special = html[index] | |
244 index = (index + 1) % len(html) | |
245 self.occupants_list[occupant].addSpecial(special) | |
246 else: | |
247 self.occupants_list[occupant].removeSpecials(html) |