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

Annotation of /trunk/Src/wptClipEncryptDlg.cpp

Parent Directory Parent Directory | Revision Log Revision Log


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


1 werner 36 /* wptClipEncryptDlg.cpp - Clipboard encrypt dialog
2 twoaday 328 * Copyright (C) 2000-2006, 2009 Timo Schulz
3 werner 36 * 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 werner 47 #include "resource.h"
25 werner 36 #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 twoaday 197 #include "wptUTF8.h"
37 twoaday 278 #include "StringBuffer.h"
38 werner 36
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 twoaday 256 gpgme_encrypt_flags_t flags;
49 werner 36 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 twoaday 273
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 twoaday 328 conversion of it, so we can safely use the textmode here. */
62 twoaday 273 gpgme_set_textmode (ctx, 1);
63 werner 36 gpgme_set_armor (ctx, 1);
64    
65 twoaday 328 err = gpg_data_utf8_new_from_clipboard (&plain, 0, NULL);
66 werner 36 if (err)
67     goto leave;
68     err = gpgme_data_new (&ciph);
69     if (err)
70 twoaday 197 goto leave;
71 twoaday 328 flags = always_trust? GPGME_ENCRYPT_ALWAYS_TRUST :
72     (gpgme_encrypt_flags_t)0;
73 twoaday 256 err = gpgme_op_encrypt (ctx, rset, flags, plain, ciph);
74 werner 36 *r_ctx = ctx;
75     if (err)
76     goto leave;
77    
78 twoaday 328 gpg_data_release_to_clipboard (ciph, 1);
79 werner 36 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 twoaday 278 StringBuffer p;
100 twoaday 225 const char *keyid;
101     const char *warn = _("key not found");
102 twoaday 278 char *uid;
103 werner 36
104     if (!ctx)
105     return -1;
106     res = gpgme_op_encrypt_result (ctx);
107     if (!res || !res->invalid_recipients)
108     return -1;
109    
110 twoaday 328 p = _("Recipients unusable for encryption:\n");
111 werner 36 for (k = res->invalid_recipients; k; k = k->next) {
112 twoaday 225 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 twoaday 278 p = p + keyid + " : " + uid + "\n";
121 twoaday 197 safe_free (uid);
122 werner 36 }
123 twoaday 278 msg_box (dlg, p.getBuffer (), _("Encryption"), MB_ERR);
124 werner 36 return 0;
125     }
126    
127    
128 twoaday 328 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 werner 36 /* Dialog procedure for the clipboard encryption. */
137     BOOL CALLBACK
138     clip_encrypt_dlg_proc (HWND dlg, UINT msg, WPARAM wparam, LPARAM lparam)
139 twoaday 328 {
140 werner 36 gpg_keycache_t kc;
141     gpgme_key_t *rset;
142     gpgme_ctx_t ctx=NULL;
143     gpgme_error_t err;
144 twoaday 256 char tmpbuf[64];
145 twoaday 328 size_t n;
146 werner 36
147 twoaday 197 switch (msg) {
148 werner 36 case WM_INITDIALOG:
149 twoaday 105 SetWindowText (dlg, _("Encryption"));
150 twoaday 328 SetDlgItemText (dlg, IDC_ENCRYPT_FINDKEY, _("&Find Key:"));
151 twoaday 105 SetDlgItemText (dlg, IDCANCEL, _("&Cancel"));
152 twoaday 328 SetDlgItemText (dlg, IDC_ENCRYPT_FNDCMD, _("&Find"));
153 twoaday 197 kc = keycache_get_ctx (KEYCACHE_PUB);
154 twoaday 328 state.kl = keylist_load (GetDlgItem (dlg, IDC_ENCRYPT_KEYLIST), kc, NULL,
155     KEYLIST_ENCRYPT_MIN, KEY_SORT_USERID);
156    
157 twoaday 197 center_window (dlg, NULL);
158     SetForegroundWindow (dlg);
159 twoaday 328
160     state.enable_redraw = 1;
161     InvalidateRect(GetDlgItem (dlg, IDC_ENCRYPT_KEYLIST), NULL, TRUE);
162 werner 36 return TRUE;
163    
164     case WM_DESTROY:
165 twoaday 328 if (state.kl) {
166     keylist_delete (state.kl);
167     state.kl = NULL;
168 werner 36 }
169 twoaday 328 break;
170 werner 36
171     case WM_NOTIFY:
172 twoaday 328 NMHDR *notify;
173 werner 36 notify = (NMHDR *)lparam;
174 twoaday 328 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 werner 36 if (notify && notify->code == NM_DBLCLK &&
189     notify->idFrom == IDC_ENCRYPT_KEYLIST)
190 twoaday 256 PostMessage( dlg, WM_COMMAND, MAKEWPARAM (IDOK, 0), 0);
191 werner 36 if (notify && notify->code == LVN_COLUMNCLICK &&
192 twoaday 236 notify->idFrom == IDC_ENCRYPT_KEYLIST) {
193 werner 36 NMLISTVIEW *p = (LPNMLISTVIEW) lparam;
194     int sortby = 0;
195    
196 twoaday 220 switch (p->iSubItem) {
197 werner 36 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 twoaday 328 if ((state.keys_sortby & ~KEYLIST_SORT_DESC) == sortby)
204     state.keys_sortby ^= KEYLIST_SORT_DESC;
205 twoaday 236 else
206 twoaday 328 state.keys_sortby = sortby;
207     keylist_sort (state.kl, state.keys_sortby);
208 werner 36 }
209 twoaday 328 break;
210 werner 36
211     case WM_COMMAND:
212 twoaday 328 state.enable_redraw = 0;
213    
214 twoaday 236 switch (LOWORD (wparam)) {
215 werner 36 case IDOK:
216 twoaday 328 int force_trust;
217     rset = keylist_get_recipients (state.kl, &force_trust, &n);
218 werner 36 if (!n) {
219 twoaday 197 msg_box (dlg, _("You must select at least one key."),
220     _("Encryption"), MB_ERR);
221     safe_free (rset);
222 werner 36 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 twoaday 220 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 twoaday 328 break;
237 werner 36
238     case IDCANCEL:
239 twoaday 220 EndDialog (dlg, FALSE);
240 twoaday 328 break;
241 werner 36
242     case IDC_ENCRYPT_FNDCMD:
243     n = GetDlgItemText (dlg, IDC_ENCRYPT_FIND, tmpbuf, DIM (tmpbuf)-1);
244     if (!n)
245     break;
246 twoaday 328 n = listview_find (state.kl->lv, tmpbuf, 0);
247 twoaday 256 if (n != -1) {
248 twoaday 328 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 werner 36 }
252     else
253 twoaday 256 log_box (_("Encryption"), MB_ERR,
254     _("No recipient found with '%s'"), tmpbuf);
255 werner 36 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