/[winpt]/trunk/Src/wptClipSignEncDlg.cpp
ViewVC logotype

Diff of /trunk/Src/wptClipSignEncDlg.cpp

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 24 by twoaday, Sat Oct 8 10:43:08 2005 UTC revision 105 by twoaday, Wed Nov 30 10:22:00 2005 UTC
# Line 1  Line 1 
1  /* wptSignEncDlg.cpp - Sign & encrypt dialog  /* wptSignEncDlg.cpp - Sign & encrypt dialog
2   *      Copyright (C) 2000-2005 Timo Schulz   *      Copyright (C) 2000-2005 Timo Schulz
3   *   *
4   * This file is part of WinPT.   * This file is part of WinPT.
5   *   *
6   * WinPT is free software; you can redistribute it and/or modify   * 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   * it under the terms of the GNU General Public License as published by
8   * the Free Software Foundation; either version 2 of the License, or   * the Free Software Foundation; either version 2 of the License, or
9   * (at your option) any later version.   * (at your option) any later version.
10   *   *
11   * WinPT is distributed in the hope that it will be useful,   * WinPT is distributed in the hope that it will be useful,
12   * but WITHOUT ANY WARRANTY; without even the implied warranty of   * but WITHOUT ANY WARRANTY; without even the implied warranty of
13   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14   * GNU General Public License for more details.   * GNU General Public License for more details.
15   *   *
16   * You should have received a copy of the GNU General Public License   * You should have received a copy of the GNU General Public License
17   * along with WinPT; if not, write to the Free Software Foundation,   * along with WinPT; if not, write to the Free Software Foundation,
18   * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA   * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
19   */   */
20    #ifdef HAVE_CONFIG_H
21  #include <windows.h>  #include <config.h>
22  #include <commctrl.h>  #endif
23    
24  #include "../resource.h"  #include <windows.h>
25  #include "wptErrors.h"  #include <commctrl.h>
26  #include "wptAgent.h"  
27  #include "wptGPG.h"  #include "resource.h"
28  #include "wptCommonCtl.h"  #include "wptErrors.h"
29  #include "wptKeylist.h"  #include "wptAgent.h"
30  #include "wptTypes.h"  #include "wptCrypto.h"
31  #include "wptNLS.h"  #include "wptGPG.h"
32  #include "wptContext.h" /* for passphrase_s */  #include "wptCommonCtl.h"
33  #include "wptDlgs.h"  #include "wptKeylist.h"
34  #include "wptW32API.h"  #include "wptTypes.h"
35  #include "wptKeylist.h"  #include "wptNLS.h"
36  #include "wptVersion.h"  #include "wptContext.h" /* for passphrase_s */
37  #include "wptGPG.h"  #include "wptDlgs.h"
38  #include "wptRegistry.h"  #include "wptW32API.h"
39  #include "wptUTF8.h"  #include "wptKeylist.h"
40    #include "wptVersion.h"
41    #include "wptGPG.h"
42  /* Encrypt the clipboard data with the recipients from @rset and  #include "wptRegistry.h"
43     additionally sign the data before @signer as the keyID.  #include "wptUTF8.h"
44     Return value: 0 on success. */  
45  gpgme_error_t  
46  gpg_clip_sign_encrypt (gpgme_ctx_t ctx, const char *signer,  /* Encrypt the clipboard data with the recipients from @rset and
47                         gpgme_key_t *rset, int opts)     additionally sign the data before @signer as the keyID.
48  {     Return value: 0 on success. */
49      gpgme_error_t err;  gpgme_error_t
50      gpgme_data_t plain = NULL;  gpg_clip_sign_encrypt (gpgme_ctx_t ctx, const char *signer,
51      gpgme_data_t ciph = NULL;                         gpgme_key_t *rset, int opts)
52      gpgme_key_t key = NULL;  {
53        gpgme_error_t err;
54      if (!signer)      gpgme_data_t plain = NULL;
55          return gpg_error (GPG_ERR_INV_ARG);      gpgme_data_t ciph = NULL;
56      if (get_pubkey (signer, &key))      gpgme_key_t key = NULL;
57          return gpg_error (GPG_ERR_NO_PUBKEY);  
58        if (!signer)
59      gpgme_set_armor (ctx, 1);          return gpg_error (GPG_ERR_INV_ARG);
60            if (get_pubkey (signer, &key))
61      err = gpg_data_new_from_clipboard (&plain, 0);          return gpg_error (GPG_ERR_NO_PUBKEY);
62      if (err)  
63          goto leave;        gpgme_set_armor (ctx, 1);
64      err = gpgme_data_new (&ciph);      
65      if (err)      err = gpg_data_new_from_clipboard (&plain, 0);
66          goto leave;      if (err)
67      err = gpgme_signers_add (ctx, key);          goto leave;  
68      if (err)      err = gpgme_data_new (&ciph);
69          goto leave;      if (err)
70            goto leave;
71      err = gpgme_op_encrypt_sign (ctx, rset,      err = gpgme_signers_add (ctx, key);
72                                   opts? GPGME_ENCRYPT_ALWAYS_TRUST : (gpgme_encrypt_flags_t)0,      if (err)
73                                   plain, ciph);          goto leave;
74      if (err)  
75          goto leave;      err = gpgme_op_encrypt_sign (ctx, rset,
76                                     opts? GPGME_ENCRYPT_ALWAYS_TRUST : (gpgme_encrypt_flags_t)0,
77      gpg_data_release_and_set_clipboard (ciph, 1);                                   plain, ciph);
78      ciph = NULL;      if (err)
79            goto leave;
80  leave:  
81      if (plain)      gpg_data_release_and_set_clipboard (ciph, 1);
82          gpgme_data_release (plain);      ciph = NULL;
83      if (ciph)  
84          gpgme_data_release (ciph);  leave:
85      gpgme_key_release (key);      if (plain)
86      return err;          gpgme_data_release (plain);
87  }      if (ciph)
88            gpgme_data_release (ciph);
89        return err;
90  /* Dialog procedure for clipboard sign + encrypt. */  }
91  BOOL CALLBACK  
92  clip_signenc_dlg_proc (HWND dlg, UINT msg, WPARAM wparam, LPARAM lparam)  
93  {  /* Dialog procedure for clipboard sign + encrypt. */
94      static listview_ctrl_t lv = NULL;  BOOL CALLBACK
95      static keylist_t list = NULL;  clip_signenc_dlg_proc (HWND dlg, UINT msg, WPARAM wparam, LPARAM lparam)
96      static int enable = 0;  {
97      gpgme_keycache_t kc;      static listview_ctrl_t lv = NULL;
98      gpgme_key_t *rset;      static keylist_t list = NULL;
99      gpgme_error_t err;      gpg_keycache_t kc;
100      gpgme_ctx_t ctx;      gpgme_key_t *rset;
101      passphrase_cb_s pwd;      gpgme_error_t err;
102      char *signer = NULL;      gpgme_ctx_t ctx;
103      int force_trust = 0, opt = 0;      passphrase_cb_s pwd;
104      int kmode = reg_prefs.keylist_mode? KEYLIST_ENCRYPT_MIN : KEYLIST_ENCRYPT;      char *signer = NULL;
105      int n;      int force_trust = 0;
106            int kmode = reg_prefs.keylist_mode? KEYLIST_ENCRYPT_MIN : KEYLIST_ENCRYPT;
107      switch( msg ) {      int n;
108      case WM_INITDIALOG:      
109          enable = 0;      switch( msg ) {
110      #ifndef LANG_DE      case WM_INITDIALOG:
111          SetWindowText( dlg, _("Sign & Encrypt") );          SetWindowText (dlg, _("Sign & Encrypt"));
112      #endif          SetDlgItemText (dlg, IDCANCEL, _("&Cancel"));
113          kc = keycache_get_ctx( KEYCACHE_PUB );          kc = keycache_get_ctx (KEYCACHE_PUB);
114          if( !kc )          if (!kc)
115              BUG( NULL );              BUG (NULL);
116          lv = keylist_load( GetDlgItem( dlg, IDC_SIGNENC_KEYLIST ),          lv = keylist_load (GetDlgItem (dlg, IDC_SIGNENC_KEYLIST),
117                             kc, NULL, kmode, KEY_SORT_USERID);                             kc, NULL, kmode, KEY_SORT_USERID);
118          seclist_init( dlg, IDC_SIGNENC_SECLIST, KEYLIST_FLAG_SHORT, &list );          seclist_init (dlg, IDC_SIGNENC_SECLIST, KEYLIST_FLAG_SHORT, &list);
119          center_window (dlg, NULL);          center_window (dlg, NULL);
120          set_active_window (dlg);          set_active_window (dlg);
121          EnableWindow (GetDlgItem (dlg, IDC_SIGNENC_SECLIST), FALSE);          EnableWindow (GetDlgItem (dlg, IDC_SIGNENC_SECLIST), FALSE);
122          SetDlgItemText (dlg, IDC_SIGNENC_SELKEY, _("Select key for signing"));          SetDlgItemText (dlg, IDC_SIGNENC_SELKEY, _("Select key for signing"));
123          SetDlgItemText (dlg, IDC_SIGNENC_SECLISTINF, _("Signing key:"));          SetDlgItemText (dlg, IDC_SIGNENC_SECLISTINF, _("Signing key:"));
124          SetForegroundWindow (dlg);          SetForegroundWindow (dlg);
125          return TRUE;          return TRUE;
126                    
127      case WM_DESTROY:      case WM_DESTROY:
128          seclist_destroy (&list);          seclist_destroy (&list);
129          reset_active_window ();          reset_active_window ();
130          if (lv) {          if (lv) {
131              keylist_delete( lv );              keylist_delete (lv);
132              lv = NULL;              lv = NULL;
133          }          }
134          return FALSE;          return FALSE;
135                    
136      case WM_NOTIFY:      case WM_NOTIFY:
137          NMHDR * notify;          NMHDR * notify;
138          notify = (NMHDR *)lparam;          notify = (NMHDR *)lparam;
139          if( notify && notify->code == NM_DBLCLK          if (notify && notify->code == NM_DBLCLK
140              && notify->idFrom == IDC_SIGNENC_KEYLIST )              && notify->idFrom == IDC_SIGNENC_KEYLIST)
141              PostMessage( dlg, WM_COMMAND, MAKEWPARAM(IDOK, 0), NULL );              PostMessage (dlg, WM_COMMAND, MAKEWPARAM (IDOK, 0), 0);
142          return TRUE;          return TRUE;
143                    
144      case WM_SYSCOMMAND:      case WM_SYSCOMMAND:
145          if( LOWORD (wparam) == SC_CLOSE )          if (LOWORD (wparam) == SC_CLOSE)
146              EndDialog( dlg, TRUE );              EndDialog (dlg, TRUE);
147          return FALSE;          return FALSE;
148                    
149      case WM_COMMAND:      case WM_COMMAND:
150          /* fixme: the enable seems to have a sync problem */          if (HIWORD (wparam) == BN_CLICKED
151          if( HIWORD( wparam ) == BN_CLICKED              && LOWORD (wparam) == IDC_SIGNENC_SELKEY) {
152              && LOWORD( wparam ) == IDC_SIGNENC_SELKEY ) {              int enable = IsDlgButtonChecked (dlg, IDC_SIGNENC_SELKEY);
153              enable ^= 1;              EnableWindow (GetDlgItem (dlg, IDC_SIGNENC_SECLIST), enable? TRUE : FALSE);
154              EnableWindow( GetDlgItem( dlg, IDC_SIGNENC_SECLIST ), enable? TRUE : FALSE );          }
155          }          switch (LOWORD (wparam)) {
156          switch( LOWORD( wparam ) ) {          case IDOK:
157          case IDOK:              rset = keylist_get_recipients (lv, &force_trust, &n);
158              rset = keylist_get_recipients( lv, &force_trust, &n );              if (!n) {
159              if (!n) {                  msg_box (dlg, _("You must select at least one key."),
160                  msg_box( dlg, _("You must select at least one key."), _("Sign & Encrypt"), MB_ERR );                           _("Sign & Encrypt"), MB_ERR);
161                  return FALSE;                  return FALSE;
162              }              }
163              if( IsDlgButtonChecked( dlg, IDC_SIGNENC_SELKEY ) ) {              if( IsDlgButtonChecked( dlg, IDC_SIGNENC_SELKEY ) ) {
164                  gpgme_key_t key;                  gpgme_key_t key;
165                  const char * s;                  const char * s;
166    
167                  if( seclist_select_key( dlg, IDC_SIGNENC_SECLIST, &key ) ) {                  if( seclist_select_key( dlg, IDC_SIGNENC_SECLIST, &key ) ) {
168                      msg_box( dlg, _("No key was selected."), _("Signing"), MB_ERR );                      msg_box (dlg, _("No key was selected."), _("Signing"), MB_ERR);
169                      return FALSE;                      return FALSE;
170                  }                  }
171                  s = key->subkeys->keyid;                  s = key->subkeys->keyid;
172                  if (s)                  if (s)
173                      signer = m_strdup(s+8);                      signer = m_strdup(s+8);
174              }              }
175              else {              else {
176                  signer = get_gnupg_default_key ();                  signer = get_gnupg_default_key ();
177                  if (!signer) {                  if (!signer) {
178                      msg_box (dlg, _("Could not get default key."), _("Signing"), MB_ERR);                      msg_box (dlg, _("Could not get default key."), _("Signing"), MB_ERR);
179                      return FALSE;                      return FALSE;
180                  }                  }
181              }              }
182    
183              err = gpgme_new (&ctx);              err = gpgme_new (&ctx);
184              if (err)              if (err)
185                  BUG (NULL);                  BUG (NULL);
186              set_gpg_passphrase_cb (&pwd, ctx, GPG_CMD_SIGN, dlg, _("Sign & Encrypt"));  
187              err = gpg_clip_sign_encrypt (ctx, signer, rset, force_trust);              set_gpg_passphrase_cb (&pwd, ctx, GPG_CMD_SIGN, dlg, _("Sign & Encrypt"));
188              release_gpg_passphrase_cb (&pwd);              err = gpg_clip_sign_encrypt (ctx, signer, rset, force_trust);
189              free (rset);              release_gpg_passphrase_cb (&pwd);
190              free_if_alloc (signer);              free (rset);
191              gpgme_release (ctx);              free_if_alloc (signer);
192              if (gpgme_err_code (err) ==  GPG_ERR_BAD_PASSPHRASE)              gpgme_release (ctx);
193                  agent_del_cache (pwd.keyid);              if (gpgme_err_code (err) ==  GPG_ERR_BAD_PASSPHRASE)
194              if (err) {                  agent_del_cache (pwd.keyid);
195                  msg_box (dlg, gpgme_strerror (err), _("Sign & Encrypt"), MB_ERR );              if (err) {
196                  return FALSE;                  msg_box (dlg, gpgme_strerror (err), _("Sign & Encrypt"), MB_ERR );
197              }                  return FALSE;
198              show_msg (dlg, 1500, _("GnuPG Status: Finished"));              }
199              EndDialog (dlg, TRUE);              show_msg (dlg, 1500, _("GnuPG Status: Finished"));
200              return TRUE;              EndDialog (dlg, TRUE);
201                            return TRUE;
202          case IDCANCEL:              
203              EndDialog( dlg, FALSE );          case IDCANCEL:
204              return FALSE;              EndDialog( dlg, FALSE );
205          }              return FALSE;
206          break;          }
207      }          break;
208            }
209      return FALSE;      
210  }      return FALSE;
211    }

Legend:
Removed from v.24  
changed lines
  Added in v.105

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26