6 import util.coroutines as coroutines
9 _moduleLogger = logging.getLogger("gvoice.addressbook")
12 class Addressbook(object):
14 def __init__(self, backend):
15 self._backend = backend
17 self._addedContacts = set()
18 self._removedContacts = set()
19 self._changedContacts = set()
21 self.updateSignalHandler = coroutines.CoTee()
23 def update(self, force=False):
24 if not force and self._contacts:
26 oldContacts = self._contacts
27 oldContactIds = set(self.get_contacts())
30 self._populate_contacts()
31 newContactIds = set(self.get_contacts())
33 self._addedContacts = newContactIds - oldContactIds
34 self._removedContacts = oldContactIds - newContactIds
35 self._changedContacts = set(
37 for contactId in newContactIds.intersection(oldContactIds)
38 if self._has_contact_changed(contactId, oldContacts)
41 message = self, self._addedContacts, self._removedContacts, self._changedContacts
42 self.updateSignalHandler.send(message)
44 def get_contacts(self):
45 return self._contacts.iterkeys()
47 def get_contact_name(self, contactId):
48 return self._contacts[contactId][0]
50 def get_contact_details(self, contactId):
51 self._populate_contact_details(contactId)
52 return self._get_contact_details(contactId)
54 def _populate_contacts(self):
57 contacts = self._backend.get_contacts()
58 for contactId, contactName in contacts:
59 self._contacts[contactId] = (contactName, {})
61 def _populate_contact_details(self, contactId):
62 if self._get_contact_details(contactId):
64 self._get_contact_details(contactId).update(
65 self._backend.get_contact_details(contactId)
68 def _get_contact_details(self, contactId):
69 return self._contacts[contactId][1]
71 def _has_contact_changed(self, contactId, oldContacts):
72 oldContact = oldContacts[contactId]
73 oldContactName = oldContact[0]
74 oldContactDetails = oldContact[1]
75 if oldContactName != self.get_contact_name(contactId):
77 if not oldContactDetails[1]:
79 # if its already in the old cache, purposefully add it into the new cache
80 return oldContactDetails != self.get_contact_details(contactId)