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

Legend:
Removed from v.24  
changed lines
  Added in v.229

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26