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

Annotation of /trunk/Src/wptKeyManagerDlg.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 22 - (hide annotations)
Wed Aug 10 11:33:35 2005 UTC (19 years, 6 months ago) by twoaday
File size: 52129 byte(s)
2005-08-06  Timo Schulz  <twoaday@freakmail.de>
 
        * wptGPGME.cpp (keycache_update): Reload OpenPGP parts
        of the secret key.
        (keycache_init): cache name of secret keyring.
        * wptKeyList.cpp (keylist_upd_key): Do not add long keyid.
        (get_key_type): Do not assume 'ultimate' means key pair.
        * wptKeyEditDlgs.cpp (diff_time): New.
        (keyedit_addsubkey_dlg_proc): Changed design and use
        diff_time. Drop checks for invalid keylength (< 1024, > 4096)
        because the combo box automatically handles this.
        * wptKeyManager.cpp (km_set_implicit_trust): Return error code.
        * wptGPG.cpp (get_backup_name): New.
        (gnupg_backup_keyrings): Rotate backup names, from 0..3.
        * wptClipImportDialog.cpp (clip_import_dlg_proc): Free memory.
        * wptKeyManagerDlg.cpp (keymanager_dlg_proc): Use 0x short keyid and
        not the long keyid.


1 twoaday 2 /* wptKeyManagerDlg.cpp - WinPT Key Manager
2 twoaday 16 * Copyright (C) 2000-2005 Timo Schulz
3 twoaday 2 * Copyright (C) 2004 Andreas Jobs
4     *
5     * This file is part of WinPT.
6     *
7     * WinPT is free software; you can redistribute it and/or modify
8     * it under the terms of the GNU General Public License as published by
9     * the Free Software Foundation; either version 2 of the License, or
10     * (at your option) any later version.
11     *
12     * WinPT is distributed in the hope that it will be useful,
13     * but WITHOUT ANY WARRANTY; without even the implied warranty of
14     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15     * GNU General Public License for more details.
16     *
17     * You should have received a copy of the GNU General Public License
18     * along with WinPT; if not, write to the Free Software Foundation,
19     * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
20     */
21    
22     #include <windows.h>
23     #include <commctrl.h>
24    
25     #include "../resource.h"
26     #include "wptTypes.h"
27     #include "wptGPG.h"
28     #include "wptCommonCtl.h"
29     #include "wptContext.h" /* for passphrase_s */
30     #include "wptDlgs.h"
31     #include "wptW32API.h"
32     #include "wptVersion.h"
33     #include "wptKeylist.h"
34     #include "wptNLS.h"
35     #include "wptErrors.h"
36     #include "wptHotkey.h"
37     #include "wptKeyManager.h"
38     #include "wptKeyserver.h"
39     #include "wptKeyEdit.h"
40     #include "wptRegistry.h"
41    
42    
43     #define WINDOWCLASS_SEPARATOR_CHILD "WINPT_SEP_CHILD"
44     #define SEPARATOR_HEIGHT 5
45    
46 twoaday 22 void km_dump_key (gpgme_key_t key);
47 twoaday 2
48 twoaday 22 //int treeview_add_item (HWND tree, HTREEITEM parent, const char * text);
49    
50 twoaday 2 static subclass_s keylist_proc;
51     static int km_index = -1;
52     static int km_def_pos = 0;
53    
54     HIMAGELIST glob_imagelist;
55    
56     struct km_info {
57     /* Window positions */
58     int pos_x, pos_y;
59     int ypos_sep;
60     int ypercent_sep;
61    
62     HWND hwnd_sep;
63     HWND toolbar;
64     HWND statbar;
65    
66     listview_ctrl_t lv;
67     int keylist_sortby;
68     };
69    
70     struct mybuttons {
71     long icon;
72     long command;
73     char *text;
74     };
75    
76     struct mybuttons myb[] = {
77     {IMI_KEY_DELETE, ID_KEYMISC_DELETE, "Delete"},
78     {IMI_KEY_PROPS, ID_KEYMISC_PROPS, "Properties"},
79     {IMI_KEY_SIGN, ID_KEYMISC_SIGN, "Sign"},
80     {IMI_KEY_IMPORT, ID_KEYMISC_IMPORT, "Import"},
81     {IMI_KEY_EXPORT, ID_KEYMISC_EXPORT, "Export"},
82     };
83    
84    
85 twoaday 22 #ifndef VK_F
86     #define VK_F 70
87     #endif
88     #ifndef VK_A
89     #define VK_A 65
90     #endif
91     #ifndef VK_C
92     #define VK_C 67
93     #endif
94     #ifndef VK_P
95     #define VK_P 80
96     #endif
97    
98 twoaday 2 static BOOL CALLBACK
99     keylist_subclass_proc (HWND dlg, UINT msg, WPARAM wparam, LPARAM lparam)
100     {
101 twoaday 22 struct listview_ctrl_s lv;
102    
103     switch (msg) {
104 twoaday 2 case WM_LBUTTONDBLCLK:
105 twoaday 22 send_cmd_id (keylist_proc.dlg, ID_KEYMISC_PROPS);
106 twoaday 2 break;
107    
108     case WM_KEYUP:
109     int virt_key = (int)wparam;
110 twoaday 22 switch (virt_key) {
111     case VK_SPACE:
112 twoaday 2 send_cmd_id( keylist_proc.dlg, ID_KEYMISC_PROPS );
113     break;
114 twoaday 22
115     case VK_DELETE:
116 twoaday 2 send_cmd_id( keylist_proc.dlg, ID_KEYMISC_DELETE );
117     break;
118 twoaday 22
119     case VK_INSERT:
120 twoaday 2 send_cmd_id (keylist_proc.dlg, ID_KEYMISC_PASTE);
121     break;
122 twoaday 22
123     case VK_A:
124     if (GetAsyncKeyState (VK_CONTROL)) {
125     lv.ctrl = GetDlgItem (keylist_proc.dlg, IDC_KEYMISC_KEYLIST);
126     listview_select_all (&lv);
127     }
128     break;
129    
130     case VK_C:
131     if (GetAsyncKeyState (VK_CONTROL)) {
132     lv.ctrl = GetDlgItem (keylist_proc.dlg, IDC_KEYMISC_KEYLIST);
133     km_index = listview_get_curr_pos (&lv);
134     km_clip_export (keylist_proc.dlg, &lv);
135     }
136     break;
137    
138     case VK_P:
139     if (GetAsyncKeyState (VK_CONTROL)) {
140     km_index = -1;
141     km_clip_import (keylist_proc.dlg);
142     }
143     break;
144    
145     case VK_F:
146     if (GetAsyncKeyState (VK_CONTROL)) {
147     lv.ctrl = GetDlgItem (keylist_proc.dlg, IDC_KEYMISC_KEYLIST);
148     km_find_key (dlg, &lv);
149     }
150     break;
151 twoaday 2 }
152     break;
153     }
154    
155     return CallWindowProc( keylist_proc.old, dlg, msg, wparam, lparam );
156     } /* keylist_subclass_proc */
157    
158    
159     static HWND
160     load_toolbar (HWND dlg, struct km_info * kmi)
161     {
162     HWND tbwnd;
163     TBSAVEPARAMS tbsp;
164     TBBUTTON tb_buttons[] = {
165     /*{imagelist_getindex(IMI_EXIT), ID_KEYMISC_QUIT, TBSTATE_ENABLED, TBSTYLE_BUTTON, 0, 0L, 0},*/
166     {imagelist_getindex(IMI_KEY_DELETE), ID_KEYMISC_DELETE, TBSTATE_ENABLED, TBSTYLE_BUTTON, {0}, 0L, 0},
167     {imagelist_getindex(IMI_KEY_PROPS), ID_KEYMISC_PROPS, TBSTATE_ENABLED, TBSTYLE_BUTTON, {0}, 0L, 0},
168     {imagelist_getindex(IMI_KEY_SIGN), ID_KEYMISC_SIGN, TBSTATE_ENABLED, TBSTYLE_BUTTON, {0}, 0L, 0},
169     {0, 0, 0, TBSTYLE_SEP, {0}, 0L, 0},
170     {imagelist_getindex(IMI_KEY_IMPORT), ID_KEYMISC_IMPORT, TBSTATE_ENABLED, TBSTYLE_BUTTON, {0}, 0L, 0},
171     {imagelist_getindex(IMI_KEY_EXPORT), ID_KEYMISC_EXPORT, TBSTATE_ENABLED, TBSTYLE_BUTTON, {0}, 0L, 0},
172     };
173    
174     tbwnd = CreateWindowEx (0, TOOLBARCLASSNAME, NULL,
175     WS_CHILD|TBSTYLE_TOOLTIPS|TBSTYLE_FLAT|CCS_ADJUSTABLE,
176     0, 0, 0, 0, dlg, (HMENU)IDR_WINPT_KMTB, glob_hinst, NULL);
177     if (tbwnd) {
178     SendMessage (tbwnd, TB_BUTTONSTRUCTSIZE, (WPARAM) sizeof(TBBUTTON), 0);
179     SendMessage (tbwnd, TB_SETIMAGELIST, 0, (LPARAM)glob_imagelist);
180     SendMessage (tbwnd, TB_AUTOSIZE, 0, 0);
181     ShowWindow (tbwnd, SW_SHOW);
182    
183     /* Restore toolbar */
184     tbsp.hkr = HKEY_CURRENT_USER;
185     tbsp.pszSubKey = "Software\\WinPT";
186     tbsp.pszValueName = "KM_toolbar";
187     if (SendMessage(tbwnd, TB_SAVERESTORE, FALSE, (LPARAM)&tbsp ) == 0)
188     SendMessage (tbwnd, TB_ADDBUTTONS, sizeof(tb_buttons) / sizeof(tb_buttons[0]), (LONG)&tb_buttons[0]);
189     }
190     return tbwnd;
191     } /* load_toolbar */
192    
193    
194     static void
195     do_center_window (HWND dlg, struct km_info * kmi)
196     {
197     RECT rect;
198     char * p;
199     int pos_x = 0, pos_y = 0;
200    
201     /* Find bottom of keylist */
202     GetWindowRect (GetDlgItem(dlg, IDC_KEYMISC_KEYLIST), &rect);
203     MapWindowPoints (NULL, dlg, (POINT*)&rect, 2);
204    
205     kmi->ypos_sep = rect.bottom;
206    
207     p = get_reg_entry( HKEY_CURRENT_USER, "Software\\WinPT", "KM_Pos_X" );
208     if( p && !strcmp( p, " " ) ) {
209     free_if_alloc( p );
210     center_window( dlg );
211     return;
212     }
213     else if( p )
214     pos_x = atol( p );
215    
216     p = get_reg_entry( HKEY_CURRENT_USER, "Software\\WinPT", "KM_Pos_Y" );
217     if( p && !strcmp( p, " " ) ) {
218     free_if_alloc( p );
219     center_window( dlg );
220     return;
221     }
222     else if( p )
223     pos_y = atol( p );
224    
225     if( !pos_y && !pos_x ) {
226     center_window( dlg );
227     return;
228     }
229    
230     if( pos_x > GetSystemMetrics( SM_CXSCREEN )
231     || pos_y > GetSystemMetrics( SM_CYSCREEN ) ) {
232     pos_x = pos_y = 0;
233     }
234     GetClientRect( dlg, &rect );
235     MoveWindow( dlg, pos_x, pos_y, rect.right, rect.bottom, TRUE );
236     }
237    
238    
239     static void
240     do_resize_window( HWND dlg, struct km_info *kmi)
241     {
242     HWND h;
243     RECT rclient, rect;
244     BOOL bRepaint = FALSE;
245    
246     /* Get rect of client area and make life easier */
247     GetClientRect( dlg, &rclient );
248    
249     /* Move toolbar to the top of the window */
250     if (kmi->toolbar) {
251     GetWindowRect(kmi->toolbar, &rect);
252     ScreenToClient(dlg, (POINT*)&rect);
253     ScreenToClient(dlg, (POINT*)&(rect.right));
254    
255     rclient.top += rect.bottom - rect.top;
256     MoveWindow (kmi->toolbar, 0, 0, rclient.right - rclient.left,
257     rect.bottom - rect.top, bRepaint);
258     }
259    
260     /* Move statusbar to the bottom of the window */
261     if (kmi->statbar) {
262     GetWindowRect( kmi->statbar, &rect );
263     ScreenToClient(dlg, (POINT*)&rect);
264     ScreenToClient(dlg, (POINT*)&(rect.right));
265    
266     rclient.bottom -= rect.bottom - rect.top;
267     MoveWindow (kmi->statbar, 0, rclient.bottom, rclient.right - rclient.left,
268     rect.bottom - rect.top, bRepaint);
269     }
270    
271     // Obtain separator information and move it to the desired posistion
272     if (kmi->ypercent_sep)
273     kmi->ypos_sep = (rclient.bottom - rclient.top) * kmi->ypercent_sep / 100;
274     else
275     kmi->ypercent_sep = kmi->ypos_sep * 100 / (rclient.bottom - rclient.top);
276    
277     // Don't move away
278     if (kmi->ypos_sep+5 > rclient.bottom)
279     kmi->ypos_sep = rclient.bottom - 5;
280     if (kmi->ypos_sep < rclient.top)
281     kmi->ypos_sep = rclient.top;
282     MoveWindow (kmi->hwnd_sep, 0, kmi->ypos_sep, (rclient.right - rclient.left), 5, bRepaint);
283    
284     // Place the keylist above the separator
285     h = GetDlgItem( dlg, IDC_KEYMISC_KEYLIST );
286     MoveWindow (h, rclient.left, rclient.top, rclient.right - rclient.left,
287     kmi->ypos_sep - rclient.top, bRepaint);
288     rclient.top = kmi->ypos_sep + 5 + 8;
289    
290     /* Place the group text and the group box below the separator */
291     h = GetDlgItem( dlg, IDC_KEYMISC_GTEXT );
292     MoveWindow( h, rclient.left, rclient.top, 100, 14, bRepaint);
293     rclient.top += 18;
294    
295     h = GetDlgItem( dlg, IDC_KEYMISC_GROUP );
296     MoveWindow (h, rclient.left, rclient.top, rclient.right - rclient.left,
297     (rclient.bottom < rclient.top) ? 0 : rclient.bottom - rclient.top, bRepaint);
298    
299     /* Repaint the whole thing */
300     InvalidateRect (dlg, NULL, TRUE);
301     } /* do_resize_window */
302    
303    
304     static void
305     do_create_minpopup (HWND dlg)
306     {
307     HMENU hm;
308     MENUITEMINFO mi;
309     char * s;
310     POINT p;
311    
312     if (gpg_read_only)
313     return;
314     hm = CreatePopupMenu ();
315     if (!hm)
316     BUG( NULL );
317     memset (&mi, 0, sizeof mi);
318     mi.cbSize = sizeof mi;
319     s = (char *)_("Paste Key from Clipboard");
320     mi.fType = MF_STRING;
321     mi.dwTypeData = s;
322     mi.cch = strlen (s);
323     mi.fMask = MIIM_DATA | MIIM_ID | MIIM_TYPE;
324     mi.wID = ID_KEYCTX_PASTE;
325     InsertMenuItem (hm, 0, FALSE, &mi);
326     GetCursorPos (&p);
327     TrackPopupMenu (hm, 0, p.x, p.y, 0, dlg, NULL);
328     DestroyMenu (hm);
329     } /* do_create_minpopup */
330    
331    
332     static void
333     do_check_cache( listview_ctrl_t lv, HWND dlg, HWND sb )
334     {
335     gpgme_keycache_t cache;
336    
337     if( keycache_get_reload( ) ) {
338     keycache_reload( dlg );
339     keycache_set_reload( 0 );
340     cache = keycache_get_ctx( 1 );
341     if( !cache )
342     BUG( dlg );
343     keylist_reload( lv, cache, KEYLIST_LIST, GPGME_ATTR_USERID );
344     km_complete_status_bar (sb, lv, km_def_pos);
345     }
346     } /* do_check_cache */
347    
348    
349     long CALLBACK
350     separator_wnd_proc( HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam )
351     {
352     static POINT last_mouse_pos;
353    
354     if (msg == WM_CREATE)
355     SetWindowLong (hwnd, GWL_USERDATA, (long)(((CREATESTRUCT*)lparam)->lpCreateParams));
356    
357     switch (msg) {
358     case WM_PAINT:
359     PAINTSTRUCT ps;
360     RECT rect;
361     HPEN hpen;
362    
363     GetClientRect (hwnd, &rect);
364     BeginPaint (hwnd, &ps);
365    
366     // Background
367     FillRect (ps.hdc, &rect, (HBRUSH)(COLOR_3DFACE+1));
368    
369     // The lines from the light into the dark
370     MoveToEx(ps.hdc, 0,0, NULL);
371     if ((hpen = CreatePen(PS_SOLID, 0, GetSysColor(COLOR_3DHILIGHT))) != NULL) {
372     SelectObject(ps.hdc, (LPVOID)hpen);
373     LineTo(ps.hdc, rect.right, 0);
374     DeleteObject(hpen);
375     }
376     MoveToEx(ps.hdc, 0, 1, NULL);
377     if ((hpen = CreatePen(PS_SOLID, 0, GetSysColor(COLOR_3DLIGHT))) != NULL) {
378     SelectObject(ps.hdc, (LPVOID)hpen);
379     LineTo(ps.hdc, rect.right, rect.bottom);
380     DeleteObject(hpen);
381     }
382    
383     MoveToEx(ps.hdc, 0, rect.bottom-1, NULL);
384     if ((hpen = CreatePen(PS_SOLID, 0, GetSysColor(COLOR_3DSHADOW))) != NULL) {
385     SelectObject(ps.hdc, (LPVOID)hpen);
386     LineTo(ps.hdc, rect.right, rect.bottom-1);
387     DeleteObject(hpen);
388     }
389     MoveToEx(ps.hdc, 0, rect.bottom, NULL);
390     if ((hpen = CreatePen(PS_SOLID, 0, GetSysColor(COLOR_3DDKSHADOW))) != NULL) {
391     SelectObject(ps.hdc, (LPVOID)hpen);
392     LineTo(ps.hdc, rect.right, rect.bottom);
393     DeleteObject(hpen);
394     }
395    
396     EndPaint (hwnd, &ps);
397     return 0;
398    
399     case WM_LBUTTONDOWN:
400     last_mouse_pos.x = LOWORD(lparam);
401     last_mouse_pos.y = HIWORD(lparam);
402     ClientToScreen (hwnd, &last_mouse_pos);
403     SetCapture (hwnd);
404     return 0;
405    
406     case WM_LBUTTONUP:
407     ReleaseCapture ();
408     return 0;
409    
410     case WM_MOUSEMOVE:
411     if (wparam == MK_LBUTTON) {
412     struct km_info *kmi;
413     POINT p;
414     RECT rect;
415    
416     if ((kmi = (struct km_info *)GetWindowLong (hwnd, GWL_USERDATA)) == NULL)
417     break;
418    
419     // Calculate mouse movement
420     p.x = LOWORD(lparam);
421     p.y = HIWORD(lparam);
422     ClientToScreen (hwnd, &p);
423    
424     GetWindowRect (hwnd, &rect);
425     rect.top += (short)(p.y - last_mouse_pos.y);
426     rect.bottom += (short)(p.y - last_mouse_pos.y);
427    
428     last_mouse_pos.y = p.y;
429    
430     // Apply mouse movement to window. Beware the MoveWindow is relaive
431     // to parent NOT screen
432     MapWindowPoints (NULL, GetParent(hwnd), (POINT*)&rect, 2);
433     kmi->ypos_sep = rect.top;
434     kmi->ypercent_sep = 0; // This forces do_resize_window to use abs. position
435     do_resize_window (GetParent(hwnd), kmi);
436     return 0;
437     }
438     }
439    
440     return DefWindowProc (hwnd, msg, wparam, lparam);
441     }
442    
443    
444     static HWND
445     regist_sep_wnd (HWND dlg, struct km_info * kmi)
446     {
447     WNDCLASS wndclass;
448     HWND h;
449    
450     wndclass.style = CS_DBLCLKS | CS_HREDRAW | CS_VREDRAW;
451     wndclass.lpfnWndProc = separator_wnd_proc;
452     wndclass.cbClsExtra = 0;
453     wndclass.cbWndExtra = sizeof (long);
454     wndclass.hInstance = glob_hinst;
455     wndclass.hIcon = NULL;
456     wndclass.hCursor = LoadCursor (NULL, IDC_SIZENS);
457     wndclass.hbrBackground = (HBRUSH) GetStockObject (LTGRAY_BRUSH);
458     wndclass.lpszMenuName = 0;
459     wndclass.lpszClassName = WINDOWCLASS_SEPARATOR_CHILD;
460     RegisterClass (&wndclass);
461    
462     h = CreateWindowEx (WS_EX_WINDOWEDGE, WINDOWCLASS_SEPARATOR_CHILD, NULL,
463     WS_CHILD | WS_CLIPCHILDREN | WS_VISIBLE,
464     0, 400, 2000, 5, dlg, (HMENU) 0, glob_hinst, kmi);
465     ShowWindow (h, SW_SHOW);
466     return h;
467     } /* regist_sep_wnd */
468    
469    
470    
471     #define enable_button(hwnd, cid) \
472     SendMessage ((hwnd), TB_ENABLEBUTTON, (cid), MAKELONG (key_selected, 0))
473    
474     void
475     update_ui_items (HWND hwnd, listview_ctrl_t lv)
476     {
477     int key_selected = 0, key_has_sec = 0;
478     int i, key_inv = 0;
479     HWND hwnd_child;
480     HMENU hmenu;
481    
482     /* Get some states */
483     key_selected = SendMessage (GetDlgItem (hwnd, IDC_KEYMISC_KEYLIST),
484     LVM_GETSELECTEDCOUNT, 0, 0)
485     ? TRUE : FALSE;
486     key_has_sec = FALSE;
487     if (key_selected) {
488     i = listview_get_curr_pos (lv);
489     key_has_sec = km_check_for_seckey( lv, i, NULL ) ? TRUE : FALSE;
490     key_inv = km_get_key_status (lv, i) & KM_FLAG_REVOKED;
491     }
492    
493     /* Enable / disable toolbar buttons */
494     hwnd_child = GetDlgItem(hwnd, IDR_WINPT_KMTB);
495     enable_button (hwnd_child, ID_KEYMISC_DELETE);
496     enable_button (hwnd_child, ID_KEYMISC_PROPS);
497     enable_button (hwnd_child, ID_KEYMISC_SIGN);
498     enable_button (hwnd_child, ID_KEYMISC_EXPORT);
499    
500     /* Enable / disable menu items */
501     hmenu = GetMenu (hwnd);
502     set_menu_state (hmenu, ID_KEYMISC_EXPORT, key_selected ? MF_ENABLED : MF_GRAYED);
503     set_menu_state (hmenu, ID_KEYMISC_EXPORT_PRIVKEY, key_has_sec ? MF_ENABLED : MF_GRAYED);
504     set_menu_state (hmenu, ID_KEYMISC_REVCERT, key_has_sec ? MF_ENABLED : MF_GRAYED);
505     set_menu_state (hmenu, ID_KEYMISC_DELETE, key_selected ? MF_ENABLED : MF_GRAYED);
506     set_menu_state (hmenu, ID_KEYMISC_PROPS, key_selected ? MF_ENABLED : MF_GRAYED);
507     set_menu_state (hmenu, ID_KEYMISC_SIGN, key_selected && !key_inv ? MF_ENABLED : MF_GRAYED);
508     } /* update_ui_items */
509    
510    
511     static void
512     popup_gpg_readonly (HWND dlg, HMENU hm)
513     {
514     int resids[] = {
515     ID_KEYCTX_PASTE,
516     ID_KEYCTX_EDIT,
517     ID_KEYCTX_SIGN,
518     ID_KEYCTX_REV,
519     ID_KEYCTX_DEL,
520     ID_KEYCTX_ENABLE,
521     ID_KEYCTX_DISABLE,
522     ID_KEYCTX_RECVFROM,
523     ID_KEYCTX_ADDKEY,
524     ID_KEYCTX_ADDPHOTO,
525     ID_KEYCTX_ADDUID,
526     ID_KEYCTX_ADDREV,
527     0
528     };
529     int i;
530    
531     if (gpg_read_only == 0)
532     return;
533     for (i=0; resids[i] != 0; i++)
534     set_menu_state (hm, resids[i], MF_GRAYED);
535     }
536    
537    
538     static void
539     menu_gpg_readonly (HWND dlg, HMENU hm, int id)
540     {
541 twoaday 19 int key_resids[] = {
542 twoaday 2 ID_KEYMISC_SIGN,
543     ID_KEYMISC_DELETE,
544     ID_KEYMISC_REVCERT,
545     ID_KEYMISC_IMPORT_HTTP,
546     ID_KEYMISC_IMPORT,
547     ID_KEYMISC_REFRESH_KEYS,
548     ID_KEYMISC_REBUILD,
549     ID_KEYMISC_KEYWIZARD,
550     ID_KEYMISC_CARDNEW,
551     ID_KEYMISC_NEWKEY,
552     0,
553     };
554 twoaday 19 int edit_resids[] = {
555 twoaday 2 ID_KEYMISC_PASTE,
556     ID_KEYMISC_OT,
557     ID_KEYMISC_EDITKEY,
558     0
559     };
560     int * resids;
561     int i;
562    
563    
564     if (gpg_read_only == 0)
565     return;
566 twoaday 19 switch (id) {
567 twoaday 2 case 0: return;
568     case 3: resids = key_resids; break;
569     case 1: resids = edit_resids;break;
570     }
571    
572     for (i=0; resids[i] != 0; i++)
573     set_menu_state (hm, resids[i], MF_GRAYED);
574     }
575    
576    
577 twoaday 22 static char*
578     gen_export_filename (const char *keyid)
579     {
580     gpgme_key_t key;
581     const char *s;
582     char *p;
583    
584     if (get_pubkey (keyid, &key))
585     return m_strdup (keyid);
586     s = gpgme_key_get_string_attr (key, GPGME_ATTR_NAME, NULL, 0);
587     if (!s)
588     return m_strdup (keyid);
589     p = new char[strlen (s) + 8];
590     if (!p)
591     BUG (0);
592     sprintf (p, "%s.asc", s);
593     for (size_t i=0; i < strlen (p); i++) {
594     if (p[i] == ' ' || p[i] == ':' || p[i] == '?' || p[i] == '|')
595     p[i] = '_';
596     }
597     return p;
598     }
599    
600    
601     static void
602     update_key (listview_ctrl_t lv, int pos, const char *keyid, int keypair)
603     {
604     gpgme_key_t key;
605    
606     keycache_update (0, keyid);
607     if (keypair)
608     keycache_update (1, keyid);
609    
610     /* because we replaced the key item, we need to update the
611     address of the pointer in the ListView control. */
612     get_pubkey (keyid, &key);
613     keylist_upd_key (lv, pos, key);
614     }
615    
616    
617    
618 twoaday 2 BOOL CALLBACK
619     keymanager_dlg_proc (HWND dlg, UINT msg, WPARAM wparam, LPARAM lparam)
620     {
621     struct km_info *kmi;
622     static km_group_s *gc = NULL;
623     static HMENU menu = NULL;
624     static int refresh_keys = 0;
625     INITCOMMONCONTROLSEX icex;
626     HWND kl;
627     HMENU hm;
628     gpgme_keycache_t c;
629 twoaday 22 gpgme_key_t key;
630     /*km_group_cb_s gcb; XXX */
631 twoaday 20 struct genkey_s genkey;
632 twoaday 2 struct winpt_key_s k = {0};
633 twoaday 19 struct URL_ctx_s *url;
634 twoaday 2 refresh_cache_s rcs = {0};
635 twoaday 22 char keyid[48], uid[128], type[32], * name;
636 twoaday 2 const char *t, * host;
637     u16 port = 0;
638     int idx = 0, i=0, rc;
639     size_t size = 0;
640    
641     if ((msg != WM_INITDIALOG)
642     && ((kmi = (struct km_info*)GetWindowLong (dlg, GWL_USERDATA)) == NULL))
643     return FALSE;
644    
645     switch (msg) {
646     case WM_INITDIALOG:
647     kmi = new struct km_info;
648     memset (kmi, 0, sizeof (struct km_info));
649     icex.dwSize = sizeof (INITCOMMONCONTROLSEX);
650     icex.dwICC = ICC_BAR_CLASSES;
651     InitCommonControlsEx (&icex);
652     kmi->hwnd_sep = regist_sep_wnd (dlg, kmi);
653     imagelist_load (dlg);
654    
655     #ifndef LANG_DE
656     SetWindowText( dlg, _("Key Manager") );
657     #endif
658     menu = LoadMenu( glob_hinst, (LPCSTR)IDR_WINPT_KEYMISC );
659     #ifndef LANG_DE
660     set_menu_text( menu, ID_KEYMISC_QUIT, _("&Quit") );
661     set_menu_text( menu, ID_KEYMISC_UID, _("User ID") );
662     set_menu_text( menu, ID_KEYMISC_NEWKEY, _("&Expert") );
663     set_menu_text( menu, ID_KEYMISC_KEYWIZARD, _("&Normal") );
664     set_menu_text( menu, ID_KEYMISC_EDIT, _("Edit") );
665     set_menu_text( menu, ID_KEYMISC_SIGN, _("&Sign") );
666     set_menu_text( menu, ID_KEYMISC_DELETE, _("&Delete") );
667     set_menu_text( menu, ID_KEYMISC_REVCERT, _("&Revoke") );
668     set_menu_text( menu, ID_KEYMISC_CHECKSIGS, _("&List Signatures") );
669     set_menu_text( menu, ID_KEYMISC_TRUSTPATH, _("List Trust Path") );
670     set_menu_text( menu, ID_KEYMISC_PROPS, _("&Properties") );
671     set_menu_text( menu, ID_KEYMISC_GPGOPT, _("Options") );
672     set_menu_text( menu, ID_KEYMISC_GPGPREFS, _("Preferences") );
673     set_menu_text( menu, ID_KEYMISC_SENDRECV, _("Keyserver") );
674     set_menu_text( menu, ID_KEYMISC_RECACHE, _("Re&load Key Cache") );
675     set_menu_text( menu, ID_KEYMISC_REBUILD, _("R&everify Signatures") );
676     set_menu_text( menu, ID_KEYMISC_INFO, _("Info") );
677     set_menu_text (menu, ID_KEYMISC_HELP, _("Help"));
678     #endif
679     SetMenu( dlg, menu );
680     if( keyring_check_last_access() )
681     keycache_set_reload( 1 );
682     if( keycache_get_reload( ) )
683     keycache_reload( dlg );
684     c = keycache_get_ctx( KEYCACHE_PUB );
685     if( !c )
686     BUG( NULL );
687     kmi->keylist_sortby = GPGME_ATTR_USERID;
688     Header_SetImageList(ListView_GetHeader(GetDlgItem( dlg, IDC_KEYMISC_KEYLIST )),
689     glob_imagelist);
690     kmi->lv = keylist_load( GetDlgItem( dlg, IDC_KEYMISC_KEYLIST ), c,
691     NULL, KEYLIST_LIST, kmi->keylist_sortby );
692     /* init subclassing for the listview */
693     kl = GetDlgItem( dlg, IDC_KEYMISC_KEYLIST );
694     keylist_proc.dlg = dlg;
695     keylist_proc.current = (WNDPROC)keylist_subclass_proc;
696     keylist_proc.old = (WNDPROC)GetWindowLong( kl, GWL_WNDPROC );
697     if( keylist_proc.old ) {
698     if( !SetWindowLong( kl, GWL_WNDPROC, (LONG)keylist_proc.current) ) {
699     msg_box( dlg, _("Could not set keylist window procedure."), _("Key Manager"), MB_ERR );
700     BUG( NULL );
701     }
702     }
703 twoaday 22 #if 0
704 twoaday 2 km_groups_new( &gc, GetDlgItem( dlg, IDC_KEYMISC_GROUP ) );
705     km_groups_load( gc );
706 twoaday 22 #endif
707 twoaday 2 SetForegroundWindow( dlg );
708    
709     kmi->statbar = CreateStatusWindow( WS_CHILD | WS_VISIBLE | CCS_BOTTOM, "status bar", dlg, 100000 );
710     ShowWindow( kmi->statbar, SW_SHOW );
711     km_update_default_key_str (kmi->statbar, &km_def_pos);
712     km_complete_status_bar( kmi->statbar, kmi->lv, km_def_pos );
713    
714     SetWindowLong (dlg, GWL_USERDATA, (LONG)kmi);
715     kmi->toolbar = load_toolbar (dlg, kmi);
716    
717     do_center_window( dlg, kmi );
718     do_resize_window( dlg, kmi );
719     update_ui_items (dlg, kmi->lv);
720     return TRUE;
721    
722     case WM_DESTROY:
723     if (kmi->lv) {
724     keylist_delete (kmi->lv);
725     kmi->lv = NULL;
726    
727     if (gc) {
728     km_groups_release (gc);
729     gc = NULL;
730     }
731     imagelist_destroy ();
732    
733     char buf[32];
734     ltoa (kmi->pos_x, buf, 10);
735     set_reg_entry( HKEY_CURRENT_USER, "Software\\WinPT", "KM_Pos_X", buf );
736     ltoa (kmi->pos_y, buf, 10);
737     set_reg_entry (HKEY_CURRENT_USER, "Software\\WinPT", "KM_Pos_Y", buf);
738     /* Remove runtime information. This should be the last action taken here. */
739     delete kmi; kmi = NULL;
740     SetWindowLong (dlg, GWL_USERDATA, NULL);
741     keycache_set_reload (refresh_keys);
742     return FALSE;
743    
744     case WM_MOVE:
745     /* kmi->pos_x = (int)(short)LOWORD(lparam);
746     kmi->pos_y = (int)(short)HIWORD(lparam); */
747     RECT r;
748     GetWindowRect (dlg, &r);
749     kmi->pos_x = r.left;
750     kmi->pos_y = r.top;
751     break;
752    
753    
754     case WM_RBUTTONUP:
755     do_create_minpopup (dlg);
756     break;
757    
758     case WM_NOTIFY:
759     NMHDR * notify;
760     POINT p;
761     HMENU popup;
762    
763     notify = (NMHDR *)lparam;
764     if (notify == NULL)
765     break;
766     switch (notify->code)
767     {
768     case TBN_QUERYDELETE:
769     SetWindowLong(dlg, DWL_MSGRESULT, TRUE);
770     return TRUE;
771    
772     case TBN_QUERYINSERT:
773     SetWindowLong(dlg, DWL_MSGRESULT, TRUE);
774     return TRUE;
775    
776     case TBN_GETBUTTONINFO:
777     LPTBNOTIFY lpTbNotify;
778     lpTbNotify = (LPTBNOTIFY)lparam;
779     if (lpTbNotify->iItem < (sizeof(myb) / sizeof(mybuttons))) {
780     lpTbNotify->tbButton.iBitmap = imagelist_getindex(myb[lpTbNotify->iItem].icon);
781     lpTbNotify->tbButton.idCommand = myb[lpTbNotify->iItem].command;
782     lpTbNotify->tbButton.fsState = TBSTATE_ENABLED;
783     lpTbNotify->tbButton.fsStyle = TBSTYLE_BUTTON;
784     lpTbNotify->tbButton.iString = 0;
785     strncpy (lpTbNotify->pszText, myb[lpTbNotify->iItem].text, lpTbNotify->cchText);
786     SetWindowLong(dlg, DWL_MSGRESULT, TRUE);
787     }
788     return TRUE;
789    
790     case TBN_RESET: /* Restore last saved toolbar status */
791     TBSAVEPARAMS tbsp;
792    
793     tbsp.hkr = HKEY_CURRENT_USER;
794     tbsp.pszSubKey = "Software\\WinPT";
795     tbsp.pszValueName = "KM_toolbar";
796     SendMessage (notify->hwndFrom, TB_SAVERESTORE, FALSE, (LPARAM)&tbsp);
797     break;
798    
799     case TBN_BEGINADJUST: /* User is about to change the toolbar. Save it */
800     tbsp.hkr = HKEY_CURRENT_USER;
801     tbsp.pszSubKey = "Software\\WinPT";
802     tbsp.pszValueName = "KM_toolbar";
803     SendMessage (notify->hwndFrom, TB_SAVERESTORE, TRUE, (LPARAM)&tbsp);
804     break;
805    
806     case TBN_ENDADJUST: /* User has finished customizing the toolbar. Save it. */
807     tbsp.hkr = HKEY_CURRENT_USER;
808     tbsp.pszSubKey = "Software\\WinPT";
809     tbsp.pszValueName = "KM_toolbar";
810     SendMessage (notify->hwndFrom, TB_SAVERESTORE, TRUE, (LPARAM)&tbsp);
811     break;
812    
813     case TTN_GETDISPINFO:
814     LPTOOLTIPTEXT lpttt;
815     lpttt = (LPTOOLTIPTEXT)lparam;
816    
817     lpttt->hinst = NULL;
818     switch (lpttt->hdr.idFrom) {
819     case ID_KEYMISC_DELETE:
820     lpttt->lpszText = (char *)_("Delete key from keyring");
821     break;
822    
823     case ID_KEYMISC_PROPS:
824     lpttt->lpszText = (char *)_("Show key properties");
825     break;
826    
827     case ID_KEYMISC_SIGN:
828     lpttt->lpszText = (char *)_("Sign key");
829     break;
830    
831     case ID_KEYMISC_IMPORT:
832     lpttt->lpszText = (char *)_("Import key to keyring");
833     break;
834    
835     case ID_KEYMISC_EXPORT:
836     lpttt->lpszText = (char *)_("Export key to a file");
837     break;
838     }
839     return TRUE;
840    
841     case LVN_ITEMCHANGED:
842     if (((LPNMLISTVIEW)lparam)->uNewState) /* item selected? */
843     {
844     update_ui_items (dlg, kmi->lv);
845     return TRUE;
846     }
847     break;
848    
849     case NM_RCLICK:
850 twoaday 22 if (notify->idFrom == IDC_KEYMISC_KEYLIST) {
851 twoaday 2 if (listview_get_curr_pos (kmi->lv) == -1)
852     return TRUE; /* Popup only when a item was selected */
853     do_check_cache (kmi->lv, dlg, kmi->statbar);
854     GetCursorPos (&p);
855     hm = LoadMenu (glob_hinst, MAKEINTRESOURCE (IDR_WINPT_KEYMISC_CTX));
856     popup = GetSubMenu (hm, 0);
857     #ifndef LANG_DE
858     set_menu_text( popup, ID_KEYCTX_UID_COPY, _("Copy User ID to Clipboard") );
859     set_menu_text( popup, ID_KEYCTX_COPY, _("Copy Key to Clipboard") );
860     set_menu_text( popup, ID_KEYCTX_PASTE, _("Paste Key from Clipboard") );
861     set_menu_text( popup, ID_KEYCTX_RECVFROM, _("Refresh from Keyserver") );
862     #endif
863     idx = listview_get_curr_pos (kmi->lv);
864 twoaday 22 if (km_check_for_seckey (kmi->lv, idx, &i))
865 twoaday 2 set_menu_state (popup, ID_KEYCTX_SETDEFKEY, MF_ENABLED);
866 twoaday 22 if (i == 0)
867 twoaday 2 set_menu_state (popup, ID_KEYCTX_MAXTRUST, MF_ENABLED);
868 twoaday 19 if (!km_check_for_seckey (kmi->lv, idx, NULL)) {
869 twoaday 2 set_menu_state( popup, ID_KEYCTX_REV, MF_DISABLED|MF_GRAYED );
870     set_menu_state( popup, ID_KEYCTX_ADDKEY, MF_DISABLED|MF_GRAYED );
871     set_menu_state( popup, ID_KEYCTX_ADDUID, MF_DISABLED|MF_GRAYED );
872     set_menu_state( popup, ID_KEYCTX_ADDREV, MF_DISABLED|MF_GRAYED );
873     set_menu_state( popup, ID_KEYCTX_ADDPHOTO, MF_DISABLED|MF_GRAYED );
874     set_menu_state (popup, ID_KEYCTX_SETPREFKS, MF_DISABLED|MF_GRAYED);
875     }
876     else if( km_check_for_seckey( kmi->lv, idx, NULL )
877     && km_key_is_v3( kmi->lv, idx ) )
878     {
879     /* PGP 2 keys, version 3 have no no support for photo-id's,
880     designated revokers and secondary keys. */
881 twoaday 20 set_menu_state (popup, ID_KEYCTX_ADDKEY, MF_DISABLED|MF_GRAYED);
882     set_menu_state (popup, ID_KEYCTX_ADDREV, MF_DISABLED|MF_GRAYED);
883     set_menu_state (popup, ID_KEYCTX_ADDPHOTO, MF_DISABLED|MF_GRAYED);
884 twoaday 2 }
885     if( km_get_key_status( kmi->lv, idx ) & KM_FLAG_DISABLED )
886     set_menu_state( popup, ID_KEYCTX_DISABLE, MF_DISABLED|MF_GRAYED );
887     else
888     set_menu_state( popup, ID_KEYCTX_ENABLE, MF_DISABLED|MF_GRAYED );
889     if (km_get_key_status (kmi->lv, idx) & KM_FLAG_REVOKED)
890     set_menu_state (popup, ID_KEYCTX_SIGN, MF_DISABLED|MF_GRAYED);
891 twoaday 22 if (mapi_init())
892     set_menu_state (popup, ID_KEYCTX_SENDMAIL, MF_DISABLED|MF_GRAYED);
893     /* Override 'Default Keyserver' with the actual name. */
894     host = kserver_get_hostname (0, -1, &port);
895     set_menu_text (popup, ID_KEYCTX_KS_DEFAULT, host);
896 twoaday 2 popup_gpg_readonly (dlg, popup);
897 twoaday 22 TrackPopupMenu (popup, TPM_RIGHTALIGN, p.x, p.y, 0, dlg, NULL);
898     DestroyMenu (popup);
899     DestroyMenu (hm);
900 twoaday 2 return TRUE;
901     }
902 twoaday 22 #if 0 /* XXX */
903 twoaday 2 if( notify->idFrom == IDC_KEYMISC_GROUP ) {
904     HWND tree = GetDlgItem( dlg, IDC_KEYMISC_GROUP );
905     if( TreeView_GetSelection( tree ) ) {
906     GetCursorPos( &p );
907     hm = LoadMenu( glob_hinst, MAKEINTRESOURCE(IDR_WINPT_GROUP_CTX) );
908     popup = GetSubMenu( hm, 0 );
909     if( km_index == -1 )
910     set_menu_state( popup, ID_GROUP_PASTE, MF_DISABLED|MF_GRAYED );
911     set_menu_text( popup, ID_GROUP_PASTE, _("Paste into this group") );
912     set_menu_text( popup, ID_GROUP_DELETE, _("Delete") );
913     TrackPopupMenu( popup, TPM_RIGHTALIGN, p.x, p.y, 0, dlg, NULL );
914     DestroyMenu( popup );
915     DestroyMenu( hm );
916     return TRUE;
917     }
918     }
919 twoaday 22 #endif
920 twoaday 2 break;
921    
922     case LVN_COLUMNCLICK:
923     if (notify->idFrom == IDC_KEYMISC_KEYLIST) {
924     NMLISTVIEW * p = (LPNMLISTVIEW) lparam;
925     int sortby = 0;
926     switch (p->iSubItem) {
927     case 0: sortby = GPGME_ATTR_USERID; break;
928     case 1: sortby = GPGME_ATTR_KEYID; break;
929     case 2: sortby = GPGME_ATTR_IS_SECRET; break;
930     case 3: sortby = GPGME_ATTR_LEN; break;
931     case 5: sortby = GPGME_ATTR_VALIDITY; break;
932     case 6: sortby = GPGME_ATTR_OTRUST; break;
933     case 7: sortby = GPGME_ATTR_CREATED; break;
934     case 8: sortby = GPGME_ATTR_ALGO; break;
935     default: return TRUE; //sortby = GPGME_ATTR_USERID; break;
936     }
937    
938     if ((kmi->keylist_sortby & ~KEYLIST_SORT_DESC) == sortby)
939     kmi->keylist_sortby ^= KEYLIST_SORT_DESC;
940     else
941     kmi->keylist_sortby = sortby;
942     keylist_sort (kmi->lv, kmi->keylist_sortby);
943     return TRUE;
944     }
945     break;
946     }
947     break;
948     }
949    
950     case WM_WINDOWPOSCHANGING:
951     if (((WINDOWPOS*)lparam)->cx < 400)
952     ((WINDOWPOS*)lparam)->cx = 400;
953     if (((WINDOWPOS*)lparam)->cy < 200)
954     ((WINDOWPOS*)lparam)->cy = 200;
955     return TRUE;
956    
957     case WM_SIZE:
958     do_resize_window (dlg, kmi);
959     return TRUE;
960    
961     case WM_SYSCOMMAND:
962     if( LOWORD (wparam) == SC_CLOSE )
963     EndDialog( dlg, TRUE );
964     return FALSE;
965    
966     case WM_MENUSELECT:
967     menu_gpg_readonly (dlg, (HMENU)lparam, LOWORD (wparam));
968     break;
969    
970     case WM_COMMAND:
971     if( gnupg_access_keyring( 1 ) ) {
972     msg_box( dlg, _("Could not access public keyring"), _("Key Manager"), MB_ERR );
973     return FALSE;
974     }
975     do_check_cache( kmi->lv, dlg, kmi->statbar );
976     switch( LOWORD( wparam ) ) {
977     case ID_KEYMISC_QUIT:
978     EndDialog( dlg, TRUE );
979     return TRUE;
980    
981     case ID_KEYMISC_MAIL:
982     DialogBoxParam (glob_hinst, (LPCTSTR)IDD_WINPT_MAIL, GetDesktopWindow (),
983     winpt_mail_proc, NULL);
984     break;
985 twoaday 22
986     case ID_KEYMISC_FIND:
987     km_find_key (dlg, kmi->lv);
988     break;
989    
990 twoaday 2 case ID_KEYMISC_DELETE:
991     if (km_delete_keys (kmi->lv, dlg))
992     return FALSE;
993     return TRUE;
994    
995 twoaday 22 case ID_KEYMISC_SIGN:
996 twoaday 2 if ( (idx = listview_get_curr_pos( kmi->lv )) == -1 ) {
997     msg_box( dlg, _("Please select a key."), _("Key Manager"),
998     MB_ERR );
999     return FALSE;
1000     }
1001 twoaday 22 if (km_check_key_status (kmi->lv, idx))
1002 twoaday 2 return FALSE;
1003 twoaday 22 key = (gpgme_key_t)listview_get_item2 (kmi->lv, idx);
1004     listview_get_item_text (kmi->lv, idx, 1, keyid, DIM (keyid)-1);
1005     memset (&k, 0, sizeof (k));
1006     k.ctx = key;
1007     k.keyid = keyid;
1008     dialog_box_param (glob_hinst, (LPCSTR)IDD_WINPT_KEYSIGN, dlg,
1009     keysign_dlg_proc, (LPARAM)&k,
1010     _("Key Signing"), IDS_WINPT_KEYSIGN);
1011     if (k.update)
1012     update_key (kmi->lv, idx, k.keyid, 0);
1013 twoaday 2 return TRUE;
1014    
1015     case ID_KEYMISC_REVCERT:
1016     idx = listview_get_curr_pos( kmi->lv );
1017     if( idx == -1 ) {
1018     msg_box( dlg, _("Please select a key."), _("Key Manager"), MB_ERR );
1019     return FALSE;
1020     }
1021     listview_get_item_text( kmi->lv, idx, 0, uid, sizeof uid -1 );
1022     listview_get_item_text( kmi->lv, idx, 1, keyid, sizeof keyid-1 );
1023     if ( !km_check_for_seckey( kmi->lv, idx, NULL ) ) {
1024     msg_box( dlg, _("There is no secret key available!"), _("Key Manager"), MB_ERR );
1025     return FALSE;
1026     }
1027    
1028     {
1029     char t[128];
1030     listview_get_item_text( kmi->lv, idx, 5, t, sizeof t -1 );
1031     if( strchr( t, 'R' ) ) {
1032     msg_box( dlg, _("Key already revoked!"), _("Key Manager"), MB_INFO );
1033     return FALSE;
1034     }
1035     }
1036    
1037 twoaday 22 name = km_quote_uid (uid);
1038 twoaday 19 memset (&k, 0, sizeof (k));
1039 twoaday 2 k.key_pair = 1;
1040     k.keyid = keyid;
1041 twoaday 22 k.uid = name;
1042 twoaday 2 k.is_protected = km_check_if_protected( kmi->lv, idx );
1043     dialog_box_param( glob_hinst, (LPCSTR)IDD_WINPT_KEYREVOKE, dlg,
1044     key_revoke_dlg_proc, (LPARAM)&k,
1045     _("Key Revocation"), IDS_WINPT_KEYREVOKE );
1046 twoaday 22 free_if_alloc (name);
1047 twoaday 2 return TRUE;
1048    
1049     case ID_KEYMISC_TRUSTPATH:
1050     idx = listview_get_curr_pos( kmi->lv );
1051     if( idx == -1 ) {
1052     msg_box( dlg, _("Please select a key."), _("Key Manager"), MB_ERR );
1053     return FALSE;
1054     }
1055     listview_get_item_text( kmi->lv, idx, 0, uid, sizeof uid -1 );
1056     listview_get_item_text( kmi->lv, idx, 1, keyid, sizeof keyid -1 );
1057     if( km_check_for_seckey( kmi->lv, idx, NULL ) ) {
1058     msg_box( dlg, _("It does not make any sense with a key pair!"), _("Key Manager"), MB_OK );
1059     return FALSE;
1060     }
1061 twoaday 19 memset (&k, 0, sizeof (k));
1062 twoaday 2 k.keyid = keyid;
1063     k.uid = uid;
1064     dialog_box_param( glob_hinst, (LPCSTR)IDD_WINPT_KEYTRUST, dlg,
1065     keytrust_dlg_proc, (LPARAM)&k,
1066     _("List Trust Path"), IDS_WINPT_KEYTRUST );
1067     return TRUE;
1068    
1069     case ID_KEYMISC_CHECKSIGS:
1070 twoaday 22 idx = listview_get_curr_pos (kmi->lv);
1071 twoaday 2 if( idx == -1 ) {
1072     msg_box( dlg, _("Please select a key."), _("Key Manager"), MB_ERR );
1073     return FALSE;
1074     }
1075 twoaday 19 listview_get_item_text (kmi->lv, idx, 0, uid, DIM (uid)-1);
1076     listview_get_item_text (kmi->lv, idx, 1, keyid, DIM (keyid)-1);
1077     memset (&k, 0, sizeof (k));
1078 twoaday 2 k.keyid = keyid;
1079     k.uid = uid;
1080 twoaday 22 k.ctx = (gpgme_key_t)listview_get_item2 (kmi->lv, idx);
1081 twoaday 19 dialog_box_param (glob_hinst, (LPCSTR)IDD_WINPT_KEYSIG, dlg,
1082 twoaday 2 keysig_dlg_proc, (LPARAM)&k,
1083 twoaday 19 _("Key Signature List" ), IDS_WINPT_KEYSIG);
1084 twoaday 2 return TRUE;
1085    
1086     case ID_KEYMISC_PROPS:
1087     idx = listview_get_curr_pos( kmi->lv );
1088     if( idx == -1 ) {
1089     msg_box( dlg, _("Please select a key."), _("Key Manager"), MB_ERR );
1090     return FALSE;
1091     }
1092 twoaday 22 listview_get_item_text (kmi->lv, idx, 1, keyid, DIM (keyid)-1);
1093     listview_get_item_text (kmi->lv, idx, 2, type, DIM (type)-1);
1094 twoaday 19 memset (&k, 0, sizeof (k));
1095 twoaday 2 k.key_pair = 0;
1096     k.keyid = keyid;
1097     if( !strcmp( type, "pub/sec" ) || !strcmp( type, "pub/crd" ) )
1098     k.key_pair = 1;
1099     k.callback.ctl = kmi->lv;
1100     k.callback.idx = idx;
1101     dialog_box_param( glob_hinst, (LPCSTR)IDD_WINPT_KEYPROPS, dlg,
1102     keyprops_dlg_proc, (LPARAM)&k,
1103     _("Key Properties"), IDS_WINPT_KEYPROPS );
1104 twoaday 22 if (k.callback.new_val != 0) {
1105 twoaday 2 t = get_key_trust_str (k.callback.new_val);
1106     listview_add_sub_item (kmi->lv, idx, 6, t);
1107     }
1108     return TRUE;
1109    
1110     case ID_KEYMISC_RECACHE:
1111     /* If there is already a reload request, don't bother the user with a message. */
1112     if( keycache_get_reload() == 1 )
1113     idx = IDYES;
1114     else {
1115     char t[256];
1116     _snprintf( t, sizeof t -1,
1117     _("This is only useful when the keyring has been "
1118     "modified (sign a key...).\n"
1119     "Do you really want to reload the keycache?") );
1120     idx = msg_box( dlg, t, _("Key Manager"), MB_YESNO );
1121     }
1122     if( idx == IDYES ) {
1123     rcs.kr_reload = rcs.kr_update = 1;
1124     rcs.tr_update = 0;
1125     DialogBoxParam( glob_hinst, (LPCSTR)IDD_WINPT_KEYCACHE, dlg,
1126     keycache_dlg_proc, (LPARAM)&rcs );
1127     c = keycache_get_ctx( 1 );
1128     if( !c )
1129     BUG( dlg );
1130     keylist_reload( kmi->lv, c, KEYLIST_LIST, GPGME_ATTR_USERID );
1131     refresh_keys = 0;
1132     }
1133     return TRUE;
1134    
1135     case ID_KEYMISC_REBUILD:
1136 twoaday 22 rcs.kr_reload = rcs.kr_update = 0;
1137 twoaday 2 rcs.tr_update = 1;
1138     DialogBoxParam( glob_hinst, (LPCSTR)IDD_WINPT_KEYCACHE, dlg,
1139     keycache_dlg_proc, (LPARAM)&rcs );
1140     c = keycache_get_ctx( 1 );
1141     if( !c )
1142     BUG( dlg );
1143     keylist_reload( kmi->lv, c, KEYLIST_LIST, GPGME_ATTR_USERID );
1144     return TRUE;
1145    
1146     case ID_KEYMISC_NEWKEY:
1147 twoaday 20 memset (&genkey, 0, sizeof (genkey));
1148     dialog_box_param (glob_hinst, (LPCSTR)IDD_WINPT_KEYGEN, dlg,
1149     keygen_dlg_proc, (LPARAM)&genkey, _("Key Generation"),
1150     IDS_WINPT_KEYGEN);
1151 twoaday 22 if (genkey.newkey != NULL)
1152 twoaday 20 keylist_add_key (kmi->lv, KEYLIST_LIST, genkey.newkey);
1153 twoaday 2 return TRUE;
1154    
1155     case ID_KEYMISC_CARDNEW:
1156     if( !scard_support ) {
1157     msg_box( dlg, _("Smart Card support is not available."), _("Key Manager"), MB_INFO );
1158     return FALSE;
1159     }
1160     dialog_box_param( glob_hinst, (LPCSTR)IDD_WINPT_CARD_KEYGEN, dlg,
1161     card_keygen_dlg_proc, NULL, _("Card Key Generation"),
1162     IDS_WINPT_CARD_KEYGEN );
1163 twoaday 22 /* XXX: use new code */
1164 twoaday 2 if( keycache_get_reload() )
1165     send_cmd_id( dlg, ID_KEYMISC_RECACHE );
1166     return TRUE;
1167    
1168     case ID_KEYMISC_KEYWIZARD:
1169 twoaday 20 memset (&genkey, 0, sizeof (genkey));
1170     dialog_box_param (glob_hinst, (LPCSTR)IDD_WINPT_KEYWIZARD, dlg,
1171     keygen_wizard_dlg_proc, (LPARAM)&genkey, _("Key Generation Wizard"),
1172     IDS_WINPT_KEYWIZARD);
1173 twoaday 22 if (genkey.newkey != NULL)
1174 twoaday 20 keylist_add_key (kmi->lv, KEYLIST_LIST, genkey.newkey);
1175 twoaday 2 return TRUE;
1176    
1177     case ID_KEYMISC_SENDRECV:
1178     dialog_box_param( glob_hinst, (LPCSTR)IDD_WINPT_KEYSERVER, dlg,
1179     keyserver_dlg_proc, NULL, _("Keyserver Access"),
1180     IDS_WINPT_KEYSERVER );
1181     return TRUE;
1182    
1183     case ID_KEYMISC_GPGPREFS:
1184     dialog_box_param( glob_hinst, (LPCSTR)IDD_WINPT_GPGPREFS, dlg,
1185     gpgprefs_dlg_proc, NULL, _("GnuPG Preferences"),
1186     IDS_WINPT_GPGPREFS );
1187     return TRUE;
1188    
1189     case ID_KEYMISC_GPGOPT:
1190     dialog_box_param( glob_hinst, (LPCSTR)IDD_WINPT_GPGOPT, dlg,
1191     gpgopt_dlg_proc, NULL, _("GnuPG Options" ),
1192     IDS_WINPT_GPGOPT );
1193     return TRUE;
1194    
1195     case ID_KEYMISC_IMPORT:
1196 twoaday 22 t = get_filename_dlg (dlg, FILE_OPEN, _("Choose Name of the Key File"), NULL, NULL);
1197     if (t)
1198     km_file_import (dlg, t);
1199 twoaday 2 return TRUE;
1200    
1201     case ID_KEYMISC_IMPORT_HTTP:
1202 twoaday 19 url = (struct URL_ctx_s*)get_http_file_dlg (dlg);
1203     if (url->cancel == 0)
1204     km_http_import (dlg, url->url);
1205     delete url; url=NULL;
1206 twoaday 2 break;
1207    
1208     case ID_KEYMISC_EXPORT:
1209 twoaday 22 idx = listview_get_curr_pos (kmi->lv);
1210     if (idx == -1) {
1211 twoaday 2 msg_box( dlg, _("Please select a key."), _("Key Manager"), MB_ERR );
1212     return FALSE;
1213     }
1214 twoaday 22 if (listview_count_items (kmi->lv, 1) > 1)
1215     name = m_strdup ("Exported_GPG_Keys.asc");
1216 twoaday 2 else {
1217 twoaday 22 listview_get_item_text (kmi->lv, idx, 1, keyid, DIM (keyid)-1);
1218     name = gen_export_filename (keyid);
1219 twoaday 2 }
1220 twoaday 22 t = get_filename_dlg (dlg, FILE_SAVE, _("Choose Name for Key File"), NULL, name);
1221     free_if_alloc (name);
1222     if (!t || overwrite_file (t) == 0)
1223 twoaday 2 return FALSE;
1224 twoaday 22 km_file_export (dlg, kmi->lv, t);
1225 twoaday 2 return TRUE;
1226    
1227     case ID_KEYMISC_EXPORT_PRIVKEY:
1228     idx = listview_get_curr_pos( kmi->lv );
1229     if( idx == -1 ) {
1230     msg_box( dlg, _("Please select a key."), _("Key Manager"), MB_ERR );
1231     return FALSE;
1232     }
1233     if( !km_check_for_seckey( kmi->lv, idx, NULL ) ) {
1234     msg_box( dlg, _("There is no corresponding secret key for this key."),
1235     _("Key Manager"), MB_ERR );
1236     return FALSE;
1237     }
1238     if( listview_count_items( kmi->lv, 1 ) > 1 ) {
1239     msg_box( dlg, _("You can only export one secret key."), _("Key Manager"), MB_ERR );
1240     return FALSE;
1241     }
1242     idx = msg_box( dlg,
1243     _("This operation will export your *SECRET* key!\n\n"
1244     "Never send this key to ANYONE, it should be available\n"
1245     "ONLY on your machine and you may use this function\n"
1246     "to copy the key to a safe place.\n\n"
1247     "Do you really want to export the key?"),
1248     _("WARNING"), MB_INFO|MB_YESNO );
1249     if( idx == IDYES ) {
1250     idx = listview_get_curr_pos( kmi->lv );
1251     listview_get_item_text( kmi->lv, idx, 1, keyid, sizeof (keyid)-8 );
1252 twoaday 22 name = gen_export_filename (keyid);
1253     t = get_filename_dlg (dlg, 1, _("Choose Name for Key File"), NULL, name);
1254     if (t && !file_exist_check (t)) {
1255     idx = log_box (_("Key Manager"), MB_INFO|MB_YESNO,
1256     _("File %s already exists.\nOverwrite?"), t);
1257     if (idx == IDNO)
1258 twoaday 2 return FALSE;
1259     }
1260 twoaday 22 if (t)
1261     km_privkey_export (dlg, kmi->lv, t);
1262 twoaday 2 }
1263     break;
1264    
1265     case ID_KEYMISC_INFO:
1266     dialog_box_param( glob_hinst, (LPCSTR)IDD_WINPT_ABOUT, glob_hwnd,
1267     about_winpt_dlg_proc, NULL, _("About WinPT"),
1268     IDS_WINPT_ABOUT );
1269     break;
1270    
1271     case ID_KEYMISC_HELP:
1272     ShellExecute (dlg, "open", "winpt.chm", NULL, NULL, SW_SHOW);
1273     break;
1274    
1275     case ID_KEYMISC_OT:
1276     dialog_box_param( glob_hinst, (LPCTSTR)IDD_WINPT_OWNERTRUST, glob_hwnd,
1277     ownertrust_dlg_proc, NULL,
1278     _("Ownertrust"), IDS_WINPT_OWNERTRUST );
1279     break;
1280    
1281     case ID_KEYMISC_EDITKEY:
1282     idx = listview_get_curr_pos (kmi->lv);
1283 twoaday 16 if (idx == -1)
1284 twoaday 2 break;
1285 twoaday 16 listview_get_item_text (kmi->lv, idx, 1, keyid, sizeof (keyid)-1);
1286     /* XXX: pub/crd = secret key does not work */
1287 twoaday 19 memset (&k, 0, sizeof (k));
1288 twoaday 16 k.is_protected = km_check_if_protected (kmi->lv, idx);
1289     k.key_pair = km_check_for_seckey (kmi->lv, idx, NULL);
1290 twoaday 2 k.keyid = keyid;
1291 twoaday 20 k.is_v3 = km_key_is_v3 (kmi->lv, idx);
1292 twoaday 16 k.flags = km_get_key_status (kmi->lv, idx);
1293     dialog_box_param (glob_hinst, (LPCTSTR)IDD_WINPT_KEYEDIT, dlg,
1294 twoaday 2 keyedit_main_dlg_proc, (LPARAM)&k,
1295 twoaday 16 _("Key Edit"), IDS_KEYCTX_EDIT);
1296 twoaday 22 if (k.update)
1297     update_key (kmi->lv, idx, keyid, 1);
1298 twoaday 2 break;
1299    
1300     case ID_KEYMISC_COPY:
1301 twoaday 22 km_index = listview_get_curr_pos (kmi->lv);
1302     km_clip_export (dlg, kmi->lv);
1303 twoaday 2 break;
1304    
1305     case ID_KEYMISC_SELALL:
1306     listview_select_all (kmi->lv);
1307     break;
1308    
1309     case ID_KEYMISC_PASTE:
1310     km_index = -1;
1311     km_clip_import (dlg);
1312     break;
1313    
1314     case ID_KEYCTX_SETPREFKS:
1315 twoaday 19 listview_get_item_text (kmi->lv, idx, 1, keyid, DIM(keyid)-1);
1316     memset (&k, 0, sizeof (k));
1317     k.keyid = keyid;
1318     keyedit_set_pref_keyserver (&k, dlg);
1319 twoaday 2 break;
1320    
1321     case ID_KEYMISC_REFRESH_KEYS:
1322 twoaday 19 if (listview_count_items (kmi->lv, 1) == 0) {
1323 twoaday 2 msg_box (dlg, _("No key was selected, select all by default."), _("Key Manager"), MB_INFO);
1324     listview_select_all (kmi->lv);
1325     }
1326     km_refresh_from_keyserver (kmi->lv, dlg);
1327     break;
1328    
1329     /** Context menu **/
1330     case ID_KEYCTX_EDIT:
1331     send_cmd_id( dlg, ID_KEYMISC_EDITKEY );
1332     break;
1333    
1334     case ID_KEYCTX_PROPS:
1335     send_cmd_id( dlg, ID_KEYMISC_PROPS );
1336     break;
1337    
1338     case ID_KEYCTX_UIDS:
1339     send_cmd_id( dlg, ID_KEYMISC_UID );
1340     break;
1341    
1342     case ID_KEYCTX_SIGN:
1343     send_cmd_id( dlg, ID_KEYMISC_SIGN );
1344     break;
1345    
1346     case ID_KEYCTX_DEL:
1347     send_cmd_id (dlg, ID_KEYMISC_DELETE);
1348     break;
1349    
1350     case ID_KEYCTX_REV:
1351     send_cmd_id( dlg, ID_KEYMISC_REVCERT );
1352     break;
1353    
1354     case ID_KEYCTX_SENDMAIL:
1355 twoaday 22 km_send_to_mail_recipient (kmi->lv, dlg);
1356 twoaday 2 break;
1357    
1358     case ID_KEYCTX_KS_DEFAULT:
1359     host = kserver_get_hostname (0, -1, &port);
1360     km_send_to_keyserver (kmi->lv, dlg, host, port);
1361     break;
1362    
1363     case ID_KEYCTX_ADDKEY:
1364 twoaday 22 idx = listview_get_curr_pos (kmi->lv);
1365 twoaday 19 listview_get_item_text( kmi->lv, idx, 1, keyid, DIM (keyid)-1);
1366     memset (&k, 0, sizeof (k));
1367 twoaday 22 k.key_pair = km_check_for_seckey (kmi->lv, idx, NULL);
1368     k.is_protected = km_check_if_protected (kmi->lv, idx);
1369 twoaday 2 k.keyid = keyid;
1370 twoaday 22 keyedit_add_subkey (&k, dlg, NULL);
1371     if (k.update)
1372     update_key (kmi->lv, idx, keyid, 1);
1373 twoaday 2 break;
1374    
1375     case ID_KEYCTX_ADDUID:
1376 twoaday 22 idx = listview_get_curr_pos (kmi->lv);
1377 twoaday 19 listview_get_item_text( kmi->lv, idx, 1, keyid, DIM (keyid)-1);
1378     memset (&k, 0, sizeof (k));
1379 twoaday 22 k.key_pair = km_check_for_seckey (kmi->lv, idx, NULL);
1380     k.is_protected = km_check_if_protected (kmi->lv, idx);
1381 twoaday 2 k.keyid = keyid;
1382 twoaday 22 keyedit_add_userid (&k, dlg, NULL);
1383     if (k.update)
1384     update_key (kmi->lv, idx, keyid, 1);
1385 twoaday 2 break;
1386    
1387     case ID_KEYCTX_ADDREV:
1388 twoaday 19 idx = listview_get_curr_pos (kmi->lv);
1389     listview_get_item_text (kmi->lv, idx, 1, keyid, DIM (keyid)-1);
1390     memset (&k, 0, sizeof (k));
1391 twoaday 2 k.keyid = keyid;
1392 twoaday 22 k.is_protected = km_check_if_protected (kmi->lv, idx);
1393 twoaday 2 k.key_pair = km_check_for_seckey( kmi->lv, idx, NULL );
1394 twoaday 22 keyedit_add_revoker (&k, dlg);
1395     if (k.update)
1396     update_key (kmi->lv, idx, keyid, 1);
1397 twoaday 2 break;
1398    
1399     case ID_KEYCTX_ADDPHOTO:
1400 twoaday 19 idx = listview_get_curr_pos (kmi->lv);
1401     listview_get_item_text (kmi->lv, idx, 1, keyid, DIM (keyid)-1);
1402     memset (&k, 0, sizeof (k));
1403 twoaday 2 k.keyid = keyid;
1404 twoaday 19 k.is_protected = km_check_if_protected (kmi->lv, idx);
1405     k.key_pair = km_check_for_seckey (kmi->lv, idx, NULL);
1406     keyedit_add_photo (&k, dlg);
1407 twoaday 22 if (k.update)
1408     update_key (kmi->lv, idx, keyid, 1);
1409 twoaday 2 break;
1410    
1411     case ID_KEYCTX_KS_NL:
1412     case ID_KEYCTX_KS_PL:
1413     case ID_KEYCTX_KS_AT:
1414     case ID_KEYCTX_KS_DE:
1415     case ID_KEYCTX_KS_DK:
1416     case ID_KEYCTX_KS_CZ:
1417     case ID_KEYCTX_KS_ES:
1418     case ID_KEYCTX_KS_UK:
1419     host = kserver_get_hostname (LOWORD (wparam) - 40107, 0, &port);
1420     km_send_to_keyserver (kmi->lv, dlg, host, port);
1421     break;
1422    
1423     case ID_KEYCTX_RECVFROM:
1424     km_refresh_from_keyserver (kmi->lv, dlg);
1425     break;
1426    
1427     case ID_KEYCTX_UID_COPY:
1428     /* XXX: add generic function to support multiple selection
1429     with a callback */
1430     idx = listview_get_curr_pos( kmi->lv );
1431     listview_get_item_text( kmi->lv, idx, 0, uid, sizeof uid-1 );
1432     set_clip_text( NULL, uid, strlen( uid ) );
1433     break;
1434    
1435     case ID_KEYCTX_KEYID_COPY:
1436     idx = listview_get_curr_pos( kmi->lv );
1437     listview_get_item_text( kmi->lv, idx, 1, uid, sizeof uid-1 );
1438     set_clip_text( NULL, uid, strlen( uid ) );
1439     break;
1440    
1441     case ID_KEYCTX_FPR_COPY:
1442     idx = listview_get_curr_pos( kmi->lv );
1443 twoaday 22 key = (gpgme_key_t) listview_get_item2 (kmi->lv, idx);
1444     if (key) {
1445     const char * s = get_key_fpr (key);
1446     set_clip_text (NULL, s? s : "", s? strlen (s): 0);
1447 twoaday 2 }
1448     break;
1449    
1450     case ID_KEYCTX_KINFO_COPY:
1451     idx = listview_get_curr_pos( kmi->lv );
1452     listview_get_item_text( kmi->lv, idx, 1, uid, sizeof uid-1 );
1453     km_set_clip_info( uid );
1454     break;
1455    
1456     case ID_KEYCTX_COPY:
1457     km_index = listview_get_curr_pos (kmi->lv);
1458     km_clip_export (dlg, kmi->lv);
1459     break;
1460    
1461     case ID_KEYCTX_PASTE:
1462     km_index = -1;
1463     km_clip_import (dlg);
1464     break;
1465    
1466     case ID_KEYCTX_DISABLE:
1467     idx = listview_get_curr_pos (kmi->lv);
1468     km_enable_disable_key (kmi->lv, dlg, idx, 0);
1469     break;
1470    
1471     case ID_KEYCTX_ENABLE:
1472     idx = listview_get_curr_pos (kmi->lv);
1473     km_enable_disable_key (kmi->lv, dlg, idx, 1);
1474     break;
1475    
1476     case ID_KEYCTX_LISTSIGS:
1477     send_cmd_id (dlg, ID_KEYMISC_CHECKSIGS);
1478     break;
1479    
1480     case ID_KEYCTX_MAXTRUST:
1481     idx = listview_get_curr_pos (kmi->lv);
1482 twoaday 22 listview_get_item_text (kmi->lv, idx, 1, keyid, DIM (keyid)-1);
1483     rc = km_set_implicit_trust (dlg, kmi->lv, idx);
1484     if (!rc)
1485     update_key (kmi->lv, idx, keyid, 0);
1486 twoaday 2 break;
1487    
1488     case ID_KEYCTX_SETDEFKEY:
1489     idx = listview_get_curr_pos (kmi->lv);
1490 twoaday 22 if (!km_check_key_status (kmi->lv, idx)) {
1491     listview_get_item_text (kmi->lv, idx, 1, keyid, DIM (keyid)-1);
1492 twoaday 2 rc = set_gnupg_default_key (keyid);
1493     if (rc)
1494     msg_box( dlg, winpt_strerror (rc), _("Key Manager"), MB_ERR);
1495     km_update_default_key_str (kmi->statbar, &km_def_pos);
1496     }
1497     break;
1498 twoaday 22
1499     #if 0 /* XXX */
1500     case ID_GROUP_NEW:
1501     memset (&gcb, 0, sizeof (gcb));
1502     gcb.gc = gc;
1503     dialog_box_param (glob_hinst, (LPCSTR)IDD_WINPT_GROUP, glob_hwnd,
1504     group_manager_dlg_proc, (LPARAM)&gcb, _("New Group"),
1505     IDS_WINPT_GROUP);
1506     if( gcb.use_name )
1507     treeview_add_item( GetDlgItem(dlg, IDC_KEYMISC_GROUP), NULL, gcb.name );
1508     return TRUE;
1509 twoaday 2
1510     case ID_GROUP_PASTE:
1511     km_groups_add (gc, kmi->lv, km_index);
1512     break;
1513    
1514     case ID_GROUP_DELETE:
1515     km_groups_del (gc);
1516     break;
1517 twoaday 22 #endif
1518     }
1519    
1520 twoaday 2 break;
1521     }
1522    
1523     return FALSE;
1524     } /* keymanager_dlg_proc */
1525    
1526    

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26