diff libervia.py @ 119:a8d11fdea090

microblog avatar update
author Goffi <goffi@goffi.org>
date Wed, 06 Jul 2011 03:03:07 +0200
parents aff34642616b
children 397a88b340f3
line wrap: on
line diff
--- a/libervia.py	Wed Jul 06 03:01:55 2011 +0200
+++ b/libervia.py	Wed Jul 06 03:03:07 2011 +0200
@@ -78,7 +78,7 @@
         LiberviaJsonProxy.__init__(self, "/json_api",
                         ["getContacts", "addContact", "sendMessage", "sendMblog", "getMblogNodes", "getProfileJid", "getHistory", "getPresenceStatus",
                          "joinMUC", "getRoomJoined", "launchTarotGame", "getTarotCardsPaths", "tarotGameReady", "tarotGameContratChoosed",
-                         "tarotGamePlayCards", "getWaitingSub", "subscription", "delContact", "updateContact"])
+                         "tarotGamePlayCards", "getWaitingSub", "subscription", "delContact", "updateContact", "getCardCache"])
 
 class BridgeSignals(LiberviaJsonProxy):
     def __init__(self, host):
@@ -119,7 +119,7 @@
         self.libervia_widgets = set() #keep track of all actives LiberviaWidgets
         self.room_list = set() #set of rooms 
         self.mblog_cache = [] #used to keep blog entries in memory, to show them in new mblog panel
-        self.avatars_cache = {} #keep track of jid's avatar hash (key=jid, value=hash)
+        self.avatars_cache = {} #keep track of jid's avatar hash (key=jid, value=file)
         #self.discuss_panel.addWidget(panels.EmptyPanel(self))
         self.discuss_panel.addWidget(panels.MicroblogPanel(self, accept_all=True))
         #self.discuss_panel.addWidget(panels.EmptyPanel(self))
@@ -140,6 +140,17 @@
             event.preventDefault()
         return True
 
+    def getAvatar(self, jid_str):
+        """Return avatar of a jid if in cache, else ask for it"""
+        def cacheCardResult(result):
+            if result.has_key('avatar'):
+                self._updatedValueCb("card_avatar", {'jid':jid_str, 'avatar':result['avatar']})
+        
+        if jid_str not in self.avatars_cache:
+            self.bridge.call('getCardCache', cacheCardResult, jid_str)
+            self.avatars_cache[jid_str] = "/media/misc/empty_avatar"
+        return self.avatars_cache[jid_str]
+
     def registerWidget(self, wid):
         print "Registering", wid
         self.libervia_widgets.add(wid)
@@ -236,6 +247,8 @@
             self._contactDeletedCb(*args)
         elif name == 'newContact':
             self._newContactCb(*args)
+        elif name == 'updatedValue':
+            self._updatedValueCb(*args)
 
     def _getProfileJidCB(self, jid):
         self.whoami = JID(jid)
@@ -381,6 +394,24 @@
     def _newContactCb(self, contact, attributes, groups):
         self.contact_panel.updateContact(contact, attributes, groups)
 
+    def _updatedValueCb(self, name, value):
+        if name == "card_avatar":
+            try:
+                jid = value['jid']
+                avatar = '/avatars/%s' % value['avatar']
+            except:
+                print ("ERROR: can't get avatar value")
+                return
+
+            self.avatars_cache[jid] = avatar
+
+            for lib_wid in self.libervia_widgets:
+                if isinstance(lib_wid, panels.MicroblogPanel):
+                    if lib_wid.isJidAccepted(jid) or (self.whoami and jid == self.whoami.bare):
+                        lib_wid.updateValue('avatar', jid, avatar)
+                        
+
+
 if __name__ == '__main__':
     pyjd.setup("http://localhost:8080/libervia.html")
     app = SatWebFrontend()