diff libervia.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/libervia.py	Fri Apr 15 15:30:31 2011 +0200
+++ b/libervia.py	Sat Apr 16 01:46:01 2011 +0200
@@ -28,7 +28,8 @@
 from pyjamas.ui.KeyboardListener import KEY_ENTER
 from browser_side.register import RegisterPanel, RegisterBox
 from browser_side.contact import ContactPanel
-from browser_side.panels import MainPanel, EmptyPanel, MicroblogPanel
+from browser_side.panels import MainPanel, EmptyPanel, MicroblogPanel, ChatPanel, StatusPanel
+from browser_side.jid import JID
 
 
 class LiberviaJsonProxy(JSONProxy):
@@ -66,7 +67,7 @@
 class BridgeCall(LiberviaJsonProxy):
     def __init__(self):
         LiberviaJsonProxy.__init__(self, "/json_api",
-                        ["getContacts", "sendMblog", "getMblogNodes"])
+                        ["getContacts", "sendMessage", "sendMblog", "getMblogNodes", "getProfileJid", "getHistory"])
 
 class BridgeSignals(LiberviaJsonProxy):
     def __init__(self):
@@ -84,8 +85,14 @@
         self.getCompletionItems().completions.append(key)
 
     def onKeyPress(self, sender, keycode, modifiers):
-        if keycode == KEY_ENTER and not self.visible: 
-            self.host.bridge.call('sendMblog', None, self.getText())
+        if keycode == KEY_ENTER and not self.visible:
+            _txt = self.getText()
+            if _txt:
+                if _txt.startswith('@'):
+                    self.host.bridge.call('sendMblog', None, self.getText())
+                elif isinstance(self.host.selected, ChatPanel):
+                    _chat = self.host.selected
+                    self.host.bridge.call('sendMessage', None, str(_chat.target), _txt, '', 'chat') 
             self.setText('')
 
     def complete(self):
@@ -96,10 +103,13 @@
 
 class SatWebFrontend:
     def onModuleLoad(self):
+        self.whoami = None
         self.bridge = BridgeCall()
         self.bridge_signals = BridgeSignals()
+        self.selected = None
         self.uniBox = UniBox(self)
         self.uniBox.addKey("@@: ")
+        self.statusPanel = StatusPanel()
         self.contactPanel = ContactPanel(self)
         self.panel = MainPanel(self)
         self.middle_panel = self.panel.middle_panel
@@ -112,6 +122,14 @@
         self._register = RegisterCall()
         self._register.call('isRegistered',self._isRegisteredCB)
 
+    def select(self, widget):
+        """Define the selected widget"""
+        if self.selected:
+            self.selected.removeStyleName('selected_widget')
+        self.selected = widget
+        if widget:
+            self.selected.addStyleName('selected_widget')
+
     def _isRegisteredCB(self, registered):
         if not registered:
             self._dialog = RegisterBox(self.logged,centered=True)
@@ -133,6 +151,8 @@
         #it's time to fill the page
         self.bridge.call('getContacts', self._getContactsCB)
         self.bridge_signals.call('getSignals', self._getSignalsCB)
+        #We want to know our own jid
+        self.bridge.call('getProfileJid', self._getProfileJidCB)
 
     def _getContactsCB(self, contacts_data):
         for contact in contacts_data:
@@ -146,6 +166,12 @@
         name,args = signal_data
         if name == 'personalEvent':
             self._personalEventCb(*args)
+        elif name == 'newMessage':
+            self._newMessageCb(*args)
+
+    def _getProfileJidCB(self, jid):
+        self.whoami = JID(jid)
+
 
     ## Signals callbacks ##
 
@@ -169,6 +195,13 @@
                     timestamp = float(data.get('timestamp',0)) #XXX: int doesn't work here
                     panel.addEntry(content, author, timestamp)
 
+    def _newMessageCb(self, from_jid, msg, msg_type, to_jid):
+        _from = JID(from_jid)
+        _to = JID(to_jid)
+        for panel in self.mpanels:
+            if isinstance(panel,ChatPanel) and (panel.target.bare == _from.bare or panel.target.bare == _to.bare):
+                panel.printMessage(_from, msg)
+
 if __name__ == '__main__':
     pyjd.setup("http://localhost:8080/libervia.html")
     app = SatWebFrontend()