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

Legend:
Removed from v.23  
changed lines
  Added in v.255

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26