2
# Copyright (C) 2009 Martin Owens (DoctorMO) <doctormo@gmail.com>
4
# This program is free software; you can redistribute it and/or modify
5
# it under the terms of the GNU General Public License as published by
6
# the Free Software Foundation; either version 3 of the License, or
7
# (at your option) any later version.
9
# This program is distributed in the hope that it will be useful,
10
# but WITHOUT ANY WARRANTY; without even the implied warranty of
11
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12
# GNU General Public License for more details.
14
# You should have received a copy of the GNU General Public License
15
# along with this program; if not, write to the Free Software
16
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19
Account options, these classes will display a text box.
25
except ImportError: print 'No GNOME keyring, there will be problems with account options'
27
from screenlets.options import _
28
from base import Option
30
class AccountOption(Option):
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.
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
44
def __init__(self, group, name, *attr, **args):
45
super(AccountOption, self).__init__ (group, name, *attr, **args)
46
# check for availability of keyring
47
if not gnomekeyring.is_available():
48
raise Exception('GnomeKeyring is not available!!')
49
# THIS IS A WORKAROUND FOR A BUG IN KEYRING (usually we would use
50
# gnomekeyring.get_default_keyring_sync() here):
51
# find first available keyring
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!')
56
# we prefer the default keyring
58
self.keyring = gnomekeyring.get_default_keyring_sync()
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"
64
print "Warning: Neither default nor session keyring found, assuming keyring %s!" % self.keyring_list[0]
65
self.keyring = self.keyring_list[0]
68
def on_import(self, strvalue):
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
# split string into username/auth_token
73
#data = strvalue.split(':', 1)
74
(name, auth_token) = strvalue.split(':', 1)
75
if name and auth_token:
76
# read pass from storage
78
pw = gnomekeyring.item_get_info_sync(self.keyring,
79
int(auth_token)).get_secret()
81
print "ERROR: Unable to read password from keyring: %s" % ex
86
raise Exception('Illegal value in AccountOption.on_import.')
88
def on_export(self, value):
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
# store password in storage
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
# build value from username and auth_token
97
return value[0] + ':' + str(auth_token)
99
def generate_widget(self, value):
100
"""Generate a textbox for a account options"""
101
self.widget = gtk.HBox()
103
input_name = gtk.Entry()
104
input_name.set_text(value[0])
106
input_pass = gtk.Entry()
107
input_pass.set_visibility(False) # password
108
input_pass.set_text(value[1])
110
but = gtk.Button(_('Apply'), gtk.STOCK_APPLY)
112
but.connect("clicked", self.has_changed)
116
but.set_tooltip_text(_('Apply username/password ...'))
117
input_name.set_tooltip_text(_('Enter username here ...'))
118
input_pass.set_tooltip_text(_('Enter password here ...'))
123
def set_value(self, value):
124
"""Set the account value as required."""
127
def has_changed(self, widget):
128
"""Executed when the widget event kicks off."""
129
# the widget is a HBox containing a VBox containing two Entries
130
# (ideally we should have a custom widget for the AccountOption)
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()
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)