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

Diff of /trunk/Src/wptKeyManagerDlg.cpp

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

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

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

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26