diff frontends/src/quick_frontend/quick_chat.py @ 2067:7834743705f0

quich frontend, primivius (chat): better avatar handling: - new getAvatar method in QuickApp which request vCard if avatar if needed and return default avatar is no avatar is found - frontends based on quick frontend can now specify if they handle avatar or not using AVATARS_HANDLER - when avatar is updated, occupant widget(s) and all message widget(s) of the concerned entity are updated
author Goffi <goffi@goffi.org>
date Fri, 09 Sep 2016 23:54:33 +0200
parents 09c18fcd8225
children 4f3ebf786fbc
line wrap: on
line diff
--- a/frontends/src/quick_frontend/quick_chat.py	Fri Sep 09 23:54:33 2016 +0200
+++ b/frontends/src/quick_frontend/quick_chat.py	Fri Sep 09 23:54:33 2016 +0200
@@ -117,8 +117,7 @@
 
     @property
     def avatar(self):
-        contact_list = self.host.contact_lists[self.profile]
-        return contact_list.getCache(self.from_jid, "avatar") or self.host.getDefaultAvatar(self.from_jid)
+        return self.host.getAvatar(self.from_jid, self.profile)
 
     def getNick(self, entity):
         """Return nick of an entity when possible"""
@@ -167,7 +166,6 @@
                 self.message[lang] = u"* " + nick + mess[3:]
 
 
-
 class Occupant(object):
     """Occupant metadata"""
 
@@ -216,6 +214,10 @@
         for w in self.widgets:
             w.updated(["state"])
 
+    def update(self, key=None):
+        for w in self.widgets:
+            w.update(key)
+
 
 class QuickChat(quick_widgets.QuickWidget):
     visible_states = ['chat_state']  # FIXME: to be removed, used only in quick_games
@@ -249,6 +251,8 @@
         self.subject = subject
         lt = time.localtime()
         self.day_change = (lt.tm_year, lt.tm_mon, lt.tm_mday, 0, 0, 0, lt.tm_wday, lt.tm_yday, lt.tm_isdst)  # struct_time of day changing time
+        if self.host.AVATARS_HANDLER:
+            self.host.addListener('avatar', self.onAvatar, profiles)
 
     def postInit(self):
         """Method to be called by frontend after widget is initialised
@@ -259,6 +263,10 @@
         if self.subject is not None:
             self.setSubject(self.subject)
 
+    def onDelete(self):
+        if self.host.AVATARS_HANDLER:
+            self.host.removeListener('avatar', self.onAvatar)
+
     @property
     def contact_list(self):
         return self.host.contact_lists[self.profile]
@@ -539,6 +547,7 @@
 
         @param entity (jid.JID): entity to update
         """
+        # FIXME: to remove ?
         raise NotImplementedError
 
     ## events ##
@@ -561,5 +570,18 @@
         else:
             mess_data.status = status
 
+    def onAvatar(self, entity, filename, profile):
+        if self.type == C.CHAT_GROUP:
+            if entity.bare == self.target and entity.resource in self.occupants:
+                self.occupants[entity.resource].update('avatar')
+                for m in self.messages.values():
+                    if m.nick == entity.resource:
+                        for w in m.widgets:
+                            w.update('avatar', filename)
+        else:
+            if entity.bare == self.target.bare:
+                log.info("entity avatar updated")
+                # TODO: call a specific method
+
 
 quick_widgets.register(QuickChat)