6 import util.coroutines as coroutines
7 import util.misc as util_misc
10 _moduleLogger = logging.getLogger("gvoice.addressbook")
13 class Addressbook(object):
18 def __init__(self, backend):
19 self._backend = backend
22 self.updateSignalHandler = coroutines.CoTee()
24 def update(self, force=False):
25 if not force and self._numbers:
27 oldContacts = self._numbers
28 oldContactNumbers = set(self.get_numbers())
31 self._populate_contacts()
32 newContactNumbers = set(self.get_numbers())
34 addedContacts = newContactNumbers - oldContactNumbers
35 removedContacts = oldContactNumbers - newContactNumbers
36 changedContacts = set(
38 for contactNumber in newContactNumbers.intersection(oldContactNumbers)
39 if self._numbers[contactNumber] != oldContacts[contactNumber]
42 if addedContacts or removedContacts or changedContacts:
43 message = self, addedContacts, removedContacts, changedContacts
44 self.updateSignalHandler.stage.send(message)
46 def get_numbers(self):
47 return self._numbers.iterkeys()
49 def get_contact_name(self, strippedNumber):
50 return self._numbers[strippedNumber][0]
52 def get_phone_type(self, strippedNumber):
53 return self._numbers[strippedNumber][1]
55 def is_blocked(self, strippedNumber):
56 return self._numbers[strippedNumber][1]["response"] == self._RESPONSE_BLOCKED
58 def _populate_contacts(self):
61 contacts = self._backend.get_contacts()
62 for contactId, contactDetails in contacts:
63 contactName = contactDetails["name"]
66 numberDetails.get("phoneType", "Mobile"),
67 util_misc.normalize_number(numberDetails["phoneNumber"]),
70 for numberDetails in contactDetails["numbers"]
73 (number, (contactName, phoneType))
74 for (phoneType, number) in contactNumbers