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