diff frontends/src/quick_frontend/quick_app.py @ 917:a9401694d2dc

bridge, frontends: display presence with the highest priority + reset your own presence when you (dis)connect
author souliane <souliane@mailoo.org>
date Thu, 20 Mar 2014 21:18:18 +0100
parents cd02f5ef30df
children 71926ec2114d
line wrap: on
line diff
--- a/frontends/src/quick_frontend/quick_app.py	Fri Mar 21 16:27:09 2014 +0100
+++ b/frontends/src/quick_frontend/quick_app.py	Thu Mar 20 21:18:18 2014 +0100
@@ -179,10 +179,10 @@
                     priority = presences[contact][res][1]
                     statuses = presences[contact][res][2]
                     self.presenceUpdate(jabber_id, show, priority, statuses, profile)
-                    data = self.bridge.getEntityData(contact, ['avatar', 'nick'], profile)
-                    for key in ('avatar', 'nick'):
-                        if key in data:
-                            self.entityDataUpdated(contact, key, data[key], profile)
+                data = self.bridge.getEntityData(contact, ['avatar', 'nick'], profile)
+                for key in ('avatar', 'nick'):
+                    if key in data:
+                        self.entityDataUpdated(contact, key, data[key], profile)
 
             #The waiting subscription requests
             waitingSub = self.bridge.getWaitingSub(profile)
@@ -313,7 +313,7 @@
                 self.setStatusOnline(True, show, statuses)
             return
 
-        self.contact_list.updatePresence(from_jid, show, priority, statuses)
+        presences = self.profiles[profile].setdefault('presences', {})
 
         if show != 'unavailable':
 
@@ -321,13 +321,32 @@
             if from_jid.bare in self.profiles[profile]['watched'] and not from_jid.bare in self.profiles[profile]['onlineContact']:
                 self.showAlert(_("Watched jid [%s] is connected !") % from_jid.bare)
 
+            presences[jabber_id] = {'show': show, 'priority': priority, 'statuses': statuses}
             self.profiles[profile]['onlineContact'].add(from_jid)  # FIXME onlineContact is useless with CM, must be removed
 
             #TODO: vcard data (avatar)
 
         if show == "unavailable" and from_jid in self.profiles[profile]['onlineContact']:
+            try:
+                del presences[jabber_id]
+            except KeyError:
+                pass
             self.profiles[profile]['onlineContact'].remove(from_jid)
 
+        # check if the contact is connected with another resource, use the one with highest priority
+        jids = [jid for jid in presences if JID(jid).bare == from_jid.bare]
+        if jids:
+            max_jid = max(jids, key=lambda jid: presences[jid]['priority'])
+            data = presences[max_jid]
+            max_priority = data['priority']
+            if show == "unavailable":  # do not check the priority here, because 'unavailable' has a dummy one
+                from_jid = JID(max_jid)
+                show, priority, statuses = data['show'], data['priority'], data['statuses']
+        if not jids or priority >= max_priority:
+            # case 1: not jids means all resources are disconnected, send the 'unavailable' presence
+            # case 2: update (or confirm) with the values of the resource which takes precedence
+            self.contact_list.updatePresence(from_jid, show, priority, statuses)
+
     def roomJoined(self, room_jid, room_nicks, user_nick, profile):
         """Called when a MUC room is joined"""
         if not self.check_profile(profile):