comparison src/browser/sat_browser/chat.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 a90cc8fc9605
children 9877607c719a
comparison
equal deleted inserted replaced
683:801eb94aa869 684:e876f493dccc
42 import html_tools 42 import html_tools
43 import libervia_widget 43 import libervia_widget
44 import base_panel 44 import base_panel
45 import contact_panel 45 import contact_panel
46 import editor_widget 46 import editor_widget
47 import contact_list
48 from constants import Const as C 47 from constants import Const as C
49 import plugin_xep_0085 48 import plugin_xep_0085
50 import game_tarot 49 import game_tarot
51 import game_radiocol 50 import game_radiocol
52 51
101 contacts_style="muc_contact", 100 contacts_style="muc_contact",
102 contacts_menus=(C.MENU_JID_CONTEXT), 101 contacts_menus=(C.MENU_JID_CONTEXT),
103 contacts_display=('resource',)) 102 contacts_display=('resource',))
104 chat_area.add(self.occupants_panel) 103 chat_area.add(self.occupants_panel)
105 DOM.setAttribute(chat_area.getWidgetTd(self.occupants_panel), "className", "occupantsPanelCell") 104 DOM.setAttribute(chat_area.getWidgetTd(self.occupants_panel), "className", "occupantsPanelCell")
105 # FIXME: workaround for a pyjamas issue: calling hash on a class method always return a different value if that method is defined directly within the class (with the "def" keyword)
106 self.presenceListener = self.onPresenceUpdate
107 self.host.addListener('presence', self.presenceListener, [C.PROF_KEY_NONE])
106 self._body.add(chat_area) 108 self._body.add(chat_area)
107 self.content = AbsolutePanel() 109 self.content = AbsolutePanel()
108 self.content.setStyleName('chatContent') 110 self.content.setStyleName('chatContent')
109 self.content_scroll = base_panel.ScrollPanelWrapper(self.content) 111 self.content_scroll = base_panel.ScrollPanelWrapper(self.content)
110 chat_area.add(self.content_scroll) 112 chat_area.add(self.content_scroll)
111 chat_area.setCellWidth(self.content_scroll, '100%') 113 chat_area.setCellWidth(self.content_scroll, '100%')
112 self.vpanel.add(self._body) 114 self.vpanel.add(self._body)
113 self.vpanel.setCellHeight(self._body, '100%') 115 self.vpanel.setCellHeight(self._body, '100%')
114 self.addStyleName('chatPanel') 116 self.addStyleName('chatPanel')
115 self.setWidget(self.vpanel) 117 self.setWidget(self.vpanel)
116 self.state_machine = plugin_xep_0085.ChatStateMachine(self.host, unicode(self.target)) 118 self.chat_state_machine = plugin_xep_0085.ChatStateMachine(self.host, unicode(self.target))
117 self._state = None
118 self.refresh() 119 self.refresh()
119 if type_ == C.CHAT_ONE2ONE: 120 if type_ == C.CHAT_ONE2ONE:
120 self.historyPrint(profile=self.profile) 121 self.historyPrint(profile=self.profile)
121 122
122 @property 123 @property
185 text, 186 text,
186 mess_type=C.MESS_TYPE_GROUPCHAT if self.type == C.CHAT_GROUP else C.MESS_TYPE_CHAT, 187 mess_type=C.MESS_TYPE_GROUPCHAT if self.type == C.CHAT_GROUP else C.MESS_TYPE_CHAT,
187 errback=self.host.sendError, 188 errback=self.host.sendError,
188 profile_key=C.PROF_KEY_NONE 189 profile_key=C.PROF_KEY_NONE
189 ) 190 )
190 self.state_machine._onEvent("active") 191 self.chat_state_machine._onEvent("active")
192
193 def onPresenceUpdate(self, entity, show, priority, statuses, profile):
194 """Update entity's presence status
195
196 @param entity(jid.JID): entity updated
197 @param show: availability
198 @parap priority: resource's priority
199 @param statuses: dict of statuses
200 @param profile: %(doc_profile)s
201 """
202 assert self.type == C.CHAT_GROUP
203 if entity.bare == self.target:
204 self.occupants_panel.setPresence(entity, show)
191 205
192 def onQuit(self): 206 def onQuit(self):
193 libervia_widget.LiberviaWidget.onQuit(self) 207 libervia_widget.LiberviaWidget.onQuit(self)
194 if self.type == C.CHAT_GROUP: 208 if self.type == C.CHAT_GROUP:
209 self.host.removeListener('presence', self.presenceListener)
195 self.host.bridge.call('mucLeave', None, unicode(self.target.bare)) 210 self.host.bridge.call('mucLeave', None, unicode(self.target.bare))
196 211
197 def setUserNick(self, nick): 212 def setUserNick(self, nick):
198 """Set the nick of the user, usefull for e.g. change the color of the user""" 213 """Set the nick of the user, usefull for e.g. change the color of the user"""
199 self.nick = nick 214 self.nick = nick
200 215
201 def setPresents(self, nicks): 216 def addUser(self, nick):
202 """Set the occupants of a group chat.
203
204 @param nicks (list[unicode]): sorted list of nicknames
205 """
206 QuickChat.setPresents(self, nicks)
207 self.occupants_panel.setList([jid.JID(u"%s/%s" % (self.target, nick)) for nick in nicks])
208
209 def replaceUser(self, nick, show_info=True):
210 """Add user if it is not in the group list""" 217 """Add user if it is not in the group list"""
211 QuickChat.replaceUser(self, nick, show_info) 218 QuickChat.addUser(self, nick)
212 occupant_jid = jid.JID("%s/%s" % (unicode(self.target), nick)) 219 occupant_jid = jid.JID("%s/%s" % (unicode(self.target), nick))
213 self.occupants_panel.addContact(occupant_jid) 220 self.occupants_panel.addContact(occupant_jid)
214 221
215 def removeUser(self, nick, show_info=True): 222 def removeUser(self, nick):
216 """Remove a user from the group list""" 223 """Remove a user from the group list"""
217 QuickChat.removeUser(self, nick, show_info) 224 QuickChat.removeUser(self, nick)
218 occupant_jid = jid.JID("%s/%s" % (unicode(self.target), nick)) 225 occupant_jid = jid.JID("%s/%s" % (unicode(self.target), nick))
219 self.occupants_panel.removeContact(occupant_jid) 226 self.occupants_panel.removeContact(occupant_jid)
220 227
221 def changeUserNick(self, old_nick, new_nick): 228 def changeUserNick(self, old_nick, new_nick):
222 assert self.type == C.CHAT_GROUP 229 assert self.type == C.CHAT_GROUP
223 # FIXME
224 # self.occupants_panel.removeOccupant(old_nick) 230 # self.occupants_panel.removeOccupant(old_nick)
225 # self.occupants_panel.addOccupant(new_nick) 231 # self.occupants_panel.addOccupant(new_nick)
226 self.printInfo(_("%(old_nick)s is now known as %(new_nick)s") % {'old_nick': old_nick, 'new_nick': new_nick}) 232 self.printInfo(_("%(old_nick)s is now known as %(new_nick)s") % {'old_nick': old_nick, 'new_nick': new_nick})
227 233
228 # def historyPrint(self, size=C.HISTORY_LIMIT_DEFAULT): 234 # def historyPrint(self, size=C.HISTORY_LIMIT_DEFAULT):
277 # None is returned, the message is managed 283 # None is returned, the message is managed
278 return 284 return
279 self.content.add(ChatText(nick, mymess, msg, extra)) 285 self.content.add(ChatText(nick, mymess, msg, extra))
280 self.content_scroll.scrollToBottom() 286 self.content_scroll.scrollToBottom()
281 287
282 def setState(self, state, nick=None): 288 def setTitle(self, title=None, extra=None):
283 """Set the chat state (XEP-0085) of the contact. Leave nick to None 289 """Refresh the title of this Chat dialog
284 to set the state for a one2one conversation, or give a nickname or 290
285 C.ALL_OCCUPANTS to set the state of a participant within a MUC. 291 @param title (unicode): main title or None to use default
286 @param state: the new chat state 292 @param extra (dict{unicode: unicode}): extra info
287 @param nick: ignored for one2one, otherwise the MUC user nick or C.ALL_OCCUPANTS 293 """
288 """ 294 if title is None:
289 return # FIXME 295 title = unicode(self.target.bare)
290 if self.type == C.CHAT_GROUP: 296 if extra:
291 assert(nick) 297 extra_title = ' '.join([u'({})'.format(value) for value in extra.values()])
292 if nick == C.ALL_OCCUPANTS: 298 title = '%s %s' % (title, extra_title)
293 occupants = self.occupants_panel.occupants_panel.keys() 299 libervia_widget.LiberviaWidget.setTitle(self, title)
294 else:
295 occupants = [nick] if nick in self.occupants_panel.occupants_panel else []
296 for occupant in occupants:
297 self.occupants_panel.occupants_panel[occupant].setState(state)
298 else:
299 self._state = state
300 self.refreshTitle()
301 self.state_machine.started = not not state # start to send "composing" state from now
302
303 def refreshTitle(self):
304 """Refresh the title of this Chat dialog"""
305 title = unicode(self.target.bare)
306 if self._state:
307 title += " (%s)".format(self._state)
308 self.setTitle(title)
309 300
310 def setConnected(self, jid_s, resource, availability, priority, statuses): 301 def setConnected(self, jid_s, resource, availability, priority, statuses):
311 """Set connection status 302 """Set connection status
312 @param jid_s (unicode): JID userhost as unicode 303 @param jid_s (unicode): JID userhost as unicode
313 """ 304 """
315 assert(jid_s == self.target.bare) 306 assert(jid_s == self.target.bare)
316 if self.type != C.CHAT_GROUP: 307 if self.type != C.CHAT_GROUP:
317 return 308 return
318 box = self.occupants_panel.getOccupantBox(resource) 309 box = self.occupants_panel.getOccupantBox(resource)
319 if box: 310 if box:
320 contact_list.setPresenceStyle(box, availability) 311 html_tools.setPresenceStyle(box, availability)
321 312
322 def updateChatState(self, from_jid, state): 313 def setOccupantStates(self, occupant_jid, states):
323 #TODO 314 """Set a MUC occupant's states.
324 pass 315
316 @param occupant_jid (jid.JID): occupant to update
317 @param states (dict{unicode: unicode}): new states
318 """
319 self.occupants_panel.getContactBox(occupant_jid).updateStates(states)
320 if 'chat_state' in states.keys(): # start/stop sending "composing" state from now
321 self.chat_state_machine.started = not not states['chat_state']
322
323 def setContactStates(self, contact_jid, states):
324 """Set a one2one contact's states.
325
326 @param contact_jid (jid.JID): contact
327 @param states (dict{unicode: unicode}): new states
328 """
329 self.setTitle(extra=states)
330 if 'chat_state' in states.keys(): # start/stop sending "composing" state from now
331 self.chat_state_machine.started = not not states['chat_state']
325 332
326 def addGamePanel(self, widget): 333 def addGamePanel(self, widget):
327 """Insert a game panel to this Chat dialog. 334 """Insert a game panel to this Chat dialog.
328 335
329 @param widget (Widget): the game panel 336 @param widget (Widget): the game panel