Show
Ignore:
Timestamp:
12/12/07 09:44:46 (12 months ago)
Author:
asterix
Message:

merge diff from trunk

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • branches/pep/src/common/contacts.py

    r8478 r9123  
    11## common/contacts.py 
    22## 
    3 ## Copyright (C) 2006 Yann Le Boulanger <asterix@lagaule.org> 
    4 ## Copyright (C) 2006 Nikos Kouremenos <kourem@gmail.com> 
     3## Copyright (C) 2006 Yann Leboulanger <asterix@lagaule.org> 
     4##                   Nikos Kouremenos <kourem@gmail.com> 
    55## Copyright (C) 2007 Lukas Petrovicky <lukas@petrovicky.net> 
    6 ## Copyright (C) 2007 Julien Pivotto <roidelapluie@gmail.com> 
     6##                    Julien Pivotto <roidelapluie@gmail.com> 
     7##                    Stephan Erb <steve-e@h3c.de>  
    78## 
     9## This file is part of Gajim. 
    810## 
    9 ## This program is free software; you can redistribute it and/or modify 
     11## Gajim is free software; you can redistribute it and/or modify 
    1012## it under the terms of the GNU General Public License as published 
    11 ## by the Free Software Foundation; version 2 only. 
     13## by the Free Software Foundation; version 3 only. 
    1214## 
    13 ## This program is distributed in the hope that it will be useful, 
     15## Gajim is distributed in the hope that it will be useful, 
    1416## but WITHOUT ANY WARRANTY; without even the implied warranty of 
    1517## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
    1618## GNU General Public License for more details. 
     19## 
     20## You should have received a copy of the GNU General Public License 
     21## along with Gajim.  If not, see <http://www.gnu.org/licenses/>. 
    1722## 
    1823 
     
    4247                # Capabilities; filled by caps.py/ConnectionCaps object 
    4348                # every time it gets these from presence stanzas 
    44                 self.caps_node=None 
    45                 self.caps_ver=None 
    46                 self.caps_exts=None 
     49                self.caps_node = None 
     50                self.caps_ver = None 
     51                self.caps_exts = None 
    4752 
    4853                # please read jep-85 http://www.jabber.org/jeps/jep-0085.html 
     
    206211                if len(self._contacts[account][contact.jid]) == 0: 
    207212                        del self._contacts[account][contact.jid] 
     213 
     214        def clear_contacts(self, account): 
     215                self._contacts[account] = {} 
    208216 
    209217        def remove_jid(self, account, jid): 
     
    282290                        our_jid = common.gajim.get_jid_from_account(account) 
    283291                        for jid in self.get_jid_list(account): 
     292                                if self.has_brother(account, jid) and not \ 
     293                                        self.is_big_brother(account, jid): 
     294                                        # count metacontacts only once 
     295                                        continue 
    284296                                if jid == our_jid: 
    285297                                        continue 
     
    301313                                                if common.gajim.jid_is_transport(jid): 
    302314                                                        contact_groups = [_('Transports')] 
     315                                                if contact.is_observer(): 
     316                                                        contact_groups = [_('Observers')] 
    303317                                                else: 
    304318                                                        contact_groups = [_('General')] 
     
    363377                                        break 
    364378                        if found: 
    365                                 self._metacontacts_tags[account][tag].remove(data) 
     379                                self._metacontacts_tags[account][tag].remove(found) 
    366380                                break 
    367381                common.gajim.connections[account].store_metacontacts( 
     
    372386                        tag = self.get_metacontacts_tag(account, jid) 
    373387                        if tag and len(self._metacontacts_tags[account][tag]) > 1: 
     388                                return True 
     389                return False 
     390 
     391        def is_big_brother(self, account, jid): 
     392                tag = self.get_metacontacts_tag(account, jid) 
     393                if tag: 
     394                        family = self.get_metacontacts_family(account, jid) 
     395                        bb_data = self.get_metacontacts_big_brother(family) 
     396                        bb_jid = bb_data['jid'] 
     397                        bb_account = bb_data['account'] 
     398                        if bb_jid == jid and bb_account == account:  
    374399                                return True 
    375400                return False 
     
    400425                return answers 
    401426 
    402         def _get_data_score(self, data): 
    403                 '''compute thescore of a gived data 
    404                 data is {'jid': jid, 'account': account, 'order': order} 
    405                 order is optional 
    406                 score = (max_order - order)*10000 + is_jabber*priority*10 + status''' 
    407                 jid = data['jid'] 
    408                 account = data['account'] 
    409                 max_order = 0 
    410                 order = 0 
    411                 if data.has_key('order'): 
    412                         order = data['order'] 
    413                 if order: 
    414                         family = self.get_metacontacts_family(account, jid) 
    415                         for data_ in family: 
    416                                 if data_.has_key('order') and data_['order'] > max_order: 
    417                                         max_order = data_['order'] 
    418                 contact = self.get_contact_with_highest_priority(account, jid) 
    419                 score = (max_order - order)*10000 
    420                  
    421                 if common.gajim.get_transport_name_from_jid(jid) is None and \ 
    422                 contact.show not in ('error', 'offline'): 
    423                         score += 10 
    424                         if contact.priority > 0: 
    425                                 score += contact.priority * 10 
    426                 score += ['not in roster', 'error', 'offline', 'invisible', 'dnd', 'xa', 
    427                 'away', 'chat', 'online', 'requested', 'message'].index(contact.show) 
    428                 if contact.show == 'offline' and contact.status: 
    429                         # Offline contacts with a status message have highest score 
    430                         score += 1 
    431                 return score 
     427        def compare_metacontacts(self, data1, data2): 
     428                '''compare 2 metacontacts. 
     429                Data is {'jid': jid, 'account': account, 'order': order} 
     430                order is optional''' 
     431                jid1 = data1['jid'] 
     432                jid2 = data2['jid'] 
     433                account1 = data1['account'] 
     434                account2 = data2['account'] 
     435                contact1 = self.get_contact_with_highest_priority(account1, jid1) 
     436                contact2 = self.get_contact_with_highest_priority(account2, jid2) 
     437                show_list = ['not in roster', 'error', 'offline', 'invisible', 'dnd', 
     438                        'xa', 'away', 'chat', 'online', 'requested', 'message'] 
     439                # contact can be null when we fill the roster on connection 
     440                if not contact1: 
     441                        show1 = 0 
     442                        priority1 = 0 
     443                else: 
     444                        show1 = show_list.index(contact1.show) 
     445                        priority1 = contact1.priority 
     446                if not contact2: 
     447                        show2 = 0 
     448                        priority2 = 0 
     449                else: 
     450                        show2 = show_list.index(contact2.show) 
     451                        priority2 = contact2.priority 
     452                # If only one is offline, it's always second 
     453                if show1 > 2 and show2 < 3: 
     454                        return 1 
     455                if show2 > 2 and show1 < 3: 
     456                        return -1 
     457                if 'order' in data1 and 'order' in data2: 
     458                        if data1['order'] > data2['order']: 
     459                                return 1 
     460                        if data1['order'] < data2['order']: 
     461                                return -1 
     462                transport1 = common.gajim.get_transport_name_from_jid(jid1) 
     463                transport2 = common.gajim.get_transport_name_from_jid(jid2) 
     464                if transport2 and not transport1: 
     465                        return 1 
     466                if transport1 and not transport2: 
     467                        return -1 
     468                if priority1 > priority2: 
     469                        return 1 
     470                if priority2 > priority1: 
     471                        return -1 
     472                if show1 > show2: 
     473                        return 1 
     474                if show2 > show1: 
     475                        return -1 
     476                if jid1 > jid2: 
     477                        return 1 
     478                if jid2 > jid1: 
     479                        return -1 
     480                # If all is the same, compare accounts, they can't be the same 
     481                if account1 > account2: 
     482                        return 1 
     483                if account2 > account1: 
     484                        return -1 
     485                return 0 
    432486 
    433487        def get_metacontacts_big_brother(self, family): 
    434488                '''which of the family will be the big brother under wich all 
    435489                others will be ?''' 
    436                 max_score = 0 
    437                 max_data = family[0] 
    438                 for data in family: 
    439                         score = self._get_data_score(data) 
    440                         if score > max_score: 
    441                                 max_score = score 
    442                                 max_data = data 
    443                 return max_data 
     490                family.sort(cmp=self.compare_metacontacts) 
     491                return family[-1] 
    444492 
    445493        def is_pm_from_jid(self, account, jid):