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

Diff of /trunk/Src/wptKeyManagerDlg.cpp

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 30 by twoaday, Sat Oct 22 10:17:59 2005 UTC revision 164 by twoaday, Fri Jan 20 09:19:15 2006 UTC
# Line 1  Line 1 
1  /* wptKeyManagerDlg.cpp - WinPT Key Manager  /* wptKeyManagerDlg.cpp - WinPT Key Manager
2   *      Copyright (C) 2000-2005 Timo Schulz   *      Copyright (C) 2000-2006 Timo Schulz
3   *      Copyright (C) 2004 Andreas Jobs   *      Copyright (C) 2004 Andreas Jobs
4   *   *
5   * This file is part of WinPT.   * This file is part of WinPT.
6   *   *
7   * WinPT is free software; you can redistribute it and/or modify   * 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   * 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   * the Free Software Foundation; either version 2 of the License, or
10   * (at your option) any later version.   * (at your option) any later version.
11   *   *
12   * WinPT is distributed in the hope that it will be useful,   * WinPT is distributed in the hope that it will be useful,
13   * but WITHOUT ANY WARRANTY; without even the implied warranty of   * but WITHOUT ANY WARRANTY; without even the implied warranty of
14   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15   * GNU General Public License for more details.   * GNU General Public License for more details.
16   *   *
17   * You should have received a copy of the GNU General Public License   * You should have received a copy of the GNU General Public License
18   * along with WinPT; if not, write to the Free Software Foundation,   * along with WinPT; if not, write to the Free Software Foundation,
19   * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA   * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
20   */   */
21    #ifdef HAVE_CONFIG_H
22  #include <windows.h>  #include <config.h>
23  #include <commctrl.h>  #endif
24    
25  #include "../resource.h"  #include <windows.h>
26  #include "wptTypes.h"  #include <commctrl.h>
27  #include "wptGPG.h"  
28  #include "wptCommonCtl.h"  #include "resource.h"
29  #include "wptContext.h" /* for passphrase_s */  #include "wptTypes.h"
30  #include "wptDlgs.h"  #include "wptGPG.h"
31  #include "wptW32API.h"  #include "wptCommonCtl.h"
32  #include "wptVersion.h"  #include "wptContext.h" /* for passphrase_s */
33  #include "wptKeylist.h"  #include "wptDlgs.h"
34  #include "wptNLS.h"  #include "wptW32API.h"
35  #include "wptErrors.h"  #include "wptVersion.h"
36  #include "wptHotkey.h"  #include "wptKeylist.h"
37  #include "wptKeyManager.h"  #include "wptNLS.h"
38  #include "wptKeyserver.h"  #include "wptErrors.h"
39  #include "wptKeyEdit.h"  #include "wptHotkey.h"
40  #include "wptRegistry.h"  #include "wptKeyManager.h"
41    #include "wptKeyserver.h"
42  #define KM_SEPARATOR_ID                10000  #include "wptKeyEdit.h"
43  #define WINDOWCLASS_SEPARATOR_CHILD "WINPT_SEP_CHILD"  #include "wptRegistry.h"
44  //#define KM_SEPARATOR_HEIGHT            5  #include "wptUTF8.h"
45    
46    /* Name and ID of the separator window. */
47  static subclass_s keylist_proc;  #define KM_SEPARATOR_ID                 10000
48  static int km_index = -1;  #define WINDOWCLASS_SEPARATOR_CHILD     "WINPT_SEP_CHILD"
49    
50  HIMAGELIST glob_imagelist;  /* Virtual key codes. */
51    #ifndef VK_F
52  struct km_info {  #define VK_F 70
53      /* Window positions */  #endif
54      int pos_x, pos_y;  #ifndef VK_A
55      int ypos_sep;  #define VK_A 65
56      int ypercent_sep;  #endif
57    #ifndef VK_C
58      HWND hwnd_sep;  #define VK_C 67
59      HWND toolbar;  #endif
60      HWND statbar;  #ifndef VK_P
61    #define VK_P 80
62      listview_ctrl_t lv;  #endif
63      int keylist_sortby;  
64  };  static subclass_s keylist_proc;
65    
66  struct mybuttons {  HIMAGELIST glob_imagelist;
67      long icon;  
68      long command;  struct km_info_s {
69      char *text;      /* Window positions */
70  };      int pos_x, pos_y;
71        int ypos_sep;
72  struct mybuttons myb[] = {      int ypercent_sep;
73      {IMI_KEY_DELETE, ID_KEYMISC_DELETE, "Delete"},  
74      {IMI_KEY_PROPS,  ID_KEYMISC_PROPS,  "Properties"},      HWND dlg;
75      {IMI_KEY_SIGN,   ID_KEYMISC_SIGN,   "Sign"},      HWND hwnd_sep;
76      {IMI_KEY_IMPORT, ID_KEYMISC_IMPORT, "Import"},      HWND toolbar;
77      {IMI_KEY_EXPORT, ID_KEYMISC_EXPORT, "Export"},      HWND statbar;
78  };  
79        listview_ctrl_t lv;
80        int             lv_idx;
81  #ifndef VK_F      int keylist_sortby;
82  #define VK_F 70  };
83  #endif  
84  #ifndef VK_A  /* Toolbar button structure. */
85  #define VK_A 65  struct mybuttons {
86  #endif      long icon;
87  #ifndef VK_C      long command;
88  #define VK_C 67      char *text;
89  #endif  };
90  #ifndef VK_P  
91  #define VK_P 80  struct mybuttons myb[] = {
92  #endif      {IMI_KEY_DELETE, ID_KEYMISC_DELETE, "Delete"},
93        {IMI_KEY_PROPS,  ID_KEYMISC_PROPS,  "Properties"},
94  static BOOL CALLBACK      {IMI_KEY_SIGN,   ID_KEYMISC_SIGN,   "Sign"},
95  keylist_subclass_proc (HWND dlg, UINT msg, WPARAM wparam, LPARAM lparam)      {IMI_KEY_IMPORT, ID_KEYMISC_IMPORT, "Import"},
96  {      {IMI_KEY_EXPORT, ID_KEYMISC_EXPORT, "Export"},
97      struct listview_ctrl_s lv;  };
98    
99      switch (msg) {  
100      case WM_LBUTTONDBLCLK:  /* Subclass the keylist listview control to allow extended commands. */
101          send_cmd_id (keylist_proc.dlg, ID_KEYMISC_PROPS);  static BOOL CALLBACK
102          break;  keylist_subclass_proc (HWND dlg, UINT msg, WPARAM wparam, LPARAM lparam)
103            {
104      case WM_KEYUP:      struct listview_ctrl_s lv;
105          int virt_key = (int)wparam;  
106          switch (virt_key) {      switch (msg) {
107          case VK_SPACE:      case WM_LBUTTONDBLCLK:
108              send_cmd_id( keylist_proc.dlg, ID_KEYMISC_PROPS );          send_cmd_id (keylist_proc.dlg, ID_KEYMISC_PROPS);
109              break;          break;
110                    
111          case VK_DELETE:      case WM_KEYUP:
112              send_cmd_id( keylist_proc.dlg, ID_KEYMISC_DELETE );          int virt_key = (int)wparam;
113              break;          switch (virt_key) {
114                    case VK_SPACE:
115          case VK_INSERT:              send_cmd_id (keylist_proc.dlg, ID_KEYMISC_PROPS);
116              send_cmd_id (keylist_proc.dlg, ID_KEYMISC_PASTE);              break;
117              break;          
118            case VK_DELETE:
119          case VK_A:              send_cmd_id (keylist_proc.dlg, ID_KEYMISC_DELETE);
120              if (GetAsyncKeyState (VK_CONTROL)) {              break;
121                  lv.ctrl = GetDlgItem (keylist_proc.dlg, IDC_KEYMISC_KEYLIST);          
122                  listview_select_all (&lv);          case VK_INSERT:
123              }              send_cmd_id (keylist_proc.dlg, ID_KEYMISC_PASTE);
124              break;              break;
125                
126          case VK_C:          case VK_A:
127              if (GetAsyncKeyState (VK_CONTROL)) {              if (GetAsyncKeyState (VK_CONTROL)) {
128                  lv.ctrl = GetDlgItem (keylist_proc.dlg, IDC_KEYMISC_KEYLIST);                  lv.ctrl = GetDlgItem (keylist_proc.dlg, IDC_KEYMISC_KEYLIST);
129                  km_index = listview_get_curr_pos (&lv);                  listview_select_all (&lv);
130                  km_clip_export (keylist_proc.dlg, &lv);              }
131              }              break;
132              break;              
133            case VK_C:
134          case VK_P:              if (GetAsyncKeyState (VK_CONTROL)) {
135              if (GetAsyncKeyState (VK_CONTROL)) {                  lv.ctrl = GetDlgItem (keylist_proc.dlg, IDC_KEYMISC_KEYLIST);
136                  km_index = -1;                  km_clip_export (keylist_proc.dlg, &lv);
137                  km_clip_import (keylist_proc.dlg);              }
138              }              break;
139              break;  
140            case VK_P:
141          case VK_F:              if (GetAsyncKeyState (VK_CONTROL))
142              if (GetAsyncKeyState (VK_CONTROL)) {                  send_cmd_id (keylist_proc.dlg, ID_KEYMISC_PASTE);
143                  lv.ctrl = GetDlgItem (keylist_proc.dlg, IDC_KEYMISC_KEYLIST);              break;
144                  km_find_key (dlg, &lv);  
145              }          case VK_F:
146              break;              if (GetAsyncKeyState (VK_CONTROL)) {
147          }                  lv.ctrl = GetDlgItem (keylist_proc.dlg, IDC_KEYMISC_KEYLIST);
148          break;                  km_find_key (dlg, &lv);
149      }              }
150                    break;
151      return CallWindowProc( keylist_proc.old, dlg, msg, wparam, lparam );          }
152  } /* keylist_subclass_proc */          break;
153        }
154        
155  static HWND      return CallWindowProc (keylist_proc.old, dlg, msg, wparam, lparam);
156  load_toolbar (HWND dlg, struct km_info * kmi)  }
157  {  
158      HWND tbwnd;  
159      TBSAVEPARAMS tbsp;  #define ico2idx(ico) imagelist_getindex((ico))
160      TBBUTTON tb_buttons[] = {  
161          /*{imagelist_getindex(IMI_EXIT),       ID_KEYMISC_QUIT,   TBSTATE_ENABLED, TBSTYLE_BUTTON, 0, 0L, 0},*/  static HWND
162          {imagelist_getindex(IMI_KEY_DELETE), ID_KEYMISC_DELETE, TBSTATE_ENABLED, TBSTYLE_BUTTON, {0}, 0L, 0},  load_toolbar (HWND dlg, struct km_info_s *kmi)
163          {imagelist_getindex(IMI_KEY_PROPS),  ID_KEYMISC_PROPS,  TBSTATE_ENABLED, TBSTYLE_BUTTON, {0}, 0L, 0},  {
164          {imagelist_getindex(IMI_KEY_SIGN),   ID_KEYMISC_SIGN,   TBSTATE_ENABLED, TBSTYLE_BUTTON, {0}, 0L, 0},      HWND tbwnd;
165          {0,                                  0,                 0,               TBSTYLE_SEP,    {0}, 0L, 0},      TBSAVEPARAMS tbsp;
166          {imagelist_getindex(IMI_KEY_IMPORT), ID_KEYMISC_IMPORT, TBSTATE_ENABLED, TBSTYLE_BUTTON, {0}, 0L, 0},      TBBUTTON tb_buttons[] = {
167          {imagelist_getindex(IMI_KEY_EXPORT), ID_KEYMISC_EXPORT, TBSTATE_ENABLED, TBSTYLE_BUTTON, {0}, 0L, 0},          {ico2idx (IMI_KEY_NEW),    ID_KEYMISC_KEYWIZARD, TBSTATE_ENABLED, TBSTYLE_BUTTON, {0}, 0L},
168       };          {ico2idx (IMI_KEY_DELETE), ID_KEYMISC_DELETE, TBSTATE_ENABLED, TBSTYLE_BUTTON, {0}, 0L, 0},
169                {ico2idx (IMI_KEY_PROPS),  ID_KEYMISC_PROPS,  TBSTATE_ENABLED, TBSTYLE_BUTTON, {0}, 0L, 0},
170      tbwnd = CreateWindowEx (0, TOOLBARCLASSNAME, NULL,          {ico2idx (IMI_KEY_SIGN),   ID_KEYMISC_SIGN,   TBSTATE_ENABLED, TBSTYLE_BUTTON, {0}, 0L, 0},
171                              WS_CHILD|TBSTYLE_TOOLTIPS|TBSTYLE_FLAT|CCS_ADJUSTABLE,          {ico2idx (IMI_KEY_SEARCH), ID_KEYMISC_SENDRECV, TBSTATE_ENABLED, TBSTYLE_BUTTON, {0}, 0L, 0},
172                              0, 0, 0, 0, dlg, (HMENU)IDR_WINPT_KMTB, glob_hinst, NULL);          {0,                                  0,                 0,               TBSTYLE_SEP,    {0}, 0L, 0},
173      if (tbwnd) {          {ico2idx (IMI_KEY_FILE_IMPORT), ID_KEYMISC_IMPORT, TBSTATE_ENABLED, TBSTYLE_BUTTON, {0}, 0L, 0},
174          SendMessage (tbwnd, TB_BUTTONSTRUCTSIZE, (WPARAM) sizeof(TBBUTTON), 0);          {ico2idx (IMI_KEY_FILE_EXPORT), ID_KEYMISC_EXPORT, TBSTATE_ENABLED, TBSTYLE_BUTTON, {0}, 0L, 0},
175          SendMessage (tbwnd, TB_SETIMAGELIST, 0, (LPARAM)glob_imagelist);          {ico2idx (IMI_KEY_IMPORT), ID_KEYCTX_PASTE, TBSTATE_ENABLED, TBSTYLE_BUTTON, {0}, 0L, 0},
176                  SendMessage (tbwnd, TB_AUTOSIZE, 0, 0);          {ico2idx (IMI_KEY_EXPORT), ID_KEYCTX_COPY, TBSTATE_ENABLED, TBSTYLE_BUTTON, {0}, 0L, 0},
177          ShowWindow (tbwnd, SW_SHOW);       };
178        
179          /* Restore toolbar */      tbwnd = CreateWindowEx (0, TOOLBARCLASSNAME, NULL,
180          tbsp.hkr = HKEY_CURRENT_USER;                              WS_CHILD|TBSTYLE_TOOLTIPS|TBSTYLE_FLAT|CCS_ADJUSTABLE,
181          tbsp.pszSubKey = "Software\\WinPT";                              0, 0, 0, 0, dlg, (HMENU)IDR_WINPT_KMTB, glob_hinst, NULL);
182          tbsp.pszValueName = "KM_toolbar";      if (tbwnd) {
183          if (SendMessage(tbwnd, TB_SAVERESTORE, FALSE, (LPARAM)&tbsp ) == 0)          SendMessage (tbwnd, TB_BUTTONSTRUCTSIZE, (WPARAM) sizeof(TBBUTTON), 0);
184              SendMessage (tbwnd, TB_ADDBUTTONS, sizeof(tb_buttons) / sizeof(tb_buttons[0]), (LONG)&tb_buttons[0]);          SendMessage (tbwnd, TB_SETIMAGELIST, 0, (LPARAM)glob_imagelist);
185       }                  SendMessage (tbwnd, TB_AUTOSIZE, 0, 0);
186       return tbwnd;          ShowWindow (tbwnd, SW_SHOW);
187  } /* load_toolbar */  
188            /* Restore toolbar */
189            tbsp.hkr = HKEY_CURRENT_USER;
190  static void          tbsp.pszSubKey = "Software\\WinPT";
191  do_center_window (HWND dlg, struct km_info * kmi)          tbsp.pszValueName = "KM_toolbar";
192  {          if (SendMessage(tbwnd, TB_SAVERESTORE, FALSE, (LPARAM)&tbsp ) == 0)
193      RECT rect;              SendMessage (tbwnd, TB_ADDBUTTONS, sizeof(tb_buttons) / sizeof(tb_buttons[0]),
194      char * p;                           (LONG)&tb_buttons[0]);
195      int pos_x = 0, pos_y = 0;       }
196                 return tbwnd;
197      /* Find bottom of keylist */  }
198      GetWindowRect (GetDlgItem(dlg, IDC_KEYMISC_KEYLIST), &rect);  
199      MapWindowPoints (NULL, dlg, (POINT*)&rect, 2);  
200    /* Restore the width of the columns from the registry.
201      kmi->ypos_sep = rect.bottom;     If no bitstring was found, the default size is used. */
202    int
203      p = get_reg_entry( HKEY_CURRENT_USER, "Software\\WinPT", "KM_Pos_X" );  restore_column_info (listview_ctrl_t hd)
204      if( p && !strcmp( p, " " ) ) {  {
205          free_if_alloc( p );          WORD *buf;
206          center_window( dlg, NULL );      HKEY root;
207          return;      DWORD type;
208      }      DWORD size = hd->cols*sizeof (WORD), i;
209      else if( p )      LONG ec;
210          pos_x = atol( p );  
211        ec = RegOpenKeyEx (HKEY_CURRENT_USER, "Software\\WinPT", 0,
212      p = get_reg_entry( HKEY_CURRENT_USER, "Software\\WinPT", "KM_Pos_Y" );                         KEY_ALL_ACCESS, &root);
213      if( p && !strcmp( p, " " ) ) {      if (ec != ERROR_SUCCESS)
214          free_if_alloc( p );          return -1;
215          center_window( dlg, NULL );  
216          return;      buf = new WORD[size/2];
217      }      if (!buf)
218      else if( p )          BUG (NULL);
219          pos_y = atol( p );      ec = RegQueryValueEx (root, "KMColumnSize", NULL, &type,
220                              (BYTE*)buf, &size);
221      if( !pos_y && !pos_x ) {      RegCloseKey (root);
222          center_window( dlg, NULL );      if (ec != ERROR_SUCCESS) {
223          return;          free_if_alloc (buf);
224      }          return -1;
225            }
226      if( pos_x > GetSystemMetrics( SM_CXSCREEN )  
227          || pos_y > GetSystemMetrics( SM_CYSCREEN ) ) {      /* check for garbled values. */
228          pos_x = pos_y = 0;      for (i=0; i < size/2; i++) {
229      }          if (buf[i] == 0 || buf[i] > 512) {
230      GetClientRect( dlg, &rect );              free_if_alloc (buf);
231      MoveWindow( dlg, pos_x, pos_y, rect.right, rect.bottom, TRUE );              return -1;
232  }          }
233        }
234        for (i=0; i < size/2; i++) {
235  static void          LVCOLUMN lvc;
236  do_resize_window( HWND dlg, struct km_info *kmi)  
237  {          memset (&lvc, 0, sizeof (lvc));
238      HWND h;          lvc.mask = LVCF_WIDTH;
239      RECT rclient, rect;          lvc.cx = buf[i];
240      BOOL bRepaint = FALSE;          ListView_SetColumn (hd->ctrl, i, &lvc);
241        }
242      /* Get rect of client area and make life easier */      free_if_alloc (buf);
243      GetClientRect( dlg, &rclient );      return 0;
244    }
245      /* Move toolbar to the top of the window */  
246      if (kmi->toolbar) {  
247          GetWindowRect(kmi->toolbar, &rect);  /* Save the current column width to the registry. */
248          ScreenToClient(dlg, (POINT*)&rect);  int
249          ScreenToClient(dlg, (POINT*)&(rect.right));  save_column_info (listview_ctrl_t hd)
250    {
251          rclient.top += rect.bottom - rect.top;      WORD *buf;
252          MoveWindow (kmi->toolbar, 0, 0, rclient.right - rclient.left,      HKEY root;
253                      rect.bottom - rect.top, bRepaint);      LONG ec;
254      }      int i;
255    
256      /* Move statusbar to the bottom of the window */      buf = new WORD[hd->cols];
257      if (kmi->statbar) {      if (!buf)
258          GetWindowRect( kmi->statbar, &rect );          BUG (NULL);
259          ScreenToClient(dlg, (POINT*)&rect);      for (i=0; i < hd->cols; i++) {
260          ScreenToClient(dlg, (POINT*)&(rect.right));          LVCOLUMN lvc;
261    
262          rclient.bottom -= rect.bottom - rect.top;          memset (&lvc, 0, sizeof (lvc));
263          MoveWindow (kmi->statbar, 0, rclient.bottom, rclient.right - rclient.left,          lvc.mask = LVCF_WIDTH;
264                      rect.bottom - rect.top, bRepaint);          ListView_GetColumn (hd->ctrl, i, &lvc);
265      }          buf[i] = lvc.cx;
266        }
267      // Obtain separator information and move it to the desired posistion  
268      if (kmi->ypercent_sep)      ec = RegOpenKeyEx (HKEY_CURRENT_USER, "Software\\WinPT", 0,
269          kmi->ypos_sep = (rclient.bottom - rclient.top) * kmi->ypercent_sep / 100;                         KEY_ALL_ACCESS, &root);
270      else      if (ec != ERROR_SUCCESS) {
271          kmi->ypercent_sep = kmi->ypos_sep * 100 / (rclient.bottom - rclient.top);          free_if_alloc (buf);
272                    return -1;
273      // Don't move away      }
274      if (kmi->ypos_sep+5 > rclient.bottom)  
275          kmi->ypos_sep = rclient.bottom - 5;      ec = RegSetValueEx (root, "KMColumnSize", 0, REG_BINARY,
276      if (kmi->ypos_sep < rclient.top)                          (const BYTE*)buf, 2*hd->cols);
277          kmi->ypos_sep = rclient.top;      RegCloseKey (root);
278      MoveWindow (kmi->hwnd_sep, 0, kmi->ypos_sep, (rclient.right - rclient.left), 5, bRepaint);      free_if_alloc (buf);
279                return ec == ERROR_SUCCESS? 0 : -1;
280      // Place the keylist above the separator  }
281      h = GetDlgItem( dlg, IDC_KEYMISC_KEYLIST );  
282      MoveWindow (h, rclient.left, rclient.top, rclient.right - rclient.left,  
283                  kmi->ypos_sep - rclient.top, bRepaint);  /* Center window @dlg. */
284      rclient.top = kmi->ypos_sep + 5 + 8;  static void
285    do_center_window (HWND dlg, struct km_info_s *kmi)
286      /* Place the group text and the group box below the separator */  {
287      h = GetDlgItem( dlg, IDC_KEYMISC_GTEXT );      RECT rect;
288      MoveWindow( h, rclient.left, rclient.top, 100, 14, bRepaint);      char *p;
289      rclient.top += 18;      int pos_x = 0;
290        int pos_y = 0;
291      h = GetDlgItem( dlg, IDC_KEYMISC_GROUP );          
292      MoveWindow (h, rclient.left, rclient.top, rclient.right - rclient.left,      /* Find bottom of keylist */
293                  (rclient.bottom < rclient.top) ? 0 : rclient.bottom - rclient.top, bRepaint);      GetWindowRect (GetDlgItem(dlg, IDC_KEYMISC_KEYLIST), &rect);
294            MapWindowPoints (NULL, dlg, (POINT*)&rect, 2);
295      /* Repaint the whole thing */  
296      InvalidateRect (dlg, NULL, TRUE);      kmi->ypos_sep = rect.bottom;
297  } /* do_resize_window */  
298        p = get_reg_entry (HKEY_CURRENT_USER, "Software\\WinPT", "KM_Pos_X");
299        if (p && !strcmp (p, " ")) {
300  static void          free_if_alloc (p);      
301  do_create_minpopup (HWND dlg)          center_window (dlg, NULL);
302  {          return;
303      HMENU hm;      }
304      MENUITEMINFO mi;      else if (p)
305      char * s;          pos_x = atol (p);
306      POINT p;  
307            p = get_reg_entry (HKEY_CURRENT_USER, "Software\\WinPT", "KM_Pos_Y");
308      if (gpg_read_only)      if (p && !strcmp (p, " ")) {
309          return;          free_if_alloc (p);
310      hm = CreatePopupMenu ();          center_window (dlg, NULL);
311      if (!hm)          return;
312          BUG( NULL );      }
313      memset (&mi, 0, sizeof mi);      else if (p)
314      mi.cbSize = sizeof mi;          pos_y = atol (p);
315      s = (char *)_("Paste Key from Clipboard");  
316      mi.fType = MF_STRING;      if (!pos_y && !pos_x) {
317      mi.dwTypeData = s;          center_window (dlg, NULL);
318      mi.cch = strlen (s);          return;
319      mi.fMask = MIIM_DATA | MIIM_ID | MIIM_TYPE;      }
320      mi.wID = ID_KEYCTX_PASTE;      
321      InsertMenuItem (hm, 0, FALSE, &mi);      if (pos_x < 0 || pos_y < 0)
322      GetCursorPos (&p);          pos_x = pos_y = 0;
323      TrackPopupMenu (hm, 0, p.x, p.y, 0, dlg, NULL);      if (pos_x > GetSystemMetrics (SM_CXSCREEN)
324      DestroyMenu (hm);          || pos_y > GetSystemMetrics (SM_CYSCREEN)) {
325  } /* do_create_minpopup */          pos_x = pos_y = 0;
326        }
327        GetClientRect (dlg, &rect);
328  static void      MoveWindow (dlg, pos_x, pos_y, rect.right, rect.bottom, TRUE);
329  do_check_cache (listview_ctrl_t lv, HWND dlg, HWND sb)  }
330  {  
331      gpg_keycache_t cache;  
332    /* Resize the key manager window with the information from @kmi. */
333      if( keycache_get_reload( ) ) {  static void
334          keycache_reload( dlg );  do_resize_window (HWND dlg, struct km_info_s *kmi)
335          keycache_set_reload( 0 );  {
336          cache = keycache_get_ctx( 1 );      HWND h;
337          if( !cache )      RECT rclient, rect;
338              BUG( dlg );      BOOL bRepaint = FALSE;
339          keylist_reload( lv, cache, KEYLIST_LIST, KEY_SORT_USERID );  
340          km_complete_status_bar (sb, lv);      /* Get rect of client area and make life easier */
341      }      GetClientRect (dlg, &rclient);
342  } /* do_check_cache */  
343        /* Move toolbar to the top of the window */
344        if (kmi->toolbar) {
345  long CALLBACK          GetWindowRect (kmi->toolbar, &rect);
346  separator_wnd_proc( HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam )          ScreenToClient (dlg, (POINT*)&rect);
347  {          ScreenToClient (dlg, (POINT*)&(rect.right));
348      static POINT last_mouse_pos;  
349            rclient.top += rect.bottom - rect.top;
350      if (msg == WM_CREATE)          MoveWindow (kmi->toolbar, 0, 0, rclient.right - rclient.left,
351          SetWindowLong (hwnd, GWL_USERDATA, (long)(((CREATESTRUCT*)lparam)->lpCreateParams));                      rect.bottom - rect.top, bRepaint);
352        }
353      switch (msg) {  
354      case WM_PAINT:      /* Move statusbar to the bottom of the window */
355          PAINTSTRUCT ps;      if (kmi->statbar) {
356          RECT rect;          GetWindowRect (kmi->statbar, &rect);
357          HPEN hpen;          ScreenToClient (dlg, (POINT*)&rect);
358            ScreenToClient (dlg, (POINT*)&(rect.right));
359          GetClientRect (hwnd, &rect);  
360          BeginPaint (hwnd, &ps);          rclient.bottom -= rect.bottom - rect.top;
361            MoveWindow (kmi->statbar, 0, rclient.bottom,
362          // Background                      rclient.right - rclient.left,
363          FillRect (ps.hdc, &rect, (HBRUSH)(COLOR_3DFACE+1));                      rect.bottom - rect.top, bRepaint);
364        }
365          // The lines from the light into the dark  
366          MoveToEx(ps.hdc, 0,0, NULL);      /* Obtain separator information and move it to the desired posistion */
367          if ((hpen = CreatePen(PS_SOLID, 0, GetSysColor(COLOR_3DHILIGHT))) != NULL) {      if (kmi->ypercent_sep)
368              SelectObject(ps.hdc, (LPVOID)hpen);          kmi->ypos_sep = (rclient.bottom - rclient.top) * kmi->ypercent_sep / 100;
369              LineTo(ps.hdc, rect.right, 0);      else
370              DeleteObject(hpen);          kmi->ypercent_sep = kmi->ypos_sep * 100 / (rclient.bottom - rclient.top);
371          }          
372          MoveToEx(ps.hdc, 0, 1, NULL);      /* Don't move away */
373          if ((hpen = CreatePen(PS_SOLID, 0, GetSysColor(COLOR_3DLIGHT))) != NULL) {      if (kmi->ypos_sep+5 > rclient.bottom)
374               SelectObject(ps.hdc, (LPVOID)hpen);          kmi->ypos_sep = rclient.bottom - 5;
375               LineTo(ps.hdc, rect.right, rect.bottom);      if (kmi->ypos_sep < rclient.top)
376               DeleteObject(hpen);          kmi->ypos_sep = rclient.top;
377           }      MoveWindow (kmi->hwnd_sep, 0, kmi->ypos_sep,
378                    (rclient.right - rclient.left), 5, bRepaint);
379          MoveToEx(ps.hdc, 0, rect.bottom-1, NULL);          
380          if ((hpen = CreatePen(PS_SOLID, 0, GetSysColor(COLOR_3DSHADOW))) != NULL) {      /* Place the keylist above the separator */
381              SelectObject(ps.hdc, (LPVOID)hpen);      h = GetDlgItem (dlg, IDC_KEYMISC_KEYLIST);
382              LineTo(ps.hdc, rect.right, rect.bottom-1);      MoveWindow (h, rclient.left, rclient.top, rclient.right - rclient.left,
383              DeleteObject(hpen);                  kmi->ypos_sep - rclient.top, bRepaint);
384          }      rclient.top = kmi->ypos_sep + 5 + 8;
385          MoveToEx(ps.hdc, 0, rect.bottom, NULL);  
386          if ((hpen = CreatePen(PS_SOLID, 0, GetSysColor(COLOR_3DDKSHADOW))) != NULL) {  #if 0 /* Disable the code until we support groups again. */
387              SelectObject(ps.hdc, (LPVOID)hpen);      /* Place the group text and the group box below the separator */
388              LineTo(ps.hdc, rect.right, rect.bottom);      h = GetDlgItem (dlg, IDC_KEYMISC_GTEXT);
389              DeleteObject(hpen);      MoveWindow (h, rclient.left, rclient.top, 100, 14, bRepaint);
390          }      rclient.top += 18;
391    
392          EndPaint (hwnd, &ps);      h = GetDlgItem (dlg, IDC_KEYMISC_GROUP);
393          return 0;      MoveWindow (h, rclient.left, rclient.top, rclient.right - rclient.left,
394                    (rclient.bottom < rclient.top) ?
395      case WM_LBUTTONDOWN:                  0 : rclient.bottom - rclient.top, bRepaint);
396          last_mouse_pos.x = LOWORD(lparam);  #endif
397          last_mouse_pos.y = HIWORD(lparam);      
398          ClientToScreen (hwnd, &last_mouse_pos);      /* Repaint the whole thing */
399          SetCapture (hwnd);      InvalidateRect (dlg, NULL, TRUE);
400          return 0;  }
401    
402      case WM_LBUTTONUP:  
403          ReleaseCapture ();  /* Return true if the clipboard contains an OpenPGP key. */
404          return 0;  static bool
405    clip_contains_pgpkey (void)
406      case WM_MOUSEMOVE:  {
407          if (wparam == MK_LBUTTON) {      char *ctxt;
408              struct km_info *kmi;      bool val = false;
409              POINT p;  
410              RECT rect;      ctxt = get_clip_text (NULL);
411        if (!ctxt || strlen (ctxt) < 512)
412              if ((kmi = (struct km_info *)GetWindowLong (hwnd, GWL_USERDATA)) == NULL)          val = false;
413                  break;      else if (strstr (ctxt, "BEGIN PGP") && strstr (ctxt, "KEY BLOCK") &&
414                 strstr (ctxt, "END PGP"))
415              // Calculate mouse movement          val = true;
416              p.x = LOWORD(lparam);      free_if_alloc (ctxt);
417              p.y = HIWORD(lparam);      return val;
418              ClientToScreen (hwnd, &p);  }
419    
420              GetWindowRect (hwnd, &rect);  
421              rect.top += (short)(p.y - last_mouse_pos.y);  /* Show a mini popup menu to import keys. */
422              rect.bottom += (short)(p.y - last_mouse_pos.y);  static void
423    do_create_minpopup (HWND dlg)
424              last_mouse_pos.y = p.y;  {
425        HMENU hm;
426              // Apply mouse movement to window. Beware the MoveWindow is relaive      MENUITEMINFO mi;
427              // to parent NOT screen      char * s;
428              MapWindowPoints (NULL, GetParent(hwnd), (POINT*)&rect, 2);      POINT p;
429              kmi->ypos_sep = rect.top;      
430              kmi->ypercent_sep = 0; // This forces do_resize_window to use abs. position      if (gpg_read_only || !clip_contains_pgpkey ())
431              do_resize_window (GetParent(hwnd), kmi);          return;
432              return 0;      hm = CreatePopupMenu ();
433          }      if (!hm)
434      }          BUG( NULL );
435        memset (&mi, 0, sizeof mi);
436      return DefWindowProc (hwnd, msg, wparam, lparam);      mi.cbSize = sizeof mi;
437  }      s = (char *)_("Paste Key from Clipboard");
438        mi.fType = MF_STRING;
439        mi.dwTypeData = s;
440  static HWND      mi.cch = strlen (s);
441  regist_sep_wnd (HWND dlg, struct km_info * kmi)      mi.fMask = MIIM_DATA | MIIM_ID | MIIM_TYPE;
442  {      mi.wID = ID_KEYCTX_PASTE;
443      WNDCLASS wndclass;      InsertMenuItem (hm, 0, FALSE, &mi);
444      HWND h;      GetCursorPos (&p);
445        TrackPopupMenu (hm, 0, p.x, p.y, 0, dlg, NULL);
446      wndclass.style         = CS_DBLCLKS | CS_HREDRAW | CS_VREDRAW;      DestroyMenu (hm);
447      wndclass.lpfnWndProc   = separator_wnd_proc;  }
448      wndclass.cbClsExtra    = 0;  
449      wndclass.cbWndExtra    = sizeof (long);  
450      wndclass.hInstance     = glob_hinst;  /* Update the default key entry in the status bar for dialog @dlg. */
451      wndclass.hIcon         = NULL;  void
452      wndclass.hCursor       = LoadCursor (NULL, IDC_SIZENS);  update_default_key_str (HWND dlg)
453      wndclass.hbrBackground = (HBRUSH) GetStockObject (LTGRAY_BRUSH);  {
454      wndclass.lpszMenuName  = 0;      const char *fmt;
455      wndclass.lpszClassName = WINDOWCLASS_SEPARATOR_CHILD;      char *keyid;
456      RegisterClass (&wndclass);      char defkeyinf[128];
457        
458      h = CreateWindowEx (WS_EX_WINDOWEDGE, WINDOWCLASS_SEPARATOR_CHILD, NULL,      /* XXX: also show the name? */
459                          WS_CHILD | WS_CLIPCHILDREN | WS_VISIBLE,      keyid = get_gnupg_default_key ();
460                          0, 400, 2000, 5, dlg, (HMENU) 0, glob_hinst, kmi);          if (!keyid)
461      ShowWindow (h, SW_SHOW);          return;
462      return h;      if ((keyid[0] >= 'A' && keyid[0] <= 'Z') ||
463  } /* regist_sep_wnd */          (keyid[0] >= 'a' && keyid[0] <= 'z') ||
464            (keyid[0] == '0' && keyid[1] == 'x'))
465            fmt = _("Default Key: %s");
466        else
467  #define enable_button(hwnd, cid) \          fmt = _("Default Key: 0x%s");
468      SendMessage ((hwnd), TB_ENABLEBUTTON, (cid), MAKELONG (key_selected, 0))      _snprintf (defkeyinf, sizeof (defkeyinf) - 1, fmt, keyid);
469        SendMessage (dlg, SB_SETTEXT, 0, (LPARAM)defkeyinf);
470        free_if_alloc (keyid);
471  /* Interactive modification of the dialog item which depend if an item  }
472     is selected or not and what kind of item. @lv is the list view with  
473     the items. */  
474  void  /* Count all keys and show from @lv results in the status bar @sb. */
475  update_ui_items (HWND hwnd, listview_ctrl_t lv)  void
476  {  update_status_bar (HWND sb, listview_ctrl_t lv)
477      int key_selected = 0, key_has_sec = 0;  {
478      int i, key_inv = 0;      char txt_sec[128], txt_pub[128];
479      HWND hwnd_child;      int nkeys = 0, nsec = 0;
480      HMENU hmenu;      int i;
481        
482      /* Get some states */      nkeys = listview_count_items (lv, 0);
483      key_selected = SendMessage (GetDlgItem (hwnd, IDC_KEYMISC_KEYLIST),      for (i = 0; i < nkeys; i++) {
484                                             LVM_GETSELECTEDCOUNT, 0, 0)          if (km_check_for_seckey (lv, i, NULL))
485                                             ? TRUE : FALSE;              nsec++;
486      key_has_sec = FALSE;      }
487      if (key_selected) {      _snprintf (txt_sec, sizeof (txt_sec)-1, _("%d secret keys"), nsec);
488          i = listview_get_curr_pos (lv);      _snprintf (txt_pub, sizeof (txt_pub)-1, _("%d keys"), nkeys);
489          key_has_sec = km_check_for_seckey (lv, i, NULL) ? TRUE : FALSE;      SendMessage (sb, SB_SETTEXT, 1, (LPARAM)txt_sec);
490          key_inv = km_get_key_status (lv, i) & KM_FLAG_REVOKED;      SendMessage (sb, SB_SETTEXT, 2, (LPARAM)txt_pub);
491      }  }
492    
493      /* Enable / disable toolbar buttons */  
494      hwnd_child = GetDlgItem (hwnd, IDR_WINPT_KMTB);  long CALLBACK
495      enable_button (hwnd_child, ID_KEYMISC_DELETE);  separator_wnd_proc (HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam)
496      enable_button (hwnd_child, ID_KEYMISC_PROPS);  {
497      enable_button (hwnd_child, ID_KEYMISC_SIGN);      static POINT last_mouse_pos;
498      enable_button (hwnd_child, ID_KEYMISC_EXPORT);  
499        if (msg == WM_CREATE)
500      /* Enable / disable menu items */          SetWindowLong (hwnd, GWL_USERDATA,
501      hmenu = GetMenu (hwnd);                         (long)(((CREATESTRUCT*)lparam)->lpCreateParams));
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);      switch (msg) {
504      set_menu_state (hmenu, ID_KEYMISC_REVCERT, key_has_sec ? MF_ENABLED : MF_GRAYED);      case WM_PAINT:
505      set_menu_state (hmenu, ID_KEYMISC_DELETE, key_selected ? MF_ENABLED : MF_GRAYED);          PAINTSTRUCT ps;
506      set_menu_state (hmenu, ID_KEYMISC_PROPS, key_selected ? MF_ENABLED : MF_GRAYED);          RECT rect;
507      set_menu_state (hmenu, ID_KEYMISC_SIGN, key_selected && !key_inv ? MF_ENABLED : MF_GRAYED);          HPEN hpen;
508      set_menu_state (hmenu, ID_KEYMISC_EDITKEY, key_selected? MF_ENABLED : MF_GRAYED);  
509      set_menu_state (hmenu, ID_KEYMISC_CHECKSIGS, key_selected? MF_ENABLED : MF_GRAYED);          GetClientRect (hwnd, &rect);
510  }          BeginPaint (hwnd, &ps);
511    
512            /* Background */
513  /* The items which are shown when GPG is in read-only mode. */          FillRect (ps.hdc, &rect, (HBRUSH)(COLOR_3DFACE+1));
514  static void  
515  popup_gpg_readonly (HWND dlg, HMENU hm)          /* The lines from the light into the dark */
516  {          MoveToEx(ps.hdc, 0,0, NULL);
517      int resids[] = {          if ((hpen = CreatePen (PS_SOLID, 0, GetSysColor(COLOR_3DHILIGHT))) != NULL) {
518          ID_KEYCTX_PASTE,              SelectObject (ps.hdc, (LPVOID)hpen);
519          ID_KEYCTX_EDIT,              LineTo (ps.hdc, rect.right, 0);
520          ID_KEYCTX_SIGN,              DeleteObject (hpen);
521          ID_KEYCTX_REV,          }
522          ID_KEYCTX_DEL,          MoveToEx(ps.hdc, 0, 1, NULL);
523          ID_KEYCTX_ENABLE,          if ((hpen = CreatePen (PS_SOLID, 0, GetSysColor(COLOR_3DLIGHT))) != NULL) {
524          ID_KEYCTX_DISABLE,               SelectObject (ps.hdc, (LPVOID)hpen);
525          ID_KEYCTX_RECVFROM,               LineTo (ps.hdc, rect.right, rect.bottom);
526          ID_KEYCTX_ADDKEY,               DeleteObject (hpen);
527          ID_KEYCTX_ADDPHOTO,           }
528          ID_KEYCTX_ADDUID,  
529          ID_KEYCTX_ADDREV,          MoveToEx(ps.hdc, 0, rect.bottom-1, NULL);
530          0          if ((hpen = CreatePen (PS_SOLID, 0, GetSysColor(COLOR_3DSHADOW))) != NULL) {
531      };              SelectObject (ps.hdc, (LPVOID)hpen);
532      int i;              LineTo (ps.hdc, rect.right, rect.bottom-1);
533                DeleteObject (hpen);
534      if (gpg_read_only == 0)          }
535          return;          MoveToEx(ps.hdc, 0, rect.bottom, NULL);
536      for (i=0; resids[i] != 0; i++)          if ((hpen = CreatePen(PS_SOLID, 0, GetSysColor(COLOR_3DDKSHADOW))) != NULL) {
537          set_menu_state (hm, resids[i], MF_GRAYED);              SelectObject (ps.hdc, (LPVOID)hpen);
538  }              LineTo (ps.hdc, rect.right, rect.bottom);
539                DeleteObject (hpen);
540            }
541  static void  
542  menu_gpg_readonly (HWND dlg, HMENU hm, int id)          EndPaint (hwnd, &ps);
543  {          return 0;
544      int key_resids[] = {  
545          ID_KEYMISC_SIGN,      case WM_LBUTTONDOWN:
546          ID_KEYMISC_DELETE,          last_mouse_pos.x = LOWORD (lparam);
547          ID_KEYMISC_REVCERT,          last_mouse_pos.y = HIWORD (lparam);
548          ID_KEYMISC_IMPORT_HTTP,          ClientToScreen (hwnd, &last_mouse_pos);
549          ID_KEYMISC_IMPORT,          SetCapture (hwnd);
550          ID_KEYMISC_REFRESH_KEYS,          return 0;
551          ID_KEYMISC_REBUILD,  
552          ID_KEYMISC_KEYWIZARD,      case WM_LBUTTONUP:
553          ID_KEYMISC_CARDNEW,          ReleaseCapture ();
554          ID_KEYMISC_NEWKEY,          return 0;
555          0,  
556      };      case WM_MOUSEMOVE:
557      int edit_resids[] = {          if (wparam == MK_LBUTTON) {
558          ID_KEYMISC_PASTE,              struct km_info_s *kmi;
559          ID_KEYMISC_OT,              POINT p;
560          ID_KEYMISC_EDITKEY,              RECT r;
561          0  
562      };              kmi = (struct km_info_s *)GetWindowLong (hwnd, GWL_USERDATA);
563      int * resids;              if (kmi == NULL)
564      int i;                  break;
565    
566                    /* Calculate mouse movement */
567      if (gpg_read_only == 0)              p.x = LOWORD(lparam);
568          return;              p.y = HIWORD(lparam);
569      switch (id) {              ClientToScreen (hwnd, &p);
570      case 0: return;  
571      case 3: resids = key_resids; break;              GetWindowRect (hwnd, &r);
572      case 1: resids = edit_resids;break;              r.top += (short)(p.y - last_mouse_pos.y);
573      }              r.bottom += (short)(p.y - last_mouse_pos.y);
574    
575      for (i=0; resids[i] != 0; i++)              last_mouse_pos.y = p.y;
576          set_menu_state (hm, resids[i], MF_GRAYED);  
577  }              /* Apply mouse movement to window. Beware the MoveWindow is relaive
578                   to parent NOT screen */
579                MapWindowPoints (NULL, GetParent(hwnd), (POINT*)&r, 2);
580  static char*              kmi->ypos_sep = r.top;
581  gen_export_filename (const char *keyid, int is_secret)              kmi->ypercent_sep = 0; /* This forces do_resize_window to use abs. position */
582  {              do_resize_window (GetParent(hwnd), kmi);
583      gpgme_key_t key;              return 0;
584      const char *s;          }
585      char *p;      }
586    
587      if (get_pubkey (keyid, &key))      return DefWindowProc (hwnd, msg, wparam, lparam);
588          return m_strdup (keyid);  }
589      s = key->uids->name;  
590      if (!s)  
591          return m_strdup (keyid);  /* Register the separator window with @dlg as the parent window. */
592      p = new char[strlen (s) + 8 + 16];  static HWND
593      if (!p)  regist_sep_wnd (HWND dlg, struct km_info_s *kmi)
594          BUG (0);  {
595      sprintf (p, "%s%s.asc", s, is_secret? "_sec" : "");      WNDCLASS wndclass;
596      for (size_t i=0; i < strlen (p); i++) {      HWND h;
597          if (p[i] == ' ' || p[i] == ':' || p[i] == '?' || p[i] == '|')  
598              p[i] = '_';      wndclass.style         = CS_DBLCLKS | CS_HREDRAW | CS_VREDRAW;
599      }      wndclass.lpfnWndProc   = separator_wnd_proc;
600      return p;      wndclass.cbClsExtra    = 0;
601  }      wndclass.cbWndExtra    = sizeof (long);
602        wndclass.hInstance     = glob_hinst;
603        wndclass.hIcon         = NULL;
604  static void      wndclass.hCursor       = LoadCursor (NULL, IDC_SIZENS);
605  update_key (listview_ctrl_t lv, int pos, const char *keyid, int keypair)      wndclass.hbrBackground = (HBRUSH) GetStockObject (LTGRAY_BRUSH);
606  {      wndclass.lpszMenuName  = 0;
607      gpgme_key_t key;      wndclass.lpszClassName = WINDOWCLASS_SEPARATOR_CHILD;
608        RegisterClass (&wndclass);
609      keycache_update (0, keyid);  
610      if (keypair)      h = CreateWindowEx (WS_EX_WINDOWEDGE, WINDOWCLASS_SEPARATOR_CHILD, NULL,
611          keycache_update (1, keyid);                          WS_CHILD | WS_CLIPCHILDREN | WS_VISIBLE,
612                            0, 400, 2000, 5, dlg, (HMENU) 0, glob_hinst, kmi);    
613      /* because we replaced the key item, we need to update the      ShowWindow (h, SW_SHOW);
614         address of the pointer in the ListView control. */      return h;
615      get_pubkey (keyid, &key);  }
616      keylist_upd_key (lv, pos, key);  
617  }  
618    #define enable_button(hwnd, cid, item_selected) \
619        SendMessage ((hwnd), TB_ENABLEBUTTON, (cid), MAKELONG ((item_selected), 0))
620  /* Setup status bar for the main window @dlg. */  
621  static HWND  
622  setup_status_bar (HWND dlg, listview_ctrl_t lv)  /* Interactive modification of the dialog item which depend if an item
623  {           is selected or not and what kind of item. @lv is the list view with
624      HWND statbar;         the items. */
625      RECT r;  void
626      int partpos[3];  update_ui_items (HWND hwnd, listview_ctrl_t lv)
627      int i;  {
628        HWND tb_hwnd;
629      GetClientRect (dlg, &r);      HMENU hmenu;
630      for (i=1; i <= 3; i++)      int mult_resids[] = {ID_KEYMISC_PROPS, ID_KEYMISC_SIGN, ID_KEYMISC_EDITKEY,
631          partpos[i-1] = r.right/3*i;                           ID_KEYMISC_CHECKSIGS, ID_KEYMISC_REVCERT, 0};
632      statbar = CreateStatusWindow (WS_CHILD | WS_VISIBLE | CCS_BOTTOM,      int key_selected = 0;
633                                    "status bar", dlg, KM_SEPARATOR_ID);      int key_has_sec = 0;
634        int key_inv = 0;
635      ShowWindow (statbar, SW_SHOW);      int i, state=0;
636      SendMessage (statbar, SB_SETPARTS, (WPARAM)3, (LPARAM)partpos);  
637        /* Get some states */
638      km_update_default_key_str (statbar);      key_selected = SendMessage (GetDlgItem (hwnd, IDC_KEYMISC_KEYLIST),
639      km_complete_status_bar (statbar, lv);                                             LVM_GETSELECTEDCOUNT, 0, 0)
640                                               ? TRUE : FALSE;
641      return statbar;      key_has_sec = FALSE;
642  }      if (key_selected) {
643            i = listview_get_curr_pos (lv);
644            key_has_sec = km_check_for_seckey (lv, i, NULL) ? TRUE : FALSE;
645  BOOL CALLBACK          key_inv = km_get_key_status (lv, i) & KM_FLAG_REVOKED ||
646  keymanager_dlg_proc (HWND dlg, UINT msg, WPARAM wparam, LPARAM lparam)                    km_get_key_status (lv, i) & KM_FLAG_EXPIRED;
647  {      }
648      struct km_info *kmi;  
649      static km_group_s *gc = NULL;      /* Enable / disable toolbar buttons */
650      static HMENU menu = NULL;      tb_hwnd = GetDlgItem (hwnd, IDR_WINPT_KMTB);
651      static int refresh_keys = 0;          enable_button (tb_hwnd, ID_KEYMISC_DELETE, key_selected);
652      INITCOMMONCONTROLSEX icex;      enable_button (tb_hwnd, ID_KEYMISC_PROPS, key_selected);
653      HWND kl;      enable_button (tb_hwnd, ID_KEYMISC_SIGN, key_selected && !key_inv);
654      HMENU hm;      enable_button (tb_hwnd, ID_KEYMISC_EXPORT, key_selected);
655      gpg_keycache_t c;      enable_button (tb_hwnd, ID_KEYCTX_COPY, key_selected);
656      gpgme_key_t key;      enable_button (tb_hwnd, ID_KEYCTX_PASTE, clip_contains_pgpkey ());
657      /*km_group_cb_s gcb; XXX */  
658      struct genkey_s genkey;      /* Enable / disable menu items */
659      struct winpt_key_s k = {0};      state = key_selected? MF_ENABLED : MF_DISABLED|MF_GRAYED;
660      struct URL_ctx_s *url;      hmenu = GetMenu (hwnd);
661      refresh_cache_s rcs = {0};      set_menu_state (hmenu, ID_KEYMISC_EXPORT, state);
662      char keyid[48], uid[128], type[32], *name;      set_menu_state (hmenu, ID_KEYMISC_DELETE, state);
663      const char *t, * host;      set_menu_state (hmenu, ID_KEYMISC_PROPS, state);
664      u16 port = 0;      set_menu_state (hmenu, ID_KEYMISC_EDITKEY, state);
665      int idx = 0, i=0, rc;      set_menu_state (hmenu, ID_KEYMISC_CHECKSIGS, state);
666      size_t size = 0;      set_menu_state (hmenu, ID_KEYMISC_SIGN,
667                        key_selected && !key_inv ? MF_ENABLED : MF_GRAYED);
668      if ((msg != WM_INITDIALOG)      set_menu_state (hmenu, ID_KEYMISC_EXPORT_PRIVKEY,
669          && ((kmi = (struct km_info*)GetWindowLong (dlg, GWL_USERDATA)) == NULL))                      key_selected && key_has_sec? MF_ENABLED : MF_GRAYED);
670          return FALSE;      set_menu_state (hmenu, ID_KEYMISC_REVCERT,
671                        key_selected && key_has_sec? MF_ENABLED : MF_GRAYED);
672      switch (msg) {  
673      case WM_INITDIALOG:      /* Disable some menu items when multiple keys are selected. */
674          kmi = new struct km_info;      if (listview_count_items (lv, 1) > 1) {
675          memset (kmi, 0, sizeof (struct km_info));          for (i=0; mult_resids[i] != 0; i++)
676          icex.dwSize = sizeof (INITCOMMONCONTROLSEX);              set_menu_state (hmenu, mult_resids[i], MF_GRAYED);
677          icex.dwICC  = ICC_BAR_CLASSES;      }
678          InitCommonControlsEx (&icex);  
679          kmi->hwnd_sep = regist_sep_wnd (dlg, kmi);      /* Disable all secret-key functions when no secret key is available. */
680          imagelist_load (dlg);      {
681            gpg_keycache_t sec = keycache_get_ctx (0);
682  #ifndef LANG_DE          if (gpg_keycache_get_size (sec) == 0) {
683          SetWindowText( dlg, _("Key Manager") );              enable_button (tb_hwnd, ID_KEYMISC_SIGN, FALSE);
684  #endif                      set_menu_state (hmenu, ID_KEYMISC_SIGN, MF_GRAYED);
685          menu = LoadMenu( glob_hinst, (LPCSTR)IDR_WINPT_KEYMISC );          }
686  #ifndef LANG_DE      }
687          set_menu_text (menu, ID_KEYMISC_MAIL, _("Send Mail..."));  }
688          set_menu_text (menu, ID_KEYMISC_OT, _("Ownertrust")); /* XXX */  
689          set_menu_text (menu, ID_KEYMISC_COPY, _("&Copy\tCtrl+C"));  
690          set_menu_text (menu, ID_KEYMISC_PASTE, _("&Paste\tCtrl+V"));  /* Disable some context menu items when multiple keys are selected. */
691          set_menu_text (menu, ID_KEYMISC_FIND, _("Search...\tCtrl+F"));  static void
692          set_menu_text (menu, ID_KEYMISC_SELALL, _("Select All\tCtrl+A"));  popup_multiple (HWND dlg, HMENU hm)
693          set_menu_text (menu, ID_KEYMISC_QUIT, _("&Quit"));  {
694          set_menu_text (menu, ID_KEYMISC_UID, _("User ID"));      int resids[] = {
695          set_menu_text (menu, ID_KEYMISC_NEWKEY, _("&Expert"));          ID_KEYCTX_EDIT,
696          set_menu_text (menu, ID_KEYMISC_KEYWIZARD, _("&Normal"));          ID_KEYCTX_SIGN,
697          set_menu_text (menu, ID_KEYMISC_EDIT, _("Edit"));          ID_KEYCTX_REV,
698          set_menu_text (menu, ID_KEYMISC_SIGN, _("&Sign"));          ID_KEYCTX_ENABLE,
699          set_menu_text (menu, ID_KEYMISC_DELETE, _("&Delete"));          ID_KEYCTX_DISABLE,
700          set_menu_text (menu, ID_KEYMISC_REVCERT, _("&Revoke"));          ID_KEYCTX_ADDKEY,
701          set_menu_text (menu, ID_KEYMISC_CHECKSIGS, _("&List Signatures"));          ID_KEYCTX_ADDPHOTO,
702          set_menu_text (menu, ID_KEYMISC_TRUSTPATH, _("List Trust Path"));          ID_KEYCTX_ADDUID,
703          set_menu_text (menu, ID_KEYMISC_EXPORT, _("&Export..."));          ID_KEYCTX_ADDREV,
704          set_menu_text (menu, ID_KEYMISC_IMPORT, _("&Import..."));          ID_KEYCTX_LISTSIGS,
705          set_menu_text (menu, ID_KEYMISC_PROPS, _("&Properties"));          ID_KEYCTX_MAXTRUST,
706          set_menu_text (menu, ID_KEYMISC_GPGOPT, _("Options"));          ID_KEYCTX_PROPS,
707          set_menu_text (menu, ID_KEYMISC_GPGPREFS, _("Preferences"));          ID_KEYCTX_SENDMAIL,
708          set_menu_text (menu, ID_KEYMISC_SENDRECV, _("Keyserver") );          0};
709          set_menu_text (menu, ID_KEYMISC_EXPORT_PRIVKEY, _("E&xport Secret Key"));      int i;
710          set_menu_text (menu, ID_KEYMISC_RECACHE, _("Re&load Key Cache"));      for (i=0; i < resids[i] != 0; i++)
711          set_menu_text (menu, ID_KEYMISC_REBUILD, _("R&everify Signatures"));          set_menu_state (hm, resids[i], MF_GRAYED);
712          set_menu_text (menu, ID_KEYMISC_REFRESH_KEYS, _("Refresh &Keys (Keyserver)"));  }
713          set_menu_text (menu, ID_KEYMISC_INFO, _("Info") );  
714          set_menu_text (menu, ID_KEYMISC_HELP, _("&Help"));  
715            /* The items which are shown when GPG is in read-only mode. */
716  #endif  static void
717          SetMenu (dlg, menu);  popup_gpg_readonly (HWND dlg, HMENU hm)
718          if( keyring_check_last_access() )  {
719              keycache_set_reload( 1 );      int resids[] = {
720          if( keycache_get_reload( ) )          ID_KEYCTX_PASTE,
721              keycache_reload( dlg );          ID_KEYCTX_EDIT,
722          c = keycache_get_ctx( KEYCACHE_PUB );          ID_KEYCTX_SIGN,
723          if( !c )          ID_KEYCTX_REV,
724              BUG( NULL );          ID_KEYCTX_DEL,
725          kmi->keylist_sortby = KEY_SORT_USERID;          ID_KEYCTX_ENABLE,
726          Header_SetImageList(ListView_GetHeader(GetDlgItem( dlg, IDC_KEYMISC_KEYLIST )),          ID_KEYCTX_DISABLE,
727                              glob_imagelist);          ID_KEYCTX_RECVFROM,
728          kmi->lv = keylist_load( GetDlgItem( dlg, IDC_KEYMISC_KEYLIST ), c,          ID_KEYCTX_ADDKEY,
729                                  NULL, KEYLIST_LIST, kmi->keylist_sortby );          ID_KEYCTX_ADDPHOTO,
730          /* init subclassing for the listview */          ID_KEYCTX_ADDUID,
731          kl = GetDlgItem( dlg, IDC_KEYMISC_KEYLIST );          ID_KEYCTX_ADDREV,
732          keylist_proc.dlg = dlg;          0
733          keylist_proc.current = (WNDPROC)keylist_subclass_proc;      };
734          keylist_proc.old = (WNDPROC)GetWindowLong( kl, GWL_WNDPROC );      int i;
735          if( keylist_proc.old ) {  
736              if( !SetWindowLong( kl, GWL_WNDPROC, (LONG)keylist_proc.current) ) {      if (gpg_read_only == 0)
737                  msg_box( dlg, _("Could not set keylist window procedure."), _("Key Manager"), MB_ERR );          return;
738                  BUG( NULL );      for (i=0; resids[i] != 0; i++)
739              }          set_menu_state (hm, resids[i], MF_GRAYED);
740          }  }
741          #if 0  
742          km_groups_new( &gc, GetDlgItem( dlg, IDC_KEYMISC_GROUP ) );  
743          km_groups_load( gc );  /* Change the 'Edit' menu based on the current state. */
744          #endif  static void
745          SetClassLong (dlg, GCL_HICON, (LONG)LoadIcon (glob_hinst, (LPCTSTR)IDI_WINPT));  change_edit_menu (listview_ctrl_t lv, HMENU hm, int id)
746          SetForegroundWindow (dlg);  {
747        enum item { EDIT_MENU = 1 };
748          kmi->statbar = setup_status_bar (dlg, kmi->lv);      int no_sel;
749    
750          SetWindowLong (dlg, GWL_USERDATA, (LONG)kmi);      if (id != EDIT_MENU)
751          kmi->toolbar = load_toolbar (dlg, kmi);          return;
752    
753          do_center_window (dlg, kmi);      if (!clip_contains_pgpkey ())
754          do_resize_window (dlg, kmi);          set_menu_state (hm, ID_KEYMISC_PASTE, MF_GRAYED);
755          update_ui_items (dlg, kmi->lv);      else
756          return TRUE;          set_menu_state (hm, ID_KEYMISC_PASTE, MF_ENABLED);
757                no_sel = listview_get_curr_pos (lv) == -1? 1 : 0;
758      case WM_DESTROY:      set_menu_state (hm, ID_KEYMISC_DELETE2, no_sel? MF_GRAYED: MF_ENABLED);
759          if (kmi->lv) {      set_menu_state (hm, ID_KEYMISC_COPY, no_sel? MF_GRAYED : MF_ENABLED);
760              keylist_delete (kmi->lv);  }
761              kmi->lv = NULL;        
762            
763           /*  
764           if (gc) {  /* Show limited key menu entries when GPG is in read-only mode. */
765              km_groups_release (gc);  static void
766              gc = NULL;  change_key_menu (HMENU hm, int id)
767          }*/  {
768         imagelist_destroy ();      int key_resids[] = {
769            ID_KEYMISC_SIGN,
770         char buf[32];          ID_KEYMISC_DELETE,
771         ltoa (kmi->pos_x, buf, 10);          ID_KEYMISC_REVCERT,
772         set_reg_entry( HKEY_CURRENT_USER, "Software\\WinPT", "KM_Pos_X", buf );          ID_KEYMISC_IMPORT_HTTP,
773         ltoa (kmi->pos_y, buf, 10);          ID_KEYMISC_IMPORT,
774         set_reg_entry (HKEY_CURRENT_USER, "Software\\WinPT", "KM_Pos_Y", buf);          ID_KEYMISC_REFRESH_KEYS,
775         /* Remove runtime information. This should be the last action taken here. */          ID_KEYMISC_REBUILD,
776         delete kmi; kmi = NULL;          ID_KEYMISC_KEYWIZARD,
777         SetWindowLong (dlg, GWL_USERDATA, NULL);              ID_KEYMISC_CARDNEW,
778         keycache_set_reload (refresh_keys);          ID_KEYMISC_NEWKEY,
779         return FALSE;          0,
780        };
781      case WM_MOVE:            int edit_resids[] = {
782          /* kmi->pos_x = (int)(short)LOWORD(lparam);          ID_KEYMISC_PASTE,
783             kmi->pos_y = (int)(short)HIWORD(lparam); */          ID_KEYMISC_OT,
784          RECT r;          ID_KEYMISC_EDITKEY,
785          GetWindowRect (dlg, &r);          0
786          kmi->pos_x = r.left;      };
787          kmi->pos_y = r.top;          int *resids;
788          break;      int i;
789            
790        
791      case WM_RBUTTONUP:      if (gpg_read_only == 0)
792          do_create_minpopup (dlg);          return;
793          break;      switch (id) {
794        case 0: return;
795      case WM_NOTIFY:                  case 3: resids = key_resids; break;
796          NMHDR * notify;      case 1: resids = edit_resids;break;
797          POINT p;      default:resids = edit_resids; break;
798          HMENU popup;      }
799            
800          notify = (NMHDR *)lparam;      for (i=0; resids[i] != 0; i++)
801          if (notify == NULL)          set_menu_state (hm, resids[i], MF_GRAYED);
802              break;  }
803          switch (notify->code)  
804          {  
805          case TBN_QUERYDELETE:  /* Reload a single key in the cache. */
806              SetWindowLong(dlg, DWL_MSGRESULT, TRUE);  static void
807              return TRUE;  update_key (listview_ctrl_t lv, int pos, const char *keyid, int keypair)
808            {
809          case TBN_QUERYINSERT:      gpgme_key_t key;
810              SetWindowLong(dlg, DWL_MSGRESULT, TRUE);  
811              return TRUE;      keycache_update (0, keyid);
812        if (keypair)
813          case TBN_GETBUTTONINFO:          keycache_update (1, keyid);
814              LPTBNOTIFY lpTbNotify;  
815              lpTbNotify = (LPTBNOTIFY)lparam;      /* because we replaced the key item, we need to update the
816              if (lpTbNotify->iItem < (sizeof(myb) / sizeof(mybuttons))) {         address of the pointer in the ListView control. */
817                  lpTbNotify->tbButton.iBitmap = imagelist_getindex(myb[lpTbNotify->iItem].icon);      get_pubkey (keyid, &key);
818                  lpTbNotify->tbButton.idCommand = myb[lpTbNotify->iItem].command;      keylist_upd_key (lv, pos, key);
819                  lpTbNotify->tbButton.fsState = TBSTATE_ENABLED;      keyring_check_last_access ();
820                  lpTbNotify->tbButton.fsStyle = TBSTYLE_BUTTON;  }
821                  lpTbNotify->tbButton.iString = 0;  
822                  strncpy (lpTbNotify->pszText, myb[lpTbNotify->iItem].text, lpTbNotify->cchText);  
823                  SetWindowLong(dlg, DWL_MSGRESULT, TRUE);  /* Setup status bar for the main window @dlg. */
824              }  static HWND
825              return TRUE;  setup_status_bar (HWND dlg, listview_ctrl_t lv)
826    {      
827          case TBN_RESET: /* Restore last saved toolbar status */      HWND statbar;
828              TBSAVEPARAMS tbsp;      RECT r;
829        int partpos[3];
830              tbsp.hkr = HKEY_CURRENT_USER;      int i;
831              tbsp.pszSubKey = "Software\\WinPT";  
832              tbsp.pszValueName = "KM_toolbar";      GetClientRect (dlg, &r);
833              SendMessage (notify->hwndFrom, TB_SAVERESTORE, FALSE, (LPARAM)&tbsp);      for (i=1; i <= 3; i++)
834              break;          partpos[i-1] = r.right/3*i;
835        statbar = CreateStatusWindow (WS_CHILD | WS_VISIBLE | CCS_BOTTOM,
836          case TBN_BEGINADJUST: /* User is about to change the toolbar. Save it */                                    "status bar", dlg, KM_SEPARATOR_ID);
837              tbsp.hkr = HKEY_CURRENT_USER;  
838              tbsp.pszSubKey = "Software\\WinPT";      ShowWindow (statbar, SW_SHOW);
839              tbsp.pszValueName = "KM_toolbar";      SendMessage (statbar, SB_SETPARTS, (WPARAM)3, (LPARAM)partpos);
840              SendMessage (notify->hwndFrom, TB_SAVERESTORE, TRUE, (LPARAM)&tbsp);  
841              break;      update_default_key_str (statbar);
842        update_status_bar (statbar, lv);
843          case TBN_ENDADJUST: /* User has finished customizing the toolbar. Save it. */  
844              tbsp.hkr = HKEY_CURRENT_USER;      return statbar;
845              tbsp.pszSubKey = "Software\\WinPT";  }
846              tbsp.pszValueName = "KM_toolbar";  
847              SendMessage (notify->hwndFrom, TB_SAVERESTORE, TRUE, (LPARAM)&tbsp);  
848              break;  /* Remove or add columns which depends on the state of @checked. */
849    void
850          case TTN_GETDISPINFO:  modify_listview_columns (km_info_s *kmi, UINT m_uid, BOOL checked)
851              LPTOOLTIPTEXT lpttt;  {
852              lpttt = (LPTOOLTIPTEXT)lparam;      UINT resids[] = {
853            0,
854              lpttt->hinst = NULL;          ID_KEYMISC_VIEWKEYID,
855              switch (lpttt->hdr.idFrom) {          ID_KEYMISC_VIEWTYPE,
856              case ID_KEYMISC_DELETE:          0,
857                  lpttt->lpszText = (char *)_("Delete key from keyring");          ID_KEYMISC_VIEWCIPHER,
858                  break;          0,
859            0,
860              case ID_KEYMISC_PROPS:          ID_KEYMISC_VIEWCREAT,
861                  lpttt->lpszText = (char *)_("Show key properties");          -1
862                  break;      };
863        listview_column_s cols[] = {
864              case ID_KEYMISC_SIGN:      {0, 240, (char *)_("User ID")},
865                  lpttt->lpszText = (char *)_("Sign key");      {1, 78, (char *)_("Key ID")},
866                  break;      {2, 52, (char *)_("Type")},    
867        {3, 66, (char *)_("Size")},
868              case ID_KEYMISC_IMPORT:      {4, 60, (char *)_("Cipher")},
869                  lpttt->lpszText = (char *)_("Import key to keyring");      {5, 66, (char *)_("Validity")},
870                  break;      {6, 58, (char *)_("Trust")},
871        {7, 72, (char *)_("Creation")},
872              case ID_KEYMISC_EXPORT:      {0, 0, NULL}
873                  lpttt->lpszText = (char *)_("Export key to a file");      };
874                  break;      UINT pos;
875              }  
876              return TRUE;      for (pos=0; resids[pos] != -1; pos++) {
877                        if (m_uid == resids[pos])
878          case LVN_ITEMCHANGED:              break;
879              if (((LPNMLISTVIEW)lparam)->uNewState) /* item selected? */      }
880              {      if (!checked)
881                  update_ui_items (dlg, kmi->lv);          listview_del_column (kmi->lv, (int)pos);
882                  return TRUE;      else {      
883              }          listview_add_column (kmi->lv, &cols[pos]);
884              break;          keylist_upd_col (kmi->lv, pos);
885        }
886          case NM_RCLICK:  }
887              if (notify->idFrom == IDC_KEYMISC_KEYLIST) {  
888                  if (listview_get_curr_pos (kmi->lv) == -1)  
889                      return TRUE; /* Popup only when a item was selected */  /* Helper to handle the help file. If @check is 1
890                  do_check_cache (kmi->lv, dlg, kmi->statbar);     the existence of the file is checked.
891                  GetCursorPos (&p);     Otherwise the help file will be loaded. */
892                  hm = LoadMenu (glob_hinst, MAKEINTRESOURCE (IDR_WINPT_KEYMISC_CTX));  static bool
893                  popup = GetSubMenu (hm, 0);  start_help (HWND dlg, int check)
894              #ifndef LANG_DE  {
895                  set_menu_text (popup, ID_KEYCTX_UID_COPY, _("Copy User ID to Clipboard"));      DWORD n;
896                  set_menu_text (popup, ID_KEYCTX_KEYID_COPY, _("Copy Key ID to Clipboard"));      char path[MAX_PATH+1+32];
897                  set_menu_text (popup, ID_KEYCTX_FPR_COPY, _("Copy Fingerprint to Clipboard"));  
898                  set_menu_text (popup, ID_KEYCTX_KINFO_COPY, _("Copy Key Info to Clipboard"));      n = GetModuleFileName (NULL, path, sizeof (path)-1-32);
899                  set_menu_text (popup, ID_KEYCTX_COPY, _("Copy Key to Clipboard"));      if (!n)
900                  set_menu_text (popup, ID_KEYCTX_PASTE, _("Paste Key from Clipboard"));          return false;
901                  set_menu_text (popup, ID_KEYCTX_RECVFROM, _("Refresh from Keyserver"));      path[n] = 0;
902                  set_menu_text (popup, ID_KEYCTX_MAXTRUST, _("Set Implicit &Trust"));      while (n-- && path[n] != '\\')
903                  set_menu_text (popup, ID_KEYCTX_LISTSIGS, _("&List Signatures"));          ;
904                  set_menu_text (popup, ID_KEYCTX_PROPS, _("&Key Properties"));      path[n+1] = 0;
905                  set_menu_text (popup, ID_KEYCTX_EDIT, _("Key Edit"));      strcat (path, "winpt.chm");
906                  set_menu_text (popup, ID_KEYCTX_DEL, _("&Delete"));      if (!check)
907                  set_menu_text (popup, ID_KEYCTX_REV, _("&Revoke"));          ShellExecute (dlg, "open", path, NULL, NULL, SW_SHOW);
908                  set_menu_text (popup, ID_KEYCTX_SIGN, _("&Sign"));      return file_exist_check (path) == 0? true : false;
909                  set_menu_text (popup, ID_KEYCTX_ENABLE, _("&Enable"));  }
910                  set_menu_text (popup, ID_KEYCTX_DISABLE, _("&Disable"));  
911                  set_menu_text (popup, ID_KEYCTX_RECVFROM, _("Re&fresh from Keyserver"));  /* Translate all menu strings. */
912                  set_menu_text (popup, ID_KEYCTX_SETPREFKS, _("Set preferred Keyserver URL"));  static void
913                  set_menu_text (popup, ID_KEYCTX_SENDMAIL, _("Send Key to Mail Recipient"));  translate_menu_strings (HWND dlg)
914                  set_menu_text (popup, ID_KEYCTX_SETDEFKEY, _("Set as Default Key"));  {
915                  /* change popup texts */      HMENU menu;
916                  set_menu_text_bypos (popup, 0, _("Key Attributes"));  
917                  set_menu_text_bypos (popup, 6, _("Add"));      menu = LoadMenu (glob_hinst, (LPCSTR)IDR_WINPT_KEYMISC);
918                  set_menu_text_bypos (popup, 19, _("Send to Keyserver"));      set_menu_text_bypos (menu, 0, _("File"));
919              #endif      set_menu_text_bypos (menu, 1, _("Edit"));
920                  idx = listview_get_curr_pos (kmi->lv);      set_menu_text_bypos (menu, 2, _("View"));
921                  if (km_check_for_seckey (kmi->lv, idx, &i))      set_menu_text_bypos (menu, 3, _("Key"));
922                      set_menu_state (popup, ID_KEYCTX_SETDEFKEY, MF_ENABLED);      set_menu_text_bypos (menu, 4, _("Groups"));
923                  if (i == 0)  
924                      set_menu_state (popup, ID_KEYCTX_MAXTRUST, MF_ENABLED);      set_menu_text (menu, ID_KEYMISC_EDITKEY, _("Edit"));
925                  if (!km_check_for_seckey (kmi->lv, idx, NULL)) {      set_menu_text (menu, ID_KEYMISC_MAIL, _("Send Mail..."));
926                      set_menu_state( popup, ID_KEYCTX_REV, MF_DISABLED|MF_GRAYED );      set_menu_text (menu, ID_KEYMISC_OT, _("Ownertrust")); /* XXX */
927                      set_menu_state( popup, ID_KEYCTX_ADDKEY, MF_DISABLED|MF_GRAYED );      set_menu_text (menu, ID_KEYMISC_COPY, _("&Copy\tCtrl+C"));
928                      set_menu_state( popup, ID_KEYCTX_ADDUID, MF_DISABLED|MF_GRAYED );      set_menu_text (menu, ID_KEYMISC_PASTE, _("&Paste\tCtrl+V"));
929                      set_menu_state( popup, ID_KEYCTX_ADDREV, MF_DISABLED|MF_GRAYED );      set_menu_text (menu, ID_KEYMISC_FIND, _("Search...\tCtrl+F"));
930                      set_menu_state( popup, ID_KEYCTX_ADDPHOTO, MF_DISABLED|MF_GRAYED );      set_menu_text (menu, ID_KEYMISC_SELALL, _("Select All\tCtrl+A"));
931                      set_menu_state (popup, ID_KEYCTX_SETPREFKS, MF_DISABLED|MF_GRAYED);      set_menu_text (menu, ID_KEYMISC_QUIT, _("&Quit"));
932                  }      set_menu_text (menu, ID_KEYMISC_UID, _("User ID"));
933                  else if( km_check_for_seckey (kmi->lv, idx, NULL)      set_menu_text (menu, ID_KEYMISC_NEWKEY, _("&Expert"));
934                        && km_key_is_v3 (kmi->lv, idx)) {      set_menu_text (menu, ID_KEYMISC_KEYWIZARD, _("&Normal"));
935                      /* PGP 2 keys, version 3 have no no support for photo-id's,      set_menu_text (menu, ID_KEYMISC_EDIT, _("Edit"));
936                         designated revokers and secondary keys. */      set_menu_text (menu, ID_KEYMISC_SIGN, _("&Sign"));
937                      set_menu_state (popup, ID_KEYCTX_ADDKEY, MF_DISABLED|MF_GRAYED);      set_menu_text (menu, ID_KEYMISC_DELETE, _("&Delete"));
938                      set_menu_state (popup, ID_KEYCTX_ADDREV, MF_DISABLED|MF_GRAYED);      set_menu_text (menu, ID_KEYMISC_DELETE2, _("&Delete"));
939                      set_menu_state (popup, ID_KEYCTX_ADDPHOTO, MF_DISABLED|MF_GRAYED);      set_menu_text (menu, ID_KEYMISC_REVCERT, _("&Revoke Cert"));
940                  }      set_menu_text (menu, ID_KEYMISC_CHECKSIGS, _("&List Signatures"));
941                  if( km_get_key_status( kmi->lv, idx ) & KM_FLAG_DISABLED )      set_menu_text (menu, ID_KEYMISC_TRUSTPATH, _("List Trust Path"));
942                      set_menu_state( popup, ID_KEYCTX_DISABLE, MF_DISABLED|MF_GRAYED );      set_menu_text (menu, ID_KEYMISC_EXPORT, _("&Export..."));
943                  else      set_menu_text (menu, ID_KEYMISC_IMPORT, _("&Import..."));
944                      set_menu_state( popup, ID_KEYCTX_ENABLE, MF_DISABLED|MF_GRAYED );      set_menu_text (menu, ID_KEYMISC_PROPS, _("&Properties"));
945                  if (km_get_key_status (kmi->lv, idx) & KM_FLAG_REVOKED)      set_menu_text (menu, ID_KEYMISC_GPGOPT, _("Options"));
946                      set_menu_state (popup, ID_KEYCTX_SIGN, MF_DISABLED|MF_GRAYED);      set_menu_text (menu, ID_KEYMISC_GPGPREFS, _("Preferences"));
947                  if (mapi_init())      set_menu_text (menu, ID_KEYMISC_SENDRECV, _("Keyserver") );
948                      set_menu_state (popup, ID_KEYCTX_SENDMAIL, MF_DISABLED|MF_GRAYED);      set_menu_text (menu, ID_KEYMISC_EXPORT_PRIVKEY, _("E&xport Secret Key"));
949                  /* Override 'Default Keyserver' with the actual name. */      set_menu_text (menu, ID_KEYMISC_RECACHE, _("Re&load Key Cache"));
950                  host = kserver_get_hostname (0, -1, &port);      set_menu_text (menu, ID_KEYMISC_REBUILD, _("R&everify Signatures"));
951                  set_menu_text (popup, ID_KEYCTX_KS_DEFAULT, host);      set_menu_text (menu, ID_KEYMISC_REFRESH_KEYS, _("Refresh &Keys (Keyserver)"));
952                  popup_gpg_readonly (dlg, popup);      set_menu_text (menu, ID_KEYMISC_INFO, _("Info") );
953                  TrackPopupMenu (popup, TPM_RIGHTALIGN, p.x, p.y, 0, dlg, NULL);      set_menu_text (menu, ID_KEYMISC_HELP, _("&Help"));
954                  DestroyMenu (popup);  
955                  DestroyMenu (hm);      set_menu_text (menu, ID_KEYMISC_VIEWKEYID, _("Key ID"));
956                  return TRUE;      set_menu_text (menu, ID_KEYMISC_VIEWCIPHER, _("Cipher"));
957              }      set_menu_text (menu, ID_KEYMISC_VIEWTYPE, _("Type"));
958              #if 0 /* XXX */      set_menu_text (menu, ID_KEYMISC_VIEWCREAT, _("Creation"));
959              if( notify->idFrom == IDC_KEYMISC_GROUP ) {  
960                  HWND tree = GetDlgItem( dlg, IDC_KEYMISC_GROUP );      if (!start_help (NULL, 1))
961                  if( TreeView_GetSelection( tree ) ) {          set_menu_state (menu, ID_KEYMISC_HELP, MF_GRAYED);
962                      GetCursorPos( &p );  
963                      hm = LoadMenu( glob_hinst, MAKEINTRESOURCE(IDR_WINPT_GROUP_CTX) );      SetMenu (dlg, menu);
964                      popup = GetSubMenu( hm, 0 );  }
965                      if( km_index == -1 )  
966                          set_menu_state( popup, ID_GROUP_PASTE, MF_DISABLED|MF_GRAYED );  
967                      set_menu_text( popup, ID_GROUP_PASTE, _("Paste into this group") );  /* Translate popup menu strings. */
968                      set_menu_text( popup, ID_GROUP_DELETE, _("Delete") );  static void
969                      TrackPopupMenu( popup, TPM_RIGHTALIGN, p.x, p.y, 0, dlg, NULL );  translate_popupmenu_strings (HMENU popup)
970                      DestroyMenu( popup );  {
971                      DestroyMenu( hm );      set_menu_text (popup, ID_KEYCTX_UID_COPY, _("Copy User ID to Clipboard"));
972                      return TRUE;      set_menu_text (popup, ID_KEYCTX_KEYID_COPY, _("Copy Key ID to Clipboard"));
973                  }      set_menu_text (popup, ID_KEYCTX_FPR_COPY, _("Copy Fingerprint to Clipboard"));
974              }      set_menu_text (popup, ID_KEYCTX_KINFO_COPY, _("Copy Key Info to Clipboard"));
975              #endif      set_menu_text (popup, ID_KEYCTX_COPY, _("Copy Key to Clipboard"));
976              break;      set_menu_text (popup, ID_KEYCTX_PASTE, _("Paste Key from Clipboard"));
977        set_menu_text (popup, ID_KEYCTX_RECVFROM, _("Refresh from Keyserver"));
978          case LVN_COLUMNCLICK:      set_menu_text (popup, ID_KEYCTX_MAXTRUST, _("Set Implicit &Trust"));
979              if (notify->idFrom == IDC_KEYMISC_KEYLIST) {      set_menu_text (popup, ID_KEYCTX_LISTSIGS, _("&List Signatures"));
980                  NMLISTVIEW * p = (LPNMLISTVIEW) lparam;      set_menu_text (popup, ID_KEYCTX_PROPS, _("&Properties"));
981                  int sortby = 0;      set_menu_text (popup, ID_KEYCTX_EDIT, _("Key Edit"));
982                  switch (p->iSubItem) {      set_menu_text (popup, ID_KEYCTX_DEL, _("&Delete"));
983                  case 0:  sortby = KEY_SORT_USERID; break;      set_menu_text (popup, ID_KEYCTX_REV, _("&Revoke Cert"));
984                  case 1:  sortby = KEY_SORT_KEYID; break;      set_menu_text (popup, ID_KEYCTX_SIGN, _("&Sign"));
985                  case 2:  sortby = KEY_SORT_IS_SECRET; break;      set_menu_text (popup, ID_KEYCTX_ENABLE, _("&Enable"));
986                  case 3:  sortby = KEY_SORT_LEN; break;      set_menu_text (popup, ID_KEYCTX_DISABLE, _("&Disable"));
987                  case 5:  sortby = KEY_SORT_VALIDITY; break;      set_menu_text (popup, ID_KEYCTX_RECVFROM, _("Re&fresh from Keyserver"));
988                  case 6:  sortby = KEY_SORT_OTRUST; break;      set_menu_text (popup, ID_KEYCTX_SETPREFKS, _("Set preferred Keyserver URL"));
989                  case 7:  sortby = KEY_SORT_CREATED; break;      set_menu_text (popup, ID_KEYCTX_SENDMAIL, _("Send Key to Mail Recipient"));
990                  case 8:  sortby = KEY_SORT_ALGO; break;      set_menu_text (popup, ID_KEYCTX_SETDEFKEY, _("Set as Default Key"));
991                  default: return TRUE; //sortby = KEY_SORT_USERID; break;  
992                  }      set_menu_text (popup, ID_KEYCTX_ADDKEY, _("Key..."));
993        set_menu_text (popup, ID_KEYCTX_ADDUID, _("User ID..."));
994                  if ((kmi->keylist_sortby & ~KEYLIST_SORT_DESC) == sortby)      set_menu_text (popup, ID_KEYCTX_ADDPHOTO, _("Photo ID..."));
995                      kmi->keylist_sortby ^= KEYLIST_SORT_DESC;      set_menu_text (popup, ID_KEYCTX_ADDREV, _("Revoker..."));
996                  else  
997                      kmi->keylist_sortby = sortby;      /* change popup texts */
998                  keylist_sort (kmi->lv, kmi->keylist_sortby);      set_menu_text_bypos (popup, 0, _("Key Attributes"));
999                  return TRUE;      set_menu_text_bypos (popup, 6, _("Add"));
1000              }      set_menu_text_bypos (popup, 19, _("Send to Keyserver"));
1001              break;  }
1002          }  
1003          break;  
1004          }  /* Return true if the cache contain marked keys. */
1005    static bool
1006      case WM_WINDOWPOSCHANGING:  updated_keys_avail (void)
1007          if (((WINDOWPOS*)lparam)->cx < 400)  {
1008              ((WINDOWPOS*)lparam)->cx = 400;      gpg_keycache_t kc = keycache_get_ctx (1);
1009          if (((WINDOWPOS*)lparam)->cy < 200)      struct keycache_s *k;
1010              ((WINDOWPOS*)lparam)->cy = 200;  
1011           return TRUE;      for (k = kc->item; k; k = k->next) {
1012                    if (k->flags)
1013      case WM_SIZE:              return true;
1014          do_resize_window (dlg, kmi);      }
1015          return TRUE;      return false;
1016            }
1017      case WM_SYSCOMMAND:  
1018          if( LOWORD (wparam) == SC_CLOSE )  
1019              EndDialog( dlg, TRUE );  /* Find the index of the key identified by @key. */
1020          return FALSE;  static int
1021            find_keypos (listview_ctrl_t lv, gpgme_key_t key)
1022      case WM_MENUSELECT:  {
1023          menu_gpg_readonly (dlg, (HMENU)lparam, LOWORD (wparam));      gpgme_key_t src;
1024          break;      int i;
1025    
1026      case WM_INITMENUPOPUP:      for (i=0; i < listview_count_items (lv, 0); i++) {
1027          if ((UINT)LOWORD (lparam) == 3) {          src = (gpgme_key_t)listview_get_item2 (lv, i);
1028              HMENU hm = (HMENU)wparam;          if (src && src->subkeys->keyid == key->subkeys->keyid)
1029              set_menu_text_bypos (hm, 0, _("New"));              return i;
1030          }      }
1031          return FALSE;      return -1;
1032    }
1033      case WM_COMMAND:  
1034          if( gnupg_access_keyring( 1 ) ) {  
1035              msg_box( dlg, _("Could not access public keyring"), _("Key Manager"), MB_ERR );  /* Add all recently updated keys in the cache to the list
1036              return FALSE;     and refresh all GUI elements. */
1037          }  static void
1038          do_check_cache( kmi->lv, dlg, kmi->statbar );  refresh_keylist (struct km_info_s *kmi)
1039          switch( LOWORD( wparam ) ) {  {
1040          case ID_KEYMISC_QUIT:      struct keycache_s *ctx;
1041              EndDialog( dlg, TRUE );      gpg_keycache_t kc;
1042              return TRUE;      int status=0;
1043                
1044          case ID_KEYMISC_MAIL:      kc = keycache_get_ctx (1);
1045              /* XXX      while (!gpg_keycache_next_updated_key (kc, &ctx, &status)) {
1046              DialogBoxParam (glob_hinst, (LPCTSTR)IDD_WINPT_MAIL, GetDesktopWindow (),          if (status == KC_FLAG_ADD)
1047                              winpt_mail_proc, NULL);*/              keylist_add_key (kmi->lv, KEYLIST_LIST, ctx->key);
1048              break;          else
1049                        keylist_upd_key (kmi->lv, find_keypos (kmi->lv, ctx->key), ctx->key);
1050          case ID_KEYMISC_FIND:          /*log_box ("debug", 0, "(%d) %s", status, ctx->key->uids->name);*/
1051              km_find_key (dlg, kmi->lv);      }
1052              break;      keylist_sort (kmi->lv, KEY_SORT_USERID);
1053        update_status_bar (kmi->statbar, kmi->lv);
1054          case ID_KEYMISC_DELETE:      keyring_check_last_access ();
1055              km_delete_keys (kmi->lv, dlg);  }
1056              return TRUE;  
1057                
1058          case ID_KEYMISC_SIGN:          static void
1059              if ( (idx = listview_get_curr_pos( kmi->lv )) == -1 ) {  reload_keylist (struct km_info_s *kmi)
1060                  msg_box( dlg, _("Please select a key."),  _("Key Manager"),  {
1061                           MB_ERR );      keycache_reload (kmi->dlg);
1062                  return TRUE;;      keylist_reload (kmi->lv, keycache_get_ctx (1),
1063              }                      KEYLIST_LIST, KEY_SORT_USERID);
1064              if (km_check_key_status (kmi->lv, idx))  }
1065                  return TRUE;  
1066              key = (gpgme_key_t)listview_get_item2 (kmi->lv, idx);  
1067              listview_get_item_text (kmi->lv, idx, 1, keyid, DIM (keyid)-1);  /* Dialog box procedure for the Key Manager. */
1068              memset (&k, 0, sizeof (k));  BOOL CALLBACK
1069              k.ctx = key;  keymanager_dlg_proc (HWND dlg, UINT msg, WPARAM wparam, LPARAM lparam)
1070              k.keyid = keyid;  {
1071              dialog_box_param (glob_hinst, (LPCSTR)IDD_WINPT_KEYSIGN, dlg,      struct km_info_s *kmi;
1072                                keysign_dlg_proc, (LPARAM)&k,      INITCOMMONCONTROLSEX icex;
1073                                _("Key Signing"), IDS_WINPT_KEYSIGN);      HWND kl;
1074              if (k.update)      HMENU hm;
1075                  update_key (kmi->lv, idx, k.keyid, 0);      gpg_keycache_t c;
1076              return TRUE;      gpgme_key_t key;
1077                    struct genkey_s genkey;
1078          case ID_KEYMISC_REVCERT:      struct winpt_key_s k = {0};
1079              idx = listview_get_curr_pos( kmi->lv );      struct URL_ctx_s *url;
1080              if( idx == -1 ) {      refresh_cache_s rcs = {0};
1081                  msg_box( dlg, _("Please select a key."), _("Key Manager"), MB_ERR );      char type[32], *name;
1082                  return TRUE;      const char *t, *host;
1083              }      WORD port = 0;
1084              listview_get_item_text( kmi->lv, idx, 0, uid, sizeof uid -1 );      int l_idx = 0, i=0, rc;
1085              listview_get_item_text( kmi->lv, idx, 1, keyid, sizeof keyid-1 );  
1086              if ( !km_check_for_seckey( kmi->lv, idx, NULL ) ) {      if ((msg != WM_INITDIALOG)
1087                  msg_box( dlg, _("There is no secret key available!"), _("Key Manager"), MB_ERR );          && ((kmi = (struct km_info_s*)GetWindowLong (dlg, GWL_USERDATA)) == NULL))
1088                  return TRUE;          return FALSE;
1089              }  
1090                    switch (msg) {
1091              {      case WM_INITDIALOG:
1092                  char t[128];          kmi = new struct km_info_s;
1093                  listview_get_item_text( kmi->lv, idx, 5, t, sizeof t -1 );          memset (kmi, 0, sizeof (struct km_info_s));
1094                  if( strchr( t, 'R' ) ) {          kmi->lv_idx = -1;
1095                      msg_box( dlg, _("Key already revoked!"), _("Key Manager"), MB_INFO );          icex.dwSize = sizeof (INITCOMMONCONTROLSEX);
1096                      return TRUE;          icex.dwICC  = ICC_BAR_CLASSES;
1097                  }          InitCommonControlsEx (&icex);
1098              }          kmi->hwnd_sep = regist_sep_wnd (dlg, kmi);
1099                        imagelist_load (dlg);
1100              memset (&k, 0, sizeof (k));          translate_menu_strings (dlg);
1101              k.key_pair = 1;          SetWindowText (dlg, _("Key Manager"));
1102              k.keyid = keyid;  
1103              k.is_protected = km_check_if_protected (kmi->lv, idx);          c = keycache_get_ctx (KEYCACHE_PUB);
1104              dialog_box_param(glob_hinst, (LPCSTR)IDD_WINPT_KEYREVOKE, dlg,          if (!c)
1105                               key_revoke_dlg_proc, (LPARAM)&k,              BUG (NULL);
1106                               _("Key Revocation"), IDS_WINPT_KEYREVOKE);          kl = GetDlgItem (dlg, IDC_KEYMISC_KEYLIST);
1107              return TRUE;          kmi->keylist_sortby = KEY_SORT_USERID;
1108                        Header_SetImageList(ListView_GetHeader (kl), glob_imagelist);
1109          case ID_KEYMISC_TRUSTPATH:          kmi->lv = keylist_load (GetDlgItem (dlg, IDC_KEYMISC_KEYLIST), c,
1110              idx = listview_get_curr_pos( kmi->lv );                                  NULL, KEYLIST_LIST, kmi->keylist_sortby);
1111              if( idx == -1 ) {          /* init subclassing for the listview */
1112                  msg_box( dlg, _("Please select a key."), _("Key Manager"), MB_ERR );          keylist_proc.dlg = dlg;
1113                  return TRUE;          keylist_proc.current = (WNDPROC)keylist_subclass_proc;
1114              }          keylist_proc.old = (WNDPROC)GetWindowLong(kl, GWL_WNDPROC);
1115              listview_get_item_text( kmi->lv, idx, 0, uid, sizeof uid -1 );          if (keylist_proc.old) {
1116              listview_get_item_text( kmi->lv, idx, 1, keyid, sizeof keyid -1 );              if( !SetWindowLong (kl, GWL_WNDPROC, (LONG)keylist_proc.current)) {
1117              if( km_check_for_seckey( kmi->lv, idx, NULL ) ) {                  msg_box (dlg, _("Could not set keylist window procedure."),
1118                  msg_box( dlg, _("It does not make any sense with a key pair!"), _("Key Manager"), MB_OK );                           _("Key Manager"), MB_ERR);
1119                  return FALSE;                  BUG (NULL);
1120              }              }
1121              memset (&k, 0, sizeof (k));          }
1122              k.keyid = keyid;          kmi->statbar = setup_status_bar (dlg, kmi->lv);
1123              k.uid = uid;          SetWindowLong (dlg, GWL_USERDATA, (LONG)kmi);
1124              dialog_box_param( glob_hinst, (LPCSTR)IDD_WINPT_KEYTRUST, dlg,          kmi->toolbar = load_toolbar (dlg, kmi);
1125                                keytrust_dlg_proc, (LPARAM)&k,          kmi->dlg = dlg;
1126                                _("List Trust Path"), IDS_WINPT_KEYTRUST );  
1127              return TRUE;          do_center_window (dlg, kmi);
1128                        do_resize_window (dlg, kmi);
1129          case ID_KEYMISC_CHECKSIGS:          update_ui_items (dlg, kmi->lv);
1130              idx = listview_get_curr_pos (kmi->lv);          restore_column_info (kmi->lv);
1131              if( idx == -1 ) {  
1132                  msg_box( dlg, _("Please select a key."), _("Key Manager"), MB_ERR );          SetDlgItemText (dlg, IDC_KEYMISC_GTEXT, _("Groups"));
1133                  return FALSE;          SetClassLong (dlg, GCL_HICON, (LONG)LoadIcon (glob_hinst,
1134              }                        (LPCTSTR)IDI_WINPT));
1135              listview_get_item_text (kmi->lv, idx, 0, uid, DIM (uid)-1);          SetForegroundWindow (dlg);
1136              listview_get_item_text (kmi->lv, idx, 1, keyid, DIM (keyid)-1);          force_foreground_window (dlg, 1000);
1137              memset (&k, 0, sizeof (k));          return TRUE;
1138              k.keyid = keyid;          
1139              k.uid = uid;      case WM_DESTROY:
1140              k.ctx = (gpgme_key_t)listview_get_item2 (kmi->lv, idx);          save_column_info (kmi->lv);
1141              dialog_box_param (glob_hinst, (LPCSTR)IDD_WINPT_KEYSIG, dlg,          if (kmi->lv) {
1142                                keysig_dlg_proc, (LPARAM)&k,              keylist_delete (kmi->lv);
1143                                _("Key Signature List" ), IDS_WINPT_KEYSIG);              kmi->lv = NULL;      
1144              return TRUE;          }
1145                        imagelist_destroy ();
1146          case ID_KEYMISC_PROPS:  
1147              idx = listview_get_curr_pos( kmi->lv );          ltoa (kmi->pos_x, type, 10);
1148              if( idx == -1 ) {          set_reg_entry (HKEY_CURRENT_USER, "Software\\WinPT", "KM_Pos_X", type);
1149                  msg_box( dlg, _("Please select a key."), _("Key Manager"), MB_ERR );          ltoa (kmi->pos_y, type, 10);
1150                  return FALSE;          set_reg_entry (HKEY_CURRENT_USER, "Software\\WinPT", "KM_Pos_Y", type);
1151              }          
1152              listview_get_item_text (kmi->lv, idx, 1, keyid, DIM (keyid)-1);          /* Remove runtime information. This should be the last action taken here. */
1153              listview_get_item_text (kmi->lv, idx, 2, type, DIM (type)-1);          delete kmi; kmi = NULL;
1154              memset (&k, 0, sizeof (k));          SetWindowLong (dlg, GWL_USERDATA, 0);
1155              k.key_pair = 0;          return FALSE;
1156              k.keyid = keyid;  
1157              if( !strcmp( type, "pub/sec" ) || !strcmp( type, "pub/crd" ) )      case WM_SHOWWINDOW:
1158                  k.key_pair = 1;          /*refresh_keylist (kmi);*/
1159              k.callback.ctl = kmi->lv;          break;
1160              k.callback.idx = idx;  
1161              k.is_v3 = km_key_is_v3 (kmi->lv, idx);      case WM_MOVE:
1162              dialog_box_param( glob_hinst, (LPCSTR)IDD_WINPT_KEYPROPS, dlg,          RECT r;
1163                                keyprops_dlg_proc, (LPARAM)&k,          GetWindowRect (dlg, &r);
1164                                _("Key Properties"), IDS_WINPT_KEYPROPS );          kmi->pos_x = r.left;
1165              if (k.callback.new_val != 0) {          kmi->pos_y = r.top;    
1166                  t = get_key_trust_str (k.callback.new_val);          break;
1167                  listview_add_sub_item (kmi->lv, idx, 6, t);          
1168              }  
1169              return TRUE;      case WM_RBUTTONUP:
1170                        do_create_minpopup (dlg);
1171          case ID_KEYMISC_RECACHE:          break;
1172              /* If there is already a reload request, don't bother the user with a message. */  
1173              if( keycache_get_reload() == 1 )      case WM_NOTIFY:            
1174                  idx = IDYES;          NMHDR *notify;
1175              else {          POINT p;
1176                  char t[256];          HMENU popup;
1177                  _snprintf( t, sizeof t -1,          
1178                             _("This is only useful when the keyring has been "          notify = (NMHDR *)lparam;
1179                               "modified (sign a key...).\n"          if (!notify)
1180                               "Do you really want to reload the keycache?") );              break;
1181                  idx = msg_box( dlg, t, _("Key Manager"), MB_YESNO );          switch (notify->code) {
1182              }          case TBN_QUERYDELETE:
1183              if( idx == IDYES ) {              SetWindowLong (dlg, DWL_MSGRESULT, TRUE);
1184                  rcs.kr_reload = rcs.kr_update = 1;              return TRUE;
1185                  rcs.tr_update = 0;          
1186                  DialogBoxParam( glob_hinst, (LPCSTR)IDD_WINPT_KEYCACHE, dlg,          case TBN_QUERYINSERT:
1187                                  keycache_dlg_proc, (LPARAM)&rcs );              SetWindowLong (dlg, DWL_MSGRESULT, TRUE);
1188                  c = keycache_get_ctx( 1 );              return TRUE;
1189                  if( !c )  
1190                      BUG( dlg );          case TBN_GETBUTTONINFO:
1191                  keylist_reload( kmi->lv, c, KEYLIST_LIST, KEY_SORT_USERID );              LPTBNOTIFY lpTbNotify;
1192                  refresh_keys = 0;              lpTbNotify = (LPTBNOTIFY)lparam;
1193              }              if (lpTbNotify->iItem < (sizeof(myb) / sizeof(mybuttons))) {
1194              return TRUE;                  lpTbNotify->tbButton.iBitmap = imagelist_getindex (myb[lpTbNotify->iItem].icon);
1195                                lpTbNotify->tbButton.idCommand = myb[lpTbNotify->iItem].command;
1196          case ID_KEYMISC_REBUILD:                  lpTbNotify->tbButton.fsState = TBSTATE_ENABLED;
1197              name=NULL;                  lpTbNotify->tbButton.fsStyle = TBSTYLE_BUTTON;
1198              gpg_rebuild_cache (&name);                  lpTbNotify->tbButton.iString = 0;
1199              if (name) {                  strncpy (lpTbNotify->pszText, myb[lpTbNotify->iItem].text, lpTbNotify->cchText);
1200                  char *p = strchr (name, '\n');                  SetWindowLong(dlg, DWL_MSGRESULT, TRUE);
1201                  show_msg (dlg, 2000, p? name + (p-name)+1 : name);              }
1202                  free (name);              return TRUE;
1203              }  
1204              return TRUE;          case TBN_RESET: /* Restore last saved toolbar status */
1205                            TBSAVEPARAMS tbsp;
1206          case ID_KEYMISC_NEWKEY:  
1207              memset (&genkey, 0, sizeof (genkey));              tbsp.hkr = HKEY_CURRENT_USER;
1208              dialog_box_param (glob_hinst, (LPCSTR)IDD_WINPT_KEYGEN, dlg,              tbsp.pszSubKey = "Software\\WinPT";
1209                                keygen_dlg_proc, (LPARAM)&genkey, _("Key Generation"),              tbsp.pszValueName = "KM_toolbar";
1210                                IDS_WINPT_KEYGEN);              SendMessage (notify->hwndFrom, TB_SAVERESTORE, FALSE, (LPARAM)&tbsp);
1211              if (genkey.newkey != NULL)              break;
1212                  keylist_add_key (kmi->lv, KEYLIST_LIST, genkey.newkey);  
1213              return TRUE;          case TBN_BEGINADJUST: /* User is about to change the toolbar. Save it */
1214                tbsp.hkr = HKEY_CURRENT_USER;
1215          case ID_KEYMISC_CARDNEW:              tbsp.pszSubKey = "Software\\WinPT";
1216              if( !scard_support ) {              tbsp.pszValueName = "KM_toolbar";
1217                  msg_box( dlg, _("Smart Card support is not available."), _("Key Manager"), MB_INFO );              SendMessage (notify->hwndFrom, TB_SAVERESTORE, TRUE, (LPARAM)&tbsp);
1218                  return TRUE;              break;
1219              }  
1220              dialog_box_param( glob_hinst, (LPCSTR)IDD_WINPT_CARD_KEYGEN, dlg,          case TBN_ENDADJUST: /* User has finished customizing the toolbar. Save it. */
1221                                card_keygen_dlg_proc, NULL, _("Card Key Generation"),              tbsp.hkr = HKEY_CURRENT_USER;
1222                                IDS_WINPT_CARD_KEYGEN );              tbsp.pszSubKey = "Software\\WinPT";
1223              /* XXX: use new code */              tbsp.pszValueName = "KM_toolbar";
1224              if( keycache_get_reload() )              SendMessage (notify->hwndFrom, TB_SAVERESTORE, TRUE, (LPARAM)&tbsp);
1225                  send_cmd_id( dlg, ID_KEYMISC_RECACHE );              break;
1226              return TRUE;  
1227            case TTN_GETDISPINFO:
1228          case ID_KEYMISC_KEYWIZARD:              LPTOOLTIPTEXT lpttt;
1229              memset (&genkey, 0, sizeof (genkey));              lpttt = (LPTOOLTIPTEXT)lparam;
1230              dialog_box_param (glob_hinst, (LPCSTR)IDD_WINPT_KEYWIZARD, dlg,  
1231                                keygen_wizard_dlg_proc, (LPARAM)&genkey, _("Key Generation Wizard"),              lpttt->hinst = NULL;
1232                                IDS_WINPT_KEYWIZARD);              switch (lpttt->hdr.idFrom) {
1233              if (genkey.newkey != NULL)              case ID_KEYMISC_KEYWIZARD:
1234                  keylist_add_key (kmi->lv, KEYLIST_LIST, genkey.newkey);                  lpttt->lpszText = (char*)_("Generate new key pair");
1235              return TRUE;                  break;
1236                
1237          case ID_KEYMISC_SENDRECV:              case ID_KEYMISC_SENDRECV:
1238              dialog_box_param( glob_hinst, (LPCSTR)IDD_WINPT_KEYSERVER, dlg,                  lpttt->lpszText = (char*)_("Search for a specific key");
1239                                keyserver_dlg_proc, NULL, _("Keyserver Access"),                  break;
1240                                IDS_WINPT_KEYSERVER );  
1241              return TRUE;              case ID_KEYMISC_DELETE:
1242                                lpttt->lpszText = (char *)_("Delete key from keyring");
1243          case ID_KEYMISC_GPGPREFS:                  break;
1244              dialog_box_param( glob_hinst, (LPCSTR)IDD_WINPT_GPGPREFS, dlg,  
1245                                gpgprefs_dlg_proc, NULL, _("GnuPG Preferences"),              case ID_KEYMISC_PROPS:
1246                                IDS_WINPT_GPGPREFS );                  lpttt->lpszText = (char *)_("Show key properties");
1247              return TRUE;                  break;
1248                
1249          case ID_KEYMISC_GPGOPT:              case ID_KEYMISC_SIGN:
1250              dialog_box_param( glob_hinst, (LPCSTR)IDD_WINPT_GPGOPT, dlg,                  lpttt->lpszText = (char *)_("Sign key");
1251                                gpgopt_dlg_proc, NULL, _("GnuPG Options" ),                  break;
1252                                IDS_WINPT_GPGOPT );  
1253              return TRUE;              case ID_KEYCTX_COPY:
1254                                lpttt->lpszText = (char *)_("Copy key to clipboard");
1255          case ID_KEYMISC_IMPORT:                  break;
1256              t = get_filename_dlg (dlg, FILE_OPEN, _("Choose Name of the Key File"), NULL, NULL);  
1257              if (t)              case ID_KEYCTX_PASTE:
1258                  km_file_import (dlg, t);                  lpttt->lpszText = (char*)_("Paste key from clipboard");
1259              return TRUE;                  break;
1260    
1261          case ID_KEYMISC_IMPORT_HTTP:              case ID_KEYMISC_IMPORT:
1262              url = (struct URL_ctx_s*)get_http_file_dlg (dlg);                  lpttt->lpszText = (char *)_("Import key to keyring");
1263              if (url->cancel == 0)                  break;
1264                  km_http_import (dlg, url->url);  
1265              delete url; url=NULL;              case ID_KEYMISC_EXPORT:
1266              break;                  lpttt->lpszText = (char *)_("Export key to a file");
1267                                break;
1268          case ID_KEYMISC_EXPORT:              }
1269              idx = listview_get_curr_pos (kmi->lv);              return TRUE;
1270              if (idx == -1) {              
1271                  msg_box( dlg, _("Please select a key."), _("Key Manager"), MB_ERR );          case LVN_ITEMCHANGED:
1272                  return TRUE;              if (((LPNMLISTVIEW)lparam)->uNewState) { /* item selected? */
1273              }                  kmi->lv_idx = listview_get_curr_pos (kmi->lv);
1274              if (listview_count_items (kmi->lv, 1) > 1)                  update_ui_items (dlg, kmi->lv);
1275                  name = m_strdup ("Exported_GPG_Keys.asc");                  return TRUE;
1276              else {              }
1277                  listview_get_item_text (kmi->lv, idx, 1, keyid, DIM (keyid)-1);              break;
1278                  name = gen_export_filename (keyid, 0);  
1279              }          case NM_RCLICK:
1280              t = get_filename_dlg (dlg, FILE_SAVE, _("Choose Name for Key File"), NULL, name);              if (notify->idFrom == IDC_KEYMISC_KEYLIST) {
1281              free_if_alloc (name);                  l_idx =listview_get_curr_pos (kmi->lv);
1282              if (t == NULL)                  if (l_idx == -1)
1283                  return TRUE;                      return TRUE; /* Popup only when a item was selected */              
1284              km_file_export (dlg, kmi->lv, t);                  GetCursorPos (&p);
1285              return TRUE;                  hm = LoadMenu (glob_hinst, MAKEINTRESOURCE (IDR_WINPT_KEYMISC_CTX));
1286                                popup = GetSubMenu (hm, 0);
1287          case ID_KEYMISC_EXPORT_PRIVKEY:                  translate_popupmenu_strings (popup);
1288              idx = listview_get_curr_pos( kmi->lv );  
1289              if( idx == -1 ) {                  if (km_check_for_seckey (kmi->lv, l_idx, &i))
1290                  msg_box( dlg, _("Please select a key."), _("Key Manager"), MB_ERR );                      set_menu_state (popup, ID_KEYCTX_SETDEFKEY, MF_ENABLED);
1291                  return TRUE;                  if (i == 0)
1292              }                      set_menu_state (popup, ID_KEYCTX_MAXTRUST, MF_ENABLED);
1293              if( !km_check_for_seckey( kmi->lv, idx, NULL ) ) {                  if (!km_check_for_seckey (kmi->lv, l_idx, NULL) ||
1294                  msg_box( dlg, _("There is no corresponding secret key for this key."),                      (km_get_key_status (kmi->lv, l_idx) & KM_FLAG_REVOKED)) {
1295                          _("Key Manager"), MB_ERR );                      set_menu_state (popup, ID_KEYCTX_REV, MF_DISABLED|MF_GRAYED);
1296                  return TRUE;                      set_menu_state (popup, ID_KEYCTX_ADDKEY, MF_DISABLED|MF_GRAYED);
1297              }                      set_menu_state (popup, ID_KEYCTX_ADDUID, MF_DISABLED|MF_GRAYED);
1298              if( listview_count_items( kmi->lv, 1 ) > 1 ) {                      set_menu_state (popup, ID_KEYCTX_ADDREV, MF_DISABLED|MF_GRAYED);
1299                  msg_box( dlg, _("You can only export one secret key."), _("Key Manager"), MB_ERR );                      set_menu_state (popup, ID_KEYCTX_ADDPHOTO, MF_DISABLED|MF_GRAYED );
1300                  return TRUE;                      set_menu_state (popup, ID_KEYCTX_SETPREFKS, MF_DISABLED|MF_GRAYED);
1301              }                  }
1302              idx = msg_box( dlg,                  else if (km_check_for_seckey (kmi->lv, l_idx, NULL) &&
1303                            _("This operation will export your *SECRET* key!\n\n"                           km_key_is_v3 (kmi->lv, l_idx)) {
1304                              "Never send this key to ANYONE, it should be available\n"                      /* PGP 2 keys, version 3 have no no support for photo-id's,
1305                              "ONLY on your machine and you may use this function\n"                         designated revokers and secondary keys. */
1306                              "to copy the key to a safe place.\n\n"                      set_menu_state (popup, ID_KEYCTX_ADDKEY, MF_DISABLED|MF_GRAYED);
1307                              "Do you really want to export the key?"),                      set_menu_state (popup, ID_KEYCTX_ADDREV, MF_DISABLED|MF_GRAYED);
1308                            _("WARNING"), MB_INFO|MB_YESNO );                      set_menu_state (popup, ID_KEYCTX_ADDPHOTO, MF_DISABLED|MF_GRAYED);
1309              if( idx == IDYES ) {                  }
1310                  idx = listview_get_curr_pos( kmi->lv );                  if (km_get_key_status( kmi->lv, l_idx ) & KM_FLAG_DISABLED)
1311                  listview_get_item_text( kmi->lv, idx, 1, keyid, sizeof (keyid)-8 );                      set_menu_state (popup, ID_KEYCTX_DISABLE, MF_DISABLED|MF_GRAYED);
1312                  name = gen_export_filename (keyid, 1);                  else
1313                  t = get_filename_dlg (dlg, FILE_SAVE, _("Choose Name for Key File"), NULL, name);                      set_menu_state (popup, ID_KEYCTX_ENABLE, MF_DISABLED|MF_GRAYED);
1314                  if (t != NULL)                            if (km_get_key_status (kmi->lv, l_idx) & KM_FLAG_REVOKED ||
1315                      km_privkey_export (dlg, kmi->lv, t);                      km_get_key_status (kmi->lv, l_idx) & KM_FLAG_EXPIRED)
1316              }                      set_menu_state (popup, ID_KEYCTX_SIGN, MF_DISABLED|MF_GRAYED);
1317              break;                  if (!clip_contains_pgpkey ())
1318                        set_menu_state (popup, ID_KEYCTX_PASTE, MF_DISABLED|MF_GRAYED);
1319          case ID_KEYMISC_INFO:                  if (mapi_init ())
1320              dialog_box_param( glob_hinst, (LPCSTR)IDD_WINPT_ABOUT, glob_hwnd,                      set_menu_state (popup, ID_KEYCTX_SENDMAIL, MF_DISABLED|MF_GRAYED);
1321                                about_winpt_dlg_proc, NULL, _("About WinPT"),                  /* Override 'Default Keyserver' with the actual name. */
1322                                IDS_WINPT_ABOUT );                  host = kserver_get_hostname (0, -1, &port);
1323              break;                  set_menu_text (popup, ID_KEYCTX_KS_DEFAULT, host);
1324                    popup_gpg_readonly (dlg, popup);
1325          case ID_KEYMISC_HELP:                  if (listview_count_items (kmi->lv, 1) > 1)
1326              ShellExecute (dlg, "open", "winpt.chm", NULL, NULL, SW_SHOW);                      popup_multiple (dlg, popup);
1327              break;                  TrackPopupMenu (popup, TPM_RIGHTALIGN, p.x, p.y, 0, dlg, NULL);
1328                    DestroyMenu (popup);
1329          case ID_KEYMISC_OT:                  DestroyMenu (hm);
1330              dialog_box_param( glob_hinst, (LPCTSTR)IDD_WINPT_OWNERTRUST, glob_hwnd,                  return TRUE;
1331                                ownertrust_dlg_proc, NULL,              }
1332                                _("Ownertrust"), IDS_WINPT_OWNERTRUST );              break;
1333              break;  
1334            case LVN_COLUMNCLICK:
1335          case ID_KEYMISC_EDITKEY:              if (notify->idFrom == IDC_KEYMISC_KEYLIST) {
1336              idx = listview_get_curr_pos (kmi->lv);                  NMLISTVIEW *nft = (LPNMLISTVIEW) lparam;
1337              if (idx == -1)                  int sortby = 0;
1338                  break;                  switch (nft->iSubItem) {
1339              listview_get_item_text (kmi->lv, idx, 1, keyid, sizeof (keyid)-1);                  case 0:  sortby = KEY_SORT_USERID; break;
1340              /* XXX: pub/crd = secret key does not work */                  case 1:  sortby = KEY_SORT_KEYID; break;
1341              memset (&k, 0, sizeof (k));                  case 2:  sortby = KEY_SORT_IS_SECRET; break;
1342              k.is_protected = km_check_if_protected (kmi->lv, idx);                  case 3:  sortby = KEY_SORT_LEN; break;
1343              k.key_pair = km_check_for_seckey (kmi->lv, idx, NULL);                  case 5:  sortby = KEY_SORT_VALIDITY; break;
1344              k.keyid = keyid;                  case 6:  sortby = KEY_SORT_OTRUST; break;
1345              k.is_v3 = km_key_is_v3 (kmi->lv, idx);                  case 7:  sortby = KEY_SORT_CREATED; break;
1346              k.flags = km_get_key_status (kmi->lv, idx);                  case 8:  sortby = KEY_SORT_ALGO; break;
1347              dialog_box_param (glob_hinst, (LPCTSTR)IDD_WINPT_KEYEDIT, dlg,                  default: return TRUE; //sortby = KEY_SORT_USERID; break;
1348                                keyedit_main_dlg_proc, (LPARAM)&k,                  }
1349                                _("Key Edit"), IDS_KEYCTX_EDIT);  
1350              if (k.update)                  if ((kmi->keylist_sortby & ~KEYLIST_SORT_DESC) == sortby)
1351                  update_key (kmi->lv,  idx, keyid, 1);                      kmi->keylist_sortby ^= KEYLIST_SORT_DESC;
1352              break;                  else
1353                                    kmi->keylist_sortby = sortby;
1354          case ID_KEYMISC_COPY:                  keylist_sort (kmi->lv, kmi->keylist_sortby);
1355              km_index = listview_get_curr_pos (kmi->lv);                  return TRUE;
1356              km_clip_export (dlg, kmi->lv);              }
1357              break;              break;
1358                        }
1359          case ID_KEYMISC_SELALL:          break;
1360              listview_select_all (kmi->lv);  
1361              break;      case WM_WINDOWPOSCHANGING:
1362            if (((WINDOWPOS*)lparam)->cx < 400)
1363          case ID_KEYMISC_PASTE:              ((WINDOWPOS*)lparam)->cx = 400;
1364              km_index = -1;          if (((WINDOWPOS*)lparam)->cy < 200)
1365              km_clip_import (dlg);              ((WINDOWPOS*)lparam)->cy = 200;
1366              break;           return TRUE;
1367                        
1368          case ID_KEYCTX_SETPREFKS:      case WM_SIZE:
1369              listview_get_item_text (kmi->lv, idx, 1, keyid, DIM(keyid)-1);          do_resize_window (dlg, kmi);
1370              memset (&k, 0, sizeof (k));          return TRUE;
1371              k.keyid = keyid;          
1372              keyedit_set_pref_keyserver (&k, dlg);      case WM_SYSCOMMAND:
1373              break;          if (LOWORD (wparam) == SC_CLOSE)
1374                EndDialog (dlg, TRUE);
1375          case ID_KEYMISC_REFRESH_KEYS:          return FALSE;
1376              if (listview_count_items (kmi->lv, 1) == 0) {          
1377                  msg_box (dlg, _("No key was selected, select all by default."), _("Key Manager"), MB_INFO);      case WM_MENUSELECT:
1378                  listview_select_all (kmi->lv);          change_edit_menu (kmi->lv, (HMENU)lparam, LOWORD (wparam));
1379              }          change_key_menu ((HMENU)lparam, LOWORD (wparam));
1380              km_refresh_from_keyserver (kmi->lv, dlg);          break;
1381              break;  
1382                                case WM_INITMENUPOPUP:
1383          /** Context menu **/          if ((UINT)LOWORD (lparam) == 3) {
1384          case ID_KEYCTX_EDIT:              HMENU h = (HMENU)wparam;
1385              send_cmd_id( dlg, ID_KEYMISC_EDITKEY );              set_menu_text_bypos (h, 0, _("New"));
1386              break;          }
1387            /* XXX: before we can use it, we need to find a way to
1388          case ID_KEYCTX_PROPS:                  update the gpg access timestamp after each operation.
1389              send_cmd_id( dlg, ID_KEYMISC_PROPS );          if (keyring_check_last_access ())
1390              break;              reload_keylist (kmi);
1391            */
1392          case ID_KEYCTX_UIDS:          return FALSE;
1393              send_cmd_id( dlg, ID_KEYMISC_UID );  
1394              break;      case WM_COMMAND:
1395            /* Allow at least 'Exit' in such a case. */
1396          case ID_KEYCTX_SIGN:          if (gnupg_access_keyring (1) && LOWORD (wparam) != ID_KEYMISC_QUIT) {
1397              send_cmd_id( dlg, ID_KEYMISC_SIGN );              msg_box (dlg, _("Could not access public keyring"),
1398              break;                       _("Key Manager"), MB_ERR);
1399                return FALSE;
1400          case ID_KEYCTX_DEL:          }
1401              send_cmd_id (dlg, ID_KEYMISC_DELETE);  
1402              break;          switch (LOWORD (wparam)) {
1403            case ID_KEYMISC_QUIT:
1404          case ID_KEYCTX_REV:              EndDialog (dlg, TRUE);
1405              send_cmd_id( dlg, ID_KEYMISC_REVCERT );              return TRUE;
1406              break;          
1407            case ID_KEYMISC_FIND:
1408          case ID_KEYCTX_SENDMAIL:              km_find_key (dlg, kmi->lv);
1409              km_send_to_mail_recipient (kmi->lv, dlg);              break;
1410              break;  
1411            case ID_KEYMISC_DELETE:
1412          case ID_KEYCTX_KS_DEFAULT:          case ID_KEYMISC_DELETE2:
1413              host = kserver_get_hostname (0, -1, &port);              if (!km_delete_keys (kmi->lv, dlg))
1414              km_send_to_keyserver (kmi->lv, dlg, host, port);                  update_status_bar (kmi->statbar, kmi->lv);
1415              break;              return TRUE;
1416                
1417          case ID_KEYCTX_ADDKEY:          case ID_KEYMISC_SIGN:
1418              idx = listview_get_curr_pos (kmi->lv);              if (kmi->lv_idx == -1) {
1419              listview_get_item_text( kmi->lv, idx, 1, keyid, DIM (keyid)-1);                  msg_box (dlg, _("Please select a key."),  
1420              memset (&k, 0, sizeof (k));                           _("Key Manager"), MB_ERR);
1421              k.key_pair = km_check_for_seckey (kmi->lv, idx, NULL);                  return TRUE;
1422              k.is_protected = km_check_if_protected (kmi->lv, idx);              }
1423              k.keyid = keyid;              if (km_check_key_status (kmi->lv, kmi->lv_idx))
1424              keyedit_add_subkey (&k, dlg, NULL);                  return TRUE;
1425              if (k.update)              key = (gpgme_key_t)listview_get_item2 (kmi->lv, kmi->lv_idx);
1426                  update_key (kmi->lv, idx, keyid, 1);              memset (&k, 0, sizeof (k));
1427              break;              strncpy (k.tmp_keyid, key->subkeys->keyid+8, 8);
1428                k.ctx = key;
1429          case ID_KEYCTX_ADDUID:              k.keyid = key->subkeys->keyid;
1430              idx = listview_get_curr_pos (kmi->lv);              dialog_box_param (glob_hinst, (LPCSTR)IDD_WINPT_KEYSIGN, dlg,
1431              listview_get_item_text( kmi->lv, idx, 1, keyid, DIM (keyid)-1);                                keysign_dlg_proc, (LPARAM)&k,
1432              memset (&k, 0, sizeof (k));                                _("Key Signing"), IDS_WINPT_KEYSIGN);
1433              k.key_pair = km_check_for_seckey (kmi->lv, idx, NULL);              if (k.update)
1434              k.is_protected = km_check_if_protected (kmi->lv, idx);                  update_key (kmi->lv, kmi->lv_idx, k.tmp_keyid, 0);
1435              k.keyid = keyid;              return TRUE;
1436              keyedit_add_userid (&k, dlg, NULL);              
1437              if (k.update)          case ID_KEYMISC_REVCERT:
1438                  update_key (kmi->lv, idx, keyid, 1);              if (kmi->lv_idx == -1) {
1439              break;                  msg_box (dlg, _("Please select a key."), _("Key Manager"), MB_ERR);
1440                    return TRUE;
1441          case ID_KEYCTX_ADDREV:              }
1442              idx = listview_get_curr_pos (kmi->lv);              key = (gpgme_key_t)listview_get_item2 (kmi->lv, kmi->lv_idx);
1443              listview_get_item_text (kmi->lv, idx, 1, keyid, DIM (keyid)-1);              if (!key)
1444              memset (&k, 0, sizeof (k));                  BUG (NULL);
1445              k.keyid = keyid;              if (!km_check_for_seckey (kmi->lv, kmi->lv_idx, NULL)) {
1446              k.is_protected = km_check_if_protected (kmi->lv, idx);                  msg_box (dlg, _("There is no secret key available!"),
1447              k.key_pair = km_check_for_seckey( kmi->lv, idx, NULL );                          _("Key Manager"), MB_ERR);
1448              keyedit_add_revoker (&k, dlg);                  return TRUE;
1449              if (k.update)              }
1450                  update_key (kmi->lv, idx, keyid, 1);              
1451              break;              {
1452                    char state[64];
1453          case ID_KEYCTX_ADDPHOTO:                  listview_get_item_text (kmi->lv, kmi->lv_idx, 5,
1454              idx = listview_get_curr_pos (kmi->lv);                                          state, sizeof (state) -1);
1455              listview_get_item_text (kmi->lv, idx, 1, keyid, DIM (keyid)-1);                  if (strchr (state, 'R' )) {
1456              memset (&k, 0, sizeof (k));                      msg_box (dlg, _("Key already revoked!"),
1457              k.keyid = keyid;                               _("Key Manager"), MB_INFO);
1458              k.is_protected = km_check_if_protected (kmi->lv, idx);                      return TRUE;
1459              k.key_pair = km_check_for_seckey (kmi->lv, idx, NULL);                  }
1460              keyedit_add_photo (&k, dlg);              }
1461              if (k.update)              
1462                  update_key (kmi->lv, idx, keyid, 1);              memset (&k, 0, sizeof (k));
1463              break;              k.key_pair = 1;
1464                k.keyid = key->subkeys->keyid+8;
1465          case ID_KEYCTX_KS_NL:              k.is_protected = km_check_if_protected (kmi->lv, kmi->lv_idx);
1466          case ID_KEYCTX_KS_PL:              dialog_box_param(glob_hinst, (LPCSTR)IDD_WINPT_KEYREVOKE, dlg,
1467          case ID_KEYCTX_KS_AT:                               key_revoke_dlg_proc, (LPARAM)&k,
1468          case ID_KEYCTX_KS_DE:                               _("Key Revocation Cert"), IDS_WINPT_KEYREVOKE);
1469          case ID_KEYCTX_KS_DK:              return TRUE;
1470          case ID_KEYCTX_KS_CZ:              
1471          case ID_KEYCTX_KS_ES:          case ID_KEYMISC_TRUSTPATH:
1472          case ID_KEYCTX_KS_UK:              if (kmi->lv_idx == -1) {
1473              host = kserver_get_hostname (LOWORD (wparam) - 40107, 0, &port);                  msg_box (dlg, _("Please select a key."), _("Key Manager"), MB_ERR);
1474              km_send_to_keyserver (kmi->lv, dlg, host, port);                  return TRUE;
1475              break;              }
1476                key = (gpgme_key_t)listview_get_item2 (kmi->lv, kmi->lv_idx);
1477          case ID_KEYCTX_RECVFROM:              if (km_check_for_seckey (kmi->lv, kmi->lv_idx, NULL)) {
1478              km_refresh_from_keyserver (kmi->lv, dlg);                  msg_box (dlg, _("It does not make any sense with a key pair!"),
1479              break;                           _("Key Manager"), MB_ERR);
1480                    return TRUE;
1481          case ID_KEYCTX_UID_COPY:              }
1482              /* XXX: add generic function to support multiple selection              memset (&k, 0, sizeof (k));
1483                      with a callback */              k.keyid = key->subkeys->keyid+8;
1484              idx = listview_get_curr_pos( kmi->lv );              k.uid = key->uids->uid;
1485              listview_get_item_text( kmi->lv, idx, 0, uid, sizeof uid-1 );              dialog_box_param (glob_hinst, (LPCSTR)IDD_WINPT_KEYTRUST, dlg,
1486              set_clip_text( NULL, uid, strlen( uid ) );                                keytrust_dlg_proc, (LPARAM)&k,
1487              break;                                _("List Trust Path"), IDS_WINPT_KEYTRUST);
1488                return TRUE;
1489          case ID_KEYCTX_KEYID_COPY:              
1490              idx = listview_get_curr_pos( kmi->lv );          case ID_KEYMISC_CHECKSIGS:          
1491              listview_get_item_text( kmi->lv, idx, 1, uid, sizeof uid-1 );              if (kmi->lv_idx == -1) {
1492              set_clip_text( NULL, uid, strlen( uid ) );                  msg_box (dlg, _("Please select a key."), _("Key Manager"), MB_ERR);
1493              break;                  return TRUE;
1494                }
1495          case ID_KEYCTX_FPR_COPY:              key = (gpgme_key_t)listview_get_item2 (kmi->lv, kmi->lv_idx);
1496              idx = listview_get_curr_pos( kmi->lv );              if (!key)
1497              key = (gpgme_key_t) listview_get_item2 (kmi->lv, idx);                          BUG (NULL);
1498              if (key) {              memset (&k, 0, sizeof (k));
1499                  const char * s = get_key_fpr (key);              strncpy (k.tmp_keyid, key->subkeys->keyid+8, 8);
1500                  set_clip_text (NULL, s? s : "", s? strlen (s): 0);              k.keyid = k.tmp_keyid;
1501              }              k.uid = key->uids->uid;
1502              break;              k.ctx = (gpgme_key_t)listview_get_item2 (kmi->lv, kmi->lv_idx);
1503                dialog_box_param (glob_hinst, (LPCSTR)IDD_WINPT_KEYSIG, dlg,
1504          case ID_KEYCTX_KINFO_COPY:                                keysig_dlg_proc, (LPARAM)&k,
1505              idx = listview_get_curr_pos( kmi->lv );                                _("Key Signature List" ), IDS_WINPT_KEYSIG);
1506              listview_get_item_text( kmi->lv, idx, 1, uid, sizeof uid-1 );              if (k.update)
1507              km_set_clip_info( uid );                          update_key (kmi->lv, kmi->lv_idx, k.tmp_keyid, 0);
1508              break;              return TRUE;
1509                
1510          case ID_KEYCTX_COPY:          case ID_KEYMISC_PROPS:      
1511              km_index = listview_get_curr_pos (kmi->lv);              if (kmi->lv_idx == -1) {
1512              km_clip_export (dlg, kmi->lv);                  msg_box( dlg, _("Please select a key."), _("Key Manager"), MB_ERR );
1513              break;                  return TRUE;
1514                }
1515          case ID_KEYCTX_PASTE:                key = (gpgme_key_t)listview_get_item2 (kmi->lv, kmi->lv_idx);
1516              km_index = -1;              if (!key)
1517              km_clip_import (dlg);                  BUG (NULL);
1518              break;              memset (&k, 0, sizeof (k));
1519                k.key_pair = km_check_for_seckey (kmi->lv, kmi->lv_idx, NULL)? 1 : 0;
1520          case ID_KEYCTX_DISABLE:              k.keyid = key->subkeys->keyid+8;
1521              idx = listview_get_curr_pos (kmi->lv);              k.callback.ctl = kmi->lv;
1522              km_enable_disable_key (kmi->lv, dlg, idx, 0);              k.callback.idx = kmi->lv_idx;
1523              break;              k.is_v3 = km_key_is_v3 (kmi->lv, kmi->lv_idx);
1524                dialog_box_param( glob_hinst, (LPCSTR)IDD_WINPT_KEYPROPS, dlg,
1525          case ID_KEYCTX_ENABLE:                                keyprops_dlg_proc, (LPARAM)&k,
1526              idx = listview_get_curr_pos (kmi->lv);                                _("Key Properties"), IDS_WINPT_KEYPROPS );
1527              km_enable_disable_key (kmi->lv, dlg, idx, 1);              if (k.callback.new_val != 0) {
1528              break;                  t = get_key_trust_str (k.callback.new_val);
1529                    listview_add_sub_item (kmi->lv, kmi->lv_idx, 6, t);
1530          case ID_KEYCTX_LISTSIGS:              }
1531              send_cmd_id (dlg, ID_KEYMISC_CHECKSIGS);              if (k.update)
1532              break;                  refresh_keylist (kmi);
1533                return TRUE;
1534          case ID_KEYCTX_MAXTRUST:              
1535              idx = listview_get_curr_pos (kmi->lv);          case ID_KEYMISC_RECACHE:
1536              listview_get_item_text (kmi->lv, idx, 1, keyid, DIM (keyid)-1);              if (updated_keys_avail ())
1537              rc = km_set_implicit_trust (dlg, kmi->lv, idx);                  l_idx = IDYES;
1538              if (!rc)              else {
1539                  update_key (kmi->lv, idx, keyid, 0);                  char inf[256];
1540              break;                  _snprintf (inf, sizeof (inf) -1,
1541                               _("This is only useful when the keyring has been "
1542          case ID_KEYCTX_SETDEFKEY:                               "modified (sign a key...).\n"
1543              idx = listview_get_curr_pos (kmi->lv);                               "Do you really want to reload the keycache?"));
1544              if (!km_check_key_status (kmi->lv, idx)) {                  l_idx = msg_box (dlg, inf, _("Key Manager"), MB_YESNO);
1545                  listview_get_item_text (kmi->lv, idx, 1, keyid, DIM (keyid)-1);              }
1546                  rc = set_gnupg_default_key (keyid);              if (l_idx == IDYES) {
1547                  if (rc)                  rcs.kr_reload = rcs.kr_update = 1;
1548                      msg_box( dlg, winpt_strerror (rc), _("Key Manager"), MB_ERR);                  rcs.tr_update = 0;
1549                  km_update_default_key_str (kmi->statbar);                  DialogBoxParam (glob_hinst, (LPCSTR)IDD_WINPT_KEYCACHE, dlg,
1550              }                                  keycache_dlg_proc, (LPARAM)&rcs);
1551              break;                  c = keycache_get_ctx (1);
1552                    if (!c)
1553          #if 0 /* XXX */                      BUG (dlg);
1554          case ID_GROUP_NEW:                  keylist_reload (kmi->lv, c, KEYLIST_LIST, KEY_SORT_USERID);
1555              memset (&gcb, 0, sizeof (gcb));              }
1556              gcb.gc = gc;              SetForegroundWindow (dlg);
1557              dialog_box_param (glob_hinst, (LPCSTR)IDD_WINPT_GROUP, glob_hwnd,              return TRUE;
1558                               group_manager_dlg_proc, (LPARAM)&gcb, _("New Group"),              
1559                               IDS_WINPT_GROUP);          case ID_KEYMISC_REBUILD:
1560              if( gcb.use_name )              name = NULL;
1561                  treeview_add_item( GetDlgItem(dlg, IDC_KEYMISC_GROUP), NULL, gcb.name );              gpg_rebuild_cache (&name);
1562              return TRUE;              if (name != NULL) {
1563                                char *line = strchr (name, '\n');
1564          case ID_GROUP_PASTE:                  show_msg (dlg, 2000, line? name + (line-name)+1 : name);
1565              km_groups_add (gc, kmi->lv, km_index);                  safe_free (name);
1566              break;              }
1567                            SetForegroundWindow (dlg);
1568          case ID_GROUP_DELETE:              return TRUE;
1569              km_groups_del (gc);              
1570              break;          case ID_KEYMISC_NEWKEY:
1571          #endif              memset (&genkey, 0, sizeof (genkey));
1572          }              dialog_box_param (glob_hinst, (LPCSTR)IDD_WINPT_KEYGEN, dlg,
1573                                          keygen_dlg_proc, (LPARAM)&genkey, _("Key Generation"),
1574          break;                                IDS_WINPT_KEYGEN);
1575      }              if (genkey.cancel == 0)
1576                        refresh_keylist (kmi);
1577      return FALSE;              return TRUE;
1578  }  
1579            case ID_KEYMISC_CARDNEW:
1580                if (!scard_support) {
1581                    msg_box (dlg, _("Smart Card support is not available."),
1582                             _("Key Manager"), MB_INFO);
1583                    return TRUE;
1584                }
1585                dialog_box_param (glob_hinst, (LPCSTR)IDD_WINPT_CARD_KEYGEN, dlg,
1586                                  card_keygen_dlg_proc, 0, _("Card Key Generation"),
1587                                  IDS_WINPT_CARD_KEYGEN);
1588                if (updated_keys_avail ())
1589                    send_cmd_id (dlg, ID_KEYMISC_RECACHE);
1590                return TRUE;
1591    
1592            case ID_KEYMISC_KEYWIZARD:
1593                memset (&genkey, 0, sizeof (genkey));
1594                dialog_box_param (glob_hinst, (LPCSTR)IDD_WINPT_KEYWIZARD, dlg,
1595                                  keygen_wizard_dlg_proc, (LPARAM)&genkey,
1596                                  _("Key Generation Wizard"),
1597                                  IDS_WINPT_KEYWIZARD);
1598                if (genkey.cancel == 0)
1599                    refresh_keylist (kmi);
1600                return TRUE;
1601                
1602            case ID_KEYMISC_SENDRECV:
1603                dialog_box_param (glob_hinst, (LPCSTR)IDD_WINPT_KEYSERVER, dlg,
1604                                  keyserver_dlg_proc, 0, _("Keyserver Access"),
1605                                  IDS_WINPT_KEYSERVER);
1606                refresh_keylist (kmi);
1607                return TRUE;
1608                
1609            case ID_KEYMISC_GPGPREFS:
1610                dialog_box_param (glob_hinst, (LPCSTR)IDD_WINPT_GPGPREFS, dlg,
1611                                  gpgprefs_dlg_proc, 0, _("GnuPG Preferences"),
1612                                  IDS_WINPT_GPGPREFS);
1613                return TRUE;
1614                
1615            case ID_KEYMISC_GPGOPT:
1616                dialog_box_param (glob_hinst, (LPCSTR)IDD_WINPT_GPGOPT, dlg,
1617                                  gpgopt_dlg_proc, 0, _("GnuPG Options"),
1618                                  IDS_WINPT_GPGOPT);
1619                return TRUE;
1620                
1621            case ID_KEYMISC_IMPORT:
1622                t = get_fileopen_dlg (dlg, _("Choose Name of the Key File"),
1623                                      NULL, NULL);
1624                if (!t)
1625                    break;
1626                if (!km_file_import (dlg, t, &i)) {
1627                    if (i < KM_PRELOAD_KEYS)
1628                        refresh_keylist (kmi);
1629                    else /* avoid to spawn too much processes. */
1630                        reload_keylist (kmi);
1631                }
1632                return TRUE;
1633    
1634            case ID_KEYMISC_IMPORT_HTTP:
1635                url = (struct URL_ctx_s*)get_http_file_dlg (dlg);
1636                if (url->cancel == 0) {
1637                    km_http_import (dlg, url->url);
1638                    refresh_keylist (kmi);
1639                }
1640                free_if_alloc (url);
1641                break;
1642                
1643            case ID_KEYMISC_EXPORT:
1644                if (kmi->lv_idx == -1) {
1645                    msg_box (dlg, _("Please select a key."),
1646                             _("Key Manager"), MB_ERR);
1647                    return TRUE;
1648                }
1649                if (listview_count_items (kmi->lv, 1) > 1)
1650                    name = m_strdup ("Exported_GPG_Keys.asc");
1651                else {
1652                    key = (gpgme_key_t)listview_get_item2 (kmi->lv, kmi->lv_idx);          
1653                    name = km_gen_export_filename (key->subkeys->keyid+8, 0);
1654                }
1655                t = get_filesave_dlg (dlg, _("Choose Name for Key File"), NULL, name);
1656                free_if_alloc (name);
1657                if (t == NULL)
1658                    return TRUE;
1659                km_file_export (dlg, kmi->lv, t);
1660                return TRUE;
1661                
1662            case ID_KEYMISC_EXPORT_PRIVKEY:
1663                if (kmi->lv_idx == -1) {
1664                    msg_box (dlg, _("Please select a key."), _("Key Manager"), MB_ERR);
1665                    return TRUE;
1666                }
1667                if( !km_check_for_seckey( kmi->lv, kmi->lv_idx, NULL ) ) {
1668                    msg_box (dlg, _("There is no corresponding secret key for this key."),
1669                             _("Key Manager"), MB_ERR);
1670                    return TRUE;
1671                }
1672                if (listview_count_items (kmi->lv, 1) > 1) {
1673                    msg_box (dlg, _("You can only export one secret key."),
1674                             _("Key Manager"), MB_ERR);
1675                    return TRUE;
1676                }
1677                i = msg_box (dlg,
1678                              _("This operation will export your *SECRET* key!\n\n"
1679                                "Never send this key to ANYONE, it should be available\n"
1680                                "ONLY on your machine and you may use this function\n"
1681                                "to copy the key to a safe place.\n\n"
1682                                "Do you really want to export the key?"),
1683                              _("WARNING"), MB_INFO|MB_YESNO);
1684                if (i == IDYES) {
1685                    key = (gpgme_key_t)listview_get_item2 (kmi->lv, kmi->lv_idx);
1686                    if (!key)
1687                        BUG (NULL);
1688                    name = km_gen_export_filename (key->subkeys->keyid+8, 1);
1689                    t = get_filesave_dlg (dlg, _("Choose Name for Key File"), NULL, name);
1690                    if (t != NULL)          
1691                        km_privkey_export (dlg, kmi->lv, t);
1692                }
1693                return TRUE;
1694    
1695            case ID_KEYMISC_INFO:
1696                dialog_box_param (glob_hinst, (LPCSTR)IDD_WINPT_ABOUT, glob_hwnd,
1697                                  about_winpt_dlg_proc, 0, _("About WinPT"),
1698                                  IDS_WINPT_ABOUT);
1699                break;
1700    
1701            case ID_KEYMISC_WEBSITE:
1702                ShellExecute (dlg, "open", "http://www.winpt.org",
1703                              NULL, NULL, SW_SHOW);
1704                break;
1705    
1706            case ID_KEYMISC_HELP:
1707                start_help (dlg, 0);
1708                break;
1709    
1710            case ID_KEYMISC_OT:
1711                dialog_box_param (glob_hinst, (LPCTSTR)IDD_WINPT_OWNERTRUST,
1712                                  glob_hwnd, ownertrust_dlg_proc, 0,
1713                                  _("Ownertrust"), IDS_WINPT_OWNERTRUST);
1714                break;
1715    
1716            case ID_KEYMISC_EDITKEY:
1717                if (kmi->lv_idx == -1)
1718                    break;
1719                key = (gpgme_key_t)listview_get_item2 (kmi->lv, kmi->lv_idx);
1720                if (!key)
1721                    BUG (NULL);
1722                /* XXX: pub/crd = secret key does not work */
1723                memset (&k, 0, sizeof (k));
1724                strncpy (k.tmp_keyid, key->subkeys->keyid+8, 8);
1725                k.keyid = k.tmp_keyid;
1726                k.is_protected = km_check_if_protected (kmi->lv, kmi->lv_idx);
1727                k.key_pair = km_check_for_seckey (kmi->lv, kmi->lv_idx, NULL);
1728                k.is_v3 = km_key_is_v3 (kmi->lv, kmi->lv_idx);
1729                k.flags = km_get_key_status (kmi->lv, kmi->lv_idx);
1730                dialog_box_param (glob_hinst, (LPCTSTR)IDD_WINPT_KEYEDIT, dlg,
1731                                  keyedit_main_dlg_proc, (LPARAM)&k,
1732                                  _("Key Edit"), IDS_KEYCTX_EDIT);
1733                if (k.update)
1734                    update_key (kmi->lv,  kmi->lv_idx, k.tmp_keyid, 1);
1735                break;
1736                
1737            case ID_KEYMISC_COPY:
1738                km_clip_export (dlg, kmi->lv);
1739                break;
1740                
1741            case ID_KEYMISC_SELALL:
1742                listview_select_all (kmi->lv);
1743                break;
1744    
1745            case ID_KEYMISC_PASTE:
1746                if (!km_clip_import (dlg, &i)) {
1747                    if (i < KM_PRELOAD_KEYS)
1748                        refresh_keylist (kmi);
1749                    else
1750                        reload_keylist (kmi);
1751                }
1752                break;
1753                
1754            case ID_KEYCTX_SETPREFKS:
1755                key = (gpgme_key_t)listview_get_item2 (kmi->lv, kmi->lv_idx);
1756                if (!key)
1757                    BUG (NULL);
1758                memset (&k, 0, sizeof (k));
1759                k.keyid = key->subkeys->keyid+8;
1760                keyedit_set_pref_keyserver (&k, dlg);
1761                break;
1762    
1763            case ID_KEYMISC_REFRESH_KEYS:
1764                if (listview_count_items (kmi->lv, 1) == 0) {
1765                    msg_box (dlg, _("No key was selected, select all by default."),
1766                             _("Key Manager"), MB_INFO);
1767                    listview_select_all (kmi->lv);
1768                }
1769                km_refresh_from_keyserver (kmi->lv, dlg);
1770                break;
1771                            
1772            /** Context menu **/
1773            case ID_KEYCTX_EDIT:
1774                send_cmd_id( dlg, ID_KEYMISC_EDITKEY );
1775                break;
1776    
1777            case ID_KEYCTX_PROPS:
1778                send_cmd_id( dlg, ID_KEYMISC_PROPS );
1779                break;
1780    
1781            case ID_KEYCTX_UIDS:
1782                send_cmd_id( dlg, ID_KEYMISC_UID );
1783                break;
1784    
1785            case ID_KEYCTX_SIGN:
1786                send_cmd_id( dlg, ID_KEYMISC_SIGN );
1787                break;
1788    
1789            case ID_KEYCTX_DEL:
1790                send_cmd_id (dlg, ID_KEYMISC_DELETE);
1791                break;
1792    
1793            case ID_KEYCTX_REV:
1794                send_cmd_id( dlg, ID_KEYMISC_REVCERT );
1795                break;
1796    
1797            case ID_KEYCTX_SENDMAIL:
1798                km_send_to_mail_recipient (kmi->lv, dlg);
1799                break;
1800    
1801            case ID_KEYCTX_KS_DEFAULT:
1802                host = kserver_get_hostname (0, -1, &port);
1803                km_send_to_keyserver (kmi->lv, dlg, host, port);
1804                break;
1805    
1806            case ID_KEYCTX_ADDKEY:
1807                key = (gpgme_key_t)listview_get_item2 (kmi->lv, kmi->lv_idx);
1808                if (!key)
1809                    BUG (NULL);
1810                memset (&k, 0, sizeof (k));
1811                strncpy (k.tmp_keyid, key->subkeys->keyid+8, 8);
1812                k.keyid = k.tmp_keyid;
1813                k.key_pair = km_check_for_seckey (kmi->lv, kmi->lv_idx, NULL);
1814                k.is_protected = km_check_if_protected (kmi->lv, kmi->lv_idx);
1815                keyedit_add_subkey (&k, dlg, NULL);
1816                if (k.update)
1817                    update_key (kmi->lv, kmi->lv_idx, k.tmp_keyid, 1);
1818                break;
1819    
1820            case ID_KEYCTX_ADDUID:
1821                key = (gpgme_key_t)listview_get_item2 (kmi->lv, kmi->lv_idx);
1822                if (!key)
1823                    BUG (NULL);
1824                memset (&k, 0, sizeof (k));
1825                strncpy (k.tmp_keyid, key->subkeys->keyid+8, 8);
1826                k.keyid = k.tmp_keyid;
1827                k.key_pair = km_check_for_seckey (kmi->lv, kmi->lv_idx, NULL);
1828                k.is_protected = km_check_if_protected (kmi->lv, kmi->lv_idx);
1829                keyedit_add_userid (&k, dlg, NULL);
1830                if (k.update)
1831                    update_key (kmi->lv, kmi->lv_idx, k.tmp_keyid, 1);
1832                break;
1833    
1834            case ID_KEYCTX_ADDREV:
1835                key = (gpgme_key_t)listview_get_item2 (kmi->lv, kmi->lv_idx);
1836                if (!key)
1837                    BUG (NULL);
1838                memset (&k, 0, sizeof (k));
1839                strncpy (k.tmp_keyid, key->subkeys->keyid+8, 8);
1840                k.keyid = k.tmp_keyid;
1841                k.is_protected = km_check_if_protected (kmi->lv, kmi->lv_idx);
1842                k.key_pair = km_check_for_seckey (kmi->lv, kmi->lv_idx, NULL);
1843                keyedit_add_revoker (&k, dlg);
1844                if (k.update)
1845                    update_key (kmi->lv, kmi->lv_idx, k.tmp_keyid, 1);
1846                break;
1847    
1848            case ID_KEYCTX_ADDPHOTO:
1849                key = (gpgme_key_t)listview_get_item2 (kmi->lv, kmi->lv_idx);
1850                if (!key)
1851                    BUG (NULL);
1852                memset (&k, 0, sizeof (k));
1853                strncpy (k.tmp_keyid, key->subkeys->keyid+8, 8);
1854                k.keyid = k.tmp_keyid;
1855                k.is_protected = km_check_if_protected (kmi->lv, kmi->lv_idx);
1856                k.key_pair = km_check_for_seckey (kmi->lv, kmi->lv_idx, NULL);
1857                keyedit_add_photo (&k, dlg);
1858                if (k.update)
1859                    update_key (kmi->lv, kmi->lv_idx, k.tmp_keyid, 1);
1860                break;
1861    
1862            case ID_KEYCTX_KS_NL:
1863            case ID_KEYCTX_KS_PL:
1864            case ID_KEYCTX_KS_AT:
1865            case ID_KEYCTX_KS_DE:
1866            case ID_KEYCTX_KS_DK:
1867            case ID_KEYCTX_KS_CZ:
1868            case ID_KEYCTX_KS_ES:
1869            case ID_KEYCTX_KS_UK:
1870                host = kserver_get_hostname (LOWORD (wparam) - 40107, 0, &port);
1871                km_send_to_keyserver (kmi->lv, dlg, host, port);
1872                break;
1873    
1874            case ID_KEYCTX_RECVFROM:
1875                km_refresh_from_keyserver (kmi->lv, dlg);
1876                break;
1877    
1878            case ID_KEYCTX_UID_COPY:
1879                /* XXX: add generic function to support multiple selection
1880                        with a callback */
1881                key = (gpgme_key_t)listview_get_item2 (kmi->lv, kmi->lv_idx);
1882                name = utf8_to_wincp2 (key->uids->uid);
1883                set_clip_text (NULL, name, strlen (name));
1884                safe_free (name);
1885                break;
1886    
1887            case ID_KEYCTX_KEYID_COPY:
1888                key = (gpgme_key_t)listview_get_item2 (kmi->lv, kmi->lv_idx);
1889                set_clip_text (NULL, key->subkeys->keyid+8,
1890                               strlen (key->subkeys->keyid+8));
1891                break;
1892    
1893            case ID_KEYCTX_FPR_COPY:
1894                key = (gpgme_key_t) listview_get_item2 (kmi->lv, kmi->lv_idx);
1895                t = key->subkeys->fpr;
1896                set_clip_text (NULL, t? t : "", t? strlen (t): 0);
1897                break;
1898    
1899            case ID_KEYCTX_KINFO_COPY:
1900                key = (gpgme_key_t) listview_get_item2 (kmi->lv, kmi->lv_idx);
1901                if (!key)
1902                    BUG (NULL);
1903                km_set_clip_info (key->subkeys->keyid+8);
1904                break;
1905    
1906            case ID_KEYCTX_COPY:
1907                send_cmd_id (dlg, ID_KEYMISC_COPY);
1908                break;
1909    
1910            case ID_KEYCTX_PASTE:
1911                send_cmd_id (dlg, ID_KEYMISC_PASTE);
1912                break;
1913    
1914            case ID_KEYCTX_DISABLE:
1915            case ID_KEYCTX_ENABLE:
1916                i = LOWORD (wparam) == ID_KEYCTX_ENABLE? 1 : 0;
1917                key = (gpgme_key_t)listview_get_item2 (kmi->lv, kmi->lv_idx);
1918                if (!key)
1919                    BUG (NULL);
1920                rc = km_enable_disable_key (kmi->lv, dlg, kmi->lv_idx, i);
1921                if (!rc)
1922                    update_key (kmi->lv, kmi->lv_idx, key->subkeys->keyid+8, 0);
1923                /* XXX: switching a key from disabled -> enabled. does not
1924                   change the validity field in the KM. */
1925                break;
1926    
1927            case ID_KEYCTX_LISTSIGS:
1928                send_cmd_id (dlg, ID_KEYMISC_CHECKSIGS);
1929                break;
1930    
1931            case ID_KEYCTX_MAXTRUST:
1932                key = (gpgme_key_t)listview_get_item2 (kmi->lv, kmi->lv_idx);
1933                if (!key)
1934                    BUG (NULL);
1935                rc = km_set_implicit_trust (dlg, kmi->lv, kmi->lv_idx);
1936                if (!rc)
1937                    update_key (kmi->lv, kmi->lv_idx, key->subkeys->keyid+8, 0);
1938                break;
1939    
1940            case ID_KEYCTX_SETDEFKEY:
1941                if (!km_check_key_status (kmi->lv, kmi->lv_idx)) {
1942                    key = (gpgme_key_t)listview_get_item2 (kmi->lv, kmi->lv_idx);
1943                    rc = set_gnupg_default_key (key->subkeys->keyid+8);
1944                    if (rc)
1945                        msg_box (dlg, winpt_strerror (rc), _("Key Manager"), MB_ERR);
1946                    update_default_key_str (kmi->statbar);
1947                }
1948                break;
1949    
1950            case ID_KEYMISC_VIEWKEYID:
1951            case ID_KEYMISC_VIEWCIPHER:
1952            case ID_KEYMISC_VIEWTYPE:
1953            case ID_KEYMISC_VIEWCREAT:
1954                DWORD n;
1955    
1956                hm = GetMenu (dlg);
1957                n = get_menu_state (hm, LOWORD (wparam));
1958                set_menu_state (hm, LOWORD (wparam),
1959                                n & MFS_CHECKED? MFS_UNCHECKED : MFS_CHECKED);
1960                modify_listview_columns (kmi, LOWORD (wparam), !(n & MFS_CHECKED));
1961                break;
1962    
1963            case ID_GROUP_NEW:
1964            case ID_GROUP_PASTE:
1965            case ID_GROUP_DELETE:
1966                /* XXX: Implement group manager. */
1967                return TRUE;
1968    
1969            case IDCANCEL:
1970                EndDialog (dlg, TRUE);
1971                return TRUE;
1972            }
1973            
1974            break;
1975        }
1976        
1977        return FALSE;
1978    }

Legend:
Removed from v.30  
changed lines
  Added in v.164

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26