1 from org.maemo.hermes.engine.friend import Friend
5 """Encapsulate the process of syncing online friends' information with the
6 Evolution contacts' database. This should be used as follows:
8 * Initialise, passing in a GUI callback.
10 * Retrieve information on changes effected.
11 * Call update_contact to enact manual mapping.
13 Copyright (c) Andrew Flegg <andrew@bleb.org> 2010.
14 Released under the Artistic Licence."""
17 # -----------------------------------------------------------------------
18 def __init__(self, services, gui_progress):
19 """Constructor. Passed a list of services, and a callback method
20 which must implement the following API.
22 progress(i, j) - the application is currently processing friend 'i' of
23 'j'. Should be used to provide the user a progress bar.
26 # -- These fields are currently part of the API...
34 # -- Other initialisation...
36 self._services = services
37 self._progress = gui_progress
40 # -----------------------------------------------------------------------
41 def run(self, resync=False):
42 """Load information on the authenticated user's friends. Synchronise Facebook
43 profiles to contact database. If resync is false, no existing information
44 will be overwritten."""
48 return "Fredrik Wendt"
50 return ["fredrik@wendt.se", "maemohermes@wendt.se"]
53 def get_mapped_to(self):
54 return set(["facebook", "gravatar"])
55 self.matched = [FakeContact()]
57 # self._sync_job = SyncJob(services, [FakeContact()], self.progress)
58 # self._sync_job.run()
59 # self._sync_job.get_unmatched_friends()
60 # self._sync_job.get_updated_contacts()
61 # self._sync_job.get_matched_contacts()
65 # -----------------------------------------------------------------------
66 def run_alt(self, overwrite_existing_fields=False):
67 print "+++ Syncing contacts..."
68 self.addresses = evolution.ebook.open_addressbook('default')
69 print "+++ Contact store created..."
70 contacts = self.addresses.get_all_contacts()
73 for service in self._services:
74 for contact in contacts:
75 service.pre_process_contact(contact)
78 for service in self._services:
79 service.process_friends()
81 # combine results into one friend
82 for contact in contacts:
84 for service in self._services:
85 friend = service.process_contact(contact)
87 friend.decorate(result)
89 self.update_contact(friend, overwrite_existing_fields)
92 # -----------------------------------------------------------------------
93 def update_contact(self, contact, friend, resync=False):
94 """Update the given contact with information from the given friend."""
96 print "updating contact ", contact, " with friend ", friend
98 def create_contact_from_friend(self, friend):
99 contact = evolution.ebook.EContact()
100 contact.props.full_name = friend['name']
101 contact.props.given_name = friend['first_name']
102 contact.props.family_name = friend['last_name']
104 self.update_contact(contact, friend)
106 self.addresses.add_contact(contact)
107 self.updated.append(contact)
108 self.addresses.commit_contact(contact)
110 print "Created [%s]" % (contact.get_name())
111 self.matched.append(contact)