Dependency inject service ID, so that it can be stamped on friends and
[hermes] / package / src / org / maemo / hermes / engine / facebook / service.py
index f917131..6fa6c68 100644 (file)
@@ -1,6 +1,7 @@
 import org.maemo.hermes.engine.service
 
 from org.maemo.hermes.engine.names import canonical
+from org.maemo.hermes.engine.friend import Friend
 
 class Service(org.maemo.hermes.engine.service.Service):
     """Facebook backend for Hermes.
@@ -10,17 +11,17 @@ class Service(org.maemo.hermes.engine.service.Service):
        Released under the Artistic Licence."""
        
     attrs = ['uid', 'name', 'pic_big', 'birthday_date', 'profile_url', 'first_name', 'last_name', 'website']
-       
+
+
     # -----------------------------------------------------------------------
-    def __init__(self, facebook, autocreate = False, gui_callback = None):
+    def __init__(self, service_id, facebook, create_birthday_only = False):
         """Initialise the Facebook service, finding Facebook API keys in gconf and
            having a gui_callback available."""
         
-        self._gui = gui_callback
-        self._autocreate = autocreate
         self.fb = facebook
+        self._service_id = service_id
         
-        self._friends_by_id = {}
+        self._friends_by_name = {}
         self._friends_by_url = {}
         self._friends_by_contact = {}
         self._contacts_by_friend = {}
@@ -32,7 +33,7 @@ class Service(org.maemo.hermes.engine.service.Service):
     def get_name(self):
         return "Facebook"
     
-    
+
     # -----------------------------------------------------------------------
     def get_friends(self):
         return self._contacts_by_friend.keys()
@@ -42,7 +43,8 @@ class Service(org.maemo.hermes.engine.service.Service):
         return self._friends_by_contact
     
     def get_unmatched_friends(self):
-        return self._friends_by_id.values()
+        return self._friends_by_name.values()
+
 
     # -----------------------------------------------------------------------
     def pre_process_contact(self, contact):
@@ -79,38 +81,42 @@ class Service(org.maemo.hermes.engine.service.Service):
             self._friends_by_url[url] = friend
             
             if url not in self._known_urls:
-                self._friends_by_id[key] = friend
+                self._friends_by_name[key] = friend
+
 
     # -----------------------------------------------------------------------
     def process_contact(self, contact):
+        matched_friend = None
         if self._friends_by_contact.has_key(contact):
-            return
+            matched_friend = self._friends_by_contact[contact]
         
-        matched_friend = None
         # we might get a hit if the friend has setup a URL with another service,
         # such as putting the id link to Facebook on the Twitter account's profile
-        for url in contact.get_urls():
-            if url in self._friends_by_url:
-                matched_friend = self._friends_by_url[url]
-                self._register_match(contact, matched_friend)
-                print contact.get_name(), " -> match by url -> ", matched_friend
-                break
+        if not matched_friend:
+            for url in contact.get_urls():
+                if url in self._friends_by_url:
+                    matched_friend = self._friends_by_url[url]
+                    self._register_match(contact, matched_friend)
+                    break
 
         if not matched_friend:
             for id in contact.get_identifiers():
-                if id in self._friends_by_id:
-                    matched_friend = self._friends_by_id.pop(id)
+                if id in self._friends_by_name:
+                    matched_friend = self._friends_by_name.pop(id)
                     self._register_match(contact, matched_friend)
-                    print contact.get_name(), " -> match by name -> ", matched_friend
                     break
+                
+        return matched_friend
     
 
     # -----------------------------------------------------------------------
     def _register_match(self, contact, friend):
+        friend.set_contact(contact)
         self._friends_without_contact.discard(friend)
         self._friends_by_contact[contact] = friend
         self._contacts_by_friend[friend] = contact
 
+
     # -----------------------------------------------------------------------
     def _get_friends_data(self):
         """Returns a list of dicts, where each dict represents a friend/contact"""