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

Legend:
Removed from v.24  
changed lines
  Added in v.350

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26