Philipp Hörist pushed to branch master at gajim / gajim

Commits:

2 changed files:

Changes:

  • gajim/common/caps_cache.py
    ... ... @@ -108,37 +108,16 @@ def compute_caps_hash(identities, features, dataforms=None, hash_method='sha-1')
    108 108
         """
    
    109 109
         if dataforms is None:
    
    110 110
             dataforms = []
    
    111
    -    def sort_identities_func(i1, i2):
    
    112
    -        cat1 = i1['category']
    
    113
    -        cat2 = i2['category']
    
    114
    -        if cat1 < cat2:
    
    115
    -            return -1
    
    116
    -        if cat1 > cat2:
    
    117
    -            return 1
    
    118
    -        type1 = i1.get('type', '')
    
    119
    -        type2 = i2.get('type', '')
    
    120
    -        if type1 < type2:
    
    121
    -            return -1
    
    122
    -        if type1 > type2:
    
    123
    -            return 1
    
    124
    -        lang1 = i1.get('xml:lang', '')
    
    125
    -        lang2 = i2.get('xml:lang', '')
    
    126
    -        if lang1 < lang2:
    
    127
    -            return -1
    
    128
    -        if lang1 > lang2:
    
    129
    -            return 1
    
    130
    -        return 0
    
    131
    -
    
    132
    -    def sort_dataforms_func(d1, d2):
    
    133
    -        f1 = d1.getField('FORM_TYPE')
    
    134
    -        f2 = d2.getField('FORM_TYPE')
    
    135
    -        if f1 and f2 and (f1.getValue() < f2.getValue()):
    
    136
    -            return -1
    
    137
    -        return 1
    
    111
    +
    
    112
    +    def sort_identities_key(i):
    
    113
    +        return (i['category'], i.get('type', ''), i.get('xml:lang', ''))
    
    114
    +
    
    115
    +    def sort_dataforms_key(dataform):
    
    116
    +        f = dataform.getField('FORM_TYPE')
    
    117
    +        return (bool(f), f.getValue())
    
    138 118
     
    
    139 119
         S = ''
    
    140
    -    from functools import cmp_to_key
    
    141
    -    identities.sort(key=cmp_to_key(sort_identities_func))
    
    120
    +    identities.sort(key=sort_identities_key)
    
    142 121
         for i in identities:
    
    143 122
             c = i['category']
    
    144 123
             type_ = i.get('type', '')
    
    ... ... @@ -148,7 +127,7 @@ def compute_caps_hash(identities, features, dataforms=None, hash_method='sha-1')
    148 127
         features.sort()
    
    149 128
         for f in features:
    
    150 129
             S += '%s<' % f
    
    151
    -    dataforms.sort(key=cmp_to_key(sort_dataforms_func))
    
    130
    +    dataforms.sort(key=sort_dataforms_key)
    
    152 131
         for dataform in dataforms:
    
    153 132
             # fields indexed by var
    
    154 133
             fields = {}
    

  • gajim/common/contacts.py
    ... ... @@ -28,8 +28,6 @@
    28 28
     ## along with Gajim. If not, see <http://www.gnu.org/licenses/>.
    
    29 29
     ##
    
    30 30
     
    
    31
    -from functools import cmp_to_key
    
    32
    -
    
    33 31
     try:
    
    34 32
         from gajim.common import caps_cache
    
    35 33
         from gajim.common.account import Account
    
    ... ... @@ -788,88 +786,29 @@ class MetacontactManager():
    788 786
                         answers.append(data)
    
    789 787
             return answers
    
    790 788
     
    
    791
    -    def _compare_metacontacts(self, data1, data2):
    
    789
    +    def _metacontact_key(self, data):
    
    792 790
             """
    
    793
    -        Compare 2 metacontacts
    
    794
    -
    
    795 791
             Data is {'jid': jid, 'account': account, 'order': order} order is
    
    796 792
             optional
    
    797 793
             """
    
    798
    -        jid1 = data1['jid']
    
    799
    -        jid2 = data2['jid']
    
    800
    -        account1 = data1['account']
    
    801
    -        account2 = data2['account']
    
    802
    -        contact1 = self._contacts.get_contact_with_highest_priority(account1, jid1)
    
    803
    -        contact2 = self._contacts.get_contact_with_highest_priority(account2, jid2)
    
    804 794
             show_list = ['not in roster', 'error', 'offline', 'invisible', 'dnd',
    
    805
    -                'xa', 'away', 'chat', 'online', 'requested', 'message']
    
    795
    +                     'xa', 'away', 'chat', 'online', 'requested', 'message']
    
    796
    +
    
    797
    +        jid = data['jid']
    
    798
    +        account = data['account']
    
    806 799
             # contact can be null when a jid listed in the metacontact data
    
    807 800
             # is not in our roster
    
    808
    -        if not contact1:
    
    809
    -            if contact2:
    
    810
    -                return -1 # prefer the known contact
    
    811
    -            else:
    
    812
    -                show1 = 0
    
    813
    -                priority1 = 0
    
    814
    -        else:
    
    815
    -            show1 = show_list.index(contact1.show)
    
    816
    -            priority1 = contact1.priority
    
    817
    -        if not contact2:
    
    818
    -            if contact1:
    
    819
    -                return 1 # prefer the known contact
    
    820
    -            else:
    
    821
    -                show2 = 0
    
    822
    -                priority2 = 0
    
    823
    -        else:
    
    824
    -            show2 = show_list.index(contact2.show)
    
    825
    -            priority2 = contact2.priority
    
    826
    -        # If only one is offline, it's always second
    
    827
    -        if show1 > 2 and show2 < 3:
    
    828
    -            return 1
    
    829
    -        if show2 > 2 and show1 < 3:
    
    830
    -            return -1
    
    831
    -        if 'order' in data1 and 'order' in data2:
    
    832
    -            if data1['order'] > data2['order']:
    
    833
    -                return 1
    
    834
    -            if data1['order'] < data2['order']:
    
    835
    -                return -1
    
    836
    -        if 'order' in data1:
    
    837
    -            return 1
    
    838
    -        if 'order' in data2:
    
    839
    -            return -1
    
    840
    -        transport1 = common.app.get_transport_name_from_jid(jid1)
    
    841
    -        transport2 = common.app.get_transport_name_from_jid(jid2)
    
    842
    -        if transport2 and not transport1:
    
    843
    -            return 1
    
    844
    -        if transport1 and not transport2:
    
    845
    -            return -1
    
    846
    -        if show1 > show2:
    
    847
    -            return 1
    
    848
    -        if show2 > show1:
    
    849
    -            return -1
    
    850
    -        if priority1 > priority2:
    
    851
    -            return 1
    
    852
    -        if priority2 > priority1:
    
    853
    -            return -1
    
    854
    -        server1 = common.app.get_server_from_jid(jid1)
    
    855
    -        server2 = common.app.get_server_from_jid(jid2)
    
    856
    -        myserver1 = common.app.config.get_per('accounts', account1, 'hostname')
    
    857
    -        myserver2 = common.app.config.get_per('accounts', account2, 'hostname')
    
    858
    -        if server1 == myserver1:
    
    859
    -            if server2 != myserver2:
    
    860
    -                return 1
    
    861
    -        elif server2 == myserver2:
    
    862
    -            return -1
    
    863
    -        if jid1 > jid2:
    
    864
    -            return 1
    
    865
    -        if jid2 > jid1:
    
    866
    -            return -1
    
    867
    -        # If all is the same, compare accounts, they can't be the same
    
    868
    -        if account1 > account2:
    
    869
    -            return 1
    
    870
    -        if account2 > account1:
    
    871
    -            return -1
    
    872
    -        return 0
    
    801
    +        contact = self._contacts.get_contact_with_highest_priority(
    
    802
    +            account, jid)
    
    803
    +        show = show_list.index(contact.show) if contact else 0
    
    804
    +        priority = contact.priority if contact else 0
    
    805
    +        has_order = 'order' in data
    
    806
    +        order = data.get('order', 0)
    
    807
    +        transport = common.app.get_transport_name_from_jid(jid)
    
    808
    +        server = common.app.get_server_from_jid(jid)
    
    809
    +        myserver = common.app.config.get_per('accounts', account, 'hostname')
    
    810
    +        return (bool(contact), show > 2, has_order, order, bool(transport),
    
    811
    +                show, priority, server == myserver, jid, account)
    
    873 812
     
    
    874 813
         def get_nearby_family_and_big_brother(self, family, account):
    
    875 814
             """
    
    ... ... @@ -901,8 +840,7 @@ class MetacontactManager():
    901 840
             Which of the family will be the big brother under wich all others will be
    
    902 841
             ?
    
    903 842
             """
    
    904
    -        family.sort(key=cmp_to_key(self._compare_metacontacts))
    
    905
    -        return family[-1]
    
    843
    +        return max(family, key=self._metacontact_key)
    
    906 844
     
    
    907 845
     
    
    908 846
     if __name__ == "__main__":