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

Legend:
Removed from v.2  
changed lines
  Added in v.23

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26