Revision 56 src/select-keys.c
| select-keys.c (revision 56) | ||
|---|---|---|
| 45 | 45 |
#include "gtkutils.h" |
| 46 | 46 |
#include "inputdialog.h" |
| 47 | 47 |
#include "manage_window.h" |
| 48 |
#include "alertpanel.h" |
|
| 48 | 49 |
|
| 49 | 50 |
#define DIM(v) (sizeof(v)/sizeof((v)[0])) |
| 50 | 51 |
#define DIMof(type,member) DIM(((type *)0)->member) |
| ... | ... | |
| 66 | 67 |
GtkLabel *toplabel; |
| 67 | 68 |
GtkCList *clist; |
| 68 | 69 |
const char *pattern; |
| 69 |
GpgmeRecipients rset; |
|
| 70 |
GpgmeCtx select_ctx; |
|
| 70 |
unsigned int num_keys; |
|
| 71 |
gpgme_key_t *kset; |
|
| 72 |
gpgme_ctx_t select_ctx; |
|
| 71 | 73 |
|
| 72 | 74 |
GtkSortType sort_type; |
| 73 | 75 |
enum col_titles sort_column; |
| ... | ... | |
| 75 | 77 |
}; |
| 76 | 78 |
|
| 77 | 79 |
|
| 78 |
static void set_row (GtkCList *clist, GpgmeKey key);
|
|
| 80 |
static void set_row (GtkCList *clist, gpgme_key_t key);
|
|
| 79 | 81 |
static void fill_clist (struct select_keys_s *sk, const char *pattern); |
| 80 | 82 |
static void create_dialog (struct select_keys_s *sk); |
| 81 | 83 |
static void open_dialog (struct select_keys_s *sk); |
| ... | ... | |
| 91 | 93 |
static void sort_keys_name (GtkWidget *widget, gpointer data); |
| 92 | 94 |
static void sort_keys_email (GtkWidget *widget, gpointer data); |
| 93 | 95 |
|
| 96 |
static gboolean use_untrusted (gpgme_key_t); |
|
| 94 | 97 |
|
| 95 | 98 |
static void |
| 96 | 99 |
update_progress (struct select_keys_s *sk, int running, const char *pattern) |
| ... | ... | |
| 111 | 114 |
|
| 112 | 115 |
|
| 113 | 116 |
/** |
| 114 |
* select_keys_get_recipients:
|
|
| 117 |
* gpgmegtk_recipient_selection:
|
|
| 115 | 118 |
* @recp_names: A list of email addresses |
| 116 | 119 |
* |
| 117 | 120 |
* Select a list of recipients from a given list of email addresses. |
| ... | ... | |
| 120 | 123 |
* |
| 121 | 124 |
* Return value: NULL on error or a list of list of recipients. |
| 122 | 125 |
**/ |
| 123 |
GpgmeRecipients
|
|
| 126 |
gpgme_key_t *
|
|
| 124 | 127 |
gpgmegtk_recipient_selection (GSList *recp_names) |
| 125 | 128 |
{
|
| 126 | 129 |
struct select_keys_s sk; |
| 127 |
GpgmeError err; |
|
| 128 | 130 |
|
| 129 | 131 |
memset (&sk, 0, sizeof sk); |
| 130 | 132 |
|
| 131 |
err = gpgme_recipients_new (&sk.rset); |
|
| 132 |
if (err) {
|
|
| 133 |
g_warning ("failed to allocate recipients set: %s",
|
|
| 134 |
gpgme_strerror (err)); |
|
| 135 |
return NULL; |
|
| 136 |
} |
|
| 137 |
|
|
| 138 | 133 |
open_dialog (&sk); |
| 139 | 134 |
|
| 140 | 135 |
do {
|
| ... | ... | |
| 150 | 145 |
close_dialog (&sk); |
| 151 | 146 |
|
| 152 | 147 |
if (!sk.okay) {
|
| 153 |
gpgme_recipients_release (sk.rset); |
|
| 154 |
sk.rset = NULL; |
|
| 148 |
g_free(sk.kset); |
|
| 149 |
sk.kset = NULL; |
|
| 150 |
} else {
|
|
| 151 |
sk.kset = g_realloc(sk.kset, sizeof(gpgme_key_t) * (sk.num_keys + 1)); |
|
| 152 |
sk.kset[sk.num_keys] = NULL; |
|
| 155 | 153 |
} |
| 156 |
return sk.rset;
|
|
| 154 |
return sk.kset;
|
|
| 157 | 155 |
} |
| 158 | 156 |
|
| 159 | 157 |
static void |
| 160 | 158 |
destroy_key (gpointer data) |
| 161 | 159 |
{
|
| 162 |
GpgmeKey key = data;
|
|
| 160 |
gpgme_key_t key = data;
|
|
| 163 | 161 |
gpgme_key_release (key); |
| 164 | 162 |
} |
| 165 | 163 |
|
| 166 | 164 |
static void |
| 167 |
set_row (GtkCList *clist, GpgmeKey key)
|
|
| 165 |
set_row (GtkCList *clist, gpgme_key_t key)
|
|
| 168 | 166 |
{
|
| 169 | 167 |
const char *s; |
| 170 | 168 |
const char *text[N_COL_TITLES]; |
| ... | ... | |
| 173 | 171 |
|
| 174 | 172 |
/* first check whether the key is capable of encryption which is not |
| 175 | 173 |
* the case for revoked, expired or sign-only keys */ |
| 176 |
if ( !gpgme_key_get_ulong_attr (key, GPGME_ATTR_CAN_ENCRYPT, NULL, 0 ) )
|
|
| 174 |
if (!key->can_encrypt)
|
|
| 177 | 175 |
return; |
| 178 |
|
|
| 179 |
algo_buf = g_strdup_printf ("%lu/%s",
|
|
| 180 |
gpgme_key_get_ulong_attr (key, GPGME_ATTR_LEN, NULL, 0 ), |
|
| 181 |
gpgme_key_get_string_attr (key, GPGME_ATTR_ALGO, NULL, 0 ) ); |
|
| 176 |
algo_buf = g_strdup_printf ("%du/%s",
|
|
| 177 |
key->subkeys->length, |
|
| 178 |
gpgme_pubkey_algo_name(key->subkeys->pubkey_algo) ); |
|
| 182 | 179 |
text[COL_ALGO] = algo_buf; |
| 183 | 180 |
|
| 184 |
s = gpgme_key_get_string_attr (key, GPGME_ATTR_KEYID, NULL, 0);
|
|
| 181 |
s = key->subkeys->keyid;
|
|
| 185 | 182 |
if (strlen (s) == 16) |
| 186 | 183 |
s += 8; /* show only the short keyID */ |
| 187 | 184 |
text[COL_KEYID] = s; |
| 188 | 185 |
|
| 189 |
s = gpgme_key_get_string_attr (key, GPGME_ATTR_NAME, NULL, 0);
|
|
| 186 |
s = key->uids->name;
|
|
| 190 | 187 |
text[COL_NAME] = s; |
| 191 | 188 |
|
| 192 |
s = gpgme_key_get_string_attr (key, GPGME_ATTR_EMAIL, NULL, 0);
|
|
| 189 |
s = key->uids->email;
|
|
| 193 | 190 |
text[COL_EMAIL] = s; |
| 194 | 191 |
|
| 195 |
s = gpgme_key_get_string_attr (key, GPGME_ATTR_VALIDITY, NULL, 0); |
|
| 192 |
switch (key->uids->validity) |
|
| 193 |
{
|
|
| 194 |
case GPGME_VALIDITY_UNDEFINED: |
|
| 195 |
s = "q"; |
|
| 196 |
break; |
|
| 197 |
case GPGME_VALIDITY_NEVER: |
|
| 198 |
s = "n"; |
|
| 199 |
break; |
|
| 200 |
case GPGME_VALIDITY_MARGINAL: |
|
| 201 |
s = "m"; |
|
| 202 |
break; |
|
| 203 |
case GPGME_VALIDITY_FULL: |
|
| 204 |
s = "f"; |
|
| 205 |
break; |
|
| 206 |
case GPGME_VALIDITY_ULTIMATE: |
|
| 207 |
s = "u"; |
|
| 208 |
break; |
|
| 209 |
case GPGME_VALIDITY_UNKNOWN: |
|
| 210 |
default: |
|
| 211 |
s = "?"; |
|
| 212 |
break; |
|
| 213 |
} |
|
| 196 | 214 |
text[COL_VALIDITY] = s; |
| 197 | 215 |
|
| 198 | 216 |
row = gtk_clist_append (clist, (gchar**)text); |
| ... | ... | |
| 201 | 219 |
gtk_clist_set_row_data_full (clist, row, key, destroy_key); |
| 202 | 220 |
} |
| 203 | 221 |
|
| 204 |
|
|
| 205 | 222 |
static void |
| 206 | 223 |
fill_clist (struct select_keys_s *sk, const char *pattern) |
| 207 | 224 |
{
|
| 208 | 225 |
GtkCList *clist; |
| 209 |
GpgmeCtx ctx;
|
|
| 210 |
GpgmeError err;
|
|
| 211 |
GpgmeKey key;
|
|
| 226 |
gpgme_ctx_t ctx;
|
|
| 227 |
gpgme_error_t err;
|
|
| 228 |
gpgme_key_t key;
|
|
| 212 | 229 |
int running=0; |
| 213 | 230 |
|
| 214 | 231 |
g_return_if_fail (sk); |
| ... | ... | |
| 232 | 249 |
debug_print ("** gpgme_op_keylist_start(%s) failed: %s",
|
| 233 | 250 |
pattern, gpgme_strerror (err)); |
| 234 | 251 |
sk->select_ctx = NULL; |
| 252 |
gpgme_release(ctx); |
|
| 235 | 253 |
return; |
| 236 | 254 |
} |
| 237 | 255 |
update_progress (sk, ++running, pattern); |
| ... | ... | |
| 243 | 261 |
gtk_main_iteration (); |
| 244 | 262 |
} |
| 245 | 263 |
debug_print ("%% %s:%d: ready\n", __FILE__ ,__LINE__ );
|
| 246 |
if (err != GPGME_EOF)
|
|
| 264 |
if (gpgme_err_code(err) != GPG_ERR_EOF) {
|
|
| 247 | 265 |
debug_print ("** gpgme_op_keylist_next failed: %s",
|
| 248 | 266 |
gpgme_strerror (err)); |
| 267 |
gpgme_op_keylist_end(ctx); |
|
| 268 |
} |
|
| 249 | 269 |
sk->select_ctx = NULL; |
| 250 | 270 |
gpgme_release (ctx); |
| 251 | 271 |
/*gtk_clist_thaw (select_keys.clist);*/ |
| ... | ... | |
| 396 | 416 |
{
|
| 397 | 417 |
struct select_keys_s *sk = data; |
| 398 | 418 |
int row; |
| 399 |
GpgmeKey key; |
|
| 419 |
gboolean use_key; |
|
| 420 |
gpgme_key_t key; |
|
| 400 | 421 |
|
| 401 | 422 |
g_return_if_fail (sk); |
| 402 | 423 |
if (!sk->clist->selection) {
|
| ... | ... | |
| 406 | 427 |
row = GPOINTER_TO_INT(sk->clist->selection->data); |
| 407 | 428 |
key = gtk_clist_get_row_data(sk->clist, row); |
| 408 | 429 |
if (key) {
|
| 409 |
const char *s = gpgme_key_get_string_attr (key,
|
|
| 410 |
GPGME_ATTR_FPR,
|
|
| 411 |
NULL, 0 );
|
|
| 412 |
if ( gpgme_key_get_ulong_attr (key, GPGME_ATTR_VALIDITY, NULL, 0 )
|
|
| 413 |
< GPGME_VALIDITY_FULL ) {
|
|
| 414 |
debug_print ("** FIXME: we are faking the trust calculation");
|
|
| 430 |
if ( key->uids->validity < GPGME_VALIDITY_FULL ) {
|
|
| 431 |
use_key = use_untrusted(key);
|
|
| 432 |
if (!use_key) {
|
|
| 433 |
debug_print ("** Key untrusted, will not encrypt");
|
|
| 434 |
return;
|
|
| 435 |
}
|
|
| 415 | 436 |
} |
| 416 |
if (!gpgme_recipients_add_name_with_validity (sk->rset, s, |
|
| 417 |
GPGME_VALIDITY_FULL) ) {
|
|
| 437 |
sk->kset = g_realloc(sk->kset, |
|
| 438 |
sizeof(gpgme_key_t) * (sk->num_keys + 1)); |
|
| 439 |
gpgme_key_ref(key); |
|
| 440 |
sk->kset[sk->num_keys] = key; |
|
| 441 |
sk->num_keys++; |
|
| 418 | 442 |
sk->okay = 1; |
| 419 | 443 |
gtk_main_quit (); |
| 420 | 444 |
} |
| 421 |
} |
|
| 422 | 445 |
} |
| 423 | 446 |
|
| 424 | 447 |
|
| ... | ... | |
| 453 | 476 |
} |
| 454 | 477 |
|
| 455 | 478 |
|
| 479 |
static gboolean |
|
| 480 |
use_untrusted (gpgme_key_t key) |
|
| 481 |
{
|
|
| 482 |
AlertValue aval; |
|
| 483 |
|
|
| 484 |
aval = alertpanel |
|
| 485 |
(_("Trust key"),
|
|
| 486 |
_("The selected key is not fully trusted.\n"
|
|
| 487 |
"If you choose to encrypt the message with this key you don't\n" |
|
| 488 |
"know for sure that it will go to the person you mean it to.\n" |
|
| 489 |
"Do you trust it enough to use it anyway?"), |
|
| 490 |
GTK_STOCK_YES, GTK_STOCK_NO, NULL); |
|
| 491 |
if (aval == G_ALERTDEFAULT) |
|
| 492 |
return TRUE; |
|
| 493 |
else |
|
| 494 |
return FALSE; |
|
| 495 |
} |
|
| 496 |
|
|
| 497 |
|
|
| 456 | 498 |
static gint |
| 457 |
cmp_attr (gconstpointer pa, gconstpointer pb, GpgmeAttr attr)
|
|
| 499 |
cmp_name (GtkCList *clist, gconstpointer pa, gconstpointer pb)
|
|
| 458 | 500 |
{
|
| 459 |
GpgmeKey a = ((GtkCListRow *)pa)->data;
|
|
| 460 |
GpgmeKey b = ((GtkCListRow *)pb)->data;
|
|
| 501 |
gpgme_key_t a = ((GtkCListRow *)pa)->data;
|
|
| 502 |
gpgme_key_t b = ((GtkCListRow *)pb)->data;
|
|
| 461 | 503 |
const char *sa, *sb; |
| 462 | 504 |
|
| 463 |
sa = a? gpgme_key_get_string_attr (a, attr, NULL, 0 ) : NULL;
|
|
| 464 |
sb = b? gpgme_key_get_string_attr (b, attr, NULL, 0 ) : NULL;
|
|
| 505 |
sa = a? a->uids->name : NULL;
|
|
| 506 |
sb = b? b->uids->name : NULL;
|
|
| 465 | 507 |
if (!sa) |
| 466 | 508 |
return !!sb; |
| 467 | 509 |
if (!sb) |
| ... | ... | |
| 470 | 512 |
} |
| 471 | 513 |
|
| 472 | 514 |
static gint |
| 473 |
cmp_name (GtkCList *clist, gconstpointer pa, gconstpointer pb) |
|
| 474 |
{
|
|
| 475 |
return cmp_attr (pa, pb, GPGME_ATTR_NAME); |
|
| 476 |
} |
|
| 477 |
|
|
| 478 |
static gint |
|
| 479 | 515 |
cmp_email (GtkCList *clist, gconstpointer pa, gconstpointer pb) |
| 480 | 516 |
{
|
| 481 |
return cmp_attr (pa, pb, GPGME_ATTR_EMAIL); |
|
| 517 |
gpgme_key_t a = ((GtkCListRow *)pa)->data; |
|
| 518 |
gpgme_key_t b = ((GtkCListRow *)pb)->data; |
|
| 519 |
const char *sa, *sb; |
|
| 520 |
|
|
| 521 |
sa = a? a->uids->email : NULL; |
|
| 522 |
sb = b? b->uids->email : NULL; |
|
| 523 |
if (!sa) |
|
| 524 |
return !!sb; |
|
| 525 |
if (!sb) |
|
| 526 |
return -1; |
|
| 527 |
return strcasecmp(sa, sb); |
|
| 482 | 528 |
} |
| 483 | 529 |
|
| 484 | 530 |
static void |
Also available in: Unified diff