Provide Twitter authentication preferences.
[hermes] / package / src / org / maemo / hermes / engine / twitter / provider.py
1 import gnome.gconf
2 import gtk, hildon
3 import org.maemo.hermes.engine.provider
4 import org.maemo.hermes.engine.twitter.service
5 import twitter
6
7 class Provider(org.maemo.hermes.engine.provider.Provider):
8     """Twitter provider for Hermes. 
9
10        Copyright (c) Andrew Flegg <andrew@bleb.org> 2010.
11        Released under the Artistic Licence."""
12
13
14     # -----------------------------------------------------------------------
15     def __init__(self):
16         self._gconf  = gnome.gconf.client_get_default()
17
18
19     # -----------------------------------------------------------------------
20     def get_name(self):
21         """Return the display name of this service. An icon, of with the lower-case,
22            all-alphabetic version of this name is expected to be provided."""
23            
24         return 'Twitter'
25     
26     
27     # -----------------------------------------------------------------------
28     def has_preferences(self):
29         """Whether or not this provider has any preferences. If it does not,
30            open_preferences must NOT be called; as the behaviour is undetermined."""
31            
32         return True
33     
34     
35     # -----------------------------------------------------------------------
36     def open_preferences(self, parent):
37         """Show the username/password dialogue."""
38            
39         dialog = gtk.Dialog(self.get_name(), parent)
40         dialog.add_button(_('Disable'), gtk.RESPONSE_NO)
41         dialog.add_button(_('Enable'), gtk.RESPONSE_YES)
42
43         # -- Username...
44         #
45         hbox = gtk.HBox()
46         hbox.pack_start(gtk.Label(_("Username")))
47         
48         username = hildon.Entry(gtk.HILDON_SIZE_FINGER_HEIGHT)
49         username.set_property('is-focus', True)
50         username.set_property('hildon-input-mode', gtk.HILDON_GTK_INPUT_MODE_FULL)
51         # TODO Connect change signal: if blank, disable "Enable"
52         username.set_text(self._gconf.get_string("/apps/maemo/hermes/twitter_user") or '')
53
54         hbox.pack_start(username)
55         dialog.vbox.add(hbox)
56         
57         # -- Password...
58         #
59         hbox = gtk.HBox()
60         hbox.pack_start(gtk.Label(_("Password")))
61         
62         password = hildon.Entry(gtk.HILDON_SIZE_FINGER_HEIGHT)
63         password.set_property('hildon-input-mode', gtk.HILDON_GTK_INPUT_MODE_FULL | gtk.HILDON_GTK_INPUT_MODE_INVISIBLE)
64         # TODO Connect change signal: if blank, disable "Enable"
65         password.set_text(self._gconf.get_string("/apps/maemo/hermes/twitter_pwd") or '')
66         hbox.pack_start(password)
67         dialog.vbox.add(hbox)
68
69         # -- Run the dialogue...
70         #
71         dialog.show_all()
72         result = dialog.run()
73         dialog.hide()
74         if result == gtk.RESPONSE_CANCEL or result == gtk.RESPONSE_DELETE_EVENT:
75             return None
76  
77         self._gconf.set_string("/apps/maemo/hermes/twitter_user", username.get_text())
78         self._gconf.set_string("/apps/maemo/hermes/twitter_pwd", password.get_text())
79         return result == gtk.RESPONSE_YES
80
81
82     # -----------------------------------------------------------------------
83     def get_account_detail(self):
84         """Return the Twitter username."""
85         
86         return self._gconf.get_string("/apps/maemo/hermes/twitter_user")
87     
88     
89     # -----------------------------------------------------------------------
90     def service(self, gui_callback):
91         """Return the service backend. This must be a class which implements the
92            following methods:
93                * get_friends
94                * process_contact
95                * finalise
96         
97            See Service for more details."""
98     
99         username = self._gconf.get_string("/apps/maemo/hermes/twitter_user") or ''
100         password = self._gconf.get_string("/apps/maemo/hermes/twitter_pwd") or ''
101         
102         api = twitter.Api(username=username, password=password)
103
104         return org.maemo.hermes.engine.twitter.service.Service(username, password, gui_callback)