updated and cleaned up Gravatar
[hermes] / package / src / org / maemo / hermes / engine / gravatar / service.py
index bc48b11..f3bb225 100644 (file)
@@ -1,5 +1,6 @@
 import urllib, hashlib, xmlrpclib
 import org.maemo.hermes.engine.service
+from org.maemo.hermes.engine.friend import Friend
 
 class Service(org.maemo.hermes.engine.service.Service):
     """Gravatar backend for Hermes.
@@ -10,77 +11,82 @@ class Service(org.maemo.hermes.engine.service.Service):
     _image_url_base = "http://www.gravatar.com/avatar.php?"
        
     # -----------------------------------------------------------------------
-    def __init__(self, api_email, api_key):
-        """Initialise the Gravatar service
+    def __init__(self, service_id, api_email, api_key):
+        """Initialise the Gravatar service.
         
         api_email is the email address to use when talking to the backend.
-        api_key is the "secrect" key used when talking to the backend
+        api_key is the "secret" key to use when talking to the backend.
         """
+        
+        org.maemo.hermes.engine.service.Service.__init__(self, service_id)
+
         self._api_email = api_email
         self._api_key = api_key
-        if self._api_key is None or self._api_email is None:
-            raise Exception('No Gravatar application keys found. Installation error.')
          
         self._address_to_contact = {}
         self._hash_to_address = {}
         self._hash_has_gravatar = {}
-        self._empty_cache = True
         
         self._friends_by_contact = {}
         self._contacts_by_friend = {}
         
         self._api_url = 'https://secure.gravatar.com/xmlrpc?user=' + hashlib.md5(self._api_email).hexdigest()
-   
-
-    # -----------------------------------------------------------------------
-    def get_name(self):
-        return "Gravatar"
     
     
     # -----------------------------------------------------------------------
     def pre_process_contact(self, contact):
         """Extracts addresses from the contact."""
+        
         for address in contact.get_emails():
             self._address_to_contact[address] = contact
     
     
     # -----------------------------------------------------------------------
+    def process_friends(self):
+        self._lookup_addresses()
+
+    
+    # -----------------------------------------------------------------------
     def process_contact(self, contact):
-        """On first call (with a contact missing a photo), go get data from Gravatar's servers."""
+        """On first call (with a contact missing a photo), go get data from 
+           Gravatar's servers."""
         
         if not self._has_photo(contact):
             for address in contact.get_emails():
                 hash = self._get_hash_for_address(address)
                 if (self._hash_has_gravatar.has_key(hash) and self._hash_has_gravatar[hash]):
-                    friend = self._create_friend(contact.get_name())
+                    friend = Friend(contact.get_name())
                     friend.set_photo_url(self._get_url_for_email_hash(hash))
                     self._register_match(contact, friend)
+                    return friend
+        
+        return None
 
 
     # -----------------------------------------------------------------------
-    def process_friends(self):
-        self._lookup_addresses()
+    def get_unmatched_friends(self):
+        """Will always return empty list - Gravatar only reacts on e-mail 
+           addresses from existing contacts."""
 
+        return []
     
+
     # -----------------------------------------------------------------------
-    def get_friends(self):
+    def _get_friends(self):
         return self._contacts_by_friend.keys()
 
-    
-    def get_contacts_with_match(self):
-        """Returns a dict with Contact objects as keys and Friend objects as values"""
-        return self._friends_by_contact
-    
 
-    def get_unmatched_friends(self):
-        """Will always return None - Gravatar only reacts on e-mail address input."""
-        return None
-    
-    
+    # -----------------------------------------------------------------------    
+    def _get_contacts_with_match(self):
+        """Returns a dict with Contact objects as keys and Friend objects as 
+           values"""
+        
+        return self._friends_by_contact
 
 
     # -----------------------------------------------------------------------
     def _register_match(self, contact, friend):
+        friend.set_contact(contact)
         self._friends_by_contact[contact] = friend
         self._contacts_by_friend[friend] = contact
         
@@ -91,7 +97,6 @@ class Service(org.maemo.hermes.engine.service.Service):
     
         
     # -----------------------------------------------------------------------
-
     def _lookup_addresses(self):
         """Constructs hashes for address_to_contact, makes call to the Gravatar.com service and updates
         self._hash_has_gravatar"""
@@ -118,7 +123,6 @@ class Service(org.maemo.hermes.engine.service.Service):
     # -----------------------------------------------------------------------
     def _set_hash_information(self, hash_info):
         self._hash_has_gravatar = hash_info
-        self._empty_cache = False
         
 
     # -----------------------------------------------------------------------