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()
24 oldContacts = self._contacts
25 oldContactIds = set(self.get_contacts())
28 self._populate_contacts()
29 newContactIds = set(self.get_contacts())
31 self._addedContacts = newContactIds - oldContactIds
32 self._removedContacts = oldContactIds - newContactIds
33 self._changedContacts = set(
35 for contactId in newContactIds.intersection(oldContactIds)
36 if self._has_contact_changed(contactId, oldContacts)
39 message = self, self._addedContacts, self._removedContacts, self._changedContacts
40 self.updateSignalHandler.send(message)
42 def get_contacts(self):
43 return self._contacts.iterkeys()
45 def get_contact_name(self, contactId):
46 return self._contacts[contactId][0]
48 def get_contact_details(self, contactId):
49 self._populate_contact_details(contactId)
50 return self._get_contact_details(contactId)
52 def _populate_contacts(self):
55 contacts = self._backend.get_contacts()
56 for contactId, contactName in contacts:
57 self._contacts[contactId] = (contactName, {})
59 def _populate_contact_details(self, contactId):
60 if self._get_contact_details(contactId):
62 self._get_contact_details(contactId).update(
63 self._backend.get_contact_details(contactId)
66 def _get_contact_details(self, contactId):
67 return self._contacts[contactId][1]
69 def _has_contact_changed(self, contactId, oldContacts):
70 oldContact = oldContacts[contactId]
71 oldContactName = oldContact[0]
72 oldContactDetails = oldContact[1]
73 if oldContactName != self.get_contact_name(contactId):
75 if not oldContactDetails[1]:
77 # if its already in the old cache, purposefully add it into the new cache
78 return oldContactDetails != self.get_contact_details(contactId)