732b04df55e7220e1bb5b8db9c0862e53be236af
[hermes] / package / src / org / maemo / hermes / engine / hermes.py
1 from org.maemo.hermes.engine.friend import Friend
2 from org.maemo.hermes.engine.contact import Contact
3 import evolution
4
5 class Hermes:
6     """Encapsulate the process of syncing online friends' information with the
7        Evolution contacts' database. This should be used as follows:
8        
9          * Initialise, passing in a GUI callback.
10          * Call Syncjob.
11          * Retrieve information on changes effected.
12          * Call update_contact to enact manual mapping.
13          
14        Copyright (c) Andrew Flegg <andrew@bleb.org> 2010.
15        Released under the Artistic Licence."""
16
17     
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.
22                               
23              progress(i, j) - the application is currently processing friend 'i' of
24                               'j'. Should be used to provide the user a progress bar.
25         """
26         
27         # -- These fields are currently part of the API...
28         #
29         self.updated = []
30         self.matched = []
31         self.unmatched = []
32         self.friends = {}
33         self.addresses = None
34         
35         # -- Other initialisation...
36         #
37         self._services = services
38         self._progress = gui_progress
39     
40     
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."""
46
47         class FakeContact():
48             def get_name(self):
49                 return "Fredrik Wendt"
50             def get_emails(self):
51                 return ["fredrik@wendt.se", "maemohermes@wendt.se"]
52             def get_photo(self):
53                 return None
54             def get_mapped_to(self):
55                 return set(["facebook", "gravatar"])
56         self.matched = [FakeContact()]
57  
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()
63         pass
64     
65     
66     # -----------------------------------------------------------------------
67     def run_alt(self, overwrite_existing_fields=False):
68         contacts = []
69         book = evolution.ebook.open_addressbook('default')
70         for contact in book.get_all_contacts():
71             contacts.append(Contact(book, contact))
72
73         # warm up
74         for service in self._services:
75             for contact in contacts:
76                 service.pre_process_contact(contact)
77                 
78         # fetch data
79         for service in self._services:
80             service.process_friends()
81         
82         # combine results into one friend
83         for contact in contacts:
84             result = Friend()
85             for service in self._services:
86                 friend = service.process_contact(contact)
87                 if (friend):
88                     friend.decorate(result)
89             
90             self.update_contact(friend, overwrite_existing_fields)
91         
92
93     # -----------------------------------------------------------------------
94     def update_contact(self, contact, friend, resync=False):
95         """Update the given contact with information from the given friend."""
96         
97         print "updating contact ", contact, " with friend ", friend
98
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']
104         
105         self.update_contact(contact, friend)
106         
107         self.addresses.add_contact(contact)
108         self.updated.append(contact)
109         self.addresses.commit_contact(contact)
110         
111         print "Created [%s]" % (contact.get_name())
112         self.matched.append(contact)
113
114
115     def commit(self):
116         self.store.close()