/[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 22 by twoaday, Wed Aug 10 11:33:35 2005 UTC revision 24 by twoaday, Sat Oct 8 10:43:08 2005 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-2005 Timo Schulz
3     *      Copyright (C) 2005 g10 Code GmbH
4   *   *
5   * This file is part of WinPT.   * This file is part of WinPT.
6   *   *
# Line 35  Line 36 
36  #include "wptDlgs.h"  #include "wptDlgs.h"
37    
38    
39    /* Encrypt the contents of the clipboard with the keys in @rset.
40       If @always_trust is set, GPG is forced to trust any recipients.
41       The context is returned in @r_ctx.
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)
45    {
46        gpgme_error_t err;
47        gpgme_ctx_t ctx = NULL;
48        gpgme_data_t plain = NULL;
49        gpgme_data_t ciph = NULL;
50        
51        err = gpgme_new(&ctx);
52        if (err)
53            return err;
54        
55        gpgme_set_armor (ctx, 1);
56        
57        err = gpg_data_new_from_clipboard (&plain, 0);
58        if (err)
59            goto leave;
60        err = gpgme_data_new (&ciph);
61        if (err)
62            goto leave;
63        err = gpgme_op_encrypt (ctx, rset,
64                                always_trust?GPGME_ENCRYPT_ALWAYS_TRUST : (gpgme_encrypt_flags_t)0,
65                                plain, ciph);
66        if (err)
67            goto leave;
68    
69        gpg_data_release_and_set_clipboard (ciph, 1);
70        ciph = NULL;
71        *r_ctx = ctx;
72    
73    leave:
74        if (err) {
75            gpgme_release (ctx);
76            *r_ctx = NULL;
77        }
78        if (ciph)
79            gpgme_data_release (ciph);
80        gpgme_data_release (plain);
81        return err;
82    }
83    
84    
85    /* Show all invalid recipients if possible. @dlg is the
86       handle to the calling dialog and @ctx is the context
87       used for encryption.
88       Return value: 0 if invalid recipients exist -1 otherwise. */
89    static int
90    show_invalid_recipients (HWND dlg, gpgme_ctx_t ctx)
91    {
92        gpgme_encrypt_result_t res;
93        gpgme_invalid_key_t k;
94        gpgme_key_t key;
95        size_t len=0;
96        char *p;
97    
98        res = gpgme_op_encrypt_result (ctx);
99        if (!res || !res->invalid_recipients)
100            return -1;
101    
102        for (k=res->invalid_recipients; k; k = k->next) {
103            get_pubkey (k->fpr, &key);
104            len += (32 + 16 + strlen (key->uids->name) + 2) + 4;
105        }
106    
107        p = (char *)calloc (1, len+64);
108        if (!p)
109            BUG (NULL);
110        sprintf (p, _("Recipients unsuable for encryption:\n"));
111        for (k = res->invalid_recipients; k; k = k->next) {
112            get_pubkey (k->fpr, &key);
113            strcat (p, key->subkeys->keyid);
114            strcat (p, " : ");
115            strcat (p, key->uids->name);
116            strcat (p, "\n");
117        }
118        msg_box (dlg, p, _("Encryption"), MB_ERR);
119        free (p);
120        return 0;
121    }
122    
123    
124    /* Dialog procedure for the clipboard encryption. */
125  BOOL CALLBACK  BOOL CALLBACK
126  clip_encrypt_dlg_proc( HWND dlg, UINT msg, WPARAM wparam, LPARAM lparam )  clip_encrypt_dlg_proc (HWND dlg, UINT msg, WPARAM wparam, LPARAM lparam)
127  {  {
128      static listview_ctrl_t lv = NULL;      static listview_ctrl_t lv = NULL;
129      gpgme_keycache_t kc;      gpgme_keycache_t kc;
130      gpgme_recipients_t rset;      gpgme_key_t *rset;
131      gpgme_ctx_t ctx;      gpgme_ctx_t ctx;
132      gpgme_error_t err;      gpgme_error_t err;
133      refresh_cache_s rcs = {0};      refresh_cache_s rcs = {0};
134      int force_trust = 0, opt = 0;      int force_trust = 0, opt = 0;
135      int kmode = reg_prefs.keylist_mode? KEYLIST_ENCRYPT_MIN : KEYLIST_ENCRYPT;      int kmode = reg_prefs.keylist_mode? KEYLIST_ENCRYPT_MIN : KEYLIST_ENCRYPT;
136        int n;
137            
138      switch( msg ) {      switch( msg ) {
139      case WM_INITDIALOG:      case WM_INITDIALOG:
# Line 53  clip_encrypt_dlg_proc( HWND dlg, UINT ms Line 141  clip_encrypt_dlg_proc( HWND dlg, UINT ms
141          kc = keycache_get_ctx( KEYCACHE_PUB );          kc = keycache_get_ctx( KEYCACHE_PUB );
142          if( !kc )          if( !kc )
143              BUG( NULL );              BUG( NULL );
144          lv = keylist_load( GetDlgItem( dlg, IDC_ENCRYPT_KEYLIST ), kc, NULL,          lv = keylist_load (GetDlgItem( dlg, IDC_ENCRYPT_KEYLIST ), kc, NULL,
145                              kmode, GPGME_ATTR_USERID );                             kmode, KEY_SORT_USERID);
146          center_window( dlg );          center_window( dlg, NULL );
147          SetForegroundWindow( dlg );          SetForegroundWindow( dlg );
148          set_active_window( dlg );          set_active_window( dlg );
149          return TRUE;          return TRUE;
# Line 72  clip_encrypt_dlg_proc( HWND dlg, UINT ms Line 160  clip_encrypt_dlg_proc( HWND dlg, UINT ms
160          NMHDR *notify;          NMHDR *notify;
161                    
162          notify = (NMHDR *)lparam;          notify = (NMHDR *)lparam;
163          if( notify && notify->code == NM_DBLCLK && notify->idFrom == IDC_ENCRYPT_KEYLIST )          if (notify && notify->code == NM_DBLCLK &&
164                notify->idFrom == IDC_ENCRYPT_KEYLIST)
165              PostMessage( dlg, WM_COMMAND, MAKEWPARAM( IDOK, 0 ), NULL );              PostMessage( dlg, WM_COMMAND, MAKEWPARAM( IDOK, 0 ), NULL );
166          if( notify && notify->code == LVN_COLUMNCLICK && notify->idFrom == IDC_ENCRYPT_KEYLIST ) {          if (notify && notify->code == LVN_COLUMNCLICK &&
167                notify->idFrom == IDC_ENCRYPT_KEYLIST ) {
168              NMLISTVIEW *p = (LPNMLISTVIEW) lparam;              NMLISTVIEW *p = (LPNMLISTVIEW) lparam;
169              int sortby = 0;              int sortby = 0;
170    
171              switch( p->iSubItem ) {              switch( p->iSubItem ) {
172              case  0: sortby = GPGME_ATTR_USERID; break;              case  0: sortby = KEY_SORT_USERID; break;
173              case  1: sortby = GPGME_ATTR_KEYID; break;              case  1: sortby = KEY_SORT_KEYID; break;
174              case  2: sortby = GPGME_ATTR_LEN; break;              case  2: sortby = KEY_SORT_LEN; break;
175              case  4: sortby = GPGME_ATTR_VALIDITY; break;              case  4: sortby = KEY_SORT_VALIDITY; break;
176              default: sortby = GPGME_ATTR_USERID; break;              default: sortby = KEY_SORT_USERID; break;
177              }              }
178              keylist_sort( lv, sortby );              keylist_sort( lv, sortby );
179          }          }
# Line 97  clip_encrypt_dlg_proc( HWND dlg, UINT ms Line 187  clip_encrypt_dlg_proc( HWND dlg, UINT ms
187      case WM_COMMAND:      case WM_COMMAND:
188          switch( LOWORD( wparam ) ) {          switch( LOWORD( wparam ) ) {
189          case IDOK:          case IDOK:
190              rset = keylist_get_recipients (lv, &force_trust, NULL);              rset = keylist_get_recipients (lv, &force_trust, &n);
191              if( !gpgme_recipients_count( rset ) ) {              if (!n) {
192                  msg_box( dlg, _("You must select at least one key."), _("Encryption"), MB_ERR );                  msg_box (dlg, _("You must select at least one key."), _("Encryption"), MB_ERR);
193                  gpgme_recipients_release( rset );                  free (rset);
194                  return FALSE;                  return FALSE;
195              }              }
196              if( force_trust )              err = gpg_clip_encrypt (rset, force_trust, &ctx);
197                  opt |= GPGME_CTRL_FORCETRUST;              if (err) {
198              if( reg_prefs.use_tmpfiles )                  if (show_invalid_recipients (dlg, ctx))
199                  opt |= GPGME_CTRL_TMPFILES;                      msg_box (dlg, gpgme_strerror (err), _("Encryption"), MB_ERR);
200              err = gpgme_op_clip_encrypt( rset, opt, &ctx );                  gpgme_release (ctx);
201              if( err && err != GPGME_Inv_Recipients ) {                  free (rset);
                 if( err == GPGME_Internal_GPG_Problem )  
                     gnupg_display_error ();  
                 else  
                     msg_box( dlg, gpgme_strerror( err ), _("Encryption"), MB_ERR );  
                 gpgme_release( ctx );  
                 gpgme_recipients_release( rset );  
202                  return FALSE;                  return FALSE;
203              }              }
             else if( err == GPGME_Inv_Recipients ) {  
                 int ncount = gpgme_recperr_count_items( ctx );  
                 gpgme_error_t code = GPGME_No_Error;  
   
                 while( ncount-- ) {  
                     const char *s = gpgme_recperr_get( ctx, ncount, &code );  
                     if( s )  
                         log_box( _("Encryption"), MB_ERR,  
                                  _("Unusable recipient \"%s\": %s"), s,  
                                  gpgme_strerror( code ) );  
                 }  
             }  
204              else              else
205                  show_msg( dlg, 1500, _("GnuPG Status: Finished") );                  show_msg( dlg, 1500, _("GnuPG Status: Finished") );
206              gpgme_recipients_release( rset );              free (rset);
207              gpgme_release( ctx );              gpgme_release (ctx);
208              EndDialog( dlg, TRUE );              EndDialog (dlg, TRUE);
209              return TRUE;              return TRUE;
210                            
211          case IDCANCEL:          case IDCANCEL:
# Line 163  clip_encrypt_dlg_proc( HWND dlg, UINT ms Line 235  clip_encrypt_dlg_proc( HWND dlg, UINT ms
235      }      }
236            
237      return FALSE;      return FALSE;
238  } /* clip_encrypt_dlg_proc */  }
239    

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

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26