Make cancelling Facebook prefs return 'None', as per API. Add ESBox
[hermes] / package / src / org / maemo / hermes / engine / facebook / provider.py
1 from facebook import Facebook, FacebookError
2 import gnome.gconf
3 import gtk, hildon
4 import org.maemo.hermes.engine.provider
5 import org.maemo.hermes.engine.facebook.service
6
7 class Provider(org.maemo.hermes.engine.provider.Provider):
8     """Facebook provider for Hermes. 
9
10        This requires two gconf paths to contain Facebook application keys:
11            /apps/maemo/hermes/facebook_app
12            /apps/maemo/hermes/facebook_secret
13        
14        Copyright (c) Andrew Flegg <andrew@bleb.org> 2010.
15        Released under the Artistic Licence."""
16
17     # -----------------------------------------------------------------------
18     def __init__(self):
19         """Initialise the provider, and ensure the environment is going to work."""
20
21         self._gc = gnome.gconf.client_get_default()
22         if (self._gc.get_string('/desktop/gnome/url-handlers/http/command') == 'epiphany %s'):
23             raise Exception('Browser in gconf invalid (see NB#136012). Installation error.')
24
25         key_app    = self._gc.get_string('/apps/maemo/hermes/facebook_app')
26         key_secret = self._gc.get_string('/apps/maemo/hermes/facebook_secret')
27         if key_app is None or key_secret is None:
28             raise Exception('No Facebook application keys found. Installation error.')
29         
30         self.fb = Facebook(key_app, key_secret)
31         self.fb.desktop = True
32
33         if self.fb.session_key is None:
34             self.fb.session_key = self._gc.get_string('/apps/maemo/hermes/facebook_session_key')
35             self.fb.secret = self._gc.get_string('/apps/maemo/hermes/facebook_secret_key')
36             self.fb.uid = self._gc.get_string('/apps/maemo/hermes/facebook_uid')
37
38
39     # -----------------------------------------------------------------------
40     def get_name(self):
41         """Return the display name of this service. An icon, of with the lower-case,
42            all-alphabetic version of this name is expected to be provided."""
43            
44         return 'Facebook'
45
46     
47     # -----------------------------------------------------------------------
48     def get_account_detail(self):
49         """Return the email address associated with the user, if available."""
50         
51         name = self._gc.get_string('/apps/maemo/hermes/facebook_user')
52         return name and name or _('Pending authorisation')
53     
54     
55     # -----------------------------------------------------------------------
56     def has_preferences(self):
57         """Whether or not this provider has any preferences. If it does not,
58            open_preferences must NOT be called; as the behaviour is undetermined."""
59            
60         return True
61     
62     
63     # -----------------------------------------------------------------------
64     def open_preferences(self, parent):
65         """Open the preferences for this provider as a child of the 'parent' widget."""
66
67         dialog = gtk.Dialog(self.get_name(), parent)
68         dialog.add_button(_('Clear'), gtk.RESPONSE_OK)
69         dialog.add_button(_('Disable'), gtk.RESPONSE_NO)
70         dialog.add_button(_('Enable'), gtk.RESPONSE_YES)
71         
72         dialog.vbox.add(gtk.Label(_('Note: authentication via web page')))
73         
74         checkbox = hildon.CheckButton(gtk.HILDON_SIZE_FINGER_HEIGHT)
75         checkbox.set_label(_('Create birthday-only contacts'))
76         checkbox.set_active(self._gc.get_bool('/apps/maemo/hermes/facebook_birthday_only'))
77         dialog.vbox.add(checkbox)
78         dialog.vbox.add(gtk.Label(''))
79         
80         while True:
81             dialog.show_all()
82             result = dialog.run()
83             dialog.hide()
84             if result == gtk.RESPONSE_CANCEL or result == gtk.RESPONSE_DELETE_EVENT:
85                 return None
86             elif result == gtk.RESPONSE_OK:
87                 self._gc.unset('/apps/maemo/hermes/facebook_session_key')
88                 self._gc.unset('/apps/maemo/hermes/facebook_secret_key')
89                 self._gc.unset('/apps/maemo/hermes/facebook_uid')
90                 self._gc.unset('/apps/maemo/hermes/facebook_user')
91             else:
92                 break
93     
94         self._gc.set_bool('/apps/maemo/hermes/facebook_birthday_only', checkbox.get_active())
95         return result == gtk.RESPONSE_YES
96         
97     
98     # -----------------------------------------------------------------------
99     def service(self, gui_callback):
100         """Return a service instance."""
101         
102         self._gui = gui_callback
103         
104         # Check the available session is still valid...
105         while True:
106             try:
107                 if self.fb.users.getLoggedInUser() and self.fb.session_key:
108                     break
109             except FacebookError:
110                 pass
111             self._do_fb_login()
112
113         return org.maemo.hermes.engine.facebook.service.Service(self.fb)
114
115
116     # -----------------------------------------------------------------------
117     def _do_fb_login(self):
118         """Perform authentication against Facebook and store the result in gconf
119              for later use. Uses the 'need_auth' and 'block_for_auth' methods on
120              the callback class. The former allows a message to warn the user
121              about what is about to happen to be shown; the second is to wait
122              for the user to confirm they have logged in."""
123         self.fb.session_key = None
124         self.fb.secret = None
125         self.fb.uid = None
126         
127         if self._gui:
128             self._gui.need_auth()
129             
130         self.fb.auth.createToken()
131         self.fb.login()
132         
133         if self._gui:
134             self._gui.block_for_auth()
135           
136         session = self.fb.auth.getSession()
137         self._gc.set_string('/apps/maemo/hermes/facebook_session_key', session['session_key'])
138         self._gc.set_string('/apps/maemo/hermes/facebook_secret_key', session['secret'])
139         self._gc.set_string('/apps/maemo/hermes/facebook_uid', str(session['uid']))
140
141         info = self.fb.users.getInfo([self.fb.uid], ['name'])
142         self._gc.set_string('/apps/maemo/hermes/facebook_user', info[0]['name'])
143