~ubuntu-branches/ubuntu/trusty/screenlets/trusty

« back to all changes in this revision

Viewing changes to src/lib/options/account_option.py

  • Committer: Bazaar Package Importer
  • Author(s): Julien Lavergne
  • Date: 2011-07-02 11:06:42 UTC
  • mfrom: (1.1.5 upstream)
  • Revision ID: james.westby@ubuntu.com-20110702110642-d0oqdiy8sye8cli2
Tags: 0.1.4-0ubuntu1
* New upstream release.
* debian/patches:
 - 90-disable-resize-grip.patch : Drop, merged upstream.
 - 91_use_webkit.patch: Drop, merged upstream.
* debian/rules:
 - Only fix the permission on 1 file.
* debian/copyright:
 - Update copyright holders.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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, write to the Free Software
 
16
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 
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
 
 
30
class AccountOption(Option):
 
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
        # 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!')
 
55
        else:
 
56
            # we prefer the default keyring
 
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
 
 
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
 
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
            # return
 
84
            return (name, pw)
 
85
        else:
 
86
            raise Exception('Illegal value in AccountOption.on_import.')
 
87
 
 
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)
 
98
 
 
99
    def generate_widget(self, value):
 
100
        """Generate a textbox for a account options"""
 
101
        self.widget = gtk.HBox()
 
102
        vb = gtk.VBox()
 
103
        input_name = gtk.Entry()
 
104
        input_name.set_text(value[0])
 
105
        input_name.show()
 
106
        input_pass = gtk.Entry()
 
107
        input_pass.set_visibility(False)    # password
 
108
        input_pass.set_text(value[1])
 
109
        input_pass.show()
 
110
        but = gtk.Button(_('Apply'), gtk.STOCK_APPLY)
 
111
        but.show()
 
112
        but.connect("clicked", self.has_changed)
 
113
        vb.add(input_name)
 
114
        vb.add(input_pass)
 
115
        vb.show()
 
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 ...'))
 
119
        self.widget.add(vb)
 
120
        self.widget.add(but)
 
121
        return self.widget
 
122
 
 
123
    def set_value(self, value):
 
124
        """Set the account value as required."""
 
125
        self.value = value
 
126
 
 
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()
 
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