1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 """
18 Account options, these classes will display a text box.
19 """
20
21 import gtk
22 try:
23 import gnomekeyring
24 except ImportError: print 'No GNOME keyring, there will be problems with account options'
25
26 from screenlets.options import _
27 from base import Option
28
30 """
31 An Option-type for username/password combos. Stores the password in
32 the gnome-keyring (if available) and only saves username and auth_token
33 through the screenlets-backend.
34 TODO:
35 - not create new token for any change (use "set" instead of "create" if
36 the given item already exists)
37 - use usual storage if no keyring is available but output warning
38 - on_delete-function for removing the data from keyring when the
39 Screenlet holding the option gets deleted
40 """
41 protected = True
42
43 - def __init__(self, group, name, *attr, **args):
44 super(AccountOption, self).__init__ (group, name, *attr, **args)
45
46 if not gnomekeyring.is_available():
47 raise Exception('GnomeKeyring is not available!!')
48
49
50
51 self.keyring_list = gnomekeyring.list_keyring_names_sync()
52 if len(self.keyring_list) == 0:
53 raise Exception('No keyrings found. Please create one first!')
54 else:
55
56 try:
57 self.keyring = gnomekeyring.get_default_keyring_sync()
58 except:
59 if "session" in self.keyring_list:
60 print "Warning: No default keyring found, using session keyring. Storage is not permanent!"
61 self.keyring = "session"
62 else:
63 print "Warning: Neither default nor session keyring found, assuming keyring %s!" % self.keyring_list[0]
64 self.keyring = self.keyring_list[0]
65
66
68 """Import account info from a string (like 'username:auth_token'),.
69 retrieve the password from the storage and return a tuple containing
70 username and password."""
71
72
73 (name, auth_token) = strvalue.split(':', 1)
74 if name and auth_token:
75
76 try:
77 pw = gnomekeyring.item_get_info_sync(self.keyring,
78 int(auth_token)).get_secret()
79 except Exception, ex:
80 print "ERROR: Unable to read password from keyring: %s" % ex
81 pw = ''
82
83 return (name, pw)
84 else:
85 raise Exception('Illegal value in AccountOption.on_import.')
86
88 """Export the given tuple/list containing a username and a password. The
89 function stores the password in the gnomekeyring and returns a
90 string in form 'username:auth_token'."""
91
92 attribs = dict(name=value[0])
93 auth_token = gnomekeyring.item_create_sync(self.keyring,
94 gnomekeyring.ITEM_GENERIC_SECRET, value[0], attribs, value[1], True)
95
96 return value[0] + ':' + str(auth_token)
97
121
123 """Set the account value as required."""
124 self.value = value
125
127 """Executed when the widget event kicks off."""
128
129
130 for c in self.widget.get_children():
131 if c.__class__ == gtk.VBox:
132 c2 = c.get_children()
133 self.value = (c2[0].get_text(), c2[1].get_text())
134 super(AccountOption, self).has_changed()
135
136 """#TEST:
137 o = AccountOption('None', 'pop3_account', ('',''), 'Username/Password', 'Enter username/password here ...')
138 # save option to keyring
139 exported_account = o.on_export(('RYX', 'mysecretpassword'))
140 print exported_account
141 # and read option back from keyring
142 print o.on_import(exported_account)
143 """
144