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

Legend:
Removed from v.22  
changed lines
  Added in v.205

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26