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

Legend:
Removed from v.104  
changed lines
  Added in v.248

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26