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

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

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26