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

Annotation of /trunk/Src/wptClipSignEncDlg.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 25 - (hide annotations)
Wed Oct 12 10:04:26 2005 UTC (19 years, 4 months ago) by twoaday
File size: 6326 byte(s)
First testing phase finished.
Provide bug fixes for a lot of (minor) problems.

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

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26