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

Contents of /trunk/Src/wptClipEncryptDlg.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 328 - (show annotations)
Fri Sep 25 16:07:38 2009 UTC (15 years, 5 months ago) by twoaday
File size: 7277 byte(s)


1 /* wptClipEncryptDlg.cpp - Clipboard encrypt dialog
2 * Copyright (C) 2000-2006, 2009 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 #ifdef HAVE_CONFIG_H
18 #include <config.h>
19 #endif
20
21 #include <windows.h>
22 #include <commctrl.h>
23
24 #include "resource.h"
25 #include "wptTypes.h"
26 #include "wptW32API.h"
27 #include "wptVersion.h"
28 #include "wptErrors.h"
29 #include "wptCommonCtl.h"
30 #include "wptGPG.h"
31 #include "wptKeylist.h"
32 #include "wptNLS.h"
33 #include "wptContext.h" /* for passphrase_s */
34 #include "wptRegistry.h"
35 #include "wptDlgs.h"
36 #include "wptUTF8.h"
37 #include "StringBuffer.h"
38
39
40 /* Encrypt the contents of the clipboard with the keys in @rset.
41 If @always_trust is set, GPG is forced to trust any recipients.
42 The context is returned in @r_ctx.
43 Return value: 0 on success. */
44 gpgme_error_t
45 gpg_clip_encrypt (gpgme_key_t rset[], int always_trust, gpgme_ctx_t *r_ctx)
46 {
47 gpgme_error_t err;
48 gpgme_encrypt_flags_t flags;
49 gpgme_ctx_t ctx = NULL;
50 gpgme_data_t plain = NULL;
51 gpgme_data_t ciph = NULL;
52
53 err = gpgme_new (&ctx);
54 if (err)
55 return err;
56
57 /* Recently a lot of users got in trouble because they enabled
58 the textmode even for non-text (binary) data. This leads to
59 serious trouble. But because our clipboard functions only support
60 the text format, the returned data is pure text or at least a
61 conversion of it, so we can safely use the textmode here. */
62 gpgme_set_textmode (ctx, 1);
63 gpgme_set_armor (ctx, 1);
64
65 err = gpg_data_utf8_new_from_clipboard (&plain, 0, NULL);
66 if (err)
67 goto leave;
68 err = gpgme_data_new (&ciph);
69 if (err)
70 goto leave;
71 flags = always_trust? GPGME_ENCRYPT_ALWAYS_TRUST :
72 (gpgme_encrypt_flags_t)0;
73 err = gpgme_op_encrypt (ctx, rset, flags, plain, ciph);
74 *r_ctx = ctx;
75 if (err)
76 goto leave;
77
78 gpg_data_release_to_clipboard (ciph, 1);
79 ciph = NULL;
80
81 leave:
82 if (ciph)
83 gpgme_data_release (ciph);
84 gpgme_data_release (plain);
85 return err;
86 }
87
88
89 /* Show all invalid recipients if possible. @dlg is the
90 handle to the calling dialog and @ctx is the context
91 used for encryption.
92 Return value: 0 if invalid recipients exist -1 otherwise. */
93 static int
94 show_invalid_recipients (HWND dlg, gpgme_ctx_t ctx)
95 {
96 gpgme_encrypt_result_t res;
97 gpgme_invalid_key_t k;
98 gpgme_key_t key;
99 StringBuffer p;
100 const char *keyid;
101 const char *warn = _("key not found");
102 char *uid;
103
104 if (!ctx)
105 return -1;
106 res = gpgme_op_encrypt_result (ctx);
107 if (!res || !res->invalid_recipients)
108 return -1;
109
110 p = _("Recipients unusable for encryption:\n");
111 for (k = res->invalid_recipients; k; k = k->next) {
112 if (!get_pubkey (k->fpr, &key)) {
113 uid = utf8_to_native (key->uids->name);
114 keyid = key->subkeys->keyid+8;
115 }
116 else {
117 uid = strdup (warn);
118 keyid = k->fpr;
119 }
120 p = p + keyid + " : " + uid + "\n";
121 safe_free (uid);
122 }
123 msg_box (dlg, p.getBuffer (), _("Encryption"), MB_ERR);
124 return 0;
125 }
126
127
128 struct dlg_ctx_s {
129 int keys_sortby;
130 int enable_redraw;
131 keylist_ctrl_t kl;
132 };
133 // FIXME: associate the context with the window
134 static struct dlg_ctx_s state;
135
136 /* Dialog procedure for the clipboard encryption. */
137 BOOL CALLBACK
138 clip_encrypt_dlg_proc (HWND dlg, UINT msg, WPARAM wparam, LPARAM lparam)
139 {
140 gpg_keycache_t kc;
141 gpgme_key_t *rset;
142 gpgme_ctx_t ctx=NULL;
143 gpgme_error_t err;
144 char tmpbuf[64];
145 size_t n;
146
147 switch (msg) {
148 case WM_INITDIALOG:
149 SetWindowText (dlg, _("Encryption"));
150 SetDlgItemText (dlg, IDC_ENCRYPT_FINDKEY, _("&Find Key:"));
151 SetDlgItemText (dlg, IDCANCEL, _("&Cancel"));
152 SetDlgItemText (dlg, IDC_ENCRYPT_FNDCMD, _("&Find"));
153 kc = keycache_get_ctx (KEYCACHE_PUB);
154 state.kl = keylist_load (GetDlgItem (dlg, IDC_ENCRYPT_KEYLIST), kc, NULL,
155 KEYLIST_ENCRYPT_MIN, KEY_SORT_USERID);
156
157 center_window (dlg, NULL);
158 SetForegroundWindow (dlg);
159
160 state.enable_redraw = 1;
161 InvalidateRect(GetDlgItem (dlg, IDC_ENCRYPT_KEYLIST), NULL, TRUE);
162 return TRUE;
163
164 case WM_DESTROY:
165 if (state.kl) {
166 keylist_delete (state.kl);
167 state.kl = NULL;
168 }
169 break;
170
171 case WM_NOTIFY:
172 NMHDR *notify;
173 notify = (NMHDR *)lparam;
174 if (!notify)
175 break;
176
177 /*
178 if (state.enable_redraw) {
179 int ret;
180 ret = keylist_listview_notify (dlg, state.kl->keys,
181 IDC_ENCRYPT_KEYLIST, lparam);
182 if (ret != 0) {
183 SetWindowLong (dlg, DWL_MSGRESULT, ret);
184 return TRUE;
185 }
186 }*/
187
188 if (notify && notify->code == NM_DBLCLK &&
189 notify->idFrom == IDC_ENCRYPT_KEYLIST)
190 PostMessage( dlg, WM_COMMAND, MAKEWPARAM (IDOK, 0), 0);
191 if (notify && notify->code == LVN_COLUMNCLICK &&
192 notify->idFrom == IDC_ENCRYPT_KEYLIST) {
193 NMLISTVIEW *p = (LPNMLISTVIEW) lparam;
194 int sortby = 0;
195
196 switch (p->iSubItem) {
197 case 0: sortby = KEY_SORT_USERID; break;
198 case 1: sortby = KEY_SORT_KEYID; break;
199 case 2: sortby = KEY_SORT_LEN; break;
200 case 4: sortby = KEY_SORT_VALIDITY; break;
201 default: sortby = KEY_SORT_USERID; break;
202 }
203 if ((state.keys_sortby & ~KEYLIST_SORT_DESC) == sortby)
204 state.keys_sortby ^= KEYLIST_SORT_DESC;
205 else
206 state.keys_sortby = sortby;
207 keylist_sort (state.kl, state.keys_sortby);
208 }
209 break;
210
211 case WM_COMMAND:
212 state.enable_redraw = 0;
213
214 switch (LOWORD (wparam)) {
215 case IDOK:
216 int force_trust;
217 rset = keylist_get_recipients (state.kl, &force_trust, &n);
218 if (!n) {
219 msg_box (dlg, _("You must select at least one key."),
220 _("Encryption"), MB_ERR);
221 safe_free (rset);
222 return FALSE;
223 }
224 err = gpg_clip_encrypt (rset, force_trust, &ctx);
225 if (err) {
226 if (show_invalid_recipients (dlg, ctx))
227 msg_box (dlg, gpgme_strerror (err), _("Encryption"), MB_ERR);
228 }
229 else
230 show_msg (dlg, 1500, _("GnuPG Status: Finished"));
231 safe_free (rset);
232 if (ctx)
233 gpgme_release (ctx);
234 if (!err)
235 EndDialog (dlg, TRUE);
236 break;
237
238 case IDCANCEL:
239 EndDialog (dlg, FALSE);
240 break;
241
242 case IDC_ENCRYPT_FNDCMD:
243 n = GetDlgItemText (dlg, IDC_ENCRYPT_FIND, tmpbuf, DIM (tmpbuf)-1);
244 if (!n)
245 break;
246 n = listview_find (state.kl->lv, tmpbuf, 0);
247 if (n != -1) {
248 int oldpos = listview_get_curr_pos (state.kl->lv);
249 listview_select_one (state.kl->lv, n);
250 listview_scroll (state.kl->lv, oldpos, n);
251 }
252 else
253 log_box (_("Encryption"), MB_ERR,
254 _("No recipient found with '%s'"), tmpbuf);
255 break;
256 }
257 break;
258 }
259
260 return FALSE;
261 }
262

Properties

Name Value
svn:eol-style native

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26