diff libervia.py @ 1:0a7c685faa53

ContactPanel: first draft
author Goffi <goffi@goffi.org>
date Mon, 31 Jan 2011 20:31:25 +0100
parents 140cec48224a
children 669c531a857e
line wrap: on
line diff
--- a/libervia.py	Sun Jan 30 21:50:22 2011 +0100
+++ b/libervia.py	Mon Jan 31 20:31:25 2011 +0100
@@ -32,6 +32,7 @@
 from pyjamas.ui.AutoComplete import AutoCompleteTextBox
 from pyjamas.JSONService import JSONProxy
 from register import RegisterPanel, RegisterBox
+from pyjamas import DOM
 
 
 class LiberviaJsonProxy(JSONProxy):
@@ -57,8 +58,6 @@
                 Window.alert("Error %s: %s" % (errobj['message']['faultCode'], errobj['message']['faultString']))
             else:
                 Window.alert("Error: %s" % errobj['message'])
-                import pdb
-                pdb.set_trace()
 
 class RegisterCall(LiberviaJsonProxy):
     def __init__(self):
@@ -112,30 +111,127 @@
 
     def __init__(self):
         AutoCompleteTextBox.__init__(self)
-        self.setCompletionItems(['@amis: ','@famille: ', '@collègues'])
+
+    def addKey(self, key):
+        self.getCompletionItems().completions.append(key)
+
+class GroupLabel(Label):
+    def __init__(self, group):
+        Label.__init__(self, group)
+        self.group = group
+        self.setStyleName('group')
+
+class ContactLabel(Label):
+    def __init__(self, jid, name=None):
+        if not name:
+            name=jid
+        Label.__init__(self, name)
+        self.jid=jid
+        self.setStyleName('contact')
+
+class GroupList(VerticalPanel):
+
+    def __init__(self, parent):
+        VerticalPanel.__init__(self)
+        self._parent = parent
+
+    def add(self, group):
+        _item = GroupLabel(group)
+        _item.addMouseListener(self._parent)
+        DOM.setStyleAttribute(_item.getElement(), "cursor", "pointer")
+        VerticalPanel.add(self, _item)
+    
+class ContactList(VerticalPanel):
+
+    def __init__(self):
+        VerticalPanel.__init__(self)
+        self.contacts=[]
+
+    def __iter__(self):
+        return self.contacts.__iter__()
+    
+    def add(self, jid, name=None):
+        _item = ContactLabel(jid, name)
+        DOM.setStyleAttribute(_item.getElement(), "cursor", "pointer")
+        VerticalPanel.add(self, _item)
+        self.contacts.append(_item)
 
 class ContactPanel(SimplePanel):
+    """Manage the contacts and groups"""
     
-    def __init__(self):
+    def __init__(self, host):
         SimplePanel.__init__(self)
+        self.host = host
+        self.groups={}
 
+        self.vPanel = VerticalPanel()
+        _title = Label('Contacts')
+        _title.setStyleName('contactTitle')
+
+        self._contactList = ContactList()
+        self._contactList.setStyleName('contactList')
+        self._groupList = GroupList(self)
+        self._groupList.setStyleName('groupList')
+        
+        self.vPanel.add(_title)
+        self.vPanel.add(self._groupList)
+        self.vPanel.add(self._contactList)
+        self.add(self.vPanel)
+        self.setStyleName('contactBox')
+
+    def addContact(self, jid, attributes, groups):
+        """Add a contact to the panel
+        @param jid: jid
+        @attributes: cf SàT Bridge API's newContact
+        @param groups: list of groups"""
+        for group in groups:
+            if not self.groups.has_key(group):
+                self.groups[group] = set()
+                self._groupList.add(group)
+                self.host.magicBox.addKey("@%s: " % group)
+            self.groups[group].add(jid)
+        self._contactList.add(jid)
+
+    def onMouseMove(self, sender, x, y):
+        pass
+        
+    def onMouseDown(self, sender, x, y):
+        pass
+
+    def onMouseUp(self, sender, x, y):
+        pass
+
+    def onMouseEnter(self, sender):
+        if isinstance(sender, GroupLabel):
+            for contact in self._contactList:
+                if contact.jid in self.groups[sender.group]:
+                    contact.addStyleName("selected")
+    
+    def onMouseLeave(self, sender):
+        if isinstance(sender, GroupLabel):
+            for contact in self._contactList:
+                if contact.jid in self.groups[sender.group]:
+                    contact.removeStyleName("selected")
 
 class MiddlePannel(HorizontalPanel):
     
-    def __init__(self):
+    def __init__(self,host):
+        self.host=host
         HorizontalPanel.__init__(self)
+        self.add(self.host.contactPanel)
 
 class MainPanel(VerticalPanel):
 
-    def __init__(self):
+    def __init__(self, host):
+        self.host=host
         VerticalPanel.__init__(self)
 
         self.setHorizontalAlignment(HasAlignment.ALIGN_LEFT)
         self.setVerticalAlignment(HasAlignment.ALIGN_TOP)
 
         menu = Menu()
-        magic_box = MagicBox()
-        middle_panel = MiddlePannel()
+        magic_box = host.magicBox
+        middle_panel = MiddlePannel(self.host)
 
         self.add(menu)
         self.add(magic_box)
@@ -152,20 +248,22 @@
 
 class SatWebFrontend:
     def onModuleLoad(self):
-        self.panel = MainPanel()
+        self.magicBox = MagicBox()
+        self.contactPanel = ContactPanel(self)
+        self.panel = MainPanel(self)
         self._dialog = None
         RootPanel().add(self.panel)
         self._register = RegisterCall()
-        self._register.call('isRegistered',self.isRegistered)
+        self._register.call('isRegistered',self._isRegisteredCB)
 
-    def isRegistered(self, registered):
+    def _isRegisteredCB(self, registered):
         if not registered:
             self._dialog = RegisterBox(self.logged,centered=True)
             self._dialog.show()
         else:
-            self._register.call('isConnected', self.isConnected)
+            self._register.call('isConnected', self._isConnectedCB)
 
-    def isConnected(self, connected):
+    def _isConnectedCB(self, connected):
         if not connected:
             self._register.call('connect',self.logged)
         else:
@@ -175,11 +273,19 @@
         if self._dialog:
             self._dialog.hide()
             del self._dialog # don't work if self._dialog is None
-        Window.alert("Logged successfuly :o)")
+        
+        #it's time to fill the page
+        bridge = BrigeCall()
+        bridge.call('getContacts', self._getContactsCB)
+
+    def _getContactsCB(self, contacts_data):
+        for contact in contacts_data:
+            jid, attributes, groups = contact
+            self.contactPanel.addContact(jid, attributes, groups)
 
 
 if __name__ == '__main__':
-    pyjd.setup("./public/Libervia.html")
+    pyjd.setup("http://localhost:8080/libervia.html")
     app = SatWebFrontend()
     app.onModuleLoad()
     pyjd.run()