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
8 class Provider(org.maemo.hermes.engine.provider.Provider):
9 """Facebook provider for Hermes.
11 This requires two gconf paths to contain Facebook application keys:
12 /apps/maemo/hermes/facebook_app
13 /apps/maemo/hermes/facebook_secret
15 Copyright (c) Andrew Flegg <andrew@bleb.org> 2010.
16 Released under the Artistic Licence."""
18 # -----------------------------------------------------------------------
20 """Initialise the provider, and ensure the environment is going to work."""
22 self._gc = gnome.gconf.client_get_default()
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.')
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)
34 # -----------------------------------------------------------------------
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."""
42 # -----------------------------------------------------------------------
43 def get_account_detail(self):
44 """Return the email address associated with the user, if available."""
46 return self._gc.get_string('/apps/maemo/hermes/facebook_user')
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."""
57 # -----------------------------------------------------------------------
58 def open_preferences(self, parent):
59 """Open the preferences for this provider as a child of the 'parent' widget."""
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)
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)
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(""))
80 if result == gtk.RESPONSE_CANCEL or result == gtk.RESPONSE_DELETE_EVENT:
83 self._gc.set_bool('/apps/maemo/hermes/facebook_birthday_only', checkbox.get_active())
84 return result == gtk.RESPONSE_YES
87 # -----------------------------------------------------------------------
88 def _handle_button(self, e, button, enable):
89 """Ensure the button state is correct."""
91 authenticated = self._gc.get_string('/apps/maemo/hermes/facebook_access_token') is not None
94 self._gc.unset('/apps/maemo/hermes/facebook_access_token')
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())
100 authenticated = self._gc.get_string('/apps/maemo/hermes/facebook_access_token') is not None
102 button.set_title(authenticated and _("Clear authorisation") or _("Authorise"))
103 enable.set_sensitive(authenticated)
106 # -----------------------------------------------------------------------
107 def service(self, gui_callback):
108 """Return a service instance."""
110 self._gui = gui_callback
111 return Service(self.get_id(), self.api, self._gc.get_bool('/apps/maemo/hermes/facebook_birthday_only'))