changeset 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 741db5abf077
files frontends/src/primitivus/chat.py frontends/src/primitivus/primitivus frontends/src/quick_frontend/quick_app.py frontends/src/quick_frontend/quick_chat.py
diffstat 4 files changed, 42 insertions(+), 7 deletions(-) [+]
line wrap: on
line diff
--- a/frontends/src/primitivus/chat.py	Fri Sep 09 23:54:33 2016 +0200
+++ b/frontends/src/primitivus/chat.py	Fri Sep 09 23:54:33 2016 +0200
@@ -600,7 +600,7 @@
 
     def onDelete(self):
         # FIXME: to be checked after refactoring
-        quick_chat.QuickChat.onDelete(self)
+        super(Chat, self).onDelete()
         if self.type == C.CHAT_GROUP:
             self.host.removeListener('presence', self.presenceListener)
 
--- a/frontends/src/primitivus/primitivus	Fri Sep 09 23:54:33 2016 +0200
+++ b/frontends/src/primitivus/primitivus	Fri Sep 09 23:54:33 2016 +0200
@@ -272,7 +272,8 @@
 
 
 class PrimitivusApp(QuickApp, InputHistory):
-    MB_HANDLE = False
+    MB_HANDLER = False
+    AVATAR_HANDLER = False
 
     def __init__(self):
         QuickApp.__init__(self, create_bridge=DBusBridgeFrontend, xmlui=xmlui, check_options=quick_utils.check_options)
--- a/frontends/src/quick_frontend/quick_app.py	Fri Sep 09 23:54:33 2016 +0200
+++ b/frontends/src/quick_frontend/quick_app.py	Fri Sep 09 23:54:33 2016 +0200
@@ -186,7 +186,8 @@
 
 class QuickApp(object):
     """This class contain the main methods needed for the frontend"""
-    MB_HANDLE = True # Set to false if the frontend doesn't manage microblog
+    MB_HANDLER = True  # Set to False if the frontend doesn't manage microblog
+    AVATARS_HANDLER = True  # set to False if avatars are not used
 
     def __init__(self, create_bridge, xmlui, check_options=None):
         """Create a frontend application
@@ -670,7 +671,7 @@
         """
         service_s = jid.JID(service_s)
 
-        if category == C.PS_MICROBLOG and self.MB_HANDLE:
+        if category == C.PS_MICROBLOG and self.MB_HANDLER:
             if event_type == C.PS_PUBLISH:
                 if not 'content' in data:
                     log.warning("No content found in microblog data")
@@ -831,6 +832,17 @@
 
         self.bridge.launchAction(callback_id, data, profile, callback=action_cb, errback=self.dialogFailure)
 
+    def getAvatar(self, entity, profile):
+        """return avatar path for an entity"""
+        contact_list = self.contact_lists[profile]
+        avatar = contact_list.getCache(entity, "avatar")
+        if avatar is None:
+            self.bridge.getCard(unicode(entity), profile)
+            contact_list.setCache(entity, "avatar", "")
+        if not avatar:
+            avatar = self.getDefaultAvatar(entity)
+        return avatar
+
     def getDefaultAvatar(self, entity=None):
         """return default avatar to use with given entity
 
--- 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)