fixed list error (add > append)
[hermes] / package / src / org / maemo / hermes / engine / hermes.py
index 8f8c571..fd19358 100644 (file)
@@ -1,3 +1,7 @@
+from org.maemo.hermes.engine.friend import Friend
+from org.maemo.hermes.engine.contact import Contact
+import evolution
+
 class Hermes:
     """Encapsulate the process of syncing online friends' information with the
        Evolution contacts' database. This should be used as follows:
@@ -22,10 +26,10 @@ class Hermes:
         
         # -- These fields are currently part of the API...
         #
-        self.updated   = []
-        self.matched   = []
+        self.updated = []
+        self.matched = []
         self.unmatched = []
-        self.friends   = {}
+        self.friends = {}
         self.addresses = None
         
         # -- Other initialisation...
@@ -35,7 +39,7 @@ class Hermes:
     
     
     # -----------------------------------------------------------------------
-    def run(self, resync = False):
+    def run(self, resync=False):
         """Load information on the authenticated user's friends. Synchronise Facebook
            profiles to contact database. If resync is false, no existing information
            will be overwritten."""
@@ -44,7 +48,7 @@ class Hermes:
             def get_name(self):
                 return "Fredrik Wendt"
             def get_emails(self):
-                return ["fredrik@wendt.se","maemohermes@wendt.se"]
+                return ["fredrik@wendt.se", "maemohermes@wendt.se"]
             def get_photo(self):
                 return None
             def get_mapped_to(self):
@@ -57,33 +61,78 @@ class Hermes:
 #        self._sync_job.get_updated_contacts()
 #        self._sync_job.get_matched_contacts()
         pass
+    
+    
+    # -----------------------------------------------------------------------
+    def run_alt(self, overwrite_existing_fields=False):
+        contacts = []
+        self.addresses = evolution.ebook.open_addressbook('default')
+        for contact in self.addresses.get_all_contacts():
+            contacts.append(Contact(self.addresses, contact))
 
+        # warm up
+        for service in self._services:
+            print "pre-process:", service.get_id()
+            for contact in contacts:
+                service.pre_process_contact(contact)
+                
+        # fetch data
+        for service in self._services:
+            print "process_friends:", service.get_id()
+            service.process_friends()
+        
+        # combine results into one friend
+        for contact in contacts:
+            result = Friend()
+            for service in self._services:
+                print "process_contact:", service.get_id()
+                friend = service.process_contact(contact)
+                if friend:
+                    contact.add_mapping(service.get_id())
+                    friend.decorate(result)
+            
+            if result.get_name() is not None:
+                self.update_contact(result, overwrite_existing_fields)
+            else:
+                self.unmatched.append(contact)
+            
+        # give services a chance to create new contacts
+        for service in self._services:
+            print "create_contacts:", service.get_id()
+            for friend in service.create_contacts():
+                self.create_contact_from_friend(friend)
+                
+        # finalisation
+        for service in self._services:
+            print "finalize:", service.get_id()
+            service.finalise(self.updated, overwrite_existing_fields)
+            
+        # commit changes
+        for contact in self.updated:
+            print "committing changes to:", contact.get_name(), contact
+            self.addresses.commit_contact(contact.get_econtact())
+        
 
     # -----------------------------------------------------------------------
-    def update_contact(self, contact, friend, resync = False):
+    def update_contact(self, contact, friend, resync=False):
         """Update the given contact with information from the given friend."""
         
-        friend.update_contact(contact, resync)
-    
-"""        
- friends = ()
- for service in services:
-   for friend in service.get_friends():
-     friends.add(friend)
-
- all_contacts = get_contacts_as_set()
- contacts = set()
- updated_contacts = set()
- for econtact in addressbook.get_all_contacts():
-   contact = Contact(addressbook, econtact)
-   contacts.add(contact)
-   for service in something.get_services_by_prioritisation():
-     if service.process_contact(contact):
-       updated_contacts.add(contact)
+        print "updating contact ", contact, " with friend ", friend
+        self.updated.append(contact)
+        self.matched.append(contact)
+        if friend.get_source() is not None:
+            contact.add_mapping(friend.get_source())
 
- for service in something.get_services_by_prioritisation():
-   service.finalise(updated_contacts)
 
- for contact in updated_contacts:
-   contact.save()
-"""
+    # -----------------------------------------------------------------------
+    def create_contact_from_friend(self, friend):
+        econtact = evolution.ebook.EContact()
+        econtact.props.full_name = friend['name']
+        econtact.props.given_name = friend['first_name']
+        econtact.props.family_name = friend['last_name']
+        contact = Contact(self.addresses, econtact)
+                
+        self.addresses.add_contact(contact)
+        self.update_contact(contact, friend)
+        
+        print "Created [%s]" % (contact.get_name())