/b-gtk/fix-viz

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/b-gtk/fix-viz

« back to all changes in this revision

Viewing changes to seahorse.py

  • Committer: Jelmer Vernooij
  • Date: 2008-06-29 19:34:29 UTC
  • mto: This revision was merged to the branch mainline in revision 520.
  • Revision ID: jelmer@samba.org-20080629193429-ir2ilmbko6qkubg5
Add Branch/Index option if bzr-search is available.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# This program is free software; you can redistribute it and/or modify
 
2
# it under the terms of the GNU General Public License as published by
 
3
# the Free Software Foundation; either version 2 of the License, or
 
4
# (at your option) any later version.
 
5
 
 
6
# This program is distributed in the hope that it will be useful,
 
7
# but WITHOUT ANY WARRANTY; without even the implied warranty of
 
8
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
9
# GNU General Public License for more details.
 
10
 
 
11
# You should have received a copy of the GNU General Public License
 
12
# along with this program; if not, write to the Free Software
 
13
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 
14
 
 
15
__copyright__ = 'Copyright (C) 2008 Daniel Schierbeck'
 
16
__author__ = 'Daniel Schierbeck <daniel.schierbeck@gmail.com>'
1
17
 
2
18
import dbus
3
19
 
12
28
KEY_TYPE_OPENPGP = 'openpgp'
13
29
KEY_TYPE_SSH = 'ssh'
14
30
 
15
 
bus = dbus.SessionBus()
 
31
try:
 
32
    bus = dbus.SessionBus()
 
33
except dbus.exceptions.DBusException, e:
 
34
    get_name = getattr(e, 'get_dbus_name', None)
 
35
    if get_name is not None:
 
36
        name = get_name()
 
37
    else:
 
38
        name = getattr(e, '_dbus_error_name', None)
 
39
    # DBus sometimes fails like this, just treat it as if seahorse is not
 
40
    # available rather than crashing.
 
41
    if name == "org.freedesktop.DBus.Error.Spawn.ExecFailed":
 
42
        raise ImportError
 
43
    else:
 
44
        raise
 
45
 
 
46
if hasattr(bus, 'list_activatable_names'):
 
47
    bus_names = bus.list_activatable_names()
 
48
else:
 
49
    bus_object = bus.get_object('org.freedesktop.DBus', '/org/freedesktop/DBus')
 
50
    bus_names = bus_object.ListNames(dbus_interface='org.freedesktop.DBus')
 
51
 
 
52
if BUS_NAME not in bus_names:
 
53
    raise ImportError
 
54
 
16
55
crypto = dbus.Interface(bus.get_object(BUS_NAME, CRYPTO_PATH), 
17
56
                        CRYPTO_INTERFACE)
18
57
openpgp = dbus.Interface(bus.get_object(BUS_NAME, OPENPGP_PATH),
37
76
LOCATION_REMOTE = 50
38
77
LOCATION_LOCAL = 100
39
78
 
40
 
def discover(*key_ids):
41
 
    return openpgp.DiscoverKeys(key_ids, 0)
 
79
keyset = dict()
42
80
 
43
81
def verify(crypttext):
44
82
    (cleartext, key) = crypto.VerifyText(KEY_TYPE_OPENPGP, 1, crypttext)
45
83
 
46
 
    return Key(key)
 
84
    if key != "":
 
85
        if key not in keyset:
 
86
            keyset[key] = Key(key)
 
87
 
 
88
        return keyset[key]
47
89
 
48
90
class Key:
49
91
 
50
92
    def __init__(self, key):
51
93
        self.key = key
52
 
        self.fingerprint = None
53
 
        self.trust = None
54
 
        self.flags = None
55
 
        self.display_name = None
56
 
        self.location = None
57
 
 
58
 
        discover(key)
59
 
 
60
 
    def get_field(self, field, default=None):
61
 
        (valid, value) = openpgp.GetKeyField(self.key, field)
62
 
 
63
 
        if valid:
64
 
            return value
 
94
 
 
95
        (keys, unmatched) = openpgp.MatchKeys([self.get_id()], 0x00000010)
 
96
        self.available = (key in keys)
 
97
 
 
98
        if self.available:
 
99
            fields = openpgp.GetKeyFields(key, ['fingerprint', 'trust', 'flags', 'display-name', 'location'])
65
100
        else:
66
 
            return default
 
101
            fields = dict()
 
102
 
 
103
        self.fingerprint = fields.get('fingerprint', 'N/A')
 
104
        self.trust = fields.get('trust', TRUST_UNKNOWN)
 
105
        self.flags = fields.get('flags', 0)
 
106
        self.display_name = fields.get('display-name', '')
 
107
        self.location = fields.get('location', LOCATION_MISSING)
67
108
    
68
109
    def get_flags(self):
69
 
        if self.flags is None:
70
 
            self.flags = self.get_field('flags', 0)
71
 
 
72
110
        return self.flags
73
111
 
74
112
    def get_display_name(self):
75
 
        if self.display_name is None:
76
 
            self.display_name = self.get_field('display-name')
77
 
 
78
113
        return self.display_name
79
114
 
80
115
    def get_id(self):
81
116
        return self.key.split(':')[1][8:]
82
117
 
83
118
    def get_fingerprint(self):
84
 
        if self.fingerprint is None:
85
 
            self.fingerprint = self.get_field('fingerprint')
86
 
 
87
119
        return self.fingerprint
88
120
 
89
121
    def get_trust(self):
90
 
        if self.trust is None:
91
 
            self.trust = self.get_field('trust', TRUST_UNKNOWN)
92
 
 
93
122
        return self.trust
94
123
 
95
124
    def get_location(self):
96
 
        if self.location is None:
97
 
            self.location = self.get_field('location', LOCATION_MISSING)
98
 
 
99
125
        return self.location
100
126
 
101
127
    def is_available(self):
102
 
        return self.key != ""
 
128
        return self.available
103
129
 
104
130
    def is_trusted(self):
105
 
        return self.get_flags() & FLAG_TRUSTED
 
131
        return self.flags & FLAG_TRUSTED != 0