Changeset 9043

Show
Ignore:
Timestamp:
11/23/07 20:22:27 (13 months ago)
Author:
asterix
Message:

Compute metacontact big brother by comparing them instead of computing an absolute score for each one

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • trunk/src/common/contacts.py

    r8927 r9043  
    421421                return answers 
    422422 
    423         def _get_data_score(self, data): 
    424                 '''compute thescore of a gived data 
    425                 data is {'jid': jid, 'account': account, 'order': order} 
    426                 order is optional 
    427                 score = (max_order - order)*10000 + is_jabber*priority*10 + status''' 
    428                 jid = data['jid'] 
    429                 account = data['account'] 
    430                 max_order = 0 
    431                 order = 0 
    432                 if data.has_key('order'): 
    433                         order = data['order'] 
    434                 if order: 
    435                         family = self.get_metacontacts_family(account, jid) 
    436                         for data_ in family: 
    437                                 if data_.has_key('order') and data_['order'] > max_order: 
    438                                         max_order = data_['order'] 
    439                 score = (max_order - order)*10000 
    440  
    441                 contact = self.get_contact_with_highest_priority(account, jid) 
    442                 if not contact: 
    443                         return score 
    444  
    445                 if common.gajim.get_transport_name_from_jid(jid) is None and \ 
    446                 contact.show not in ('error', 'offline'): 
    447                         score += 10 
    448                         if contact.priority > 0: 
    449                                 score += contact.priority * 10 
    450                 score += ['not in roster', 'error', 'offline', 'invisible', 'dnd', 'xa', 
    451                 'away', 'chat', 'online', 'requested', 'message'].index(contact.show) 
    452                 if contact.show == 'offline' and contact.status: 
    453                         # Offline contacts with a status message have highest score 
    454                         score += 1 
    455                 return score 
     423        def compare_metacontacts(self, data1, data2): 
     424                '''compare 2 metacontacts. 
     425                Data is {'jid': jid, 'account': account, 'order': order} 
     426                order is optional''' 
     427                if 'order' in data1 and 'order' in data2: 
     428                        if data1['order'] > data2['order']: 
     429                                return 1 
     430                        if data1['order'] < data2['order']: 
     431                                return -1 
     432                jid1 = data1['jid'] 
     433                jid2 = data2['jid'] 
     434                transport1 = common.gajim.get_transport_name_from_jid(jid1) 
     435                transport2 = common.gajim.get_transport_name_from_jid(jid2) 
     436                if transport2 and not transport1: 
     437                        return 1 
     438                if transport1 and not transport2: 
     439                        return -1 
     440                contact1 = self.get_contact_with_highest_priority(data1['account'], jid1) 
     441                contact2 = self.get_contact_with_highest_priority(data2['account'], jid2) 
     442                if contact1.priority > contact2.priority: 
     443                        return 1 
     444                if contact2.priority > contact1.priority: 
     445                        return -1 
     446                show_list = ['not in roster', 'error', 'offline', 'invisible', 'dnd', 
     447                        'xa', 'away', 'chat', 'online', 'requested', 'message'] 
     448                show1 = show_list.index(contact1.show) 
     449                show2 = show_list.index(contact2.show) 
     450                if show1 > show2: 
     451                        return 1 
     452                if show2 > show1: 
     453                        return -1 
     454                if jid1 > jid2: 
     455                        return 1 
     456                if jid2 > jid1: 
     457                        return -1 
     458                # If all is the same, compare accounts, they can't be the same 
     459                account1 = data1['account'] 
     460                account2 = data2['account'] 
     461                if account1 > account2: 
     462                        return 1 
     463                if account2 > account1: 
     464                        return -1 
     465                return 0 
    456466 
    457467        def get_metacontacts_big_brother(self, family): 
    458468                '''which of the family will be the big brother under wich all 
    459469                others will be ?''' 
    460                 max_score = 0 
    461                 max_data = family[0] 
    462                 for data in family: 
    463                         score = self._get_data_score(data) 
    464                         if score > max_score: 
    465                                 max_score = score 
    466                                 max_data = data 
    467                 return max_data 
     470                family.sort(cmp=self.compare_metacontacts) 
     471                return family[-1] 
    468472 
    469473        def is_pm_from_jid(self, account, jid):