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

Diff of /trunk/Src/wptKeyManagerDlg.cpp

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

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

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

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26