2
* Copyright (C) 2011 Collabora Ltd.
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.
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.
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
19
* Author: Stef Walter <stefw@collabora.co.uk>
24
#include "gcr-gnupg-key.h"
25
#include "gcr-gnupg-records.h"
26
#include "gcr-record.h"
27
#include "gcr-memory-icon.h"
31
#include <glib/gi18n-lib.h>
45
struct _GcrGnupgKeyPrivate {
46
GPtrArray *public_records;
47
GPtrArray *secret_records;
51
G_DEFINE_TYPE (GcrGnupgKey, _gcr_gnupg_key, G_TYPE_OBJECT);
54
calculate_name (GcrGnupgKey *self)
58
record = _gcr_records_find (self->pv->public_records, GCR_RECORD_SCHEMA_UID);
59
g_return_val_if_fail (record, NULL);
61
return _gcr_record_get_string (record, GCR_RECORD_UID_USERID);
65
calculate_markup (GcrGnupgKey *self)
68
gchar *uid, *name, *email, *comment;
70
uid = calculate_name (self);
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);
78
markup = g_markup_printf_escaped ("%s\n<small>%s</small>", name, email);
88
_gcr_gnupg_key_init (GcrGnupgKey *self)
90
self->pv = (G_TYPE_INSTANCE_GET_PRIVATE (self, GCR_TYPE_GNUPG_KEY, GcrGnupgKeyPrivate));
94
_gcr_gnupg_key_finalize (GObject *obj)
96
GcrGnupgKey *self = GCR_GNUPG_KEY (obj);
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);
103
G_OBJECT_CLASS (_gcr_gnupg_key_parent_class)->finalize (obj);
107
_gcr_gnupg_key_set_property (GObject *obj, guint prop_id, const GValue *value,
110
GcrGnupgKey *self = GCR_GNUPG_KEY (obj);
113
case PROP_PUBLIC_RECORDS:
114
_gcr_gnupg_key_set_public_records (self, g_value_get_boxed (value));
116
case PROP_SECRET_RECORDS:
117
_gcr_gnupg_key_set_secret_records (self, g_value_get_boxed (value));
120
G_OBJECT_WARN_INVALID_PROPERTY_ID (obj, prop_id, pspec);
126
_gcr_gnupg_key_get_property (GObject *obj, guint prop_id, GValue *value,
129
GcrGnupgKey *self = GCR_GNUPG_KEY (obj);
132
case PROP_PUBLIC_RECORDS:
133
g_value_set_boxed (value, self->pv->public_records);
135
case PROP_SECRET_RECORDS:
136
g_value_set_boxed (value, self->pv->secret_records);
139
g_value_set_string (value, _gcr_gnupg_key_get_keyid (self));
142
g_value_take_string (value, calculate_name (self));
144
case PROP_DESCRIPTION:
145
g_value_set_string (value, _("PGP Key"));
148
g_value_take_string (value, calculate_markup (self));
150
case PROP_SHORT_KEYID:
151
g_value_set_string (value, _gcr_gnupg_records_get_short_keyid (self->pv->public_records));
154
g_value_set_object (value, _gcr_gnupg_key_get_icon (self));
157
G_OBJECT_WARN_INVALID_PROPERTY_ID (obj, prop_id, pspec);
163
_gcr_gnupg_key_class_init (GcrGnupgKeyClass *klass)
165
GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
167
_gcr_gnupg_key_parent_class = g_type_class_peek_parent (klass);
168
g_type_class_add_private (klass, sizeof (GcrGnupgKeyPrivate));
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;
175
* GcrGnupgKey:public-records:
177
* Public key data. Should always be present.
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));
184
* GcrGnupgKey:secret-records:
186
* Secret key data. The keyid of this data must match public-dataset.
187
* If present, this key represents a secret key.
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));
198
g_object_class_install_property (gobject_class, PROP_KEYID,
199
g_param_spec_string ("keyid", "Key ID", "Key identifier",
200
"", G_PARAM_READABLE));
205
* User readable label for this key.
207
g_object_class_install_property (gobject_class, PROP_LABEL,
208
g_param_spec_string ("label", "Label", "Key label",
209
"", G_PARAM_READABLE));
212
* GcrGnupgKey::description:
214
* Description of type of key.
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));
221
* GcrGnupgKey:markup:
223
* User readable markup which contains key label.
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));
230
* GcrGnupgKey:short-keyid:
232
* User readable key identifier.
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));
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));
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.
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
257
* Returns: (transfer full): A newly allocated key.
260
_gcr_gnupg_key_new (GPtrArray *pubset, GPtrArray *secset)
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,
270
* _gcr_gnupg_key_get_public_records:
273
* Get the record data this key is based on.
275
* Returns: (transfer none): An array of GcrRecord*.
278
_gcr_gnupg_key_get_public_records (GcrGnupgKey *self)
280
g_return_val_if_fail (GCR_IS_GNUPG_KEY (self), NULL);
281
return self->pv->public_records;
285
* _gcr_gnupg_key_set_public_records:
287
* @records: The new array of GcrRecord*
289
* Change the record data that this key is based on.
292
_gcr_gnupg_key_set_public_records (GcrGnupgKey *self, GPtrArray *records)
296
g_return_if_fail (GCR_IS_GNUPG_KEY (self));
297
g_return_if_fail (records);
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);
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);
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;
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);
326
* _gcr_gnupg_key_get_secret_records:
329
* Get the record secret data this key is based on. %NULL if a public key.
331
* Returns: (transfer none) (allow-none): An array of GcrColons*.
334
_gcr_gnupg_key_get_secret_records (GcrGnupgKey *self)
336
g_return_val_if_fail (GCR_IS_GNUPG_KEY (self), NULL);
337
return self->pv->secret_records;
341
* _gcr_gnupg_key_set_secret_records:
343
* @records: (allow-none): The new array of GcrRecord*
345
* Set the secret data for this key. %NULL if public key.
348
_gcr_gnupg_key_set_secret_records (GcrGnupgKey *self, GPtrArray *records)
352
g_return_if_fail (GCR_IS_GNUPG_KEY (self));
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);
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);
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;
373
obj = G_OBJECT (self);
374
g_object_freeze_notify (obj);
375
g_object_notify (obj, "secret-records");
376
g_object_thaw_notify (obj);
380
* _gcr_gnupg_key_get_keyid:
383
* Get the keyid for this key.
385
* Returns: (transfer none): The keyid.
388
_gcr_gnupg_key_get_keyid (GcrGnupgKey *self)
390
g_return_val_if_fail (GCR_IS_GNUPG_KEY (self), NULL);
391
return _gcr_gnupg_records_get_keyid (self->pv->public_records);
395
* _gcr_gnupg_key_get_icon:
396
* @self: A gnupg key.
398
* Get the display icon for this key.
400
* Return value: (transfer none): The icon, owned by the key.
403
_gcr_gnupg_key_get_icon (GcrGnupgKey *self)
405
g_return_val_if_fail (GCR_IS_GNUPG_KEY (self), NULL);
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");
413
self->pv->icon = g_themed_icon_new ("gcr-key");
417
return self->pv->icon;
421
* _gcr_gnupg_key_get_columns:
423
* Get the columns that we should display for gnupg keys.
425
* Returns: (transfer none): The columns, NULL terminated, should not be freed.
428
_gcr_gnupg_key_get_columns (void)
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 },
439
columns[0].property_type = columns[0].column_type = G_TYPE_ICON;