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