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

Diff of /trunk/Src/wptKeyManagerDlg.cpp

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

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

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

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26