/[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 25 by twoaday, Wed Oct 12 10:04:26 2005 UTC revision 236 by twoaday, Wed Jun 28 06:59:30 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_ctx_t ctx = NULL;
53          return err;      gpgme_data_t plain = NULL;
54            gpgme_data_t ciph = NULL;
55      gpgme_set_armor (ctx, 1);      
56            err = gpgme_new (&ctx);
57      err = gpg_data_new_from_clipboard (&plain, 0);      if (err)
58      if (err)          return err;
59          goto leave;      
60      err = gpgme_data_new (&ciph);      gpgme_set_armor (ctx, 1);
61      if (err)      gpgme_set_textmode (ctx, 1);
62          goto leave;      
63      err = gpgme_op_encrypt (ctx, rset,      err = gpg_data_new_from_clipboard (&plain, 0);
64                              always_trust?GPGME_ENCRYPT_ALWAYS_TRUST : (gpgme_encrypt_flags_t)0,      if (err)
65                              plain, ciph);          goto leave;
66      *r_ctx = ctx;      err = gpgme_data_new (&ciph);
67      if (err)      if (err)
68          goto leave;          goto leave;
69            err = gpgme_op_encrypt (ctx, rset,
70      gpg_data_release_and_set_clipboard (ciph, 1);                              always_trust?GPGME_ENCRYPT_ALWAYS_TRUST : (gpgme_encrypt_flags_t)0,
71      ciph = NULL;                              plain, ciph);
72        *r_ctx = ctx;
73  leave:      if (err)
74      if (ciph)          goto leave;
75          gpgme_data_release (ciph);      
76      gpgme_data_release (plain);      gpg_data_release_and_set_clipboard (ciph, 1);
77      return err;      ciph = NULL;
78  }  
79    leave:
80        if (ciph)
81  /* Show all invalid recipients if possible. @dlg is the          gpgme_data_release (ciph);
82     handle to the calling dialog and @ctx is the context      gpgme_data_release (plain);
83     used for encryption.      return err;
84     Return value: 0 if invalid recipients exist -1 otherwise. */  }
85  static int  
86  show_invalid_recipients (HWND dlg, gpgme_ctx_t ctx)  
87  {  /* Show all invalid recipients if possible. @dlg is the
88      gpgme_encrypt_result_t res;     handle to the calling dialog and @ctx is the context
89      gpgme_invalid_key_t k;     used for encryption.
90      gpgme_key_t key;     Return value: 0 if invalid recipients exist -1 otherwise. */
91      size_t len=0;  static int
92      char *p;  show_invalid_recipients (HWND dlg, gpgme_ctx_t ctx)
93    {
94      if (!ctx)      gpgme_encrypt_result_t res;
95          return -1;      gpgme_invalid_key_t k;
96      res = gpgme_op_encrypt_result (ctx);      gpgme_key_t key;
97      if (!res || !res->invalid_recipients)      size_t len=0;
98          return -1;      const char *keyid;
99        const char *warn = _("key not found");
100      for (k=res->invalid_recipients; k; k = k->next) {      char *uid, *p;
101          get_pubkey (k->fpr, &key);  
102          len += (32 + 16 + strlen (key->uids->name) + 2) + 4;      if (!ctx)
103      }          return -1;
104        res = gpgme_op_encrypt_result (ctx);
105      p = (char *)calloc (1, len+64);      if (!res || !res->invalid_recipients)
106      if (!p)          return -1;
107          BUG (NULL);  
108      sprintf (p, _("Recipients unsuable for encryption:\n"));      for (k=res->invalid_recipients; k; k = k->next) {
109      for (k = res->invalid_recipients; k; k = k->next) {          if (!get_pubkey (k->fpr, &key))
110          get_pubkey (k->fpr, &key);              len += (32 + strlen (k->fpr)+8 + strlen (key->uids->name) + 2) + 4;
111          strcat (p, key->subkeys->keyid+8);          else
112          strcat (p, " : ");              len += strlen (warn) + strlen (k->fpr)+8 + 2 + 4;
113          strcat (p, key->uids->name);      }
114          strcat (p, "\n");  
115      }      p = (char *)calloc (1, len+64);
116      msg_box (dlg, p, _("Encryption"), MB_ERR);      if (!p)
117      free (p);          BUG (NULL);
118      return 0;      strcpy (p, _("Recipients unsuable for encryption:\n"));
119  }      for (k = res->invalid_recipients; k; k = k->next) {
120            if (!get_pubkey (k->fpr, &key)) {
121                uid = utf8_to_native (key->uids->name);
122  /* Dialog procedure for the clipboard encryption. */              keyid = key->subkeys->keyid+8;
123  BOOL CALLBACK          }
124  clip_encrypt_dlg_proc (HWND dlg, UINT msg, WPARAM wparam, LPARAM lparam)          else {
125  {              uid = strdup (warn);
126      static listview_ctrl_t lv = NULL;              keyid = k->fpr;
127      gpg_keycache_t kc;          }
128      gpgme_key_t *rset;          strcat (p, keyid);
129      gpgme_ctx_t ctx=NULL;          strcat (p, " : ");
130      gpgme_error_t err;          strcat (p, uid);
131      refresh_cache_s rcs = {0};          strcat (p, "\n");
132      int force_trust = 0, opt = 0;          safe_free (uid);
133      int kmode = reg_prefs.keylist_mode? KEYLIST_ENCRYPT_MIN : KEYLIST_ENCRYPT;      }
134      int n;      msg_box (dlg, p, _("Encryption"), MB_ERR);
135            safe_free (p);
136      switch( msg ) {      return 0;
137      case WM_INITDIALOG:  }
138          SetWindowText( dlg, _("Encryption") );  
139          kc = keycache_get_ctx( KEYCACHE_PUB );  
140          if( !kc )  /* Dialog procedure for the clipboard encryption. */
141              BUG( NULL );  BOOL CALLBACK
142          lv = keylist_load (GetDlgItem( dlg, IDC_ENCRYPT_KEYLIST ), kc, NULL,  clip_encrypt_dlg_proc (HWND dlg, UINT msg, WPARAM wparam, LPARAM lparam)
143                             kmode, KEY_SORT_USERID);  {
144          center_window( dlg, NULL );      static listview_ctrl_t lv = NULL;
145          SetForegroundWindow( dlg );      static int keys_sortby = 0;
146          set_active_window( dlg );      gpg_keycache_t kc;
147          return TRUE;      gpgme_key_t *rset;
148                gpgme_ctx_t ctx=NULL;
149      case WM_DESTROY:      gpgme_error_t err;
150          reset_active_window( );      int force_trust = 0;
151          if( lv ) {      int n;
152              keylist_delete( lv );      
153              lv = NULL;      switch (msg) {
154          }      case WM_INITDIALOG:
155          return FALSE;          SetWindowText (dlg, _("Encryption"));
156                    SetDlgItemText (dlg, IDC_ENCRYPT_FNDCMD, _("&Find"));
157      case WM_NOTIFY:          SetDlgItemText (dlg, IDCANCEL, _("&Cancel"));
158          NMHDR *notify;          kc = keycache_get_ctx (KEYCACHE_PUB);
159                    lv = keylist_load (GetDlgItem (dlg, IDC_ENCRYPT_KEYLIST), kc, NULL,
160          notify = (NMHDR *)lparam;                             KEYLIST_ENCRYPT_MIN, KEY_SORT_USERID);
161          if (notify && notify->code == NM_DBLCLK &&          center_window (dlg, NULL);
162              notify->idFrom == IDC_ENCRYPT_KEYLIST)          SetForegroundWindow (dlg);
163              PostMessage( dlg, WM_COMMAND, MAKEWPARAM( IDOK, 0 ), NULL );          return TRUE;
164          if (notify && notify->code == LVN_COLUMNCLICK &&          
165              notify->idFrom == IDC_ENCRYPT_KEYLIST ) {      case WM_DESTROY:
166              NMLISTVIEW *p = (LPNMLISTVIEW) lparam;          if (lv) {
167              int sortby = 0;              keylist_delete (lv);
168                lv = NULL;
169              switch( p->iSubItem ) {          }
170              case  0: sortby = KEY_SORT_USERID; break;          return FALSE;
171              case  1: sortby = KEY_SORT_KEYID; break;          
172              case  2: sortby = KEY_SORT_LEN; break;      case WM_NOTIFY:
173              case  4: sortby = KEY_SORT_VALIDITY; break;          NMHDR *notify;
174              default: sortby = KEY_SORT_USERID; break;          
175              }          notify = (NMHDR *)lparam;
176              keylist_sort( lv, sortby );          if (notify && notify->code == NM_DBLCLK &&
177          }              notify->idFrom == IDC_ENCRYPT_KEYLIST)
178          return TRUE;              PostMessage( dlg, WM_COMMAND, MAKEWPARAM( IDOK, 0 ), 0 );
179                    if (notify && notify->code == LVN_COLUMNCLICK &&
180      case WM_SYSCOMMAND:              notify->idFrom == IDC_ENCRYPT_KEYLIST) {
181          if( LOWORD( wparam ) == SC_CLOSE )              NMLISTVIEW *p = (LPNMLISTVIEW) lparam;
182              EndDialog( dlg, TRUE );              int sortby = 0;
183          return FALSE;  
184                        switch (p->iSubItem) {
185      case WM_COMMAND:              case  0: sortby = KEY_SORT_USERID; break;
186          switch( LOWORD( wparam ) ) {              case  1: sortby = KEY_SORT_KEYID; break;
187          case IDOK:              case  2: sortby = KEY_SORT_LEN; break;
188              rset = keylist_get_recipients (lv, &force_trust, &n);              case  4: sortby = KEY_SORT_VALIDITY; break;
189              if (!n) {              default: sortby = KEY_SORT_USERID; break;
190                  msg_box (dlg, _("You must select at least one key."), _("Encryption"), MB_ERR);              }
191                  free (rset);              if ((keys_sortby & ~KEYLIST_SORT_DESC) == sortby)
192                  return FALSE;                  keys_sortby ^= KEYLIST_SORT_DESC;
193              }              else
194              err = gpg_clip_encrypt (rset, force_trust, &ctx);                  keys_sortby = sortby;
195              if (err) {              keylist_sort (lv, keys_sortby);
196                  if (show_invalid_recipients (dlg, ctx))          }
197                      msg_box (dlg, gpgme_strerror (err), _("Encryption"), MB_ERR);          return TRUE;
198                  if (ctx)          
199                      gpgme_release (ctx);      case WM_COMMAND:
200                  free (rset);          switch (LOWORD (wparam)) {
201                  return FALSE;          case IDOK:
202              }              rset = keylist_get_recipients (lv, &force_trust, &n);
203              else              if (!n) {
204                  show_msg( dlg, 1500, _("GnuPG Status: Finished") );                  msg_box (dlg, _("You must select at least one key."),
205              free (rset);                           _("Encryption"), MB_ERR);
206              gpgme_release (ctx);                  safe_free (rset);
207              EndDialog (dlg, TRUE);                  return FALSE;
208              return TRUE;              }
209                            err = gpg_clip_encrypt (rset, force_trust, &ctx);
210          case IDCANCEL:              if (err) {
211              EndDialog( dlg, FALSE );                  if (show_invalid_recipients (dlg, ctx))
212              return FALSE;                      msg_box (dlg, gpgme_strerror (err), _("Encryption"), MB_ERR);
213                }
214          case IDC_ENCRYPT_FNDCMD:              else
215          {                  show_msg (dlg, 1500, _("GnuPG Status: Finished"));
216              char tmpbuf[64];              safe_free (rset);
217              int n;              if (ctx)
218                    gpgme_release (ctx);
219              n = GetDlgItemText (dlg, IDC_ENCRYPT_FIND, tmpbuf, DIM (tmpbuf)-1);              if (!err)
220              if (!n)                  EndDialog (dlg, TRUE);
221                  break;              return TRUE;
222              n = listview_find (lv, tmpbuf);              
223              if (n != -1) {                        case IDCANCEL:
224                  int oldpos = listview_get_curr_pos (lv);              EndDialog (dlg, FALSE);
225                  listview_select_one (lv, n);              return FALSE;
226                  listview_scroll (lv, oldpos, n);  
227              }          case IDC_ENCRYPT_FNDCMD:
228              else          {
229                  log_box (_("Encryption"), MB_ERR, _("No recipient found with '%s'"), tmpbuf);              char tmpbuf[64];
230              break;  
231          }              n = GetDlgItemText (dlg, IDC_ENCRYPT_FIND, tmpbuf, DIM (tmpbuf)-1);
232          }              if (!n)
233          break;                  break;
234      }              n = listview_find (lv, tmpbuf);
235                    if (n != -1) {              
236      return FALSE;                  int oldpos = listview_get_curr_pos (lv);
237  }                  listview_select_one (lv, n);
238                    listview_scroll (lv, oldpos, n);
239                }
240                else
241                    log_box (_("Encryption"), MB_ERR, _("No recipient found with '%s'"), tmpbuf);
242                break;
243            }
244            }
245            break;
246        }
247        
248        return FALSE;
249    }
250    

Legend:
Removed from v.25  
changed lines
  Added in v.236

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26