X-Git-Url: http://git.maemo.org/git/?p=theonering;a=blobdiff_plain;f=src%2Fgvoice%2Faddressbook.py;h=2f6c7e28c82d34653d4f383460dca797a2cb47b0;hp=f9f713b6e42a06a71781872ee100a17e5757ce77;hb=dc433fea2a937e651729013ac30341b83176f168;hpb=cd56d4ab7a3beaa59e8ef4a5d6c2624419038fee diff --git a/src/gvoice/addressbook.py b/src/gvoice/addressbook.py index f9f713b..2f6c7e2 100644 --- a/src/gvoice/addressbook.py +++ b/src/gvoice/addressbook.py @@ -4,83 +4,80 @@ import logging import util.coroutines as coroutines +import util.misc as misc_utils -_moduleLogger = logging.getLogger("gvoice.addressbook") +_moduleLogger = logging.getLogger(__name__) class Addressbook(object): + _RESPONSE_GOOD = 0 + _RESPONSE_BLOCKED = 3 + def __init__(self, backend): self._backend = backend - self._contacts = {} - self._addedContacts = set() - self._removedContacts = set() - self._changedContacts = set() + self._numbers = {} self.updateSignalHandler = coroutines.CoTee() def update(self, force=False): - if not force and self._contacts: + if not force and self._numbers: return - oldContacts = self._contacts - oldContactIds = set(self.get_contacts()) + oldContacts = self._numbers + oldContactNumbers = set(self.get_numbers()) - self._contacts = {} + self._numbers = {} self._populate_contacts() - newContactIds = set(self.get_contacts()) - - self._addedContacts = newContactIds - oldContactIds - self._removedContacts = oldContactIds - newContactIds - self._changedContacts = set( - contactId - for contactId in newContactIds.intersection(oldContactIds) - if self._has_contact_changed(contactId, oldContacts) + newContactNumbers = set(self.get_numbers()) + + addedContacts = newContactNumbers - oldContactNumbers + removedContacts = oldContactNumbers - newContactNumbers + changedContacts = set( + contactNumber + for contactNumber in newContactNumbers.intersection(oldContactNumbers) + if self._numbers[contactNumber] != oldContacts[contactNumber] ) - if self._addedContacts or self._removedContacts or self._changedContacts: - message = self, self._addedContacts, self._removedContacts, self._changedContacts + if addedContacts or removedContacts or changedContacts: + message = self, addedContacts, removedContacts, changedContacts self.updateSignalHandler.stage.send(message) - def get_contacts(self): - return self._contacts.iterkeys() - - def get_contact_name(self, contactId): - return self._contacts[contactId][0] - - def get_contact_details(self, contactId): - self._populate_contact_details(contactId) - return self._get_contact_details(contactId) + def get_numbers(self): + return self._numbers.iterkeys() + + def get_contact_name(self, strippedNumber): + """ + @throws KeyError if contact not in list (so client can choose what to display) + """ + return self._numbers[strippedNumber][0] + + def get_phone_type(self, strippedNumber): + try: + return self._numbers[strippedNumber][1] + except KeyError: + return "unknown" + + def is_blocked(self, strippedNumber): + try: + return self._numbers[strippedNumber][2]["response"] == self._RESPONSE_BLOCKED + except KeyError: + return False def _populate_contacts(self): - if self._contacts: + if self._numbers: return contacts = self._backend.get_contacts() for contactId, contactDetails in contacts: contactName = contactDetails["name"] - contactNumbers = [ - (numberDetails.get("phoneType", "Mobile"), numberDetails["phoneNumber"]) + contactNumbers = ( + ( + misc_utils.normalize_number(numberDetails["phoneNumber"]), + numberDetails.get("phoneType", "Mobile"), + ) for numberDetails in contactDetails["numbers"] - ] - self._contacts[contactId] = (contactName, contactNumbers) - - def _populate_contact_details(self, contactId): - if self._get_contact_details(contactId): - return - self._get_contact_details(contactId).extend( - self._backend.get_contact_details(contactId) - ) - - def _get_contact_details(self, contactId): - return self._contacts[contactId][1] - - def _has_contact_changed(self, contactId, oldContacts): - oldContact = oldContacts[contactId] - oldContactName = oldContact[0] - oldContactDetails = oldContact[1] - if oldContactName != self.get_contact_name(contactId): - return True - if not oldContactDetails: - return False - # if its already in the old cache, purposefully add it into the new cache - return oldContactDetails != self.get_contact_details(contactId) + ) + self._numbers.update( + (number, (contactName, phoneType, contactDetails)) + for (number, phoneType) in contactNumbers + )