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

Legend:
Removed from v.25  
changed lines
  Added in v.366

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26