Add support for judging if you've blocked a contact
[theonering] / src / gvoice / addressbook.py
1 #!/usr/bin/python
2
3
4 import logging
5
6 import util.coroutines as coroutines
7 import util.misc as util_misc
8
9
10 _moduleLogger = logging.getLogger("gvoice.addressbook")
11
12
13 class Addressbook(object):
14
15         _RESPONSE_GOOD = 0
16         _RESPONSE_BLOCKED = 3
17
18         def __init__(self, backend):
19                 self._backend = backend
20                 self._numbers = {}
21
22                 self.updateSignalHandler = coroutines.CoTee()
23
24         def update(self, force=False):
25                 if not force and self._numbers:
26                         return
27                 oldContacts = self._numbers
28                 oldContactNumbers = set(self.get_numbers())
29
30                 self._numbers = {}
31                 self._populate_contacts()
32                 newContactNumbers = set(self.get_numbers())
33
34                 addedContacts = newContactNumbers - oldContactNumbers
35                 removedContacts = oldContactNumbers - newContactNumbers
36                 changedContacts = set(
37                         contactNumber
38                         for contactNumber in newContactNumbers.intersection(oldContactNumbers)
39                         if self._numbers[contactNumber] != oldContacts[contactNumber]
40                 )
41
42                 if addedContacts or removedContacts or changedContacts:
43                         message = self, addedContacts, removedContacts, changedContacts
44                         self.updateSignalHandler.stage.send(message)
45
46         def get_numbers(self):
47                 return self._numbers.iterkeys()
48
49         def get_contact_name(self, strippedNumber):
50                 return self._numbers[strippedNumber][0]
51
52         def get_phone_type(self, strippedNumber):
53                 return self._numbers[strippedNumber][1]
54
55         def is_blocked(self, strippedNumber):
56                 return self._numbers[strippedNumber][1]["response"] == self._RESPONSE_BLOCKED
57
58         def _populate_contacts(self):
59                 if self._numbers:
60                         return
61                 contacts = self._backend.get_contacts()
62                 for contactId, contactDetails in contacts:
63                         contactName = contactDetails["name"]
64                         contactNumbers = (
65                                 (
66                                         numberDetails.get("phoneType", "Mobile"),
67                                         util_misc.normalize_number(numberDetails["phoneNumber"]),
68                                         contactDetails,
69                                 )
70                                 for numberDetails in contactDetails["numbers"]
71                         )
72                         self._numbers.update(
73                                 (number, (contactName, phoneType))
74                                 for (phoneType, number) in contactNumbers
75                         )