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

Contents of /trunk/Src/wptKeysigDlg.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 65 - (show annotations)
Thu Nov 3 16:55:25 2005 UTC (19 years, 3 months ago) by twoaday
File size: 12420 byte(s)
Minor changes to avoid GCC warnings.


1 /* wptKeysigDlg.cpp - Key signature listing
2 * Copyright (C) 2001-2005 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 * You should have received a copy of the GNU General Public License
17 * along with WinPT; if not, write to the Free Software Foundation,
18 * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
19 */
20
21 #ifdef HAVE_CONFIG_H
22 #include <config.h>
23 #endif
24
25 #include <windows.h>
26 #include <commctrl.h>
27 #include <time.h>
28
29 #include "resource.h"
30 #include "wptGPG.h"
31 #include "wptCommonCtl.h"
32 #include "wptContext.h" /* for passphrase_s */
33 #include "wptDlgs.h"
34 #include "wptW32API.h"
35 #include "wptNLS.h"
36 #include "wptKeylist.h"
37 #include "wptKeyserver.h"
38 #include "wptUTF8.h"
39 #include "wptTypes.h"
40 #include "wptVersion.h"
41 #include "wptErrors.h"
42 #include "wptKeyEdit.h"
43
44 static subclass_s siglist_proc;
45
46
47 static int
48 is_sig (listview_ctrl_t lv, int pos)
49 {
50 char tmpbuf[256];
51
52 if (pos == -1)
53 pos = listview_get_curr_pos (lv);
54 if (pos == -1)
55 return 0;
56 listview_get_item_text (lv, pos, 0, tmpbuf, 200);
57 if (*tmpbuf == ' ')
58 return -1;
59 return 0;
60 }
61
62
63 /* Delete the selected signature from list view @lv. The
64 key is given in @key. */
65 static int
66 do_delsig (HWND dlg, listview_ctrl_t lv, winpt_key_t key)
67 {
68 int pos, npos, id;
69 int signo=0, uidno=0;
70 GpgKeyEdit *ke;
71 gpgme_error_t err;
72
73 npos = pos = listview_get_curr_pos (lv);
74 if (!is_sig (lv, -1))
75 return -1;
76 while (pos > 0 && is_sig (lv, pos)) {
77 signo++;
78 pos--;
79 }
80 pos = npos;
81 while (npos > 0) {
82 if (!is_sig (lv, npos))
83 uidno++;
84 npos--;
85 }
86 uidno++;
87
88 /* XXX: do not allow to delete the self signature */
89 id = log_box (_("Key Manager"), MB_YESNO,
90 _("Are you really sure you want to delete this signature from\n"
91 " \"%s\""), key->uid);
92 if (id == IDNO)
93 return 0;
94
95 ke = new GpgKeyEdit (key->keyid);
96 if (!ke)
97 BUG (NULL);
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 delete ke;
104 key->update = 1; /*update*/
105 return err? -1 : 0;
106 }
107
108
109 /* Dialog box procedure to display the signature properties. */
110 static BOOL CALLBACK
111 sigprops_dlg_proc (HWND dlg, UINT msg, WPARAM wparam, LPARAM lparam)
112 {
113 static listview_ctrl_t lv;
114 char tmpbuf[256];
115 int n;
116 struct {
117 unsigned int exportable:1;
118 unsigned int expired:1;
119 unsigned int nrev:1;
120 unsigned int rev:1;
121 const char * alg;
122 int _class;
123 } ctx;
124 const char *fmt_templ = _("%s %s signature");
125
126 switch (msg) {
127 case WM_SYSCOMMAND:
128 if (LOWORD (wparam) == SC_CLOSE)
129 EndDialog (dlg, TRUE);
130 return FALSE;
131
132 case WM_INITDIALOG:
133 lv = (listview_ctrl_t)lparam;
134 if (!lv)
135 dlg_fatal_error (dlg, "could not get dialog param");
136 memset (&ctx, 0, sizeof ctx);
137 n = listview_get_curr_pos (lv);
138 listview_get_item_text (lv, n, 2, tmpbuf, DIM (tmpbuf)-1);
139 if (!strstr (tmpbuf, "L"))
140 ctx.exportable = 1;
141 ctx._class = atoi (tmpbuf);
142 if (ctx._class == 0)
143 ctx._class = 10;
144 else if (ctx._class < 10)
145 ctx._class += 10;
146 listview_get_item_text (lv, n, 6, tmpbuf, DIM (tmpbuf)-1);
147 if (strstr (tmpbuf, "DSA"))
148 ctx.alg = "DSA";
149 else if (strstr (tmpbuf, "RSA"))
150 ctx.alg = "RSA";
151 else
152 ctx.alg = "ELG";
153 _snprintf (tmpbuf, DIM (tmpbuf)-1, fmt_templ,
154 ctx.exportable? _("Exportable") : _("Non-exportable"), ctx.alg);
155 SetDlgItemText (dlg, IDC_SIGPROPS_INFO, tmpbuf);
156 listview_get_item_text (lv, n, 4, tmpbuf, DIM (tmpbuf)-1);
157 SetDlgItemText (dlg, IDC_SIGPROPS_KEYID, tmpbuf);
158 SetDlgItemInt (dlg, IDC_SIGPROPS_CLASS, ctx._class, FALSE);
159 if (ctx.exportable)
160 CheckDlgButton (dlg, IDC_SIGPROPS_EXP, BST_CHECKED);
161 listview_get_item_text (lv, n, 0, tmpbuf, DIM (tmpbuf)-1);
162 SetDlgItemText (dlg, IDC_SIGPROPS_ISSUER, tmpbuf+1);
163 tmpbuf[0] = 0;
164 listview_get_item_text (lv, n, 5, tmpbuf, DIM (tmpbuf)-1);
165 if (strlen (tmpbuf) == 0) {
166 ShowWindow (GetDlgItem (dlg, IDC_SIGPROPS_EXPSTR), SW_HIDE);
167 ShowWindow (GetDlgItem (dlg, IDC_SIGPROPS_EXPDATE), SW_HIDE);
168 }
169 else {
170 SYSTEMTIME st;
171 struct tm * tm;
172 time_t t = time (NULL);
173
174 memset (&st, 0, sizeof st);
175 st.wYear = atoi (tmpbuf);
176 st.wMonth = atoi (tmpbuf+5);
177 st.wDay = atoi (tmpbuf+8);
178 DateTime_SetSystemtime (GetDlgItem (dlg, IDC_SIGPROPS_EXPDATE),
179 GDT_VALID, &st);
180
181 tm = localtime (&t);
182 tm->tm_mon++;
183 tm->tm_year += 1900;
184 if (tm->tm_year > st.wYear)
185 ctx.expired = 1;
186 else if (tm->tm_mon > st.wMonth)
187 ctx.expired = 1;
188 if (ctx.expired)
189 CheckDlgButton (dlg, IDC_SIGPROPS_EXPIRED, BST_CHECKED);
190 }
191 SetDlgItemText (dlg, IDC_SIGPROPS_EXP, _("Exportable"));
192 SetDlgItemText (dlg, IDC_SIGPROPS_NREV, _("Non-revocably"));
193 SetDlgItemText (dlg, IDC_SIGPROPS_REV, _("Revoked"));
194 SetDlgItemText (dlg, IDC_SIGPROPS_EXPIRED, _("Expired"));
195 SetWindowText (dlg, _("Signature Properties"));
196 SetForegroundWindow (dlg);
197 center_window (dlg, NULL);
198 return TRUE;
199
200 case WM_COMMAND:
201 switch (LOWORD (wparam)) {
202 case IDOK:
203 EndDialog (dlg, TRUE);
204 break;
205 }
206 }
207
208 return FALSE;
209 }
210
211
212 static BOOL CALLBACK
213 subclass_dlg_proc (HWND dlg, UINT msg, WPARAM wparam, LPARAM lparam)
214 {
215 listview_ctrl_t lv;
216 winpt_key_t key;
217
218 switch (msg) {
219 case WM_KEYUP:
220 int virt_key = (int)wparam;
221 key = (winpt_key_t)siglist_proc.opaque;
222 lv = key->callback.ctl;
223 if (virt_key == VK_SPACE) {
224 if (is_sig (lv, -1))
225 DialogBoxParam (glob_hinst, (LPCTSTR)IDD_WINPT_SIGPROPS, dlg,
226 sigprops_dlg_proc, (LPARAM)lv);
227 }
228 else if (virt_key == VK_DELETE)
229 do_delsig (dlg, lv, key);
230 break;
231 }
232 return CallWindowProc (siglist_proc.old, dlg, msg, wparam, lparam);
233 }
234
235
236 /* Return 1 if the list view @lv contains non-available keys. */
237 static int
238 check_for_missing_keys (listview_ctrl_t lv)
239 {
240 int i, n;
241 char id[128];
242
243 n = listview_count_items( lv, 0 );
244 for( i = 0; i < n; i++ ) {
245 listview_get_item_text( lv, i, 1, id, sizeof (id) - 1 );
246 if( !strncmp( id, "NOKEY", 5 ) )
247 return 1;
248 }
249
250 return 0;
251 }
252
253
254 /* Receive all missing keys marked in the list @lv. */
255 static int
256 recv_missing_keys (HWND dlg, listview_ctrl_t lv)
257 {
258 int i, n, rc=0;
259 char id[128], keyid[18+1];
260
261 n = listview_count_items (lv, 0);
262 for( i = 0; i < n; i++ ) {
263 listview_get_item_text( lv, i, 1, id, sizeof id - 1 );
264 if( !strncmp( id, "NOKEY", 5 ) ) {
265 listview_get_item_text( lv, i, 4, keyid, sizeof keyid -1 );
266 rc = hkp_recv_key( dlg, default_keyserver, default_keyserver_port, keyid, 0, 0 );
267 if( rc )
268 break;
269 }
270 }
271
272 return rc;
273 }
274
275
276 /* Create a mini popup with available choices. */
277 static void
278 do_create_popup (HWND dlg)
279 {
280 HMENU hm, sm;
281 POINT p;
282
283 GetCursorPos (&p);
284 hm = LoadMenu (glob_hinst, MAKEINTRESOURCE (IDR_WINPT_KEYSIG_CTX));
285 sm = GetSubMenu (hm, 0);
286
287 set_menu_text (sm, ID_SIGCTX_PROPS, _("Signature &Properties"));
288 set_menu_text (sm, ID_SIGCTX_KEYPROPS, _("Signing &Key Properties"));
289
290 TrackPopupMenu (sm, TPM_RIGHTALIGN, p.x, p.y, 0, dlg, NULL);
291
292 DestroyMenu (hm);
293 DestroyMenu (sm);
294 }
295
296
297 /* Load the key property dialog with the selected key from @lv. */
298 static void
299 do_load_keyprops (HWND dlg, listview_ctrl_t lv)
300 {
301 winpt_key_s k;
302 gpgme_key_t key;
303 char keyid[32] = {0};
304 char status[64] = {0}, creation[64] = {0};
305 int n = listview_get_curr_pos (lv);
306
307 listview_get_item_text (lv, n, 1, status, DIM (status)-1);
308 listview_get_item_text (lv, n, 3, creation, DIM (creation)-1);
309 listview_get_item_text (lv, n, 4, keyid, DIM (keyid)-1);
310 if (!strcmp (status, "NOKEY")) {
311 msg_box (dlg, _("Key not found in keyring, please get it from the keyserver first."),
312 _("Key Manager"), MB_INFO);
313 return;
314 }
315
316 if ((strlen (keyid) < 3 ||get_pubkey (keyid, &key))) {
317 if (strlen (creation) > 0)
318 msg_box (dlg, _("Key not found in keyring."), _("Key Manager"), MB_INFO);
319 return;
320 }
321 memset (&k, 0, sizeof k);
322 k.keyid = keyid;
323 DialogBoxParam (glob_hinst, (LPCTSTR)IDD_WINPT_KEYPROPS, dlg,
324 keyprops_dlg_proc, (LPARAM)&k);
325 }
326
327
328 /* Dialog box procedure to list signatures. */
329 BOOL CALLBACK
330 keysig_dlg_proc (HWND dlg, UINT msg, WPARAM wparam, LPARAM lparam)
331 {
332 static listview_ctrl_t lv = NULL;
333 static struct winpt_key_s *k;
334 char inf[384], keyid[18+1];
335 int idx = 0, id, rc;
336 HWND sl;
337
338 switch( msg ) {
339 case WM_INITDIALOG:
340 k = (winpt_key_t) lparam;
341 if (!k)
342 BUG (0);
343 if (k->uid)
344 _snprintf (inf, DIM (inf)-1, _("Signature List for \"%s\""), k->uid);
345 SetWindowText (dlg, inf);
346 #ifndef LANG_DE
347 SetDlgItemText (dlg, IDC_KEYSIG_RECVKEY, _("&Receive Key"));
348 SetDlgItemText (dlg, IDC_KEYSIG_SIGPROPS, _("&Properties"));
349 #endif
350 lv = siglist_load (GetDlgItem (dlg, IDC_KEYSIG_LIST), k->keyid);
351 if (!check_for_missing_keys (lv))
352 EnableWindow (GetDlgItem (dlg, IDC_KEYSIG_RECVKEY), FALSE);
353 k->callback.ctl = lv;
354 sl = GetDlgItem (dlg, IDC_KEYSIG_LIST);
355 siglist_proc.dlg = dlg;
356 siglist_proc.opaque = k;
357 siglist_proc.current = (WNDPROC)subclass_dlg_proc;
358 siglist_proc.old = (WNDPROC)GetWindowLong (sl, GWL_WNDPROC);
359 if (siglist_proc.old) {
360 if (!SetWindowLong (sl, GWL_WNDPROC, (LONG)siglist_proc.current)) {
361 msg_box (dlg, _("Could not set keylist window procedure."),
362 _("Key Manager"), MB_ERR);
363 BUG (0);
364 }
365 }
366 SetForegroundWindow (dlg);
367 center_window (dlg, NULL);
368 return TRUE;
369
370 case WM_DESTROY:
371 if ( lv ) {
372 siglist_delete( lv );
373 lv = NULL;
374 }
375 return FALSE;
376
377 case WM_SYSCOMMAND:
378 if (LOWORD (wparam) == SC_CLOSE) {
379 if (k->update)
380 keycache_update (0, k->keyid);
381 EndDialog (dlg, TRUE);
382 }
383 return FALSE;
384
385 case WM_NOTIFY:
386 NMHDR *notify;
387
388 notify = (NMHDR *)lparam;
389 if (notify && notify->code == NM_DBLCLK
390 && notify->idFrom == IDC_KEYSIG_LIST)
391 do_load_keyprops (dlg, lv);
392 if (notify && notify->code == NM_RCLICK &&
393 notify->idFrom == IDC_KEYSIG_LIST)
394 do_create_popup (dlg);
395 if (notify && notify->code == LVN_ITEMCHANGED &&
396 ((LPNMLISTVIEW)lparam)->uNewState) {
397 idx = listview_get_curr_pos (lv);
398 listview_get_item_text (lv, idx, 1, inf, DIM (inf)-1);
399 EnableWindow (GetDlgItem (dlg, IDC_KEYSIG_RECVKEY),
400 strcmp (inf, "NOKEY") == 0? TRUE: FALSE);
401 }
402 break;
403
404 case WM_COMMAND:
405 switch ( LOWORD( wparam ) ) {
406
407 case ID_SIGCTX_KEYPROPS:
408 do_load_keyprops (dlg, lv);
409 break;
410
411 case ID_SIGCTX_PROPS:
412 if (is_sig (lv, -1))
413 DialogBoxParam (glob_hinst, (LPCTSTR)IDD_WINPT_SIGPROPS, dlg,
414 sigprops_dlg_proc, (LPARAM)lv);
415 break;
416
417 case IDC_KEYSIG_RECVKEY:
418 idx = listview_get_curr_pos (lv);
419 if (idx == -1) {
420 id = msg_box( dlg, _("Really receive all missing keys?"),
421 _("Key Manager"), MB_YESNO|MB_INFO );
422 if (id == IDYES) {
423 rc = recv_missing_keys (dlg, lv);
424 if (!rc)
425 keycache_set_reload (1);
426 return TRUE;
427 }
428 return TRUE;
429 }
430 listview_get_item_text (lv, idx, 4, keyid, DIM (keyid)-1);
431 rc = hkp_recv_key (dlg, default_keyserver, default_keyserver_port, keyid, 0, 0);
432 if (!rc)
433 keycache_update (0, keyid);
434 return TRUE;
435
436 case IDC_KEYSIG_SIGPROPS:
437 if (is_sig (lv, -1))
438 DialogBoxParam (glob_hinst, (LPCTSTR)IDD_WINPT_SIGPROPS, dlg,
439 sigprops_dlg_proc, (LPARAM)lv);
440 return TRUE;
441
442 case IDOK:
443 if (k->update)
444 keycache_update (0, k->keyid);
445 EndDialog (dlg, TRUE);
446 return TRUE;
447 }
448 break;
449 }
450 return FALSE;
451 }

Properties

Name Value
svn:eol-style native

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26