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

Diff of /trunk/Src/wptKeyManagerDlg.cpp

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

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

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

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26