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

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

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26