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

Diff of /trunk/Src/wptKeyManagerDlg.cpp

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

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

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

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26