seahorse-gpgme-uid.c 10.6 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
/*
 * Seahorse
 *
 * Copyright (C) 2008 Stefan Walter
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 * See the GNU General Public License for more details.
 * You should have received a copy of the GNU General Public License
Daniel Mustieles's avatar
Daniel Mustieles committed
16
 * along with this program; if not, see <http://www.gnu.org/licenses/>.
17 18 19 20 21 22
 */

#include "config.h"

#include "seahorse-gpgme.h"
#include "seahorse-gpgme-uid.h"
23
#include "seahorse-object-list.h"
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66
#include "seahorse-pgp.h"
#include "seahorse-pgp-key.h"
#include "seahorse-pgp-signature.h"

#include <string.h>

#include <glib/gi18n.h>

enum {
	PROP_0,
	PROP_PUBKEY,
	PROP_USERID,
	PROP_GPGME_INDEX,
	PROP_ACTUAL_INDEX
};

G_DEFINE_TYPE (SeahorseGpgmeUid, seahorse_gpgme_uid, SEAHORSE_TYPE_PGP_UID);

struct _SeahorseGpgmeUidPrivate {
	gpgme_key_t pubkey;         /* The public key that this uid is part of */
	gpgme_user_id_t userid;     /* The userid referred to */
	guint gpgme_index;          /* The GPGME index of the UID */
	gint actual_index;          /* The actual index of this UID */
};

/* -----------------------------------------------------------------------------
 * INTERNAL HELPERS
 */

static gchar*
convert_string (const gchar *str)
{
	if (!str)
  	      return NULL;
    
	/* If not utf8 valid, assume latin 1 */
 	if (!g_utf8_validate (str, -1, NULL))
 		return g_convert (str, -1, "UTF-8", "ISO-8859-1", NULL, NULL, NULL);

	return g_strdup (str);
}

static void
67
realize_signatures (SeahorseGpgmeUid *self)
68 69 70 71 72 73
{
	gpgme_key_sig_t gsig;
	SeahorsePgpSignature *sig;
	GList *sigs = NULL;
	guint flags;
	
74 75 76 77 78 79 80 81
	g_return_if_fail (self->pv->pubkey);
	g_return_if_fail (self->pv->userid);
	
	/* If this key was loaded without signatures, then leave them as is */
	if ((self->pv->pubkey->keylist_mode & GPGME_KEYLIST_MODE_SIGS) == 0) 
		return;
	
	for (gsig = self->pv->userid->signatures; gsig; gsig = gsig->next) {
82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102
		sig = seahorse_pgp_signature_new (gsig->keyid);
		
		/* Order of parsing these flags is important */
		flags = 0;
		if (gsig->revoked)
			flags |= SEAHORSE_FLAG_REVOKED;
		if (gsig->expired)
			flags |= SEAHORSE_FLAG_EXPIRED;
		if (flags == 0 && !gsig->invalid)
			flags = SEAHORSE_FLAG_IS_VALID;
		if (gsig->exportable)
			flags |= SEAHORSE_FLAG_EXPORTABLE;
	
		seahorse_pgp_signature_set_flags (sig, flags);
		sigs = g_list_prepend (sigs, sig);
	}
	
	seahorse_pgp_uid_set_signatures (SEAHORSE_PGP_UID (self), sigs);
	seahorse_object_list_free (sigs);
}

103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123
static gboolean
compare_strings (const gchar *a, const gchar *b)
{
	if (a == b)
		return TRUE;
	if (!a || !b)
		return FALSE;
	return g_str_equal (a, b);
}

static gboolean
compare_pubkeys (gpgme_key_t a, gpgme_key_t b)
{
	g_assert (a);
	g_assert (b);
	
	g_return_val_if_fail (a->subkeys, FALSE);
	g_return_val_if_fail (b->subkeys, FALSE);
	
	return compare_strings (a->subkeys->keyid, b->subkeys->keyid);
}
124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144

/* -----------------------------------------------------------------------------
 * OBJECT 
 */

static void
seahorse_gpgme_uid_init (SeahorseGpgmeUid *self)
{
	self->pv = G_TYPE_INSTANCE_GET_PRIVATE (self, SEAHORSE_TYPE_GPGME_UID, SeahorseGpgmeUidPrivate);
	self->pv->gpgme_index = 0;
	self->pv->actual_index = -1;
}

static GObject*
seahorse_gpgme_uid_constructor (GType type, guint n_props, GObjectConstructParam *props)
{
	GObject *obj = G_OBJECT_CLASS (seahorse_gpgme_uid_parent_class)->constructor (type, n_props, props);
	SeahorseGpgmeUid *self = NULL;
	
	if (obj) {
		self = SEAHORSE_GPGME_UID (obj);
145
		g_object_set (self, "location", SEAHORSE_LOCATION_LOCAL, NULL);
146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177
	}
	
	return obj;
}

static void
seahorse_gpgme_uid_get_property (GObject *object, guint prop_id,
                               GValue *value, GParamSpec *pspec)
{
	SeahorseGpgmeUid *self = SEAHORSE_GPGME_UID (object);
	
	switch (prop_id) {
	case PROP_PUBKEY:
		g_value_set_boxed (value, seahorse_gpgme_uid_get_pubkey (self));
		break;
	case PROP_USERID:
		g_value_set_pointer (value, seahorse_gpgme_uid_get_userid (self));
		break;
	case PROP_GPGME_INDEX:
		g_value_set_uint (value, seahorse_gpgme_uid_get_gpgme_index (self));
		break;
	case PROP_ACTUAL_INDEX:
		g_value_set_uint (value, seahorse_gpgme_uid_get_actual_index (self));
		break;
	}
}

static void
seahorse_gpgme_uid_set_property (GObject *object, guint prop_id, const GValue *value, 
                               GParamSpec *pspec)
{
	SeahorseGpgmeUid *self = SEAHORSE_GPGME_UID (object);
178 179
	gpgme_key_t pubkey;
	
180 181
	switch (prop_id) {
	case PROP_PUBKEY:
182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199
		pubkey = g_value_get_boxed (value);
		g_return_if_fail (pubkey);

		if (pubkey != self->pv->pubkey) {
			
			if (self->pv->pubkey) {
				/* Should always be set to the same actual key */
				g_return_if_fail (compare_pubkeys (pubkey, self->pv->pubkey));
				gpgme_key_unref (self->pv->pubkey);
			}
			
			self->pv->pubkey = g_value_get_boxed (value);
			if (self->pv->pubkey)
				gpgme_key_ref (self->pv->pubkey);
			
			/* This is expected to be set shortly along with pubkey */
			self->pv->userid = NULL;
		}
200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238
		break;
	case PROP_ACTUAL_INDEX:
		seahorse_gpgme_uid_set_actual_index (self, g_value_get_uint (value));
		break;
	case PROP_USERID:
		seahorse_gpgme_uid_set_userid (self, g_value_get_pointer (value));
		break;
	}
}

static void
seahorse_gpgme_uid_object_finalize (GObject *gobject)
{
	SeahorseGpgmeUid *self = SEAHORSE_GPGME_UID (gobject);

	/* Unref the key */
	if (self->pv->pubkey)
		gpgme_key_unref (self->pv->pubkey);
	self->pv->pubkey = NULL;
	self->pv->userid = NULL;
    
	G_OBJECT_CLASS (seahorse_gpgme_uid_parent_class)->finalize (gobject);
}

static void
seahorse_gpgme_uid_class_init (SeahorseGpgmeUidClass *klass)
{
	GObjectClass *gobject_class = G_OBJECT_CLASS (klass);    

	seahorse_gpgme_uid_parent_class = g_type_class_peek_parent (klass);
	g_type_class_add_private (klass, sizeof (SeahorseGpgmeUidPrivate));

	gobject_class->constructor = seahorse_gpgme_uid_constructor;
	gobject_class->finalize = seahorse_gpgme_uid_object_finalize;
	gobject_class->set_property = seahorse_gpgme_uid_set_property;
	gobject_class->get_property = seahorse_gpgme_uid_get_property;
    
	g_object_class_install_property (gobject_class, PROP_PUBKEY,
	        g_param_spec_boxed ("pubkey", "Public Key", "GPGME Public Key that this uid is on",
239
	                            SEAHORSE_GPGME_BOXED_KEY, G_PARAM_READWRITE));
240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293

	g_object_class_install_property (gobject_class, PROP_USERID,
	        g_param_spec_pointer ("userid", "User ID", "GPGME User ID",
	                              G_PARAM_READWRITE));
                      
	g_object_class_install_property (gobject_class, PROP_GPGME_INDEX,
	        g_param_spec_uint ("gpgme-index", "GPGME Index", "GPGME User ID Index",
	                           0, G_MAXUINT, 0, G_PARAM_READWRITE));
	
	g_object_class_install_property (gobject_class, PROP_ACTUAL_INDEX,
	        g_param_spec_uint ("actual-index", "Actual Index", "Actual GPG Index",
	                           0, G_MAXUINT, 0, G_PARAM_READWRITE));
}

/* -----------------------------------------------------------------------------
 * PUBLIC 
 */

SeahorseGpgmeUid* 
seahorse_gpgme_uid_new (gpgme_key_t pubkey, gpgme_user_id_t userid) 
{
	return g_object_new (SEAHORSE_TYPE_GPGME_UID, 
	                     "pubkey", pubkey, 
	                     "userid", userid, NULL);
}

gpgme_key_t
seahorse_gpgme_uid_get_pubkey (SeahorseGpgmeUid *self)
{
	g_return_val_if_fail (SEAHORSE_IS_GPGME_UID (self), NULL);
	g_return_val_if_fail (self->pv->pubkey, NULL);
	return self->pv->pubkey;
}

gpgme_user_id_t
seahorse_gpgme_uid_get_userid (SeahorseGpgmeUid *self)
{
	g_return_val_if_fail (SEAHORSE_IS_GPGME_UID (self), NULL);
	g_return_val_if_fail (self->pv->userid, NULL);
	return self->pv->userid;
}

void
seahorse_gpgme_uid_set_userid (SeahorseGpgmeUid *self, gpgme_user_id_t userid)
{
	SeahorsePgpUid *base;
	GObject *obj;
	gpgme_user_id_t uid;
	gchar *string;
	gint index, i;
	
	g_return_if_fail (SEAHORSE_IS_GPGME_UID (self));
	g_return_if_fail (userid);
	
294 295 296
	if (self->pv->userid)
		g_return_if_fail (seahorse_gpgme_uid_is_same (self, userid));
	
297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329
	/* Make sure that this userid is in the pubkey */
	index = -1;
	for (i = 0, uid = self->pv->pubkey->uids; uid; ++i, uid = uid->next) {
		if(userid == uid) {
			index = i;
			break;
		}
	}
	
	g_return_if_fail (index >= 0);
	
	self->pv->userid = userid;
	self->pv->gpgme_index = index;
	
	obj = G_OBJECT (self);
	g_object_freeze_notify (obj);
	g_object_notify (obj, "userid");
	g_object_notify (obj, "gpgme_index");
	
	base = SEAHORSE_PGP_UID (self);
	
	string = convert_string (userid->name);
	seahorse_pgp_uid_set_name (base, string);
	g_free (string);
	
	string = convert_string (userid->email);
	seahorse_pgp_uid_set_email (base, string);
	g_free (string);
	
	string = convert_string (userid->comment);
	seahorse_pgp_uid_set_comment (base, string);
	g_free (string);
	
330
	realize_signatures (self);
331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348

	seahorse_pgp_uid_set_validity (base, seahorse_gpgme_convert_validity (userid->validity));

	g_object_thaw_notify (obj);
}

guint
seahorse_gpgme_uid_get_gpgme_index (SeahorseGpgmeUid *self)
{
	g_return_val_if_fail (SEAHORSE_IS_GPGME_UID (self), 0);
	return self->pv->gpgme_index;
}

guint
seahorse_gpgme_uid_get_actual_index (SeahorseGpgmeUid *self)
{
	g_return_val_if_fail (SEAHORSE_IS_GPGME_UID (self), 0);
	if(self->pv->actual_index < 0)
349
		return self->pv->gpgme_index + 1;
350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393
	return self->pv->actual_index;
}

void
seahorse_gpgme_uid_set_actual_index (SeahorseGpgmeUid *self, guint actual_index)
{
	g_return_if_fail (SEAHORSE_IS_GPGME_UID (self));
	self->pv->actual_index = actual_index;
	g_object_notify (G_OBJECT (self), "actual-index");
}

gchar*
seahorse_gpgme_uid_calc_label (gpgme_user_id_t userid)
{
	g_return_val_if_fail (userid, NULL);
	return convert_string (userid->uid);
}

gchar*
seahorse_gpgme_uid_calc_name (gpgme_user_id_t userid)
{
	g_return_val_if_fail (userid, NULL);
	return convert_string (userid->name);
}

gchar*
seahorse_gpgme_uid_calc_markup (gpgme_user_id_t userid, guint flags)
{
	gchar *email, *name, *comment, *ret;

	g_return_val_if_fail (userid, NULL);
	
	name = convert_string (userid->name);
	email = convert_string (userid->email);
	comment = convert_string (userid->comment);
	
	ret = seahorse_pgp_uid_calc_markup (name, email, comment, flags);
	
	g_free (name);
	g_free (email);
	g_free (comment);
	
	return ret;
}
394 395 396 397 398 399 400 401 402

gboolean
seahorse_gpgme_uid_is_same (SeahorseGpgmeUid *self, gpgme_user_id_t userid)
{
	g_return_val_if_fail (SEAHORSE_IS_GPGME_UID (self), FALSE);
	g_return_val_if_fail (userid, FALSE);
	
	return compare_strings (self->pv->userid->uid, userid->uid);	
}