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

Annotation of /trunk/Src/wptKeysigDlg.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 2 - (hide annotations)
Mon Jan 31 11:02:21 2005 UTC (20 years, 1 month ago) by twoaday
File size: 11923 byte(s)
WinPT initial checkin.


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

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26