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

Diff of /trunk/Src/wptKeyManagerDlg.cpp

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

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

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

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26