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

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26