changeset 119:a8d11fdea090

microblog avatar update
author Goffi <goffi@goffi.org>
date Wed, 06 Jul 2011 03:03:07 +0200
parents a2a21e0290dc
children 054b7b3424a3
files browser_side/panels.py libervia.py
diffstat 2 files changed, 59 insertions(+), 10 deletions(-) [+]
line wrap: on
line diff
--- a/browser_side/panels.py	Wed Jul 06 03:01:55 2011 +0200
+++ b/browser_side/panels.py	Wed Jul 06 03:03:07 2011 +0200
@@ -474,24 +474,32 @@
 
 class MicroblogEntry(SimplePanel):
 
-    def __init__(self, mblog_entry):
+    def __init__(self, host, mblog_entry):
         SimplePanel.__init__(self)
 
+        self.author = mblog_entry.author
         _datetime = datetime.fromtimestamp(mblog_entry.timestamp)
 
-        panel = HTMLPanel("""
-            <div class="mb_entry_avatar"><img src="%(avatar)s" alt="%(author)s" /></div>
+        self.panel = HTMLPanel("""
+            <div class="mb_entry_avatar" id='id_avatar'></div>
             <div class="mb_entry_dialog">
                 <p class="bubble">%(body)s</p>
             </div>
             <div class='mb_entry_timestamp'>%(timestamp)s</div>
-            """ % {"avatar": "/media/misc/empty_avatar", 
-                   "author": html_sanitize(mblog_entry.author),
+            """ % {"author": html_sanitize(self.author),
                    "timestamp": _datetime,
                    "body": html_sanitize(mblog_entry.content)
                     })
-        panel.setStyleName('mb_entry')
-        self.add(panel)
+        self.avatar = Image(host.getAvatar(self.author))
+        self.panel.add(self.avatar, "id_avatar")
+        self.panel.setStyleName('mb_entry')
+        self.add(self.panel)
+
+    def updateAvatar(self, new_avatar):
+        """Change the avatar of the entry
+        @param new_avatar: path to the new image"""
+        self.avatar.setUrl(new_avatar)
+
 
 class MicroblogPanel(LiberviaWidget):
 
@@ -517,10 +525,20 @@
         @param date: when the entry was written"""
         if mblog_entry.id in self.entries:
             return
-        _entry = MicroblogEntry(mblog_entry)
+        _entry = MicroblogEntry(self.host, mblog_entry)
         self.entries[mblog_entry.id] = _entry
         self.vpanel.insert(_entry,0)
 
+    def updateValue(self, type, jid, value):
+        """Update a jid value in entries
+        @param type: one of 'avatar', 'nick'
+        @param jid: jid concerned
+        @param value: new value"""
+        if type=='avatar':
+            for entry in self.entries.values():
+                if entry.author == jid:
+                    entry.updateAvatar(value)
+
     def setAcceptedGroup(self, group):
         """Set the group which can be displayed in this panel
         @param group: string of the group, or list of string
--- 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()