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

Legend:
Removed from v.28  
changed lines
  Added in v.218

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26