/[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 12 by twoaday, Thu Apr 14 12:56:25 2005 UTC revision 68 by twoaday, Sat Nov 5 12:00:55 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    
21  #include <windows.h>  #ifdef HAVE_CONFIG_H
22  #include <commctrl.h>  #include <config.h>
23    #endif
24  #include "../resource.h"  
25  #include "wptErrors.h"  #include <windows.h>
26  #include "wptAgent.h"  #include <commctrl.h>
27  #include "wptGPG.h"  
28  #include "wptCommonCtl.h"  #include "resource.h"
29  #include "wptKeylist.h"  #include "wptErrors.h"
30  #include "wptTypes.h"  #include "wptAgent.h"
31  #include "wptNLS.h"  #include "wptCrypto.h"
32  #include "wptContext.h" /* for passphrase_s */  #include "wptGPG.h"
33  #include "wptDlgs.h"  #include "wptCommonCtl.h"
34  #include "wptW32API.h"  #include "wptKeylist.h"
35  #include "wptKeylist.h"  #include "wptTypes.h"
36  #include "wptVersion.h"  #include "wptNLS.h"
37  #include "wptGPG.h"  #include "wptContext.h" /* for passphrase_s */
38  #include "wptRegistry.h"  #include "wptDlgs.h"
39  #include "wptUTF8.h"  #include "wptW32API.h"
40    #include "wptKeylist.h"
41    #include "wptVersion.h"
42  BOOL CALLBACK  #include "wptGPG.h"
43  clip_signenc_dlg_proc (HWND dlg, UINT msg, WPARAM wparam, LPARAM lparam)  #include "wptRegistry.h"
44  {  #include "wptUTF8.h"
45      static listview_ctrl_t lv = NULL;  
46      static keylist_t list = NULL;  
47      static int enable = 0;  /* Encrypt the clipboard data with the recipients from @rset and
48      gpgme_ctx_t ctx;     additionally sign the data before @signer as the keyID.
49      gpgme_keycache_t kc;     Return value: 0 on success. */
50      gpgme_recipients_t rset;  gpgme_error_t
51      gpgme_error_t err;  gpg_clip_sign_encrypt (gpgme_ctx_t ctx, const char *signer,
52      passphrase_cb_s pwd;                         gpgme_key_t *rset, int opts)
53      char * signer = NULL;  {
54      int force_trust = 0, opt = 0;      gpgme_error_t err;
55      int kmode = reg_prefs.keylist_mode? KEYLIST_ENCRYPT_MIN : KEYLIST_ENCRYPT;      gpgme_data_t plain = NULL;
56            gpgme_data_t ciph = NULL;
57      switch( msg ) {      gpgme_key_t key = NULL;
58      case WM_INITDIALOG:  
59          enable = 0;      if (!signer)
60  #ifndef LANG_DE          return gpg_error (GPG_ERR_INV_ARG);
61          SetWindowText( dlg, _("Sign & Encrypt") );      if (get_pubkey (signer, &key))
62  #endif          return gpg_error (GPG_ERR_NO_PUBKEY);
63          kc = keycache_get_ctx( KEYCACHE_PUB );  
64          if( !kc )      gpgme_set_armor (ctx, 1);
65              BUG( NULL );      
66          lv = keylist_load( GetDlgItem( dlg, IDC_SIGNENC_KEYLIST ),      err = gpg_data_new_from_clipboard (&plain, 0);
67                             kc, NULL, kmode, GPGME_ATTR_USERID );      if (err)
68          seclist_init( dlg, IDC_SIGNENC_SECLIST, KEYLIST_FLAG_SHORT, &list );          goto leave;  
69          center_window (dlg);      err = gpgme_data_new (&ciph);
70          set_active_window (dlg);      if (err)
71          EnableWindow (GetDlgItem (dlg, IDC_SIGNENC_SECLIST), FALSE);          goto leave;
72          SetDlgItemText (dlg, IDC_SIGNENC_SELKEY, _("Select key for signing"));      err = gpgme_signers_add (ctx, key);
73          SetDlgItemText (dlg, IDC_SIGNENC_SECLISTINF, _("Signing key:"));      if (err)
74          SetForegroundWindow (dlg);          goto leave;
75          return TRUE;  
76                err = gpgme_op_encrypt_sign (ctx, rset,
77      case WM_DESTROY:                                   opts? GPGME_ENCRYPT_ALWAYS_TRUST : (gpgme_encrypt_flags_t)0,
78          seclist_destroy( &list );                                   plain, ciph);
79          reset_active_window( );      if (err)
80          if( lv ) {          goto leave;
81              keylist_delete( lv );  
82              lv = NULL;      gpg_data_release_and_set_clipboard (ciph, 1);
83          }      ciph = NULL;
84          memset( pwd.pwd, 0, sizeof pwd.pwd );  
85          return FALSE;  leave:
86                if (plain)
87      case WM_NOTIFY:          gpgme_data_release (plain);
88          NMHDR * notify;      if (ciph)
89          notify = (NMHDR *)lparam;          gpgme_data_release (ciph);
90          if( notify && notify->code == NM_DBLCLK      return err;
91              && notify->idFrom == IDC_SIGNENC_KEYLIST )  }
92              PostMessage( dlg, WM_COMMAND, MAKEWPARAM(IDOK, 0), NULL );  
93          return TRUE;  
94            /* Dialog procedure for clipboard sign + encrypt. */
95      case WM_SYSCOMMAND:  BOOL CALLBACK
96          if( LOWORD (wparam) == SC_CLOSE )  clip_signenc_dlg_proc (HWND dlg, UINT msg, WPARAM wparam, LPARAM lparam)
97              EndDialog( dlg, TRUE );  {
98          return FALSE;      static listview_ctrl_t lv = NULL;
99                static keylist_t list = NULL;
100      case WM_COMMAND:      gpg_keycache_t kc;
101          /* fixme: the enable seems to have a sync problem */      gpgme_key_t *rset;
102          if( HIWORD( wparam ) == BN_CLICKED      gpgme_error_t err;
103              && LOWORD( wparam ) == IDC_SIGNENC_SELKEY ) {      gpgme_ctx_t ctx;
104              enable ^= 1;      passphrase_cb_s pwd;
105              EnableWindow( GetDlgItem( dlg, IDC_SIGNENC_SECLIST ), enable? TRUE : FALSE );      char *signer = NULL;
106          }      int force_trust = 0;
107          switch( LOWORD( wparam ) ) {      int kmode = reg_prefs.keylist_mode? KEYLIST_ENCRYPT_MIN : KEYLIST_ENCRYPT;
108          case IDOK:      int n;
109              rset = keylist_get_recipients( lv, &force_trust, NULL );      
110              if( !gpgme_recipients_count( rset ) ) {      switch( msg ) {
111                  msg_box( dlg, _("You must select at least one key."), _("Sign & Encrypt"), MB_ERR );      case WM_INITDIALOG:
112                  gpgme_recipients_release( rset );          SetWindowText (dlg, _("Sign & Encrypt"));
113                  return FALSE;  
114              }          kc = keycache_get_ctx( KEYCACHE_PUB );
115              if( IsDlgButtonChecked( dlg, IDC_SIGNENC_SELKEY ) ) {          if( !kc )
116                  gpgme_key_t key;              BUG( NULL );
117                  const char * s;          lv = keylist_load( GetDlgItem( dlg, IDC_SIGNENC_KEYLIST ),
118                               kc, NULL, kmode, KEY_SORT_USERID);
119                  if( seclist_select_key( dlg, IDC_SIGNENC_SECLIST, &key ) ) {          seclist_init( dlg, IDC_SIGNENC_SECLIST, KEYLIST_FLAG_SHORT, &list );
120                      msg_box( dlg, _("No key was selected."), _("Signing"), MB_ERR );          center_window (dlg, NULL);
121                      return FALSE;          set_active_window (dlg);
122                  }          EnableWindow (GetDlgItem (dlg, IDC_SIGNENC_SECLIST), FALSE);
123                  s = gpgme_key_get_string_attr( key, GPGME_ATTR_KEYID, NULL, 0 );          SetDlgItemText (dlg, IDC_SIGNENC_SELKEY, _("Select key for signing"));
124                  if( s )          SetDlgItemText (dlg, IDC_SIGNENC_SECLISTINF, _("Signing key:"));
125                      signer = m_strdup( s+8 );          SetForegroundWindow (dlg);
126              }          return TRUE;
127              else {          
128                  signer = get_gnupg_default_key( );      case WM_DESTROY:
129                  if( !signer ) {          seclist_destroy (&list);
130                      msg_box( dlg, _("Could not get default key."), _("Signing"), MB_ERR );          reset_active_window ();
131                      return FALSE;          if (lv) {
132                  }              keylist_delete (lv);
133              }              lv = NULL;
134              memset( &pwd, 0, sizeof pwd );          }
135              err = gpgme_new( &ctx );          return FALSE;
136              if( err )          
137                  BUG( dlg );      case WM_NOTIFY:
138              gpgme_enable_logging( ctx );          NMHDR * notify;
139              set_gpg_passphrase_cb( ctx, &pwd, GPG_CMD_SIGN, dlg, _("Sign & Encrypt") );          notify = (NMHDR *)lparam;
140              if( force_trust )          if (notify && notify->code == NM_DBLCLK
141                  opt |= GPGME_CTRL_FORCETRUST;              && notify->idFrom == IDC_SIGNENC_KEYLIST)
142              err = gpgme_op_clip_sign_encrypt( ctx, rset, signer, opt );              PostMessage (dlg, WM_COMMAND, MAKEWPARAM (IDOK, 0), 0);
143              memset( pwd.pwd, 0, sizeof pwd.pwd );          return TRUE;
144              gpgme_recipients_release( rset );          
145              free_if_alloc( signer );      case WM_SYSCOMMAND:
146              if( err == GPGME_Bad_Passphrase )              if( LOWORD (wparam) == SC_CLOSE )
147                  agent_del_cache( pwd.keyid );              EndDialog( dlg, TRUE );
148              if( err ) {          return FALSE;
149                  gpgme_show_error( dlg, err, ctx, _("Sign & Encrypt"), MB_ERR );          
150                  gpgme_release( ctx );      case WM_COMMAND:
151                  return FALSE;          if (HIWORD (wparam) == BN_CLICKED
152              }              && LOWORD (wparam) == IDC_SIGNENC_SELKEY) {
153              gpgme_release( ctx );              int enable = IsDlgButtonChecked (dlg, IDC_SIGNENC_SELKEY);
154              show_msg( dlg, 1500, _("GnuPG Status: Finished") );              EnableWindow (GetDlgItem (dlg, IDC_SIGNENC_SECLIST), enable? TRUE : FALSE);
155              EndDialog( dlg, TRUE );          }
156              return TRUE;          switch( LOWORD( wparam ) ) {
157                        case IDOK:
158          case IDCANCEL:              rset = keylist_get_recipients( lv, &force_trust, &n );
159              EndDialog( dlg, FALSE );              if (!n) {
160              return FALSE;                  msg_box( dlg, _("You must select at least one key."), _("Sign & Encrypt"), MB_ERR );
161          }                  return FALSE;
162          break;              }
163      }              if( IsDlgButtonChecked( dlg, IDC_SIGNENC_SELKEY ) ) {
164                        gpgme_key_t key;
165      return FALSE;                  const char * s;
166  } /* clip_signenc_dlg_proc */  
167                    if( seclist_select_key( dlg, IDC_SIGNENC_SECLIST, &key ) ) {
168                        msg_box( dlg, _("No key was selected."), _("Signing"), MB_ERR );
169                        return FALSE;
170                    }
171                    s = key->subkeys->keyid;
172                    if (s)
173                        signer = m_strdup(s+8);
174                }
175                else {
176                    signer = get_gnupg_default_key ();
177                    if (!signer) {
178                        msg_box (dlg, _("Could not get default key."), _("Signing"), MB_ERR);
179                        return FALSE;
180                    }
181                }
182    
183                err = gpgme_new (&ctx);
184                if (err)
185                    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);
188                release_gpg_passphrase_cb (&pwd);
189                free (rset);
190                free_if_alloc (signer);
191                gpgme_release (ctx);
192                if (gpgme_err_code (err) ==  GPG_ERR_BAD_PASSPHRASE)
193                    agent_del_cache (pwd.keyid);
194                if (err) {
195                    msg_box (dlg, gpgme_strerror (err), _("Sign & Encrypt"), MB_ERR );
196                    return FALSE;
197                }
198                show_msg (dlg, 1500, _("GnuPG Status: Finished"));
199                EndDialog (dlg, TRUE);
200                return TRUE;
201                
202            case IDCANCEL:
203                EndDialog( dlg, FALSE );
204                return FALSE;
205            }
206            break;
207        }
208        
209        return FALSE;
210    }

Legend:
Removed from v.12  
changed lines
  Added in v.68

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26