a25db8bfe165eb2ae14593e6b1ab7ee6ebd27dc8
[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
21
22     # -----------------------------------------------------------------------
23     def get_name(self):
24         """Return the display name of this service. An icon, of with the lower-case,
25            all-alphabetic version of this name is expected to be provided."""
26            
27         return 'LinkedIn'
28     
29     
30     # -----------------------------------------------------------------------
31     def get_account_detail(self):
32         """Return the name of the linked LinkedIn account."""
33         
34         return self._gc.get_string(LinkedInApi.GCONF_USER)
35     
36     
37     # -----------------------------------------------------------------------
38     def has_preferences(self):
39         """Whether or not this provider has any preferences. If it does not,
40            open_preferences must NOT be called; as the behaviour is undetermined."""
41            
42         return True
43     
44     
45     # -----------------------------------------------------------------------
46     def open_preferences(self, parent):
47         """Open the preferences for this provider as a child of the 'parent' widget."""
48
49         self.main_window = parent
50         api = LinkedInApi(gconf = self._gc) 
51         dialog = gtk.Dialog(self.get_name(), parent)
52         dialog.add_button(_('Disable'), gtk.RESPONSE_NO)
53         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, api, button)
58         button.connect('clicked', self._handle_button, api, button)
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, api, button):
75         """Ensure the button state is correct."""
76         
77         authenticated = api.get_access_token_from_gconf() is not None
78         if e is not None:
79             if authenticated:
80                 api.remove_access_token_from_gconf()
81             else:
82                 api.authenticate(lambda: None, self.block_for_auth)
83         
84             authenticated = api.get_access_token_from_gconf() is not None
85         
86         button.set_title(authenticated and _("Clear authorisation") or _("Authorise"))
87
88         
89     # -----------------------------------------------------------------------
90     def block_for_auth(self, url):
91         """Part of the GUI callback API."""
92
93         webbrowser.open(url)
94         time.sleep(2)
95         note = gtk.Dialog(_('Service authorisation'), self.main_window)
96         note.add_button(_("Validate"), gtk.RESPONSE_OK)
97         input = hildon.Entry(gtk.HILDON_SIZE_FINGER_HEIGHT)
98         input.set_property('is-focus', False)
99         note.set_title(_("Verification code from web browser"))
100         note.vbox.add(input)
101
102         note.show_all()
103         result = note.run()
104         note.hide()
105         if result == gtk.RESPONSE_OK:
106             return input.get_text()
107         else:
108             return None
109
110     
111     # -----------------------------------------------------------------------
112     def service(self, gui_callback):
113         """Return the service backend."""
114            
115         return org.maemo.hermes.engine.linkedin.service.Service()