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

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

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26