diff browser_side/panels.py @ 35:d43d6e4b9dc8

room user joining/leaving
author Goffi <goffi@goffi.org>
date Mon, 16 May 2011 18:19:35 +0200
parents ed935f763cc8
children 1d406077b49b
line wrap: on
line diff
--- a/browser_side/panels.py	Mon May 16 03:10:11 2011 +0200
+++ b/browser_side/panels.py	Mon May 16 18:19:35 2011 +0200
@@ -423,32 +423,42 @@
 
     def __init__(self, nick):
         self.nick = nick
-        HTML.__init__(self, "<div class='occupant'>%s</div>" % html_sanitize(nick)) 
+        HTML.__init__(self, "<div class='occupant'>%s</div>" % html_sanitize(nick))
+
+    def __str__(self):
+        return nick
 
 class OccupantsList(AbsolutePanel):
     """Panel user to show occupants of a room"""
 
     def __init__(self):
         AbsolutePanel.__init__(self)
+        self.occupants_list = {}
         self.setStyleName('occupantsList')
-        self.occupants = set()
-        #self.setHeight('100%')
 
     def addOccupant(self, nick):
-       _occupant = Occupant(nick)
-       print "addOccupant: nick", nick
-       print _occupant
-       self.occupants.add(_occupant)
-       self.add(_occupant)
+        _occupant = Occupant(nick)
+        self.occupants_list[nick] = _occupant
+        self.add(_occupant)
 
-class ChatPanel(DropCell, ClickHandler, SimplePanel):
+    def removeOccupant(self, nick):
+        try:
+            self.remove(self.occupants_list[nick])
+        except KeyError:
+            print "ERROR: trying to remove an unexisting nick"
+    
+    def clear(self):
+        self.occupants_list.clear()
+        AbsolutePanel.clear(self)
+
+class ChatPanel(DropCell, ClickHandler, ScrollPanelWrapper):
 
     def __init__(self, host, target, type='one2one'):
         """Panel used for conversation (one 2 one or group chat)
         @param host: SatWebFrontend instance
         @param target: entity (JID) with who we have a conversation (contact's jid for one 2 one chat, or MUC room)
         @param type: one2one for simple conversation, group for MUC"""
-        SimplePanel.__init__(self)
+        ScrollPanelWrapper.__init__(self)
         DropCell.__init__(self)
         ClickHandler.__init__(self)
         self.vpanel = VerticalPanel()
@@ -495,9 +505,20 @@
     def setPresents(self, nicks):
         """Set the users presents in this room
         @param occupants: list of nicks (string)"""
+        self.occupants_list.clear()
         for nick in nicks:
             self.occupants_list.addOccupant(nick)
 
+    def userJoined(self, nick, data):
+        print "userJoined:", nick, data
+        self.occupants_list.addOccupant(nick)
+        self.printInfo("=> %s has joined the room" % nick)
+
+    def userLeft(self, nick, data):
+        print "userLeft:", nick, data
+        self.occupants_list.removeOccupant(nick)
+        self.printInfo("<= %s has left the room" % nick)
+
     def historyPrint(self, size=20):
         """Print the initial history"""
         def getHistoryCB(history):
@@ -506,15 +527,32 @@
             for stamp in stamps:
                 self.printMessage(history[stamp][0], history[stamp][1], stamp)
         self.host.bridge.call('getHistory', getHistoryCB, self.host.whoami.bare, str(self.target), 20)
-    
+   
+    def printInfo(self, msg, type='normal'):
+        """Print general info
+        @param msg: message to print
+        @type: one of:
+            normal: general info like "toto has joined the room"
+            me: "/me" information like "/me clenches his fist" ==> "toto clenches his fist"
+        """
+        _wid = Label(msg)
+        if type == 'normal':
+            _wid.setStyleName('chatTextInfo')
+        elif type == 'me':
+            _wid.setStyleName('chatTextMe')
+        else:
+            _wid.setStyleName('chatTextInfo')
+        self.content.add(_wid)
+        
+
     def printMessage(self, from_jid, msg, timestamp=None):
         """Print message in chat window. Must be implemented by child class"""
         _jid=JID(from_jid)
         nick = _jid.node if self.type=='one2one' else _jid.resource 
         mymess = _jid.resource == self.nick if self.type == "group" else  _jid.bare == self.host.whoami.bare #mymess = True if message comes from local user
-        """if msg.startswith('/me '):
+        if msg.startswith('/me '):
             self.printInfo('* %s %s' % (nick, msg[4:]),type='me')
-            return"""
+            return
         self.content.add(ChatText(timestamp, nick, mymess, msg))
         self.content_scroll.scrollToBottom()