Tidy up syncjob references
[hermes] / package / src / org / maemo / hermes / engine / hermes.py
index 9a00bfd..9d569d8 100644 (file)
@@ -4,13 +4,8 @@ import evolution
 
 class Hermes:
     """Encapsulate the process of syncing online friends' information with the
-       Evolution contacts' database. This should be used as follows:
+       Evolution contacts' database.
        
-         * Initialise, passing in a GUI callback.
-         * Call Syncjob.
-         * Retrieve information on changes effected.
-         * Call update_contact to enact manual mapping.
-         
        Copyright (c) Andrew Flegg <andrew@bleb.org> 2010.
        Released under the Artistic Licence."""
 
@@ -91,6 +86,7 @@ class Hermes:
             tick_increment = len(contacts) / (len(to_create) or 1)
             print tick_increment, to_create
             for friend in to_create:
+                friend.set_source(service.get_id())
                 self._progress("Creating contacts...", current_tick, total_ticks)
                 current_tick += tick_increment
                 self.create_contact_from_friend(friend)
@@ -101,7 +97,10 @@ class Hermes:
             self._progress("Finalising...", current_tick, total_ticks)
             current_tick += len(contacts)
             service.finalise(self.updated, overwrite_existing_fields)
-            self.friends[service.get_id] = service.get_unmatched_friends()
+            for friend in service.get_unmatched_friends():
+                friend.set_source(service.get_id())
+                key = unicode(friend.get_name()).encode('trans') + "_" + service.get_id()
+                self.friends[key] = friend
             
         # commit changes
         tick_increment = total_contacts / (len(self.updated) or 1)
@@ -116,11 +115,15 @@ class Hermes:
         
 
     # -----------------------------------------------------------------------
-    def update_contact(self, contact, friend, resync=False):
+    def update_contact(self, contact, friend, resync=False, commit=False):
         """Update the given contact with information from the given friend."""
         
         print "updating contact ", contact, " with friend ", friend
-        self.updated.append(contact)
+        if friend.update_contact(contact, resync):
+            self.updated.append(contact)
+            if commit:
+                self.address_book.commit_contact(contact.get_econtact())
+
         self.matched.append(contact)
         if friend.get_source() is not None:
             contact.add_mapping(friend.get_source())
@@ -144,10 +147,11 @@ class Hermes:
     # -----------------------------------------------------------------------
     def _create_empty_contact(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']
+        econtact.props.full_name = friend.get_name()
+        return econtact
     
     # -----------------------------------------------------------------------
     def _create_contact_wrapper(self, econtact):
-        return Contact(self.address_book, econtact)
\ No newline at end of file
+        return Contact(self.address_book, econtact)