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

Contents of /trunk/Src/wptClipEncryptDlg.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 241 - (show annotations)
Thu Jun 29 11:15:55 2006 UTC (18 years, 8 months ago) by twoaday
File size: 6792 byte(s)


1 /* wptClipEncryptDlg.cpp - Clipboard encrypt dialog
2 * Copyright (C) 2000-2006 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
22 #ifdef HAVE_CONFIG_H
23 #include <config.h>
24 #endif
25
26 #include <windows.h>
27 #include <commctrl.h>
28
29 #include "resource.h"
30 #include "wptTypes.h"
31 #include "wptW32API.h"
32 #include "wptVersion.h"
33 #include "wptErrors.h"
34 #include "wptCommonCtl.h"
35 #include "wptGPG.h"
36 #include "wptKeylist.h"
37 #include "wptNLS.h"
38 #include "wptContext.h" /* for passphrase_s */
39 #include "wptRegistry.h"
40 #include "wptDlgs.h"
41 #include "wptUTF8.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 gpgme_set_textmode (ctx, 1);
62
63 err = gpg_data_new_from_clipboard (&plain, 0);
64 if (err)
65 goto leave;
66 err = gpgme_data_new (&ciph);
67 if (err)
68 goto leave;
69 err = gpgme_op_encrypt (ctx, rset,
70 always_trust?GPGME_ENCRYPT_ALWAYS_TRUST : (gpgme_encrypt_flags_t)0,
71 plain, ciph);
72 *r_ctx = ctx;
73 if (err)
74 goto leave;
75
76 gpg_data_release_and_set_clipboard (ciph, 1);
77 ciph = NULL;
78
79 leave:
80 if (ciph)
81 gpgme_data_release (ciph);
82 gpgme_data_release (plain);
83 return err;
84 }
85
86
87 /* Show all invalid recipients if possible. @dlg is the
88 handle to the calling dialog and @ctx is the context
89 used for encryption.
90 Return value: 0 if invalid recipients exist -1 otherwise. */
91 static int
92 show_invalid_recipients (HWND dlg, gpgme_ctx_t ctx)
93 {
94 gpgme_encrypt_result_t res;
95 gpgme_invalid_key_t k;
96 gpgme_key_t key;
97 size_t len=0;
98 const char *keyid;
99 const char *warn = _("key not found");
100 char *uid, *p;
101
102 if (!ctx)
103 return -1;
104 res = gpgme_op_encrypt_result (ctx);
105 if (!res || !res->invalid_recipients)
106 return -1;
107
108 for (k=res->invalid_recipients; k; k = k->next) {
109 if (!get_pubkey (k->fpr, &key))
110 len += (32 + strlen (k->fpr)+8 + strlen (key->uids->name) + 2) + 4;
111 else
112 len += strlen (warn) + strlen (k->fpr)+8 + 2 + 4;
113 }
114
115 p = (char *)calloc (1, len+64);
116 if (!p)
117 BUG (NULL);
118 strcpy (p, _("Recipients unsuable for encryption:\n"));
119 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 }
124 else {
125 uid = strdup (warn);
126 keyid = k->fpr;
127 }
128 strcat (p, keyid);
129 strcat (p, " : ");
130 strcat (p, uid);
131 strcat (p, "\n");
132 safe_free (uid);
133 }
134 msg_box (dlg, p, _("Encryption"), MB_ERR);
135 safe_free (p);
136 return 0;
137 }
138
139
140 /* Dialog procedure for the clipboard encryption. */
141 BOOL CALLBACK
142 clip_encrypt_dlg_proc (HWND dlg, UINT msg, WPARAM wparam, LPARAM lparam)
143 {
144 static listview_ctrl_t lv = NULL;
145 static int keys_sortby = 0;
146 gpg_keycache_t kc;
147 gpgme_key_t *rset;
148 gpgme_ctx_t ctx=NULL;
149 gpgme_error_t err;
150 int force_trust = 0;
151 int n;
152
153 switch (msg) {
154 case WM_INITDIALOG:
155 SetWindowText (dlg, _("Encryption"));
156 SetDlgItemText (dlg, IDC_ENCRYPT_FNDCMD, _("&Find"));
157 SetDlgItemText (dlg, IDCANCEL, _("&Cancel"));
158 kc = keycache_get_ctx (KEYCACHE_PUB);
159 lv = keylist_load (GetDlgItem (dlg, IDC_ENCRYPT_KEYLIST), kc, NULL,
160 KEYLIST_ENCRYPT_MIN, KEY_SORT_USERID);
161 center_window (dlg, NULL);
162 SetForegroundWindow (dlg);
163 return TRUE;
164
165 case WM_DESTROY:
166 if (lv) {
167 keylist_delete (lv);
168 lv = NULL;
169 }
170 return FALSE;
171
172 case WM_NOTIFY:
173 NMHDR *notify;
174
175 notify = (NMHDR *)lparam;
176 if (notify && notify->code == NM_DBLCLK &&
177 notify->idFrom == IDC_ENCRYPT_KEYLIST)
178 PostMessage( dlg, WM_COMMAND, MAKEWPARAM( IDOK, 0 ), 0 );
179 if (notify && notify->code == LVN_COLUMNCLICK &&
180 notify->idFrom == IDC_ENCRYPT_KEYLIST) {
181 NMLISTVIEW *p = (LPNMLISTVIEW) lparam;
182 int sortby = 0;
183
184 switch (p->iSubItem) {
185 case 0: sortby = KEY_SORT_USERID; break;
186 case 1: sortby = KEY_SORT_KEYID; break;
187 case 2: sortby = KEY_SORT_LEN; break;
188 case 4: sortby = KEY_SORT_VALIDITY; break;
189 default: sortby = KEY_SORT_USERID; break;
190 }
191 if ((keys_sortby & ~KEYLIST_SORT_DESC) == sortby)
192 keys_sortby ^= KEYLIST_SORT_DESC;
193 else
194 keys_sortby = sortby;
195 keylist_sort (lv, keys_sortby);
196 }
197 return TRUE;
198
199 case WM_COMMAND:
200 switch (LOWORD (wparam)) {
201 case IDOK:
202 rset = keylist_get_recipients (lv, &force_trust, &n);
203 if (!n) {
204 msg_box (dlg, _("You must select at least one key."),
205 _("Encryption"), MB_ERR);
206 safe_free (rset);
207 return FALSE;
208 }
209 err = gpg_clip_encrypt (rset, force_trust, &ctx);
210 if (err) {
211 if (show_invalid_recipients (dlg, ctx))
212 msg_box (dlg, gpgme_strerror (err), _("Encryption"), MB_ERR);
213 }
214 else
215 show_msg (dlg, 1500, _("GnuPG Status: Finished"));
216 safe_free (rset);
217 if (ctx)
218 gpgme_release (ctx);
219 if (!err)
220 EndDialog (dlg, TRUE);
221 return TRUE;
222
223 case IDCANCEL:
224 EndDialog (dlg, FALSE);
225 return FALSE;
226
227 case IDC_ENCRYPT_FNDCMD:
228 {
229 char tmpbuf[64];
230
231 n = GetDlgItemText (dlg, IDC_ENCRYPT_FIND, tmpbuf, DIM (tmpbuf)-1);
232 if (!n)
233 break;
234 n = listview_find (lv, tmpbuf, 0);
235 if (n != -1) {
236 int oldpos = listview_get_curr_pos (lv);
237 listview_select_one (lv, n);
238 listview_scroll (lv, oldpos, n);
239 }
240 else
241 log_box (_("Encryption"), MB_ERR, _("No recipient found with '%s'"), tmpbuf);
242 break;
243 }
244 }
245 break;
246 }
247
248 return FALSE;
249 }
250

Properties

Name Value
svn:eol-style native

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26