/[winpt]/trunk/Src/wptKeysigDlg.cpp
ViewVC logotype

Contents of /trunk/Src/wptKeysigDlg.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 328 - (show annotations)
Fri Sep 25 16:07:38 2009 UTC (15 years, 5 months ago) by twoaday
File size: 15042 byte(s)


1 /* wptKeysigDlg.cpp - Key signature listing
2 * Copyright (C) 2001-2006, 2008 Timo Schulz
3 *
4 * This file is part of WinPT.
5 *
6 * WinPT is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * WinPT is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 */
16 #ifdef HAVE_CONFIG_H
17 #include <config.h>
18 #endif
19
20 #include <windows.h>
21 #include <commctrl.h>
22 #include <time.h>
23
24 #include "resource.h"
25 #include "wptGPG.h"
26 #include "wptCommonCtl.h"
27 #include "wptContext.h" /* for passphrase_s */
28 #include "wptDlgs.h"
29 #include "wptW32API.h"
30 #include "wptNLS.h"
31 #include "wptKeylist.h"
32 #include "wptKeyserver.h"
33 #include "wptUTF8.h"
34 #include "wptTypes.h"
35 #include "wptVersion.h"
36 #include "wptErrors.h"
37 #include "wptKeyEdit.h"
38
39 static subclass_s siglist_proc;
40
41
42 /* Return -1 if the selected item is a signature. */
43 static int
44 is_sig (listview_ctrl_t lv, int pos)
45 {
46 char tmpbuf[200];
47
48 if (pos == -1)
49 pos = listview_get_curr_pos (lv);
50 if (pos == -1)
51 return 0;
52 listview_get_item_text (lv, pos, SL_COL_UID, tmpbuf, sizeof (tmpbuf)-1);
53 if (*tmpbuf == ' ')
54 return -1;
55 return 0;
56 }
57
58
59 /* Delete the selected signature from list view @lv. The
60 key is given in @key. */
61 static int
62 do_delsig (HWND dlg, listview_ctrl_t lv, winpt_key_t key)
63 {
64 gpgme_error_t err;
65 GpgKeyEdit ke;
66 char keyid[32];
67 int pos, npos, id, is_selfsig=0;
68 int signo=0, uidno=0;
69
70 npos = pos = listview_get_curr_pos (lv);
71 if (!is_sig (lv, -1))
72 return -1;
73 listview_get_item_text (lv, pos, SL_COL_KEYID, keyid, sizeof (keyid)-1);
74 if (!strncmp (key->keyid, keyid+2, 8))
75 is_selfsig = 1;
76
77 while (pos > 0 && is_sig (lv, pos)) {
78 signo++;
79 pos--;
80 }
81 pos = npos;
82 while (npos > 0) {
83 if (!is_sig (lv, npos))
84 uidno++;
85 npos--;
86 }
87 uidno++;
88
89 /* XXX: do not allow to delete the self signature */
90 id = log_box (_("Key Manager"), is_selfsig? MB_WARN_ASK : MB_QUEST_ASK,
91 _("Are you really sure you want to delete this %s from\n\n"
92 " \"%s\""), is_selfsig? _("self signature") : _("signature"),
93 key->uid);
94 if (id == IDNO)
95 return 0;
96
97 ke.setKeyID (key->keyid);
98 err = ke.delUseridSignature (uidno, signo);
99 if (err)
100 msg_box (dlg, gpgme_strerror (err), _("Key Manager"), MB_ERR);
101 else
102 listview_del_item (lv, pos);
103 if (!err)
104 key->update = 1;
105 return err? -1 : 0;
106 }
107
108
109 /* Initialize a datepicker control with the time given in @t. */
110 static void
111 init_datepicker_from_time (HWND picker, time_t t)
112 {
113 SYSTEMTIME st;
114 struct tm *tm;
115
116 tm = localtime (&t);
117 memset (&st, 0, sizeof st);
118 st.wYear = tm->tm_year+1900;
119 st.wMonth = tm->tm_mon+1;
120 st.wDay = tm->tm_mday;
121 DateTime_SetSystemtime (picker, GDT_VALID, &st);
122 }
123
124
125 /* Dialog box procedure to display the signature properties. */
126 BOOL CALLBACK
127 sigprops_dlg_proc (HWND dlg, UINT msg, WPARAM wparam, LPARAM lparam)
128 {
129 gpgme_key_sig_t ks;
130 winpt_key_s issuer;
131 char tmpbuf[256];
132 static struct {
133 unsigned int exportable:1;
134 unsigned int expired:1;
135 unsigned int nrev:1;
136 unsigned int rev:1;
137 const char *alg;
138 int _class;
139 } ctx;
140 const char *fmt_templ = _("%s %s signature");
141 const char *s;
142
143 switch (msg) {
144 case WM_INITDIALOG:
145 ks = (gpgme_key_sig_t)lparam;
146 if (!ks)
147 BUG (0);
148 SetDlgItemText (dlg, IDC_SIGPROPS_EXP, _("Exportable"));
149 SetDlgItemText (dlg, IDC_SIGPROPS_NREV, _("Non-revocably"));
150 SetDlgItemText (dlg, IDC_SIGPROPS_EXPIRED, _("Expired"));
151 SetDlgItemText (dlg, IDC_SIGPROPS_REV, _("Revoked"));
152 SetDlgItemText (dlg, IDC_SIGPROPS_CLASSINF, _("Class"));
153 SetDlgItemText (dlg, IDC_SIGPROPS_EXPSTR, _("Expire date"));
154 SetDlgItemText (dlg, IDC_SIGPROPS_CREATSTR, _("Creation date"));
155 SetDlgItemText (dlg, IDC_SIGPROPS_KEYINF, _("Issuer key"));
156 SetDlgItemText (dlg, IDC_SIGPROPS_KEYIDINF, _("Issuer key ID"));
157 SetDlgItemText (dlg, IDC_SIGPROPS_POLICINF, _("Policy URL"));
158 SetDlgItemText (dlg, IDC_SIGPROPS_FRAME, _("Signature Info"));
159 SetWindowText (dlg, _("Signature Properties"));
160
161 memset (&ctx, 0, sizeof ctx);
162 ctx._class = ks->sig_class;
163 if (ctx._class == 0)
164 ctx._class = 16;
165 ctx._class -= 6;
166 if (ks->pubkey_algo == GPGME_PK_DSA)
167 ctx.alg = "DSA";
168 else if (ks->pubkey_algo == GPGME_PK_RSA)
169 ctx.alg = "RSA";
170 else
171 ctx.alg = "ELG";
172 ctx.exportable = ks->exportable;
173 _snprintf (tmpbuf, DIM (tmpbuf)-1, fmt_templ,
174 ctx.exportable? _("Exportable") : _("Non-exportable"),
175 ctx.alg);
176 SetDlgItemText (dlg, IDC_SIGPROPS_INFO, tmpbuf);
177
178 _snprintf (tmpbuf, DIM (tmpbuf)-1, "0x%s", ks->keyid+8);
179 SetDlgItemText (dlg, IDC_SIGPROPS_KEYID, tmpbuf);
180 SetDlgItemInt (dlg, IDC_SIGPROPS_CLASS, ctx._class, FALSE);
181 if (ctx.exportable)
182 CheckDlgButton (dlg, IDC_SIGPROPS_EXP, BST_CHECKED);
183 memset (&issuer, 0, sizeof (issuer));
184 if (!winpt_get_pubkey (ks->keyid+8, &issuer))
185 s = issuer.ext->uids->uid;
186 else
187 s = _(" user ID not found");
188 SetDlgItemText (dlg, IDC_SIGPROPS_ISSUER, s);
189
190 init_datepicker_from_time (GetDlgItem (dlg, IDC_SIGPROPS_CREATDATE),
191 ks->timestamp);
192 if (ks->expires == 0) {
193 ShowWindow (GetDlgItem (dlg, IDC_SIGPROPS_EXPSTR), SW_HIDE);
194 ShowWindow (GetDlgItem (dlg, IDC_SIGPROPS_EXPDATE), SW_HIDE);
195 }
196 else {
197 init_datepicker_from_time (GetDlgItem (dlg, IDC_SIGPROPS_EXPDATE),
198 ks->expires);
199 if (time (NULL) > ks->expires)
200 ctx.expired = 1;
201 if (ctx.expired)
202 CheckDlgButton (dlg, IDC_SIGPROPS_EXPIRED, BST_CHECKED);
203 }
204 if (ks->notations && !ks->notations->name && ks->notations->value)
205 SetDlgItemText (dlg, IDC_SIGPROPS_POLIC, ks->notations->value);
206
207 SetDlgItemText (dlg, IDC_SIGPROPS_EXP, _("Exportable"));
208 SetDlgItemText (dlg, IDC_SIGPROPS_NREV, _("Non-revocably"));
209 SetDlgItemText (dlg, IDC_SIGPROPS_REV, _("Revoked"));
210 SetDlgItemText (dlg, IDC_SIGPROPS_EXPIRED, _("Expired"));
211 SetWindowText (dlg, _("Signature Properties"));
212 SetForegroundWindow (dlg);
213 center_window (dlg, NULL);
214 return TRUE;
215
216 case WM_COMMAND:
217 if (HIWORD (wparam) == BN_CLICKED) {
218 /* Code to prevent the user changes the check buttons. */
219 switch (LOWORD (wparam)) {
220 case IDC_SIGPROPS_EXP:
221 CheckDlgButton (dlg, IDC_SIGPROPS_EXP,
222 ctx.exportable? BST_CHECKED : BST_UNCHECKED);
223 return TRUE;
224
225 case IDC_SIGPROPS_EXPIRED:
226 CheckDlgButton (dlg, IDC_SIGPROPS_EXPIRED,
227 ctx.expired? BST_CHECKED : BST_UNCHECKED);
228 return TRUE;
229
230 case IDC_SIGPROPS_REV:
231 CheckDlgButton (dlg, IDC_SIGPROPS_REV,
232 ctx.rev? BST_CHECKED : BST_UNCHECKED);
233 return TRUE;
234
235 case IDC_SIGPROPS_NREV:
236 CheckDlgButton (dlg, IDC_SIGPROPS_NREV,
237 ctx.nrev? BST_CHECKED : BST_UNCHECKED);
238 return TRUE;
239 }
240 }
241
242 switch (LOWORD (wparam)) {
243 case IDOK:
244 EndDialog (dlg, TRUE);
245 break;
246
247 case IDCANCEL:
248 EndDialog (dlg, FALSE);
249 break;
250 }
251 }
252
253 return FALSE;
254 }
255
256
257 /* Subclass window procedure for the list view. */
258 static BOOL CALLBACK
259 subclass_dlg_proc (HWND dlg, UINT msg, WPARAM wparam, LPARAM lparam)
260 {
261 listview_ctrl_t lv;
262 winpt_key_t key;
263 gpgme_key_sig_t ks;
264
265 switch (msg) {
266 case WM_KEYUP:
267 int virt_key = (int)wparam;
268 key = (winpt_key_t)siglist_proc.opaque;
269 lv = key->callback.ctl;
270 if (virt_key == VK_SPACE) {
271 if (is_sig (lv, -1)) {
272 ks = (gpgme_key_sig_t)listview_get_item2 (lv, listview_get_curr_pos (lv));
273 DialogBoxParam (glob_hinst, (LPCTSTR)IDD_WINPT_SIGPROPS, dlg,
274 sigprops_dlg_proc, (LPARAM)ks);
275 }
276 }
277 else if (virt_key == VK_DELETE)
278 do_delsig (dlg, lv, key);
279 break;
280 }
281 return CallWindowProc (siglist_proc.old, dlg, msg, wparam, lparam);
282 }
283
284
285 /* Return 1 if the list view @lv contains non-available keys. */
286 static int
287 check_for_missing_keys (listview_ctrl_t lv)
288 {
289 int n;
290 char id[128];
291 const char *txt = _("NOKEY");
292
293 n = listview_count_items (lv, 0);
294 for (int i = 0; i < n; i++) {
295 listview_get_item_text (lv, i, SL_COL_VALID, id, DIM (id) - 1);
296 if (!strncmp (id, txt, strlen (txt)))
297 return 1;
298 }
299
300 return 0;
301 }
302
303
304 /* Receive all missing keys marked in the list @lv. */
305 static int
306 recv_missing_keys (HWND dlg, listview_ctrl_t lv)
307 {
308 const char *txt = _("NOKEY");
309 char id[128], keyid[18+1];
310 int n, cnt=0;
311
312 if (msg_box (dlg, _("Really receive all missing keys?"),
313 _("Key Manager"), MB_YESNO|MB_INFO) == IDNO)
314 return 0;
315
316 n = listview_count_items (lv, 0);
317 for (int i = 0; i < n; i++) {
318 listview_get_item_text (lv, i, SL_COL_VALID, id, DIM (id) - 1);
319 if (!strncmp (id, txt, strlen (txt))) {
320 listview_get_item_text (lv, i, SL_COL_KEYID,
321 keyid, DIM (keyid) -1);
322 if (!hkp_recv_key (dlg, default_keyserver,
323 default_keyserver_port, keyid, 0, 0)) {
324 keycache_update (0, keyid);
325 cnt++;
326 }
327 else
328 break;
329 }
330 }
331 return cnt;
332 }
333
334
335 /* Create a mini popup with available choices. */
336 static void
337 do_create_popup (HWND dlg)
338 {
339 HMENU hm, sm;
340 POINT p;
341
342 GetCursorPos (&p);
343 hm = LoadMenu (glob_hinst, MAKEINTRESOURCE (IDR_WINPT_KEYSIG_CTX));
344 sm = GetSubMenu (hm, 0);
345 set_menu_text (sm, ID_SIGCTX_PROPS, _("Signature &Properties"));
346 set_menu_text (sm, ID_SIGCTX_KEYPROPS, _("Signing &Key Properties"));
347 TrackPopupMenu (sm, TPM_RIGHTALIGN, p.x, p.y, 0, dlg, NULL);
348 DestroyMenu (hm);
349 DestroyMenu (sm);
350 }
351
352
353 /* Fetch a single key and extract the keyid from the listview
354 entry at the pos @idx. */
355 static int
356 recv_single_key (HWND dlg, listview_ctrl_t lv, int idx)
357 {
358 char keyid[32];
359 int rc;
360
361 listview_get_item_text (lv, idx, SL_COL_KEYID, keyid, DIM (keyid)-1);
362 rc = hkp_recv_key (dlg, default_keyserver,
363 default_keyserver_port, keyid, 0, 0);
364 if (!rc)
365 keycache_update (0, keyid);
366 return rc;
367 }
368
369
370 /* Load the key property dialog with the selected key from @lv. */
371 static void
372 do_load_keyprops (HWND dlg, listview_ctrl_t lv)
373 {
374 winpt_key_s k;
375 char keyid[32] = {0};
376 char status[64] = {0}, creation[64] = {0};
377 int n = listview_get_curr_pos (lv);
378
379 listview_get_item_text (lv, n, SL_COL_VALID, status, DIM (status)-1);
380 listview_get_item_text (lv, n, SL_COL_CREATE, creation, DIM (creation)-1);
381 listview_get_item_text (lv, n, SL_COL_KEYID, keyid, DIM (keyid)-1);
382 if (!strcmp (status, _("NOKEY"))) {
383 int id = msg_box (dlg, _("Key not found in keyring, do you want to fetch it from the keyserver?"),
384 _("Key Manager"), MB_QUEST_ASK);
385 if (id == IDNO)
386 return;
387 if (recv_single_key (dlg, lv, n))
388 return;
389 }
390
391 memset (&k, 0, sizeof k);
392 if ((strlen (keyid) < 3 || winpt_get_pubkey (keyid, &k))) {
393 if (strlen (creation) > 0)
394 msg_box (dlg, _("Key not found in keyring."), _("Key Manager"), MB_INFO);
395 return;
396 }
397 DialogBoxParam (glob_hinst, (LPCTSTR)IDD_WINPT_KEYPROPS, dlg,
398 keyprops_dlg_proc, (LPARAM)&k);
399 }
400
401
402 /* Dialog box procedure to list signatures. */
403 BOOL CALLBACK
404 keysig_dlg_proc (HWND dlg, UINT msg, WPARAM wparam, LPARAM lparam)
405 {
406 static listview_ctrl_t lv = NULL;
407 static struct winpt_key_s *k;
408 gpgme_key_sig_t ks;
409 HWND sl;
410 char inf[384];
411 int idx = 0;
412
413 switch (msg) {
414 case WM_INITDIALOG:
415 k = (winpt_key_t) lparam;
416 if (!k)
417 BUG (0);
418 if (k->uid)
419 _snprintf (inf, DIM (inf)-1, _("Signature List for \"%s\""), k->uid);
420 SetWindowText (dlg, inf);
421 SetDlgItemText (dlg, IDC_KEYSIG_RECVKEY, _("&Receive Key"));
422 SetDlgItemText (dlg, IDC_KEYSIG_SIGPROPS, _("&Properties"));
423
424 lv = siglist_load (GetDlgItem (dlg, IDC_KEYSIG_LIST), k->keyid);
425 if (!check_for_missing_keys (lv))
426 EnableWindow (GetDlgItem (dlg, IDC_KEYSIG_RECVKEY), FALSE);
427 EnableWindow (GetDlgItem (dlg, IDC_KEYSIG_SIGPROPS), FALSE);
428 k->callback.ctl = lv;
429 sl = GetDlgItem (dlg, IDC_KEYSIG_LIST);
430 siglist_proc.dlg = dlg;
431 siglist_proc.opaque = k;
432 siglist_proc.current = (WNDPROC)subclass_dlg_proc;
433 siglist_proc.old = (WNDPROC)GetWindowLong (sl, GWL_WNDPROC);
434 if (siglist_proc.old) {
435 if (!SetWindowLong (sl, GWL_WNDPROC, (LONG)siglist_proc.current)) {
436 msg_box (dlg, "Could not set keylist window procedure.",
437 _("Key Manager"), MB_ERR);
438 BUG (0);
439 }
440 }
441 SetForegroundWindow (dlg);
442 center_window (dlg, NULL);
443 return TRUE;
444
445 case WM_DESTROY:
446 if (lv) {
447 siglist_delete (lv);
448 lv = NULL;
449 }
450 return FALSE;
451
452 case WM_NOTIFY:
453 NMHDR *notify;
454
455 notify = (NMHDR *)lparam;
456 if (!notify)
457 return FALSE;
458 if (notify->code == NM_DBLCLK &&
459 notify->idFrom == IDC_KEYSIG_LIST)
460 do_load_keyprops (dlg, lv);
461 if (notify->code == NM_RCLICK &&
462 notify->idFrom == IDC_KEYSIG_LIST &&
463 is_sig (lv, -1))
464 do_create_popup (dlg);
465 if (notify->code == LVN_ITEMCHANGED &&
466 ((LPNMLISTVIEW)lparam)->uNewState) {
467 idx = listview_get_curr_pos (lv);
468 listview_get_item_text (lv, idx, SL_COL_VALID, inf, DIM (inf)-1);
469 EnableWindow (GetDlgItem (dlg, IDC_KEYSIG_RECVKEY),
470 strcmp (inf, _("NOKEY")) == 0? TRUE: FALSE);
471 EnableWindow (GetDlgItem (dlg, IDC_KEYSIG_SIGPROPS),
472 is_sig (lv, -1)? TRUE : FALSE);
473 }
474 if (notify->code == LVN_COLUMNCLICK) {
475 NMLISTVIEW *nft = (LPNMLISTVIEW) lparam;
476 int sortby = 0;
477 switch (nft->iSubItem) {
478 //case 0: sortby = KEY_SORT_USERID; break;
479 case 1: sortby = KEY_SORT_VALIDITY; break;
480 case 2: sortby = SIG_SORT_CLASS; break;
481 case 3: sortby = KEY_SORT_CREATED; break;
482 case 4: sortby = KEY_SORT_KEYID; break;
483 case 5: sortby = SIG_SORT_EXPIRE; break;
484 case 6: sortby = KEY_SORT_ALGO; break;
485 default: return TRUE;
486 }
487 siglist_sort (lv, sortby);
488 }
489 break;
490
491 case WM_COMMAND:
492 switch (LOWORD (wparam)) {
493 case ID_SIGCTX_KEYPROPS:
494 do_load_keyprops (dlg, lv);
495 return TRUE;
496
497 case ID_SIGCTX_PROPS:
498 if (is_sig (lv, -1)) {
499 ks = (gpgme_key_sig_t)listview_get_item2 (lv, listview_get_curr_pos (lv));
500 DialogBoxParam (glob_hinst, (LPCTSTR)IDD_WINPT_SIGPROPS, dlg,
501 sigprops_dlg_proc, (LPARAM)ks);
502 }
503 return TRUE;
504
505 case IDC_KEYSIG_RECVKEY:
506 idx = listview_get_curr_pos (lv);
507 if (idx == -1)
508 recv_missing_keys (dlg, lv);
509 else
510 recv_single_key (dlg, lv, idx);
511 return TRUE;
512
513 case IDC_KEYSIG_SIGPROPS:
514 if (is_sig (lv, -1)) {
515 ks = (gpgme_key_sig_t)listview_get_item2 (lv, listview_get_curr_pos (lv));
516 DialogBoxParam (glob_hinst, (LPCTSTR)IDD_WINPT_SIGPROPS, dlg,
517 sigprops_dlg_proc, (LPARAM)ks);
518 }
519 return TRUE;
520
521 case IDCANCEL:
522 EndDialog (dlg, FALSE);
523 return TRUE;
524
525 case IDOK:
526 EndDialog (dlg, TRUE);
527 return TRUE;
528 }
529 break;
530 }
531 return FALSE;
532 }

Properties

Name Value
svn:eol-style native

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26