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

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

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26