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

Diff of /trunk/Src/wptClipEncryptDlg.cpp

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

revision 24 by twoaday, Sat Oct 8 10:43:08 2005 UTC revision 273 by twoaday, Fri Dec 8 10:22:17 2006 UTC
# Line 1  Line 1 
1  /* wptClipEncryptDlg.cpp - Clipboard encrypt dialog  /* wptClipEncryptDlg.cpp - Clipboard encrypt dialog
2   *      Copyright (C) 2000-2005 Timo Schulz   *      Copyright (C) 2000-2006 Timo Schulz
3   *      Copyright (C) 2005 g10 Code GmbH   *      Copyright (C) 2005 g10 Code GmbH
4   *   *
5   * This file is part of WinPT.   * This file is part of WinPT.
6   *   *
7   * WinPT is free software; you can redistribute it and/or modify   * WinPT is free software; you can redistribute it and/or modify
8   * it under the terms of the GNU General Public License as published by   * it under the terms of the GNU General Public License as published by
9   * the Free Software Foundation; either version 2 of the License, or   * the Free Software Foundation; either version 2 of the License, or
10   * (at your option) any later version.   * (at your option) any later version.
11   *   *
12   * WinPT is distributed in the hope that it will be useful,   * WinPT is distributed in the hope that it will be useful,
13   * but WITHOUT ANY WARRANTY; without even the implied warranty of   * but WITHOUT ANY WARRANTY; without even the implied warranty of
14   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15   * GNU General Public License for more details.   * GNU General Public License for more details.
16   *   *
17   * You should have received a copy of the GNU General Public License   * You should have received a copy of the GNU General Public License
18   * along with WinPT; if not, write to the Free Software Foundation,   * along with WinPT; if not, write to the Free Software Foundation,
19   * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA   * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
20   */   */
21    
22  #include <windows.h>  #ifdef HAVE_CONFIG_H
23  #include <commctrl.h>  #include <config.h>
24    #endif
25  #include "../resource.h"  
26  #include "wptTypes.h"  #include <windows.h>
27  #include "wptW32API.h"  #include <commctrl.h>
28  #include "wptVersion.h"  
29  #include "wptErrors.h"  #include "resource.h"
30  #include "wptCommonCtl.h"  #include "wptTypes.h"
31  #include "wptGPG.h"  #include "wptW32API.h"
32  #include "wptKeylist.h"  #include "wptVersion.h"
33  #include "wptNLS.h"  #include "wptErrors.h"
34  #include "wptContext.h" /* for passphrase_s */  #include "wptCommonCtl.h"
35  #include "wptRegistry.h"  #include "wptGPG.h"
36  #include "wptDlgs.h"  #include "wptKeylist.h"
37    #include "wptNLS.h"
38    #include "wptContext.h" /* for passphrase_s */
39  /* Encrypt the contents of the clipboard with the keys in @rset.  #include "wptRegistry.h"
40     If @always_trust is set, GPG is forced to trust any recipients.  #include "wptDlgs.h"
41     The context is returned in @r_ctx.  #include "wptUTF8.h"
42     Return value: 0 on success. */  
43  gpgme_error_t  
44  gpg_clip_encrypt (gpgme_key_t rset[], int always_trust, gpgme_ctx_t *r_ctx)  /* Encrypt the contents of the clipboard with the keys in @rset.
45  {     If @always_trust is set, GPG is forced to trust any recipients.
46      gpgme_error_t err;     The context is returned in @r_ctx.
47      gpgme_ctx_t ctx = NULL;     Return value: 0 on success. */
48      gpgme_data_t plain = NULL;  gpgme_error_t
49      gpgme_data_t ciph = NULL;  gpg_clip_encrypt (gpgme_key_t rset[], int always_trust, gpgme_ctx_t *r_ctx)
50        {
51      err = gpgme_new(&ctx);      gpgme_error_t err;
52      if (err)      gpgme_encrypt_flags_t flags;
53          return err;      gpgme_ctx_t ctx = NULL;
54            gpgme_data_t plain = NULL;
55      gpgme_set_armor (ctx, 1);      gpgme_data_t ciph = NULL;
56            
57      err = gpg_data_new_from_clipboard (&plain, 0);      err = gpgme_new (&ctx);
58      if (err)      if (err)
59          goto leave;          return err;
60      err = gpgme_data_new (&ciph);  
61      if (err)      /* Recently a lot of users got in trouble because they enabled
62          goto leave;         the textmode even for non-text (binary) data. This leads to
63      err = gpgme_op_encrypt (ctx, rset,         serious trouble. But because our clipboard functions only support
64                              always_trust?GPGME_ENCRYPT_ALWAYS_TRUST : (gpgme_encrypt_flags_t)0,         the text format, the returned data is pure text or at least a
65                              plain, ciph);         conversion of it so we can safely use the textmode here. */
66      if (err)      gpgme_set_textmode (ctx, 1);
67          goto leave;      gpgme_set_armor (ctx, 1);
68        
69      gpg_data_release_and_set_clipboard (ciph, 1);      err = gpg_data_new_from_clipboard (&plain, 0);
70      ciph = NULL;      if (err)
71      *r_ctx = ctx;          goto leave;
72        err = gpgme_data_new (&ciph);
73  leave:      if (err)
74      if (err) {          goto leave;
75          gpgme_release (ctx);      flags = always_trust? GPGME_ENCRYPT_ALWAYS_TRUST : (gpgme_encrypt_flags_t)0;
76          *r_ctx = NULL;      err = gpgme_op_encrypt (ctx, rset, flags, plain, ciph);
77      }      *r_ctx = ctx;
78      if (ciph)      if (err)
79          gpgme_data_release (ciph);          goto leave;
80      gpgme_data_release (plain);      
81      return err;      gpg_data_release_and_set_clipboard (ciph, 1);
82  }      ciph = NULL;
83    
84    leave:
85  /* Show all invalid recipients if possible. @dlg is the      if (ciph)
86     handle to the calling dialog and @ctx is the context          gpgme_data_release (ciph);
87     used for encryption.      gpgme_data_release (plain);
88     Return value: 0 if invalid recipients exist -1 otherwise. */      return err;
89  static int  }
90  show_invalid_recipients (HWND dlg, gpgme_ctx_t ctx)  
91  {  
92      gpgme_encrypt_result_t res;  /* Show all invalid recipients if possible. @dlg is the
93      gpgme_invalid_key_t k;     handle to the calling dialog and @ctx is the context
94      gpgme_key_t key;     used for encryption.
95      size_t len=0;     Return value: 0 if invalid recipients exist -1 otherwise. */
96      char *p;  static int
97    show_invalid_recipients (HWND dlg, gpgme_ctx_t ctx)
98      res = gpgme_op_encrypt_result (ctx);  {
99      if (!res || !res->invalid_recipients)      gpgme_encrypt_result_t res;
100          return -1;      gpgme_invalid_key_t k;
101        gpgme_key_t key;
102      for (k=res->invalid_recipients; k; k = k->next) {      size_t len=0;
103          get_pubkey (k->fpr, &key);      const char *keyid;
104          len += (32 + 16 + strlen (key->uids->name) + 2) + 4;      const char *warn = _("key not found");
105      }      char *uid, *p;
106    
107      p = (char *)calloc (1, len+64);      if (!ctx)
108      if (!p)          return -1;
109          BUG (NULL);      res = gpgme_op_encrypt_result (ctx);
110      sprintf (p, _("Recipients unsuable for encryption:\n"));      if (!res || !res->invalid_recipients)
111      for (k = res->invalid_recipients; k; k = k->next) {          return -1;
112          get_pubkey (k->fpr, &key);  
113          strcat (p, key->subkeys->keyid);      for (k=res->invalid_recipients; k; k = k->next) {
114          strcat (p, " : ");          if (!get_pubkey (k->fpr, &key))
115          strcat (p, key->uids->name);              len += (32 + strlen (k->fpr)+8 + strlen (key->uids->name) + 2) + 4;
116          strcat (p, "\n");          else
117      }              len += strlen (warn) + strlen (k->fpr)+8 + 2 + 4;
118      msg_box (dlg, p, _("Encryption"), MB_ERR);      }
119      free (p);  
120      return 0;      p = (char *)calloc (1, len+64);
121  }      if (!p)
122            BUG (NULL);
123        strcpy (p, _("Recipients unsuable for encryption:\n"));
124  /* Dialog procedure for the clipboard encryption. */      for (k = res->invalid_recipients; k; k = k->next) {
125  BOOL CALLBACK          if (!get_pubkey (k->fpr, &key)) {
126  clip_encrypt_dlg_proc (HWND dlg, UINT msg, WPARAM wparam, LPARAM lparam)              uid = utf8_to_native (key->uids->name);
127  {              keyid = key->subkeys->keyid+8;
128      static listview_ctrl_t lv = NULL;          }
129      gpgme_keycache_t kc;          else {
130      gpgme_key_t *rset;              uid = strdup (warn);
131      gpgme_ctx_t ctx;              keyid = k->fpr;
132      gpgme_error_t err;          }
133      refresh_cache_s rcs = {0};          strcat (p, keyid);
134      int force_trust = 0, opt = 0;          strcat (p, " : ");
135      int kmode = reg_prefs.keylist_mode? KEYLIST_ENCRYPT_MIN : KEYLIST_ENCRYPT;          strcat (p, uid);
136      int n;          strcat (p, "\n");
137                safe_free (uid);
138      switch( msg ) {      }
139      case WM_INITDIALOG:      msg_box (dlg, p, _("Encryption"), MB_ERR);
140          SetWindowText( dlg, _("Encryption") );      safe_free (p);
141          kc = keycache_get_ctx( KEYCACHE_PUB );      return 0;
142          if( !kc )  }
143              BUG( NULL );  
144          lv = keylist_load (GetDlgItem( dlg, IDC_ENCRYPT_KEYLIST ), kc, NULL,  
145                             kmode, KEY_SORT_USERID);  /* Dialog procedure for the clipboard encryption. */
146          center_window( dlg, NULL );  BOOL CALLBACK
147          SetForegroundWindow( dlg );  clip_encrypt_dlg_proc (HWND dlg, UINT msg, WPARAM wparam, LPARAM lparam)
148          set_active_window( dlg );  {
149          return TRUE;      static listview_ctrl_t lv = NULL;
150                static int keys_sortby = 0;
151      case WM_DESTROY:      gpg_keycache_t kc;
152          reset_active_window( );      gpgme_key_t *rset;
153          if( lv ) {      gpgme_ctx_t ctx=NULL;
154              keylist_delete( lv );      gpgme_error_t err;
155              lv = NULL;      char tmpbuf[64];
156          }      size_t n;
157          return FALSE;      int force_trust = 0;    
158                
159      case WM_NOTIFY:      switch (msg) {
160          NMHDR *notify;      case WM_INITDIALOG:
161                    SetWindowText (dlg, _("Encryption"));
162          notify = (NMHDR *)lparam;          SetDlgItemText (dlg, IDC_ENCRYPT_FNDCMD, _("&Find"));
163          if (notify && notify->code == NM_DBLCLK &&          SetDlgItemText (dlg, IDCANCEL, _("&Cancel"));
164              notify->idFrom == IDC_ENCRYPT_KEYLIST)          kc = keycache_get_ctx (KEYCACHE_PUB);
165              PostMessage( dlg, WM_COMMAND, MAKEWPARAM( IDOK, 0 ), NULL );          lv = keylist_load (GetDlgItem (dlg, IDC_ENCRYPT_KEYLIST), kc, NULL,
166          if (notify && notify->code == LVN_COLUMNCLICK &&                             KEYLIST_ENCRYPT_MIN, KEY_SORT_USERID);
167              notify->idFrom == IDC_ENCRYPT_KEYLIST ) {          center_window (dlg, NULL);
168              NMLISTVIEW *p = (LPNMLISTVIEW) lparam;          SetForegroundWindow (dlg);
169              int sortby = 0;          return TRUE;
170            
171              switch( p->iSubItem ) {      case WM_DESTROY:
172              case  0: sortby = KEY_SORT_USERID; break;          if (lv) {
173              case  1: sortby = KEY_SORT_KEYID; break;              keylist_delete (lv);
174              case  2: sortby = KEY_SORT_LEN; break;              lv = NULL;
175              case  4: sortby = KEY_SORT_VALIDITY; break;          }
176              default: sortby = KEY_SORT_USERID; break;          return FALSE;
177              }          
178              keylist_sort( lv, sortby );      case WM_NOTIFY:
179          }          NMHDR *notify;
180          return TRUE;          
181                    notify = (NMHDR *)lparam;
182      case WM_SYSCOMMAND:          if (notify && notify->code == NM_DBLCLK &&
183          if( LOWORD( wparam ) == SC_CLOSE )              notify->idFrom == IDC_ENCRYPT_KEYLIST)
184              EndDialog( dlg, TRUE );              PostMessage( dlg, WM_COMMAND, MAKEWPARAM (IDOK, 0), 0);
185          return FALSE;          if (notify && notify->code == LVN_COLUMNCLICK &&
186                        notify->idFrom == IDC_ENCRYPT_KEYLIST) {
187      case WM_COMMAND:              NMLISTVIEW *p = (LPNMLISTVIEW) lparam;
188          switch( LOWORD( wparam ) ) {              int sortby = 0;
189          case IDOK:  
190              rset = keylist_get_recipients (lv, &force_trust, &n);              switch (p->iSubItem) {
191              if (!n) {              case  0: sortby = KEY_SORT_USERID; break;
192                  msg_box (dlg, _("You must select at least one key."), _("Encryption"), MB_ERR);              case  1: sortby = KEY_SORT_KEYID; break;
193                  free (rset);              case  2: sortby = KEY_SORT_LEN; break;
194                  return FALSE;              case  4: sortby = KEY_SORT_VALIDITY; break;
195              }              default: sortby = KEY_SORT_USERID; break;
196              err = gpg_clip_encrypt (rset, force_trust, &ctx);              }
197              if (err) {              if ((keys_sortby & ~KEYLIST_SORT_DESC) == sortby)
198                  if (show_invalid_recipients (dlg, ctx))                  keys_sortby ^= KEYLIST_SORT_DESC;
199                      msg_box (dlg, gpgme_strerror (err), _("Encryption"), MB_ERR);              else
200                  gpgme_release (ctx);                  keys_sortby = sortby;
201                  free (rset);              keylist_sort (lv, keys_sortby);
202                  return FALSE;          }
203              }          return TRUE;
204              else          
205                  show_msg( dlg, 1500, _("GnuPG Status: Finished") );      case WM_COMMAND:
206              free (rset);          switch (LOWORD (wparam)) {
207              gpgme_release (ctx);          case IDOK:
208              EndDialog (dlg, TRUE);              rset = keylist_get_recipients (lv, &force_trust, &n);
209              return TRUE;              if (!n) {
210                                msg_box (dlg, _("You must select at least one key."),
211          case IDCANCEL:                           _("Encryption"), MB_ERR);
212              EndDialog( dlg, FALSE );                  safe_free (rset);
213              return FALSE;                  return FALSE;
214                }
215          case IDC_ENCRYPT_FNDCMD:              err = gpg_clip_encrypt (rset, force_trust, &ctx);
216          {              if (err) {
217              char tmpbuf[64];                  if (show_invalid_recipients (dlg, ctx))
218              int n;                      msg_box (dlg, gpgme_strerror (err), _("Encryption"), MB_ERR);
219                }
220              n = GetDlgItemText (dlg, IDC_ENCRYPT_FIND, tmpbuf, DIM (tmpbuf)-1);              else
221              if (!n)                  show_msg (dlg, 1500, _("GnuPG Status: Finished"));
222                  break;              safe_free (rset);
223              n = listview_find (lv, tmpbuf);              if (ctx)
224              if (n != -1) {                                gpgme_release (ctx);
225                  int oldpos = listview_get_curr_pos (lv);              if (!err)
226                  listview_select_one (lv, n);                  EndDialog (dlg, TRUE);
227                  listview_scroll (lv, oldpos, n);              return TRUE;
228              }              
229              else          case IDCANCEL:
230                  log_box (_("Encryption"), MB_ERR, _("No recipient found with '%s'"), tmpbuf);              EndDialog (dlg, FALSE);
231              break;              return FALSE;
232          }  
233          }          case IDC_ENCRYPT_FNDCMD:
234          break;              n = GetDlgItemText (dlg, IDC_ENCRYPT_FIND, tmpbuf, DIM (tmpbuf)-1);
235      }              if (!n)
236                        break;
237      return FALSE;              n = listview_find (lv, tmpbuf, 0);
238  }              if (n != -1) {
239                    int oldpos = listview_get_curr_pos (lv);
240                    listview_select_one (lv, n);
241                    listview_scroll (lv, oldpos, n);
242                }
243                else
244                    log_box (_("Encryption"), MB_ERR,
245                             _("No recipient found with '%s'"), tmpbuf);
246                break;
247            }
248            break;
249        }
250        
251        return FALSE;
252    }
253    

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

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26