diff browser_side/panels.py @ 19:e8e3704eb97f

Added basic chat panel - the chat panel show history, timestamp, and nickname (pretty similar to primitivus and wix chat window) - JID has be rewritten to work with pyjamas, and is now in browser_side directory - a widget can now be selected: the message send in uniBox will be sent to it if there is no explicit target prefix ("@something") - a basic status panel is added under the uniBox, but not used yet
author Goffi <goffi@goffi.org>
date Sat, 16 Apr 2011 01:46:01 +0200
parents 795d144fc1d2
children 8f4b1a8914c3
line wrap: on
line diff
--- a/browser_side/panels.py	Fri Apr 15 15:30:31 2011 +0200
+++ b/browser_side/panels.py	Sat Apr 16 01:46:01 2011 +0200
@@ -30,14 +30,16 @@
 from pyjamas.ui.MenuItem import MenuItem
 from pyjamas.ui.Label import Label
 from pyjamas.ui.DropWidget import DropWidget
+from pyjamas.ui.ClickListener import ClickHandler
 from pyjamas.ui import HasAlignment
 from pyjamas import Window
 from pyjamas import DOM
 
 from pyjamas.dnd import makeDraggable
 from pyjamas.ui.DragWidget import DragWidget, DragContainer
-from tools.jid import JID
+from jid import JID
 from datetime import datetime
+from time import time
 
 class MenuCmd:
 
@@ -115,21 +117,24 @@
             item_type = None
         DOM.eventPreventDefault(event)
         if item_type=="GROUP":
-            _mblog = MicroblogPanel(self.host, item)
-            _mblog.setAcceptedGroup(item)
+            _new_panel = MicroblogPanel(self.host, item)
+            _new_panel.setAcceptedGroup(item)
         elif item_type=="CONTACT":
-            _mblog = MicroblogPanel(self.host, accept_all=True)
+            _contact = JID(item)
+            _new_panel = ChatPanel(self.host, _contact)
+            _new_panel.historyPrint()
+        elif item_type=="CONTACT_TITLE":
+            _new_panel = MicroblogPanel(self.host, accept_all=True)
         self.host.mpanels.remove(self)
-        self.host.mpanels.append(_mblog)
+        self.host.mpanels.append(_new_panel)
         print "DEBUG"
         grid = self.getParent()
         row_idx, cell_idx = self._getCellAndRow(grid, event)
         self.removeFromParent()
-        grid.setWidget(row_idx, cell_idx, _mblog)
-        print "index:", row_idx, cell_idx
+        if self.host.selected == self:
+            self.host.select(None)
+        grid.setWidget(row_idx, cell_idx, _new_panel)
         #FIXME: delete object ? Check the right way with pyjamas
-        #self.host.middle_panel.changePanel(self.data,self.host.mpanels[0])
-        
 
 class EmptyPanel(DropCell, SimplePanel):
     """Empty dropable panel"""
@@ -157,7 +162,6 @@
         panel.setStyleName('microblogEntry')
         self.add(panel)
 
-
 class MicroblogPanel(DropCell, ScrollPanel):
 
     def __init__(self,host, title='&nbsp;', accept_all=False):
@@ -207,8 +211,89 @@
             if self.host.contactPanel.isContactInGroup(group, jid):
                 return True
         return False
+
+class StatusPanel(HTMLPanel):
+    def __init__(self, status=''):
+        self.status = status
+        HTMLPanel.__init__(self, self.__getContent())
+
+    def __getContent(self):
+        return "<span class='status'>%(status)s</span>" % {'status':self.status}
+
+    def changeStatus(self, new_status):
+        self.status = new_status
+        self.setHTML(self.__getContent())
+
+class ChatText(HTMLPanel):
+
+    def __init__(self, timestamp, nick, mymess, msg):
+        _date = datetime.fromtimestamp(float(timestamp or time()))
+        print "DEBUG"
+        print timestamp
+        print time()
+        print _date
+        _msg_class = ["chat_text_msg"]
+        if mymess:
+            _msg_class.append("chat_text_mymess")
+        HTMLPanel.__init__(self, "<span class='chat_text_timestamp'>%(timestamp)s</span> <span class='chat_text_nick'>%(nick)s</span> <span class='%(msg_class)s'>%(msg)s</span>" %
+            {"timestamp": _date.strftime("%H:%M"),
+            "nick": "[%s]" % nick,
+            "msg_class": ' '.join(_msg_class),
+            "msg": msg}
+            )
+        self.setStyleName('chatText')
     
+class ChatPanel(DropCell, ClickHandler, ScrollPanel):
 
+    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"""
+        self.host = host
+        if not target:
+            print "ERROR: Empty target !"
+            return
+        self.target = target
+        title="%s" % target.bare
+        title.replace('<','&lt;').replace('>','&gt;')
+        _class = ['mb_panel_header']
+        ScrollPanel.__init__(self)
+        self.content = VerticalPanel()
+        self.content.add(HTMLPanel("<div class='%s'>%s</div>" % (','.join(_class),title)))
+        self.content.setWidth('100%')
+        self.setHeight('100%')
+        self.setStyleName('chatPanel')
+        self.add(self.content)
+        DropCell.__init__(self)
+        ClickHandler.__init__(self)
+        self.addClickListener(self)
+
+    def onClick(self, sender, event):
+        self.host.select(self)
+    
+    def setUserNick(self, nick):
+        """Set the nick of the user, usefull for e.g. change the color of the user"""
+        self.nick = nick
+    
+    def historyPrint(self, size=20):
+        """Print the initial history"""
+        def getHistoryCB(history):
+            stamps=history.keys()
+            stamps.sort()
+            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 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 
+        mymess = _jid.bare == self.host.whoami.bare #mymess = True if message comes from local user
+        """if msg.startswith('/me '):
+            self.printInfo('* %s %s' % (nick, msg[4:]),type='me')
+            return"""
+        self.content.add(ChatText(timestamp, nick, mymess, msg))
 
 class MiddlePannel(HorizontalPanel):
     
@@ -241,11 +326,13 @@
 
         menu = Menu()
         uni_box = host.uniBox
+        status = host.statusPanel
         self.middle_panel = MiddlePannel(self.host)
         self.middle_panel.setWidth('100%')
 
         self.add(menu)
         self.add(uni_box)
+        self.add(status)
         self.add(self.middle_panel)
         
         self.setCellHeight(menu, "5%")