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

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

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26