Package screenlets :: Package options :: Module account_option
[hide private]
[frames] | no frames]

Source Code for Module screenlets.options.account_option

  1  #  
  2  # Copyright (C) 2009 Martin Owens (DoctorMO) <doctormo@gmail.com> 
  3  # 
  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. 
  8  #  
  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. 
 13  #  
 14  # You should have received a copy of the GNU General Public License 
 15  # along with this program.  If not, see <http://www.gnu.org/licenses/>. 
 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   
29 -class AccountOption(Option):
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 # check for availability of keyring 46 if not gnomekeyring.is_available(): 47 raise Exception('GnomeKeyring is not available!!') 48 # THIS IS A WORKAROUND FOR A BUG IN KEYRING (usually we would use 49 # gnomekeyring.get_default_keyring_sync() here): 50 # find first available keyring 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 # we prefer the default keyring 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
67 - def on_import(self, strvalue):
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 # split string into username/auth_token 72 #data = strvalue.split(':', 1) 73 (name, auth_token) = strvalue.split(':', 1) 74 if name and auth_token: 75 # read pass from storage 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 # return 83 return (name, pw) 84 else: 85 raise Exception('Illegal value in AccountOption.on_import.')
86
87 - def on_export(self, value):
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 # store password in storage 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 # build value from username and auth_token 96 return value[0] + ':' + str(auth_token)
97
98 - def generate_widget(self, value):
99 """Generate a textbox for a account options""" 100 self.widget = gtk.HBox() 101 vb = gtk.VBox() 102 input_name = gtk.Entry() 103 input_name.set_text(value[0]) 104 input_name.show() 105 input_pass = gtk.Entry() 106 input_pass.set_visibility(False) # password 107 input_pass.set_text(value[1]) 108 input_pass.show() 109 but = gtk.Button(_('Apply'), gtk.STOCK_APPLY) 110 but.show() 111 but.connect("clicked", self.has_changed) 112 vb.add(input_name) 113 vb.add(input_pass) 114 vb.show() 115 but.set_tooltip_text(_('Apply username/password ...')) 116 input_name.set_tooltip_text(_('Enter username here ...')) 117 input_pass.set_tooltip_text(_('Enter password here ...')) 118 self.widget.add(vb) 119 self.widget.add(but) 120 return self.widget
121
122 - def set_value(self, value):
123 """Set the account value as required.""" 124 self.value = value
125
126 - def has_changed(self, widget):
127 """Executed when the widget event kicks off.""" 128 # the widget is a HBox containing a VBox containing two Entries 129 # (ideally we should have a custom widget for the AccountOption) 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