~ubuntu-branches/ubuntu/trusty/gcr/trusty-proposed

« back to all changes in this revision

Viewing changes to gcr/gcr-gnupg-key.c

  • Committer: Package Import Robot
  • Author(s): Jordi Mallach
  • Date: 2012-05-03 10:18:39 UTC
  • Revision ID: package-import@ubuntu.com-20120503101839-wuvloldm7gmdsnij
Tags: upstream-3.4.1
ImportĀ upstreamĀ versionĀ 3.4.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Copyright (C) 2011 Collabora Ltd.
 
3
 *
 
4
 * This program is free software; you can redistribute it and/or modify
 
5
 * it under the terms of the GNU Lesser General Public License as
 
6
 * published by the Free Software Foundation; either version 2.1 of
 
7
 * the License, or (at your option) any later version.
 
8
 *
 
9
 * This program is distributed in the hope that it will be useful, but
 
10
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 
11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 
12
 * Lesser General Public License for more details.
 
13
 *
 
14
 * You should have received a copy of the GNU Lesser General Public
 
15
 * License along with this program; if not, write to the Free Software
 
16
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
 
17
 * 02111-1307, USA.
 
18
 *
 
19
 * Author: Stef Walter <stefw@collabora.co.uk>
 
20
 */
 
21
 
 
22
#include "config.h"
 
23
 
 
24
#include "gcr-gnupg-key.h"
 
25
#include "gcr-gnupg-records.h"
 
26
#include "gcr-record.h"
 
27
#include "gcr-memory-icon.h"
 
28
 
 
29
#include "gck/gck.h"
 
30
 
 
31
#include <glib/gi18n-lib.h>
 
32
 
 
33
enum {
 
34
        PROP_0,
 
35
        PROP_KEYID,
 
36
        PROP_PUBLIC_RECORDS,
 
37
        PROP_SECRET_RECORDS,
 
38
        PROP_LABEL,
 
39
        PROP_MARKUP,
 
40
        PROP_DESCRIPTION,
 
41
        PROP_SHORT_KEYID,
 
42
        PROP_ICON
 
43
};
 
44
 
 
45
struct _GcrGnupgKeyPrivate {
 
46
        GPtrArray *public_records;
 
47
        GPtrArray *secret_records;
 
48
        GIcon *icon;
 
49
};
 
50
 
 
51
G_DEFINE_TYPE (GcrGnupgKey, _gcr_gnupg_key, G_TYPE_OBJECT);
 
52
 
 
53
static gchar *
 
54
calculate_name (GcrGnupgKey *self)
 
55
{
 
56
        GcrRecord* record;
 
57
 
 
58
        record = _gcr_records_find (self->pv->public_records, GCR_RECORD_SCHEMA_UID);
 
59
        g_return_val_if_fail (record, NULL);
 
60
 
 
61
        return _gcr_record_get_string (record, GCR_RECORD_UID_USERID);
 
62
}
 
63
 
 
64
static gchar *
 
65
calculate_markup (GcrGnupgKey *self)
 
66
{
 
67
        gchar *markup = NULL;
 
68
        gchar *uid, *name, *email, *comment;
 
69
 
 
70
        uid = calculate_name (self);
 
71
        if (uid == NULL)
 
72
                return NULL;
 
73
 
 
74
        _gcr_gnupg_records_parse_user_id (uid, &name, &email, &comment);
 
75
        if (comment != NULL && comment[0] != '\0')
 
76
                markup = g_markup_printf_escaped ("%s\n<small>%s \'%s\'</small>", name, email, comment);
 
77
        else
 
78
                markup = g_markup_printf_escaped ("%s\n<small>%s</small>", name, email);
 
79
        g_free (name);
 
80
        g_free (email);
 
81
        g_free (comment);
 
82
        g_free (uid);
 
83
 
 
84
        return markup;
 
85
}
 
86
 
 
87
static void
 
88
_gcr_gnupg_key_init (GcrGnupgKey *self)
 
89
{
 
90
        self->pv = (G_TYPE_INSTANCE_GET_PRIVATE (self, GCR_TYPE_GNUPG_KEY, GcrGnupgKeyPrivate));
 
91
}
 
92
 
 
93
static void
 
94
_gcr_gnupg_key_finalize (GObject *obj)
 
95
{
 
96
        GcrGnupgKey *self = GCR_GNUPG_KEY (obj);
 
97
 
 
98
        if (self->pv->public_records)
 
99
                g_ptr_array_unref (self->pv->public_records);
 
100
        if (self->pv->secret_records)
 
101
                g_ptr_array_unref (self->pv->secret_records);
 
102
 
 
103
        G_OBJECT_CLASS (_gcr_gnupg_key_parent_class)->finalize (obj);
 
104
}
 
105
 
 
106
static void
 
107
_gcr_gnupg_key_set_property (GObject *obj, guint prop_id, const GValue *value,
 
108
                             GParamSpec *pspec)
 
109
{
 
110
        GcrGnupgKey *self = GCR_GNUPG_KEY (obj);
 
111
 
 
112
        switch (prop_id) {
 
113
        case PROP_PUBLIC_RECORDS:
 
114
                _gcr_gnupg_key_set_public_records (self, g_value_get_boxed (value));
 
115
                break;
 
116
        case PROP_SECRET_RECORDS:
 
117
                _gcr_gnupg_key_set_secret_records (self, g_value_get_boxed (value));
 
118
                break;
 
119
        default:
 
120
                G_OBJECT_WARN_INVALID_PROPERTY_ID (obj, prop_id, pspec);
 
121
                break;
 
122
        }
 
123
}
 
124
 
 
125
static void
 
126
_gcr_gnupg_key_get_property (GObject *obj, guint prop_id, GValue *value,
 
127
                             GParamSpec *pspec)
 
128
{
 
129
        GcrGnupgKey *self = GCR_GNUPG_KEY (obj);
 
130
 
 
131
        switch (prop_id) {
 
132
        case PROP_PUBLIC_RECORDS:
 
133
                g_value_set_boxed (value, self->pv->public_records);
 
134
                break;
 
135
        case PROP_SECRET_RECORDS:
 
136
                g_value_set_boxed (value, self->pv->secret_records);
 
137
                break;
 
138
        case PROP_KEYID:
 
139
                g_value_set_string (value, _gcr_gnupg_key_get_keyid (self));
 
140
                break;
 
141
        case PROP_LABEL:
 
142
                g_value_take_string (value, calculate_name (self));
 
143
                break;
 
144
        case PROP_DESCRIPTION:
 
145
                g_value_set_string (value, _("PGP Key"));
 
146
                break;
 
147
        case PROP_MARKUP:
 
148
                g_value_take_string (value, calculate_markup (self));
 
149
                break;
 
150
        case PROP_SHORT_KEYID:
 
151
                g_value_set_string (value, _gcr_gnupg_records_get_short_keyid (self->pv->public_records));
 
152
                break;
 
153
        case PROP_ICON:
 
154
                g_value_set_object (value, _gcr_gnupg_key_get_icon (self));
 
155
                break;
 
156
        default:
 
157
                G_OBJECT_WARN_INVALID_PROPERTY_ID (obj, prop_id, pspec);
 
158
                break;
 
159
        }
 
160
}
 
161
 
 
162
static void
 
163
_gcr_gnupg_key_class_init (GcrGnupgKeyClass *klass)
 
164
{
 
165
        GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
 
166
 
 
167
        _gcr_gnupg_key_parent_class = g_type_class_peek_parent (klass);
 
168
        g_type_class_add_private (klass, sizeof (GcrGnupgKeyPrivate));
 
169
 
 
170
        gobject_class->finalize = _gcr_gnupg_key_finalize;
 
171
        gobject_class->set_property = _gcr_gnupg_key_set_property;
 
172
        gobject_class->get_property = _gcr_gnupg_key_get_property;
 
173
 
 
174
        /**
 
175
         * GcrGnupgKey:public-records:
 
176
         *
 
177
         * Public key data. Should always be present.
 
178
         */
 
179
        g_object_class_install_property (gobject_class, PROP_PUBLIC_RECORDS,
 
180
                 g_param_spec_boxed ("public-records", "Public Records", "Public Key Colon Records",
 
181
                                     G_TYPE_PTR_ARRAY, G_PARAM_READWRITE));
 
182
 
 
183
        /**
 
184
         * GcrGnupgKey:secret-records:
 
185
         *
 
186
         * Secret key data. The keyid of this data must match public-dataset.
 
187
         * If present, this key represents a secret key.
 
188
         */
 
189
        g_object_class_install_property (gobject_class, PROP_SECRET_RECORDS,
 
190
                 g_param_spec_boxed ("secret-records", "Secret Records", "Secret Key Colon Records",
 
191
                                     G_TYPE_PTR_ARRAY, G_PARAM_READWRITE));
 
192
 
 
193
        /**
 
194
         * GcrGnupgKey:keyid:
 
195
         *
 
196
         * Key identifier.
 
197
         */
 
198
        g_object_class_install_property (gobject_class, PROP_KEYID,
 
199
                 g_param_spec_string ("keyid", "Key ID", "Key identifier",
 
200
                                      "", G_PARAM_READABLE));
 
201
 
 
202
        /**
 
203
         * GcrGnupgKey:label:
 
204
         *
 
205
         * User readable label for this key.
 
206
         */
 
207
        g_object_class_install_property (gobject_class, PROP_LABEL,
 
208
                 g_param_spec_string ("label", "Label", "Key label",
 
209
                                      "", G_PARAM_READABLE));
 
210
 
 
211
        /**
 
212
         * GcrGnupgKey::description:
 
213
         *
 
214
         * Description of type of key.
 
215
         */
 
216
        g_object_class_install_property (gobject_class, PROP_DESCRIPTION,
 
217
                 g_param_spec_string ("description", "Description", "Description of object type",
 
218
                                      "", G_PARAM_READABLE));
 
219
 
 
220
        /**
 
221
         * GcrGnupgKey:markup:
 
222
         *
 
223
         * User readable markup which contains key label.
 
224
         */
 
225
        g_object_class_install_property (gobject_class, PROP_MARKUP,
 
226
                 g_param_spec_string ("markup", "Markup", "Markup which describes key",
 
227
                                      "", G_PARAM_READABLE));
 
228
 
 
229
        /**
 
230
         * GcrGnupgKey:short-keyid:
 
231
         *
 
232
         * User readable key identifier.
 
233
         */
 
234
        g_object_class_install_property (gobject_class, PROP_SHORT_KEYID,
 
235
                 g_param_spec_string ("short-keyid", "Short Key ID", "Display key identifier",
 
236
                                      "", G_PARAM_READABLE));
 
237
 
 
238
        /**
 
239
         * GcrGnupgKey:icon:
 
240
         *
 
241
         * Icon for this key.
 
242
         */
 
243
        g_object_class_install_property (gobject_class, PROP_ICON,
 
244
                 g_param_spec_object ("icon", "Icon", "Icon for this key",
 
245
                                      G_TYPE_ICON, G_PARAM_READABLE));
 
246
}
 
247
 
 
248
/**
 
249
 * _gcr_gnupg_key_new:
 
250
 * @pubset: array of GcrRecord* representing public part of key
 
251
 * @secset: (allow-none): array of GcrRecord* representing secret part of key.
 
252
 *
 
253
 * Create a new GcrGnupgKey for the record data passed. If the secret part
 
254
 * of the key is set, then this represents a secret key; otherwise it represents
 
255
 * a public key.
 
256
 *
 
257
 * Returns: (transfer full): A newly allocated key.
 
258
 */
 
259
GcrGnupgKey*
 
260
_gcr_gnupg_key_new (GPtrArray *pubset, GPtrArray *secset)
 
261
{
 
262
        g_return_val_if_fail (pubset, NULL);
 
263
        return g_object_new (GCR_TYPE_GNUPG_KEY,
 
264
                             "public-records", pubset,
 
265
                             "secret-records", secset,
 
266
                             NULL);
 
267
}
 
268
 
 
269
/**
 
270
 * _gcr_gnupg_key_get_public_records:
 
271
 * @self: The key
 
272
 *
 
273
 * Get the record data this key is based on.
 
274
 *
 
275
 * Returns: (transfer none): An array of GcrRecord*.
 
276
 */
 
277
GPtrArray*
 
278
_gcr_gnupg_key_get_public_records (GcrGnupgKey *self)
 
279
{
 
280
        g_return_val_if_fail (GCR_IS_GNUPG_KEY (self), NULL);
 
281
        return self->pv->public_records;
 
282
}
 
283
 
 
284
/**
 
285
 * _gcr_gnupg_key_set_public_records:
 
286
 * @self: The key
 
287
 * @records: The new array of GcrRecord*
 
288
 *
 
289
 * Change the record data that this key is based on.
 
290
 */
 
291
void
 
292
_gcr_gnupg_key_set_public_records (GcrGnupgKey *self, GPtrArray *records)
 
293
{
 
294
        GObject *obj;
 
295
 
 
296
        g_return_if_fail (GCR_IS_GNUPG_KEY (self));
 
297
        g_return_if_fail (records);
 
298
 
 
299
        /* Check that it matches previous */
 
300
        if (self->pv->public_records) {
 
301
                const gchar *old_keyid = _gcr_gnupg_records_get_keyid (self->pv->public_records);
 
302
                const gchar *new_keyid = _gcr_gnupg_records_get_keyid (records);
 
303
 
 
304
                if (g_strcmp0 (old_keyid, new_keyid) != 0) {
 
305
                        g_warning ("it is an error to change a gnupg key so that the "
 
306
                                   "fingerprint is no longer the same: %s != %s",
 
307
                                   old_keyid, new_keyid);
 
308
                        return;
 
309
                }
 
310
        }
 
311
 
 
312
        g_ptr_array_ref (records);
 
313
        if (self->pv->public_records)
 
314
                g_ptr_array_unref (self->pv->public_records);
 
315
        self->pv->public_records = records;
 
316
 
 
317
        obj = G_OBJECT (self);
 
318
        g_object_freeze_notify (obj);
 
319
        g_object_notify (obj, "public-records");
 
320
        g_object_notify (obj, "label");
 
321
        g_object_notify (obj, "markup");
 
322
        g_object_thaw_notify (obj);
 
323
}
 
324
 
 
325
/**
 
326
 * _gcr_gnupg_key_get_secret_records:
 
327
 * @self: The key
 
328
 *
 
329
 * Get the record secret data this key is based on. %NULL if a public key.
 
330
 *
 
331
 * Returns: (transfer none) (allow-none): An array of GcrColons*.
 
332
 */
 
333
GPtrArray*
 
334
_gcr_gnupg_key_get_secret_records (GcrGnupgKey *self)
 
335
{
 
336
        g_return_val_if_fail (GCR_IS_GNUPG_KEY (self), NULL);
 
337
        return self->pv->secret_records;
 
338
}
 
339
 
 
340
/**
 
341
 * _gcr_gnupg_key_set_secret_records:
 
342
 * @self: The key
 
343
 * @records: (allow-none): The new array of GcrRecord*
 
344
 *
 
345
 * Set the secret data for this key. %NULL if public key.
 
346
 */
 
347
void
 
348
_gcr_gnupg_key_set_secret_records (GcrGnupgKey *self, GPtrArray *records)
 
349
{
 
350
        GObject *obj;
 
351
 
 
352
        g_return_if_fail (GCR_IS_GNUPG_KEY (self));
 
353
 
 
354
        /* Check that it matches public key */
 
355
        if (self->pv->public_records && records) {
 
356
                const gchar *pub_keyid = _gcr_gnupg_records_get_keyid (self->pv->public_records);
 
357
                const gchar *sec_keyid = _gcr_gnupg_records_get_keyid (records);
 
358
 
 
359
                if (g_strcmp0 (pub_keyid, sec_keyid) != 0) {
 
360
                        g_warning ("it is an error to create a gnupg key so that the "
 
361
                                   "fingerprint of thet pub and sec parts are not the same: %s != %s",
 
362
                                   pub_keyid, sec_keyid);
 
363
                        return;
 
364
                }
 
365
        }
 
366
 
 
367
        if (records)
 
368
                g_ptr_array_ref (records);
 
369
        if (self->pv->secret_records)
 
370
                g_ptr_array_unref (self->pv->secret_records);
 
371
        self->pv->secret_records = records;
 
372
 
 
373
        obj = G_OBJECT (self);
 
374
        g_object_freeze_notify (obj);
 
375
        g_object_notify (obj, "secret-records");
 
376
        g_object_thaw_notify (obj);
 
377
}
 
378
 
 
379
/**
 
380
 * _gcr_gnupg_key_get_keyid:
 
381
 * @self: The key
 
382
 *
 
383
 * Get the keyid for this key.
 
384
 *
 
385
 * Returns: (transfer none): The keyid.
 
386
 */
 
387
const gchar*
 
388
_gcr_gnupg_key_get_keyid (GcrGnupgKey *self)
 
389
{
 
390
        g_return_val_if_fail (GCR_IS_GNUPG_KEY (self), NULL);
 
391
        return _gcr_gnupg_records_get_keyid (self->pv->public_records);
 
392
}
 
393
 
 
394
/**
 
395
 * _gcr_gnupg_key_get_icon:
 
396
 * @self: A gnupg key.
 
397
 *
 
398
 * Get the display icon for this key.
 
399
 *
 
400
 * Return value: (transfer none): The icon, owned by the key.
 
401
 */
 
402
GIcon*
 
403
_gcr_gnupg_key_get_icon (GcrGnupgKey *self)
 
404
{
 
405
        g_return_val_if_fail (GCR_IS_GNUPG_KEY (self), NULL);
 
406
 
 
407
        if (self->pv->icon == NULL) {
 
408
                self->pv->icon = _gcr_gnupg_records_get_icon (self->pv->public_records);
 
409
                if (self->pv->icon == NULL) {
 
410
                        if (self->pv->secret_records)
 
411
                                self->pv->icon = g_themed_icon_new ("gcr-key-pair");
 
412
                        else
 
413
                                self->pv->icon = g_themed_icon_new ("gcr-key");
 
414
                }
 
415
        }
 
416
 
 
417
        return self->pv->icon;
 
418
}
 
419
 
 
420
/**
 
421
 * _gcr_gnupg_key_get_columns:
 
422
 *
 
423
 * Get the columns that we should display for gnupg keys.
 
424
 *
 
425
 * Returns: (transfer none): The columns, NULL terminated, should not be freed.
 
426
 */
 
427
const GcrColumn*
 
428
_gcr_gnupg_key_get_columns (void)
 
429
{
 
430
        static GcrColumn columns[] = {
 
431
                { "icon", /* later */ 0, /* later */ 0, NULL, 0, NULL, 0 },
 
432
                { "label", G_TYPE_STRING, G_TYPE_STRING, NC_("column", "Name"),
 
433
                  GCR_COLUMN_SORTABLE, NULL, 0 },
 
434
                { "short-keyid", G_TYPE_STRING, G_TYPE_STRING, NC_("column", "Key ID"),
 
435
                  GCR_COLUMN_SORTABLE, NULL, 0 },
 
436
                { NULL }
 
437
        };
 
438
 
 
439
        columns[0].property_type = columns[0].column_type = G_TYPE_ICON;
 
440
        return columns;
 
441
}