Random bug fixes, advancing the channels, unit tests, seperating contacts not just...
[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                 self.update()
23
24         def update(self, force=False):
25                 if not force and self._contacts:
26                         return
27                 oldContacts = self._contacts
28                 oldContactIds = set(self.get_contacts())
29
30                 self._contacts = {}
31                 self._populate_contacts()
32                 newContactIds = set(self.get_contacts())
33
34                 self._addedContacts = newContactIds - oldContactIds
35                 self._removedContacts = oldContactIds - newContactIds
36                 self._changedContacts = set(
37                         contactId
38                         for contactId in newContactIds.intersection(oldContactIds)
39                         if self._has_contact_changed(contactId, oldContacts)
40                 )
41
42                 if self._addedContacts or self._removedContacts or self._changedContacts:
43                         message = self, self._addedContacts, self._removedContacts, self._changedContacts
44                         self.updateSignalHandler.stage.send(message)
45
46         def get_contacts(self):
47                 return self._contacts.iterkeys()
48
49         def get_contact_name(self, contactId):
50                 return self._contacts[contactId][0]
51
52         def get_contact_details(self, contactId):
53                 self._populate_contact_details(contactId)
54                 return self._get_contact_details(contactId)
55
56         def _populate_contacts(self):
57                 if self._contacts:
58                         return
59                 contacts = self._backend.get_contacts()
60                 for contactId, contactName in contacts:
61                         self._contacts[contactId] = (contactName, [])
62
63         def _populate_contact_details(self, contactId):
64                 if self._get_contact_details(contactId):
65                         return
66                 self._get_contact_details(contactId).extend(
67                         self._backend.get_contact_details(contactId)
68                 )
69
70         def _get_contact_details(self, contactId):
71                 return self._contacts[contactId][1]
72
73         def _has_contact_changed(self, contactId, oldContacts):
74                 oldContact = oldContacts[contactId]
75                 oldContactName = oldContact[0]
76                 oldContactDetails = oldContact[1]
77                 if oldContactName != self.get_contact_name(contactId):
78                         return True
79                 if not oldContactDetails:
80                         return False
81                 # if its already in the old cache, purposefully add it into the new cache
82                 return oldContactDetails != self.get_contact_details(contactId)