Fix title. oops
[hermes] / package / src / org / maemo / hermes / engine / facebook / provider.py
1 import gnome.gconf
2 import gtk, hildon
3 import org.maemo.hermes.engine.provider
4 from org.maemo.hermes.engine.facebook.service import Service
5 from org.maemo.hermes.engine.facebook.api import FacebookApi
6 import oauth2
7
8 class Provider(org.maemo.hermes.engine.provider.Provider):
9     """Facebook provider for Hermes. 
10
11        This requires two gconf paths to contain Facebook application keys:
12            /apps/maemo/hermes/facebook_app
13            /apps/maemo/hermes/facebook_secret
14        
15        Copyright (c) Andrew Flegg <andrew@bleb.org> 2010.
16        Released under the Artistic Licence."""
17
18     # -----------------------------------------------------------------------
19     def __init__(self):
20         """Initialise the provider, and ensure the environment is going to work."""
21
22         self._gc = gnome.gconf.client_get_default()
23
24         key_app    = self._gc.get_string('/apps/maemo/hermes/facebook_key')
25         key_secret = self._gc.get_string('/apps/maemo/hermes/facebook_secret')
26         if key_app is None or key_secret is None:
27             raise Exception('No Facebook application keys found. Installation error.')
28         
29         access_token = self._gc.get_string('/apps/maemo/hermes/facebook_access_token')
30         self.oauth = oauth2.OAuth2(key_app, key_secret, access_token)
31         self.api = FacebookApi(self.oauth)
32
33
34     # -----------------------------------------------------------------------
35     def get_name(self):
36         """Return the display name of this service. An icon, of with the lower-case,
37            all-alphabetic version of this name is expected to be provided."""
38            
39         return 'Facebook'
40
41     
42     # -----------------------------------------------------------------------
43     def get_account_detail(self):
44         """Return the email address associated with the user, if available."""
45         
46         return self._gc.get_string('/apps/maemo/hermes/facebook_user')
47     
48     
49     # -----------------------------------------------------------------------
50     def has_preferences(self):
51         """Whether or not this provider has any preferences. If it does not,
52            open_preferences must NOT be called; as the behaviour is undetermined."""
53            
54         return True
55     
56     
57     # -----------------------------------------------------------------------
58     def open_preferences(self, parent):
59         """Open the preferences for this provider as a child of the 'parent' widget."""
60
61         dialog = gtk.Dialog(self.get_name(), parent)
62         dialog.add_button(_('Disable'), gtk.RESPONSE_NO)
63         enable = dialog.add_button(_('Enable'), gtk.RESPONSE_YES)
64
65         button = hildon.Button(gtk.HILDON_SIZE_FINGER_HEIGHT,
66                                hildon.BUTTON_ARRANGEMENT_VERTICAL)
67         self._handle_button(None, button, enable)
68         button.connect('clicked', self._handle_button, button, enable)
69         dialog.vbox.add(button)
70         
71         checkbox = hildon.CheckButton(gtk.HILDON_SIZE_FINGER_HEIGHT)
72         checkbox.set_label(_('Create birthday-only contacts'))
73         checkbox.set_active(self._gc.get_bool('/apps/maemo/hermes/facebook_birthday_only'))
74         dialog.vbox.add(checkbox)
75         dialog.vbox.add(gtk.Label(""))
76         
77         dialog.show_all()
78         result = dialog.run()
79         dialog.hide()
80         if result == gtk.RESPONSE_CANCEL or result == gtk.RESPONSE_DELETE_EVENT:
81             return None
82     
83         self._gc.set_bool('/apps/maemo/hermes/facebook_birthday_only', checkbox.get_active())
84         return result == gtk.RESPONSE_YES
85
86
87     # -----------------------------------------------------------------------
88     def _handle_button(self, e, button, enable):
89         """Ensure the button state is correct."""
90         
91         authenticated = self._gc.get_string('/apps/maemo/hermes/facebook_access_token') is not None
92         if e is not None:
93             if authenticated:
94                 self._gc.unset('/apps/maemo/hermes/facebook_access_token')
95             else:
96                 self.api.authenticate()
97                 self._gc.set_string('/apps/maemo/hermes/facebook_access_token', self.oauth.get_access_token())
98                 self._gc.set_string('/apps/maemo/hermes/facebook_user', self.api.get_user())
99         
100             authenticated = self._gc.get_string('/apps/maemo/hermes/facebook_access_token') is not None
101         
102         button.set_title(authenticated and _("Clear authorisation") or _("Authorise"))
103         enable.set_sensitive(authenticated)
104
105     
106     # -----------------------------------------------------------------------
107     def service(self, gui_callback):
108         """Return a service instance."""
109         
110         self._gui = gui_callback
111         return Service(self.get_id(), self.api, self._gc.get_bool('/apps/maemo/hermes/facebook_birthday_only'))