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

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

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26