Added caching of contacts with cache clearing under low memory situations
authorepage <eopage@byu.net>
Tue, 17 Feb 2009 00:32:45 +0000 (00:32 +0000)
committerepage <eopage@byu.net>
Tue, 17 Feb 2009 00:32:45 +0000 (00:32 +0000)
git-svn-id: file:///svnroot/gc-dialer/trunk@182 c39d3808-3fe2-4d86-a59f-b7f623ee9f21

src/gc_backend.py
src/gc_dialer.py
src/gmail_backend.py

index 6af96eb..67ca99b 100644 (file)
@@ -80,6 +80,8 @@ class GCDialer(object):
                self._callbackNumbers = {}
                self._lastAuthed = 0.0
 
+               self.__contacts = None
+
        def is_authed(self, force = False):
                """
                Attempts to detect a current session and pull the auth token ( a_t ) from the page.
@@ -127,6 +129,8 @@ class GCDialer(object):
                self._browser.cookies.clear()
                self._browser.cookies.save()
 
+               self.clear_caches()
+
        def dial(self, number):
                """
                This is the main function responsible for initating the callback
@@ -166,7 +170,7 @@ class GCDialer(object):
                return False
 
        def clear_caches(self):
-               pass
+               self.__contacts = None
 
        def is_valid_syntax(self, number):
                """
@@ -268,7 +272,7 @@ class GCDialer(object):
                @returns Iterable of (Address Book Factory, Book Id, Book Name)
                """
                yield self, "", ""
-       
+
        def open_addressbook(self, bookId):
                return self
 
@@ -284,19 +288,27 @@ class GCDialer(object):
                """
                @returns Iterable of (contact id, contact name)
                """
-               contactsPagesUrls = [GCDialer._contactsURL]
-               for contactsPageUrl in contactsPagesUrls:
-                       contactsPage = self._browser.download(contactsPageUrl)
-                       for contact_match in self._contactsRe.finditer(contactsPage):
-                               contactId = contact_match.group(1)
-                               contactName = contact_match.group(2)
-                               yield contactId, contactName
-
-                       next_match = self._contactsNextRe.match(contactsPage)
-                       if next_match is not None:
-                               newContactsPageUrl = self._contactsURL + next_match.group(1)
-                               contactsPagesUrls.append(newContactsPageUrl)
-       
+               if self.__contacts is None:
+                       self.__contacts = []
+
+                       contactsPagesUrls = [GCDialer._contactsURL]
+                       for contactsPageUrl in contactsPagesUrls:
+                               contactsPage = self._browser.download(contactsPageUrl)
+                               for contact_match in self._contactsRe.finditer(contactsPage):
+                                       contactId = contact_match.group(1)
+                                       contactName = contact_match.group(2)
+                                       contact = contactId, contactName
+                                       self.__contacts.append(contact)
+                                       yield contact
+
+                               next_match = self._contactsNextRe.match(contactsPage)
+                               if next_match is not None:
+                                       newContactsPageUrl = self._contactsURL + next_match.group(1)
+                                       contactsPagesUrls.append(newContactsPageUrl)
+               else:
+                       for contact in self.__contacts:
+                               yield contact
+
        def get_contact_details(self, contactId):
                """
                @returns Iterable of (Phone Type, Phone Number)
index ab21fa8..aa08524 100755 (executable)
@@ -129,6 +129,9 @@ class DummyAddressBook(object):
        Minimal example of both an addressbook factory and an addressbook
        """
 
+       def clear_caches(self):
+               pass
+
        def get_addressbooks(self):
                """
                @returns Iterable of (Address Book Factory, Book Id, Book Name)
@@ -169,6 +172,12 @@ class MergedAddressBook(object):
        def __init__(self, addressbooks, sorter = None):
                self.__addressbooks = addressbooks
                self.__sort_contacts = sorter if sorter is not None else self.null_sorter
+               self.__contacts = None
+
+       def clear_caches(self):
+               for addressBook in self.__addressbooks:
+                       addressBook.clear_caches()
+               self.__contacts = None
 
        def get_addressbooks(self):
                """
@@ -191,13 +200,21 @@ class MergedAddressBook(object):
                """
                @returns Iterable of (contact id, contact name)
                """
-               contacts = (
-                       ("-".join([str(bookIndex), contactId]), contactName)
-                               for (bookIndex, addressbook) in enumerate(self.__addressbooks)
-                                       for (contactId, contactName) in addressbook.get_contacts()
-               )
-               sortedContacts = self.__sort_contacts(contacts)
-               return sortedContacts
+               if self.__contacts is None:
+                       contacts = (
+                               ("-".join([str(bookIndex), contactId]), contactName)
+                                       for (bookIndex, addressbook) in enumerate(self.__addressbooks)
+                                               for (contactId, contactName) in addressbook.get_contacts()
+                       )
+                       sortedContacts = self.__sort_contacts(contacts)
+
+                       self.__contacts = []
+                       for contact in sortedContacts:
+                               self.__contacts.append(contact)
+                               yield contact
+               else:
+                       for contact in self.__contacts:
+                               yield contact
 
        def get_contact_details(self, contactId):
                """
@@ -690,6 +707,9 @@ class Dialpad(object):
                """
                if memory_low:
                        self._gcBackend.clear_caches()
+                       for factory in self._addressBookFactories:
+                               factory.clear_caches()
+                       self._addressBook.clear_caches()
                        gc.collect()
 
        def _on_connection_change(self, connection, event, magicIdentifier):
index 30d1bcf..d80ae7f 100644 (file)
@@ -20,6 +20,8 @@
 
 """
 GMail Contacts Support
+
+@todo Look into the official API http://code.google.com/p/gdata-python-client/
 """