comparison 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
comparison
equal deleted inserted replaced
53:dc7861390f10 54:f25c4077f6b9
25 from pyjamas.ui.KeyboardListener import KEY_ESCAPE 25 from pyjamas.ui.KeyboardListener import KEY_ESCAPE
26 from pyjamas import Window, DOM 26 from pyjamas import Window, DOM
27 from pyjamas.JSONService import JSONProxy 27 from pyjamas.JSONService import JSONProxy
28 from browser_side.register import RegisterPanel, RegisterBox 28 from browser_side.register import RegisterPanel, RegisterBox
29 from browser_side.contact import ContactPanel 29 from browser_side.contact import ContactPanel
30 from browser_side import panels 30 from browser_side import panels, dialog
31 from browser_side.jid import JID 31 from browser_side.jid import JID
32 from browser_side.tools import html_sanitize
32 33
33 class LiberviaJsonProxy(JSONProxy): 34 class LiberviaJsonProxy(JSONProxy):
34 def __init__(self, *args, **kwargs): 35 def __init__(self, *args, **kwargs):
35 JSONProxy.__init__(self, *args, **kwargs) 36 JSONProxy.__init__(self, *args, **kwargs)
36 self.handler=self 37 self.handler=self
63 self.cb={} 64 self.cb={}
64 65
65 class BridgeCall(LiberviaJsonProxy): 66 class BridgeCall(LiberviaJsonProxy):
66 def __init__(self): 67 def __init__(self):
67 LiberviaJsonProxy.__init__(self, "/json_api", 68 LiberviaJsonProxy.__init__(self, "/json_api",
68 ["getContacts", "sendMessage", "sendMblog", "getMblogNodes", "getProfileJid", "getHistory", "getPresenceStatus", 69 ["getContacts", "addContact", "sendMessage", "sendMblog", "getMblogNodes", "getProfileJid", "getHistory", "getPresenceStatus",
69 "joinMUC", "getRoomJoined", "launchTarotGame", "getTarotCardsPaths", "tarotGameReady", "tarotGameContratChoosed", 70 "joinMUC", "getRoomJoined", "launchTarotGame", "getTarotCardsPaths", "tarotGameReady", "tarotGameContratChoosed",
70 "tarotGamePlayCards"]) 71 "tarotGamePlayCards", "getWaitingSub", "subscription"])
71 72
72 class BridgeSignals(LiberviaJsonProxy): 73 class BridgeSignals(LiberviaJsonProxy):
73 def __init__(self): 74 def __init__(self):
74 LiberviaJsonProxy.__init__(self, "/json_signal_api", 75 LiberviaJsonProxy.__init__(self, "/json_signal_api",
75 ["getSignals"]) 76 ["getSignals"])
152 #it's time to fill the page 153 #it's time to fill the page
153 self.bridge.call('getContacts', self._getContactsCB) 154 self.bridge.call('getContacts', self._getContactsCB)
154 self.bridge_signals.call('getSignals', self._getSignalsCB) 155 self.bridge_signals.call('getSignals', self._getSignalsCB)
155 #We want to know our own jid 156 #We want to know our own jid
156 self.bridge.call('getProfileJid', self._getProfileJidCB) 157 self.bridge.call('getProfileJid', self._getProfileJidCB)
158
157 159
158 def _getContactsCB(self, contacts_data): 160 def _getContactsCB(self, contacts_data):
159 for contact in contacts_data: 161 for contact in contacts_data:
160 jid, attributes, groups = contact 162 jid, attributes, groups = contact
161 self.contact_panel.addContact(jid, attributes, groups) 163 self.contact_panel.addContact(jid, attributes, groups)
185 name == 'tarotGameInvalidCards' or \ 187 name == 'tarotGameInvalidCards' or \
186 name == 'tarotGameCardsPlayed' or \ 188 name == 'tarotGameCardsPlayed' or \
187 name == 'tarotGameYourTurn' or \ 189 name == 'tarotGameYourTurn' or \
188 name == 'tarotGameScore': 190 name == 'tarotGameScore':
189 self._tarotGameGenericCb(name, args[0], args[1:]) 191 self._tarotGameGenericCb(name, args[0], args[1:])
192 elif name == 'subscribe':
193 self._subscribeCb(*args)
190 194
191 def _getProfileJidCB(self, jid): 195 def _getProfileJidCB(self, jid):
192 self.whoami = JID(jid) 196 self.whoami = JID(jid)
193 #we can now ask our status 197 #we can now ask our status
194 self.bridge.call('getPresenceStatus', self._getPresenceStatusCB) 198 self.bridge.call('getPresenceStatus', self._getPresenceStatusCb)
195 #and the rooms where we are 199 #the rooms where we are
196 self.bridge.call('getRoomJoined', self._getRoomJoinedCB) 200 self.bridge.call('getRoomJoined', self._getRoomJoinedCb)
201 #and if there is any subscription request waiting for us
202 self.bridge.call('getWaitingSub', self._getWaitingSubCb)
197 203
198 ## Signals callbacks ## 204 ## Signals callbacks ##
199 205
200 def _personalEventCb(self, sender, event_type, data, profile): 206 def _personalEventCb(self, sender, event_type, data, profile):
201 if event_type == "MICROBLOG": 207 if event_type == "MICROBLOG":
259 def _tarotGameGenericCb(self, event_name, room_jid, args): 265 def _tarotGameGenericCb(self, event_name, room_jid, args):
260 for panel in self.mpanels + self.other_panels: 266 for panel in self.mpanels + self.other_panels:
261 if isinstance(panel,panels.ChatPanel) and panel.type == 'group' and panel.target.bare == room_jid: 267 if isinstance(panel,panels.ChatPanel) and panel.type == 'group' and panel.target.bare == room_jid:
262 getattr(panel.getGame("Tarot"), event_name)(*args) 268 getattr(panel.getGame("Tarot"), event_name)(*args)
263 269
264 def _getPresenceStatusCB(self, presence_data): 270 def _getPresenceStatusCb(self, presence_data):
265 for entity in presence_data: 271 for entity in presence_data:
266 for resource in presence_data[entity]: 272 for resource in presence_data[entity]:
267 args = presence_data[entity][resource] 273 args = presence_data[entity][resource]
268 self._presenceUpdateCb("%s/%s" % (entity, resource), *args) 274 self._presenceUpdateCb("%s/%s" % (entity, resource), *args)
269 275
270 def _getRoomJoinedCB(self, room_data): 276 def _getRoomJoinedCb(self, room_data):
271 for room in room_data: 277 for room in room_data:
272 self._roomJoinedCb(*room) 278 self._roomJoinedCb(*room)
279
280 def _getWaitingSubCb(self, waiting_sub):
281 for sub in waiting_sub:
282 self._subscribeCb(waiting_sub[sub], sub)
283
284 def _subscribeCb(self, sub_type, entity):
285 if sub_type == 'subscribed':
286 dialog.SimpleDialog('Subscription confirmation', 'The contact <b>%s</b> has added you to his/her contact list' % html_sanitize(entity)).show()
287
288 elif sub_type == 'unsubscribed':
289 dialog.SimpleDialog('Subscription refusal', 'The contact <b>%s</b> has refused to add you in his/her contact list' % html_sanitize(entity)).show()
290
291 elif sub_type == 'subscribe':
292 #The user want to subscribe to our presence
293 _dialog = None
294 msg = HTML('The contact <b>%s</b> want to add you in his/her contact list, do you accept ?' % html_sanitize(entity))
295
296 def ok_cb():
297 self.bridge.call('subscription', None, "subscribed", entity, '', _dialog.getSelectedGroups())
298 def cancel_cb():
299 self.bridge.call('subscription', None, "unsubscribed", entity, '', '')
300
301 _dialog = dialog.GroupSelector([msg], self.contact_panel.getGroups(), [], ok_cb, cancel_cb)
302 _dialog.setHTML('<b>Add contact request</b>')
303 _dialog.show()
304
305
273 306
274 if __name__ == '__main__': 307 if __name__ == '__main__':
275 pyjd.setup("http://localhost:8080/libervia.html") 308 pyjd.setup("http://localhost:8080/libervia.html")
276 app = SatWebFrontend() 309 app = SatWebFrontend()
277 app.onModuleLoad() 310 app.onModuleLoad()