Help the user by making the LinkedIn verification code input numeric only.
[hermes] / package / src / org / maemo / hermes / engine / linkedin / provider.py
1 import gnome.gconf
2 import gobject, gtk, hildon
3 import time, thread
4 import webbrowser
5 import org.maemo.hermes.engine.provider
6 import org.maemo.hermes.engine.linkedin.service
7 from org.maemo.hermes.engine.linkedin.api import LinkedInApi
8
9 class Provider(org.maemo.hermes.engine.provider.Provider):
10     """LinkedIn provider for Hermes. 
11
12        Copyright (c) Andrew Flegg <andrew@bleb.org> 2010.
13        Released under the Artistic Licence."""
14        
15     # -----------------------------------------------------------------------
16     def __init__(self):
17         """Initialise the provider, and ensure the environment is going to work."""
18
19         self._gc = gnome.gconf.client_get_default()
20         self._api = LinkedInApi(gconf = self._gc) 
21
22
23     # -----------------------------------------------------------------------
24     def get_name(self):
25         """Return the display name of this service. An icon, of with the lower-case,
26            all-alphabetic version of this name is expected to be provided."""
27            
28         return 'LinkedIn'
29     
30     
31     # -----------------------------------------------------------------------
32     def get_account_detail(self):
33         """Return the name of the linked LinkedIn account."""
34         
35         return self._gc.get_string(LinkedInApi.GCONF_USER)
36     
37     
38     # -----------------------------------------------------------------------
39     def has_preferences(self):
40         """Whether or not this provider has any preferences. If it does not,
41            open_preferences must NOT be called; as the behaviour is undetermined."""
42            
43         return True
44     
45     
46     # -----------------------------------------------------------------------
47     def open_preferences(self, parent):
48         """Open the preferences for this provider as a child of the 'parent' widget."""
49
50         self.main_window = parent
51         dialog = gtk.Dialog(self.get_name(), parent)
52         dialog.add_button(_('Disable'), gtk.RESPONSE_NO)
53         enable = dialog.add_button(_('Enable'), gtk.RESPONSE_YES)
54     
55         button = hildon.Button(gtk.HILDON_SIZE_FINGER_HEIGHT,
56                                hildon.BUTTON_ARRANGEMENT_VERTICAL)
57         self._handle_button(None, button, enable)
58         button.connect('clicked', self._handle_button, button, enable)
59             
60         dialog.vbox.add(gtk.Label(""))
61         dialog.vbox.add(button)
62         dialog.vbox.add(gtk.Label(""))
63         
64         dialog.show_all()
65         result = dialog.run()
66         dialog.hide()
67         if result == gtk.RESPONSE_CANCEL or result == gtk.RESPONSE_DELETE_EVENT:
68             return None
69     
70         return result == gtk.RESPONSE_YES
71
72
73     # -----------------------------------------------------------------------
74     def _handle_button(self, e, button, enable):
75         """Ensure the button state is correct."""
76         
77         authenticated = self._api.get_access_token_from_gconf() is not None
78         if e is not None:
79             if authenticated:
80                 self._api.remove_access_token_from_gconf()
81             else:
82                 self._api.authenticate(lambda: None, self.block_for_auth)
83         
84             authenticated = self._api.get_access_token_from_gconf() is not None
85         
86         button.set_title(authenticated and _("Clear authorisation") or _("Authorise"))
87         enable.set_sensitive(authenticated)
88
89         
90     # -----------------------------------------------------------------------
91     def block_for_auth(self, url):
92         """Part of the GUI callback API."""
93
94         webbrowser.open(url)
95         time.sleep(3)
96         note = gtk.Dialog(_('Service authorisation'), self.main_window)
97         note.add_button(_("Validate"), gtk.RESPONSE_OK)
98         input = hildon.Entry(gtk.HILDON_SIZE_FINGER_HEIGHT)
99         input.set_property('is-focus', False)
100         input.set_property('hildon-input-mode', gtk.HILDON_GTK_INPUT_MODE_NUMERIC)
101         note.set_title(_("Verification code from web browser"))
102         note.vbox.add(input)
103
104         note.show_all()
105         result = note.run()
106         note.hide()
107         if result == gtk.RESPONSE_OK:
108             return input.get_text()
109         else:
110             return None
111
112     
113     # -----------------------------------------------------------------------
114     def service(self, gui_callback):
115         """Return the service backend."""
116            
117         return org.maemo.hermes.engine.linkedin.service.Service(self.get_id(), self._api)