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

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

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26