Log In
New Account
  
Home My Page Project Cloud Code Snippets Project Openings Hermes
Summary Lists News SCM
1 from org.maemo.hermes.engine.friend import Friend
2 import evolution
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."""
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."""
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         self.matched = [FakeContact()]
54  
55 #        self._sync_job = SyncJob(services, [FakeContact()], self.progress)
56 #        self._sync_job.run()
57 #        self._sync_job.get_unmatched_friends()
58 #        self._sync_job.get_updated_contacts()
59 #        self._sync_job.get_matched_contacts()
60         pass
61     
62     
63     # -----------------------------------------------------------------------
64     def run_alt(self, overwrite_existing_fields=False):
65         print "+++ Syncing contacts..."
66         self.addresses = evolution.ebook.open_addressbook('default')
67         print "+++ Contact store created..."
68         contacts = self.addresses.get_all_contacts()
70         # warm up
71         for service in self._services:
72             for contact in contacts:
73                 service.pre_process_contact(contact)
74                 
75         # fetch data
76         for service in self._services:
77             service.process_friends()
78         
79         # combine results into one friend
80         for contact in contacts:
81             result = Friend()
82             for service in self._services:
83                 friend = service.process_contact(contact)
84                 if (friend):
85                     friend.decorate(result)
86             
87             self.update_contact(friend, overwrite_existing_fields)
88         
90     # -----------------------------------------------------------------------
91     def update_contact(self, contact, friend, resync=False):
92         """Update the given contact with information from the given friend."""
93         
94         print "updating contact ", contact, " with friend ", friend
97     def create_contact_from_friend(self, friend):
98         contact = evolution.ebook.EContact()
99         contact.props.full_name = friend['name']
100         contact.props.given_name = friend['first_name']
101         contact.props.family_name = friend['last_name']
102         
103         self.update_contact(contact, friend)
104         
105         self.addresses.add_contact(contact)
106         self.updated.append(contact)
107         self.addresses.commit_contact(contact)
108         
109         print "Created [%s]" % (contact.get_name())
110         self.matched.append(contact)
113     def commit(self):
114         self.store.close()

Terms of Use    Privacy Policy    Contribution Guidelines    Feedback

Powered By GForge Collaborative Development Environment