diff libervia.py @ 54:f25c4077f6b9

addind contact + subscription management + misc - removed bad html_sanitize (not needed in Label !) - added isContactInRoster method in ContactPanel
author Goffi <goffi@goffi.org>
date Sat, 28 May 2011 20:18:14 +0200
parents 72c51a4839cc
children d5266c41ca24
line wrap: on
line diff
--- a/libervia.py	Sat May 28 20:14:38 2011 +0200
+++ b/libervia.py	Sat May 28 20:18:14 2011 +0200
@@ -27,8 +27,9 @@
 from pyjamas.JSONService import JSONProxy
 from browser_side.register import RegisterPanel, RegisterBox
 from browser_side.contact import ContactPanel
-from browser_side import panels
+from browser_side import panels, dialog
 from browser_side.jid import JID
+from browser_side.tools import html_sanitize
 
 class LiberviaJsonProxy(JSONProxy):
     def __init__(self, *args, **kwargs):
@@ -65,9 +66,9 @@
 class BridgeCall(LiberviaJsonProxy):
     def __init__(self):
         LiberviaJsonProxy.__init__(self, "/json_api",
-                        ["getContacts", "sendMessage", "sendMblog", "getMblogNodes", "getProfileJid", "getHistory", "getPresenceStatus",
+                        ["getContacts", "addContact", "sendMessage", "sendMblog", "getMblogNodes", "getProfileJid", "getHistory", "getPresenceStatus",
                          "joinMUC", "getRoomJoined", "launchTarotGame", "getTarotCardsPaths", "tarotGameReady", "tarotGameContratChoosed",
-                         "tarotGamePlayCards"])
+                         "tarotGamePlayCards", "getWaitingSub", "subscription"])
 
 class BridgeSignals(LiberviaJsonProxy):
     def __init__(self):
@@ -154,6 +155,7 @@
         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:
@@ -187,13 +189,17 @@
              name == 'tarotGameYourTurn' or \
              name == 'tarotGameScore':
             self._tarotGameGenericCb(name, args[0], args[1:])
+        elif name == 'subscribe':
+            self._subscribeCb(*args)
 
     def _getProfileJidCB(self, jid):
         self.whoami = JID(jid)
         #we can now ask our status
-        self.bridge.call('getPresenceStatus', self._getPresenceStatusCB)
-        #and the rooms where we are
-        self.bridge.call('getRoomJoined', self._getRoomJoinedCB)
+        self.bridge.call('getPresenceStatus', self._getPresenceStatusCb)
+        #the rooms where we are
+        self.bridge.call('getRoomJoined', self._getRoomJoinedCb)
+        #and if there is any subscription request waiting for us
+        self.bridge.call('getWaitingSub', self._getWaitingSubCb)
 
     ## Signals callbacks ##
 
@@ -261,16 +267,43 @@
             if isinstance(panel,panels.ChatPanel) and panel.type == 'group' and panel.target.bare == room_jid:
                 getattr(panel.getGame("Tarot"), event_name)(*args) 
 
-    def _getPresenceStatusCB(self, presence_data):
+    def _getPresenceStatusCb(self, presence_data):
         for entity in presence_data:
             for resource in presence_data[entity]:
                 args = presence_data[entity][resource]
                 self._presenceUpdateCb("%s/%s" % (entity, resource), *args)
 
-    def _getRoomJoinedCB(self, room_data):
+    def _getRoomJoinedCb(self, room_data):
         for room in room_data:
             self._roomJoinedCb(*room)
 
+    def _getWaitingSubCb(self, waiting_sub):
+        for sub in waiting_sub:
+            self._subscribeCb(waiting_sub[sub], sub)
+
+    def _subscribeCb(self, sub_type, entity):
+        if sub_type == 'subscribed':
+            dialog.SimpleDialog('Subscription confirmation', 'The contact <b>%s</b> has added you to his/her contact list' % html_sanitize(entity)).show()
+
+        elif sub_type == 'unsubscribed':
+            dialog.SimpleDialog('Subscription refusal', 'The contact <b>%s</b> has refused to add you in his/her contact list' % html_sanitize(entity)).show()
+
+        elif sub_type == 'subscribe':
+            #The user want to subscribe to our presence
+            _dialog = None
+            msg = HTML('The contact <b>%s</b> want to add you in his/her contact list, do you accept ?' % html_sanitize(entity))
+            
+            def ok_cb():
+                self.bridge.call('subscription', None, "subscribed", entity, '', _dialog.getSelectedGroups())
+            def cancel_cb():
+                self.bridge.call('subscription', None, "unsubscribed", entity, '', '')
+            
+            _dialog = dialog.GroupSelector([msg], self.contact_panel.getGroups(), [], ok_cb, cancel_cb)
+            _dialog.setHTML('<b>Add contact request</b>')
+            _dialog.show()
+
+            
+
 if __name__ == '__main__':
     pyjd.setup("http://localhost:8080/libervia.html")
     app = SatWebFrontend()