1 from org.maemo.hermes.engine.friend import Friend
2 from org.maemo.hermes.engine.contact import Contact
6 """Encapsulate the process of syncing online friends' information with the
7 Evolution contacts' database. This should be used as follows:
9 * Initialise, passing in a GUI callback.
11 * Retrieve information on changes effected.
12 * Call update_contact to enact manual mapping.
14 Copyright (c) Andrew Flegg <andrew@bleb.org> 2010.
15 Released under the Artistic Licence."""
18 # -----------------------------------------------------------------------
19 def __init__(self, services, gui_progress):
20 """Constructor. Passed a list of services, and a callback method
21 which must implement the following API.
23 progress(i, j) - the application is currently processing friend 'i' of
24 'j'. Should be used to provide the user a progress bar.
27 # -- These fields are currently part of the API...
35 # -- Other initialisation...
37 self._services = services
38 self._progress = gui_progress
41 # -----------------------------------------------------------------------
42 def run(self, resync=False):
43 """Load information on the authenticated user's friends. Synchronise Facebook
44 profiles to contact database. If resync is false, no existing information
45 will be overwritten."""
49 return "Fredrik Wendt"
51 return ["fredrik@wendt.se", "maemohermes@wendt.se"]
54 def get_mapped_to(self):
55 return set(["facebook", "gravatar"])
56 self.matched = [FakeContact()]
58 # self._sync_job = SyncJob(services, [FakeContact()], self.progress)
59 # self._sync_job.run()
60 # self._sync_job.get_unmatched_friends()
61 # self._sync_job.get_updated_contacts()
62 # self._sync_job.get_matched_contacts()
66 # -----------------------------------------------------------------------
67 def run_alt(self, overwrite_existing_fields=False):
69 book = evolution.ebook.open_addressbook('default')
70 for contact in book.get_all_contacts():
71 contacts.append(Contact(book, contact))
74 for service in self._services:
75 for contact in contacts:
76 service.pre_process_contact(contact)
79 for service in self._services:
80 service.process_friends()
82 # combine results into one friend
83 for contact in contacts:
85 for service in self._services:
86 friend = service.process_contact(contact)
88 friend.decorate(result)
90 self.update_contact(friend, overwrite_existing_fields)
93 # -----------------------------------------------------------------------
94 def update_contact(self, contact, friend, resync=False):
95 """Update the given contact with information from the given friend."""
97 print "updating contact ", contact, " with friend ", friend
99 def create_contact_from_friend(self, friend):
100 contact = evolution.ebook.EContact()
101 contact.props.full_name = friend['name']
102 contact.props.given_name = friend['first_name']
103 contact.props.family_name = friend['last_name']
105 self.update_contact(contact, friend)
107 self.addresses.add_contact(contact)
108 self.updated.append(contact)
109 self.addresses.commit_contact(contact)
111 print "Created [%s]" % (contact.get_name())
112 self.matched.append(contact)