Tidy up syncjob references
[hermes] / package / src / org / maemo / hermes / engine / hermes.py
index c69aeaf..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."""
 
@@ -30,7 +25,7 @@ class Hermes:
         self.matched = []
         self.unmatched = []
         self.friends = {}
-        self.addresses = None
+        self.address_book = None
         
         # -- Other initialisation...
         #
@@ -43,9 +38,9 @@ class Hermes:
         self._progress("Reading contacts...", 1, 10000)
         
         contacts = []
-        self.addresses = evolution.ebook.open_addressbook('default')
-        for contact in self.addresses.get_all_contacts():
-            contacts.append(Contact(self.addresses, contact))
+        self.address_book = self._get_address_book()
+        for econtact in self.address_book.get_all_contacts():
+            contacts.append(self._create_contact_wrapper(econtact))
 
         # work out progress bar info
         total_contacts = len(contacts) * len(self._services)
@@ -77,10 +72,10 @@ class Hermes:
                 friend = service.process_contact(contact)
                 if friend:
                     contact.add_mapping(service.get_id())
-                    friend.update_friend(result)
+                    result.update_from_friend(friend)
             
             if result.get_name() is not None:
-                self.update_contact(result, overwrite_existing_fields)
+                self.update_contact(contact, result, overwrite_existing_fields)
             else:
                 self.unmatched.append(contact)
             
@@ -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,6 +97,10 @@ class Hermes:
             self._progress("Finalising...", current_tick, total_ticks)
             current_tick += len(contacts)
             service.finalise(self.updated, overwrite_existing_fields)
+            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)
@@ -109,17 +109,21 @@ class Hermes:
             print "committing changes to:", contact.get_name(), contact
             self._progress("Saving changes...", current_tick, total_ticks)
             current_tick += tick_increment
-            self.addresses.commit_contact(contact.get_econtact())
+            self.address_book.commit_contact(contact.get_econtact())
             
         self._progress('Finished', 1, -1)
         
 
     # -----------------------------------------------------------------------
-    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())
@@ -127,13 +131,27 @@ class Hermes:
 
     # -----------------------------------------------------------------------
     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)
+        econtact = self._create_empty_contact(friend)
+        contact = self._create_contact_wrapper(econtact)
                 
-        self.addresses.add_contact(contact.get_econtact())
+        self.address_book.add_contact(contact.get_econtact())
         self.update_contact(contact, friend)
         
         print "Created [%s]" % (contact.get_name())
+        
+        
+    # -----------------------------------------------------------------------
+    def _get_address_book(self):
+        return evolution.ebook.open_addressbook('default')
+
+    # -----------------------------------------------------------------------
+    def _create_empty_contact(self, friend):
+        econtact = evolution.ebook.EContact()
+        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)