Adding a contacts cache manager with callbacks
authorEd Page <eopage@byu.net>
Wed, 30 Sep 2009 02:57:39 +0000 (21:57 -0500)
committerEd Page <eopage@byu.net>
Wed, 30 Sep 2009 02:57:39 +0000 (21:57 -0500)
src/gvoice/addressbook.py [new file with mode: 0644]

diff --git a/src/gvoice/addressbook.py b/src/gvoice/addressbook.py
new file mode 100644 (file)
index 0000000..e796583
--- /dev/null
@@ -0,0 +1,38 @@
+#!/usr/bin/python
+
+
+import logging
+
+
+_moduleLogger = logging.getLogger("gvoice.addressbook")
+
+
+class Addressbook(object):
+
+       def __init__(self, backend):
+               self._backend = backend
+               self._contacts = {}
+
+       def clear_cache(self):
+               self._contacts.clear()
+
+       def get_contacts(self):
+               self._populate_contacts()
+               return self._contacts.iterkeys()
+
+       def get_contact_details(self, contactId):
+               self._populate_contacts()
+               self._populate_contact_details(contactId)
+               return self._contacts[contactId]
+
+       def _populate_contacts(self):
+               if self._contacts:
+                       return
+               contacts = self._backend.get_contacts()
+               for contactId, contactName in contacts:
+                       self._contacts[contactId] = None
+
+       def _populate_contact_details(self, contactId):
+               if self._contacts[contactId] is not None:
+                       return
+               self._contacts[contactId] = self._backend.get_contact_details(contactId)