diff 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
line wrap: on
line diff
--- a/src/browser/sat_browser/chat.py	Thu Mar 19 20:41:46 2015 +0100
+++ b/src/browser/sat_browser/chat.py	Wed Mar 18 10:17:04 2015 +0100
@@ -44,7 +44,6 @@
 import base_panel
 import contact_panel
 import editor_widget
-import contact_list
 from constants import Const as C
 import plugin_xep_0085
 import game_tarot
@@ -103,6 +102,9 @@
                                                                contacts_display=('resource',))
             chat_area.add(self.occupants_panel)
             DOM.setAttribute(chat_area.getWidgetTd(self.occupants_panel), "className", "occupantsPanelCell")
+            # 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)
+            self.presenceListener = self.onPresenceUpdate
+            self.host.addListener('presence', self.presenceListener, [C.PROF_KEY_NONE])
         self._body.add(chat_area)
         self.content = AbsolutePanel()
         self.content.setStyleName('chatContent')
@@ -113,8 +115,7 @@
         self.vpanel.setCellHeight(self._body, '100%')
         self.addStyleName('chatPanel')
         self.setWidget(self.vpanel)
-        self.state_machine = plugin_xep_0085.ChatStateMachine(self.host, unicode(self.target))
-        self._state = None
+        self.chat_state_machine = plugin_xep_0085.ChatStateMachine(self.host, unicode(self.target))
         self.refresh()
         if type_ == C.CHAT_ONE2ONE:
             self.historyPrint(profile=self.profile)
@@ -187,40 +188,45 @@
                               errback=self.host.sendError,
                               profile_key=C.PROF_KEY_NONE
                               )
-        self.state_machine._onEvent("active")
+        self.chat_state_machine._onEvent("active")
+
+    def onPresenceUpdate(self, entity, show, priority, statuses, profile):
+        """Update entity's presence status
+
+        @param entity(jid.JID): entity updated
+        @param show: availability
+        @parap priority: resource's priority
+        @param statuses: dict of statuses
+        @param profile: %(doc_profile)s
+        """
+        assert self.type == C.CHAT_GROUP
+        if entity.bare == self.target:
+            self.occupants_panel.setPresence(entity, show)
 
     def onQuit(self):
         libervia_widget.LiberviaWidget.onQuit(self)
         if self.type == C.CHAT_GROUP:
+            self.host.removeListener('presence', self.presenceListener)
             self.host.bridge.call('mucLeave', None, unicode(self.target.bare))
 
     def setUserNick(self, nick):
         """Set the nick of the user, usefull for e.g. change the color of the user"""
         self.nick = nick
 
-    def setPresents(self, nicks):
-        """Set the occupants of a group chat.
-
-        @param nicks (list[unicode]): sorted list of nicknames
-        """
-        QuickChat.setPresents(self, nicks)
-        self.occupants_panel.setList([jid.JID(u"%s/%s" % (self.target, nick)) for nick in nicks])
-
-    def replaceUser(self, nick, show_info=True):
+    def addUser(self, nick):
         """Add user if it is not in the group list"""
-        QuickChat.replaceUser(self, nick, show_info)
+        QuickChat.addUser(self, nick)
         occupant_jid = jid.JID("%s/%s" % (unicode(self.target), nick))
         self.occupants_panel.addContact(occupant_jid)
 
-    def removeUser(self, nick, show_info=True):
+    def removeUser(self, nick):
         """Remove a user from the group list"""
-        QuickChat.removeUser(self, nick, show_info)
+        QuickChat.removeUser(self, nick)
         occupant_jid = jid.JID("%s/%s" % (unicode(self.target), nick))
         self.occupants_panel.removeContact(occupant_jid)
 
     def changeUserNick(self, old_nick, new_nick):
         assert self.type == C.CHAT_GROUP
-        # FIXME
         # self.occupants_panel.removeOccupant(old_nick)
         # self.occupants_panel.addOccupant(new_nick)
         self.printInfo(_("%(old_nick)s is now known as %(new_nick)s") % {'old_nick': old_nick, 'new_nick': new_nick})
@@ -279,33 +285,18 @@
         self.content.add(ChatText(nick, mymess, msg, extra))
         self.content_scroll.scrollToBottom()
 
-    def setState(self, state, nick=None):
-        """Set the chat state (XEP-0085) of the contact. Leave nick to None
-        to set the state for a one2one conversation, or give a nickname or
-        C.ALL_OCCUPANTS to set the state of a participant within a MUC.
-        @param state: the new chat state
-        @param nick: ignored for one2one, otherwise the MUC user nick or C.ALL_OCCUPANTS
+    def setTitle(self, title=None, extra=None):
+        """Refresh the title of this Chat dialog
+
+        @param title (unicode): main title or None to use default
+        @param extra (dict{unicode: unicode}): extra info
         """
-        return # FIXME
-        if self.type == C.CHAT_GROUP:
-            assert(nick)
-            if nick == C.ALL_OCCUPANTS:
-                occupants = self.occupants_panel.occupants_panel.keys()
-            else:
-                occupants = [nick] if nick in self.occupants_panel.occupants_panel else []
-            for occupant in occupants:
-                self.occupants_panel.occupants_panel[occupant].setState(state)
-        else:
-            self._state = state
-            self.refreshTitle()
-        self.state_machine.started = not not state  # start to send "composing" state from now
-
-    def refreshTitle(self):
-        """Refresh the title of this Chat dialog"""
-        title = unicode(self.target.bare)
-        if self._state:
-            title += " (%s)".format(self._state)
-        self.setTitle(title)
+        if title is None:
+            title = unicode(self.target.bare)
+        if extra:
+            extra_title = ' '.join([u'({})'.format(value) for value in extra.values()])
+            title = '%s %s' % (title, extra_title)
+        libervia_widget.LiberviaWidget.setTitle(self, title)
 
     def setConnected(self, jid_s, resource, availability, priority, statuses):
         """Set connection status
@@ -317,11 +308,27 @@
             return
         box = self.occupants_panel.getOccupantBox(resource)
         if box:
-            contact_list.setPresenceStyle(box, availability)
+            html_tools.setPresenceStyle(box, availability)
+
+    def setOccupantStates(self, occupant_jid, states):
+        """Set a MUC occupant's states.
 
-    def updateChatState(self, from_jid, state):
-        #TODO
-        pass
+        @param occupant_jid (jid.JID): occupant to update
+        @param states (dict{unicode: unicode}): new states
+        """
+        self.occupants_panel.getContactBox(occupant_jid).updateStates(states)
+        if 'chat_state' in states.keys():  # start/stop sending "composing" state from now
+            self.chat_state_machine.started = not not states['chat_state']
+
+    def setContactStates(self, contact_jid, states):
+        """Set a one2one contact's states.
+
+        @param contact_jid (jid.JID): contact
+        @param states (dict{unicode: unicode}): new states
+        """
+        self.setTitle(extra=states)
+        if 'chat_state' in states.keys():  # start/stop sending "composing" state from now
+            self.chat_state_machine.started = not not states['chat_state']
 
     def addGamePanel(self, widget):
         """Insert a game panel to this Chat dialog.