X-Git-Url: http://git.maemo.org/git/?p=theonering;a=blobdiff_plain;f=src%2Fgvoice%2Faddressbook.py;h=f5fa02e45f6a011f74aa127a68b8cbfac9f9830c;hp=a3f9bacf5a612058f1f79675678c693c1c8c8824;hb=1c892d1b9bf14b28eb54ce3590ed2ee29d5e3d25;hpb=613ba869ba587b74ec66c0dfd0e30978ddd11cf7 diff --git a/src/gvoice/addressbook.py b/src/gvoice/addressbook.py index a3f9bac..f5fa02e 100644 --- a/src/gvoice/addressbook.py +++ b/src/gvoice/addressbook.py @@ -4,6 +4,7 @@ import logging import util.coroutines as coroutines +import util.misc as misc_utils _moduleLogger = logging.getLogger("gvoice.addressbook") @@ -11,72 +12,72 @@ _moduleLogger = logging.getLogger("gvoice.addressbook") 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() - self.update() 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, contactName in contacts: - self._contacts[contactId] = (contactName, []) - - 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) + for contactId, contactDetails in contacts: + contactName = contactDetails["name"] + contactNumbers = ( + ( + misc_utils.normalize_number(numberDetails["phoneNumber"]), + numberDetails.get("phoneType", "Mobile"), + ) + for numberDetails in contactDetails["numbers"] + ) + self._numbers.update( + (number, (contactName, phoneType, contactDetails)) + for (number, phoneType) in contactNumbers + )