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

Annotation of /trunk/Src/wptClipEncryptDlg.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 47 - (hide annotations)
Mon Oct 31 14:04:59 2005 UTC (19 years, 4 months ago) by werner
File size: 6622 byte(s)
Minor changes; compiles now but gettext is still missing.

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

Properties

Name Value
svn:eol-style native

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26