Fixing some signal handling bugs
[theonering] / src / gvoice / addressbook.py
1 #!/usr/bin/python
2
3
4 import logging
5
6 import util.coroutines as coroutines
7
8
9 _moduleLogger = logging.getLogger("gvoice.addressbook")
10
11
12 class Addressbook(object):
13
14         def __init__(self, backend):
15                 self._backend = backend
16                 self._contacts = {}
17                 self._addedContacts = set()
18                 self._removedContacts = set()
19                 self._changedContacts = set()
20
21                 self.updateSignalHandler = coroutines.CoTee()
22
23         def update(self, force=False):
24                 if not force and self._contacts:
25                         return
26                 oldContacts = self._contacts
27                 oldContactIds = set(self.get_contacts())
28
29                 self._contacts = {}
30                 self._populate_contacts()
31                 newContactIds = set(self.get_contacts())
32
33                 self._addedContacts = newContactIds - oldContactIds
34                 self._removedContacts = oldContactIds - newContactIds
35                 self._changedContacts = set(
36                         contactId
37                         for contactId in newContactIds.intersection(oldContactIds)
38                         if self._has_contact_changed(contactId, oldContacts)
39                 )
40
41                 message = self, self._addedContacts, self._removedContacts, self._changedContacts
42                 self.updateSignalHandler.stage.send(message)
43
44         def get_contacts(self):
45                 return self._contacts.iterkeys()
46
47         def get_contact_name(self, contactId):
48                 return self._contacts[contactId][0]
49
50         def get_contact_details(self, contactId):
51                 self._populate_contact_details(contactId)
52                 return self._get_contact_details(contactId)
53
54         def _populate_contacts(self):
55                 if self._contacts:
56                         return
57                 contacts = self._backend.get_contacts()
58                 for contactId, contactName in contacts:
59                         self._contacts[contactId] = (contactName, {})
60
61         def _populate_contact_details(self, contactId):
62                 if self._get_contact_details(contactId):
63                         return
64                 self._get_contact_details(contactId).update(
65                         self._backend.get_contact_details(contactId)
66                 )
67
68         def _get_contact_details(self, contactId):
69                 return self._contacts[contactId][1]
70
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):
76                         return True
77                 if not oldContactDetails[1]:
78                         return False
79                 # if its already in the old cache, purposefully add it into the new cache
80                 return oldContactDetails != self.get_contact_details(contactId)