1 |
/* wptClipEncryptDlg.cpp - Clipboard encrypt dialog |
/* wptClipEncryptDlg.cpp - Clipboard encrypt dialog |
2 |
* Copyright (C) 2000-2005 Timo Schulz |
* Copyright (C) 2000-2006 Timo Schulz |
3 |
* Copyright (C) 2005 g10 Code GmbH |
* Copyright (C) 2005 g10 Code GmbH |
4 |
* |
* |
5 |
* This file is part of WinPT. |
* This file is part of WinPT. |
38 |
#include "wptContext.h" /* for passphrase_s */ |
#include "wptContext.h" /* for passphrase_s */ |
39 |
#include "wptRegistry.h" |
#include "wptRegistry.h" |
40 |
#include "wptDlgs.h" |
#include "wptDlgs.h" |
41 |
|
#include "wptUTF8.h" |
42 |
|
|
43 |
|
|
44 |
/* Encrypt the contents of the clipboard with the keys in @rset. |
/* Encrypt the contents of the clipboard with the keys in @rset. |
49 |
gpg_clip_encrypt (gpgme_key_t rset[], int always_trust, gpgme_ctx_t *r_ctx) |
gpg_clip_encrypt (gpgme_key_t rset[], int always_trust, gpgme_ctx_t *r_ctx) |
50 |
{ |
{ |
51 |
gpgme_error_t err; |
gpgme_error_t err; |
52 |
|
gpgme_encrypt_flags_t flags; |
53 |
gpgme_ctx_t ctx = NULL; |
gpgme_ctx_t ctx = NULL; |
54 |
gpgme_data_t plain = NULL; |
gpgme_data_t plain = NULL; |
55 |
gpgme_data_t ciph = NULL; |
gpgme_data_t ciph = NULL; |
59 |
return err; |
return err; |
60 |
|
|
61 |
gpgme_set_armor (ctx, 1); |
gpgme_set_armor (ctx, 1); |
62 |
|
gpgme_set_textmode (ctx, 1); |
63 |
|
|
64 |
err = gpg_data_new_from_clipboard (&plain, 0); |
err = gpg_data_new_from_clipboard (&plain, 0); |
65 |
if (err) |
if (err) |
67 |
err = gpgme_data_new (&ciph); |
err = gpgme_data_new (&ciph); |
68 |
if (err) |
if (err) |
69 |
goto leave; |
goto leave; |
70 |
err = gpgme_op_encrypt (ctx, rset, |
flags = always_trust? GPGME_ENCRYPT_ALWAYS_TRUST : (gpgme_encrypt_flags_t)0; |
71 |
always_trust?GPGME_ENCRYPT_ALWAYS_TRUST : (gpgme_encrypt_flags_t)0, |
err = gpgme_op_encrypt (ctx, rset, flags, plain, ciph); |
|
plain, ciph); |
|
72 |
*r_ctx = ctx; |
*r_ctx = ctx; |
73 |
if (err) |
if (err) |
74 |
goto leave; |
goto leave; |
95 |
gpgme_invalid_key_t k; |
gpgme_invalid_key_t k; |
96 |
gpgme_key_t key; |
gpgme_key_t key; |
97 |
size_t len=0; |
size_t len=0; |
98 |
char *p; |
const char *keyid; |
99 |
|
const char *warn = _("key not found"); |
100 |
|
char *uid, *p; |
101 |
|
|
102 |
if (!ctx) |
if (!ctx) |
103 |
return -1; |
return -1; |
106 |
return -1; |
return -1; |
107 |
|
|
108 |
for (k=res->invalid_recipients; k; k = k->next) { |
for (k=res->invalid_recipients; k; k = k->next) { |
109 |
get_pubkey (k->fpr, &key); |
if (!get_pubkey (k->fpr, &key)) |
110 |
len += (32 + 16 + strlen (key->uids->name) + 2) + 4; |
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); |
p = (char *)calloc (1, len+64); |
117 |
BUG (NULL); |
BUG (NULL); |
118 |
strcpy (p, _("Recipients unsuable for encryption:\n")); |
strcpy (p, _("Recipients unsuable for encryption:\n")); |
119 |
for (k = res->invalid_recipients; k; k = k->next) { |
for (k = res->invalid_recipients; k; k = k->next) { |
120 |
get_pubkey (k->fpr, &key); |
if (!get_pubkey (k->fpr, &key)) { |
121 |
strcat (p, key->subkeys->keyid+8); |
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, " : "); |
strcat (p, " : "); |
130 |
strcat (p, key->uids->name); |
strcat (p, uid); |
131 |
strcat (p, "\n"); |
strcat (p, "\n"); |
132 |
|
safe_free (uid); |
133 |
} |
} |
134 |
msg_box (dlg, p, _("Encryption"), MB_ERR); |
msg_box (dlg, p, _("Encryption"), MB_ERR); |
135 |
free (p); |
safe_free (p); |
136 |
return 0; |
return 0; |
137 |
} |
} |
138 |
|
|
142 |
clip_encrypt_dlg_proc (HWND dlg, UINT msg, WPARAM wparam, LPARAM lparam) |
clip_encrypt_dlg_proc (HWND dlg, UINT msg, WPARAM wparam, LPARAM lparam) |
143 |
{ |
{ |
144 |
static listview_ctrl_t lv = NULL; |
static listview_ctrl_t lv = NULL; |
145 |
|
static int keys_sortby = 0; |
146 |
gpg_keycache_t kc; |
gpg_keycache_t kc; |
147 |
gpgme_key_t *rset; |
gpgme_key_t *rset; |
148 |
gpgme_ctx_t ctx=NULL; |
gpgme_ctx_t ctx=NULL; |
149 |
gpgme_error_t err; |
gpgme_error_t err; |
150 |
int force_trust = 0; |
char tmpbuf[64]; |
151 |
int kmode = reg_prefs.keylist_mode? KEYLIST_ENCRYPT_MIN : KEYLIST_ENCRYPT; |
size_t n; |
152 |
int n; |
int force_trust = 0; |
153 |
|
|
154 |
switch( msg ) { |
switch (msg) { |
155 |
case WM_INITDIALOG: |
case WM_INITDIALOG: |
156 |
SetWindowText( dlg, _("Encryption") ); |
SetWindowText (dlg, _("Encryption")); |
157 |
kc = keycache_get_ctx( KEYCACHE_PUB ); |
SetDlgItemText (dlg, IDC_ENCRYPT_FNDCMD, _("&Find")); |
158 |
if( !kc ) |
SetDlgItemText (dlg, IDCANCEL, _("&Cancel")); |
159 |
BUG( NULL ); |
kc = keycache_get_ctx (KEYCACHE_PUB); |
160 |
lv = keylist_load (GetDlgItem( dlg, IDC_ENCRYPT_KEYLIST ), kc, NULL, |
lv = keylist_load (GetDlgItem (dlg, IDC_ENCRYPT_KEYLIST), kc, NULL, |
161 |
kmode, KEY_SORT_USERID); |
KEYLIST_ENCRYPT_MIN, KEY_SORT_USERID); |
162 |
center_window( dlg, NULL ); |
center_window (dlg, NULL); |
163 |
SetForegroundWindow( dlg ); |
SetForegroundWindow (dlg); |
|
set_active_window( dlg ); |
|
164 |
return TRUE; |
return TRUE; |
165 |
|
|
166 |
case WM_DESTROY: |
case WM_DESTROY: |
167 |
reset_active_window( ); |
if (lv) { |
168 |
if( lv ) { |
keylist_delete (lv); |
|
keylist_delete( lv ); |
|
169 |
lv = NULL; |
lv = NULL; |
170 |
} |
} |
171 |
return FALSE; |
return FALSE; |
176 |
notify = (NMHDR *)lparam; |
notify = (NMHDR *)lparam; |
177 |
if (notify && notify->code == NM_DBLCLK && |
if (notify && notify->code == NM_DBLCLK && |
178 |
notify->idFrom == IDC_ENCRYPT_KEYLIST) |
notify->idFrom == IDC_ENCRYPT_KEYLIST) |
179 |
PostMessage( dlg, WM_COMMAND, MAKEWPARAM( IDOK, 0 ), 0); |
PostMessage( dlg, WM_COMMAND, MAKEWPARAM (IDOK, 0), 0); |
180 |
if (notify && notify->code == LVN_COLUMNCLICK && |
if (notify && notify->code == LVN_COLUMNCLICK && |
181 |
notify->idFrom == IDC_ENCRYPT_KEYLIST ) { |
notify->idFrom == IDC_ENCRYPT_KEYLIST) { |
182 |
NMLISTVIEW *p = (LPNMLISTVIEW) lparam; |
NMLISTVIEW *p = (LPNMLISTVIEW) lparam; |
183 |
int sortby = 0; |
int sortby = 0; |
184 |
|
|
185 |
switch( p->iSubItem ) { |
switch (p->iSubItem) { |
186 |
case 0: sortby = KEY_SORT_USERID; break; |
case 0: sortby = KEY_SORT_USERID; break; |
187 |
case 1: sortby = KEY_SORT_KEYID; break; |
case 1: sortby = KEY_SORT_KEYID; break; |
188 |
case 2: sortby = KEY_SORT_LEN; break; |
case 2: sortby = KEY_SORT_LEN; break; |
189 |
case 4: sortby = KEY_SORT_VALIDITY; break; |
case 4: sortby = KEY_SORT_VALIDITY; break; |
190 |
default: sortby = KEY_SORT_USERID; break; |
default: sortby = KEY_SORT_USERID; break; |
191 |
} |
} |
192 |
keylist_sort( lv, sortby ); |
if ((keys_sortby & ~KEYLIST_SORT_DESC) == sortby) |
193 |
|
keys_sortby ^= KEYLIST_SORT_DESC; |
194 |
|
else |
195 |
|
keys_sortby = sortby; |
196 |
|
keylist_sort (lv, keys_sortby); |
197 |
} |
} |
198 |
return TRUE; |
return TRUE; |
199 |
|
|
|
case WM_SYSCOMMAND: |
|
|
if( LOWORD( wparam ) == SC_CLOSE ) |
|
|
EndDialog( dlg, TRUE ); |
|
|
return FALSE; |
|
|
|
|
200 |
case WM_COMMAND: |
case WM_COMMAND: |
201 |
switch( LOWORD( wparam ) ) { |
switch (LOWORD (wparam)) { |
202 |
case IDOK: |
case IDOK: |
203 |
rset = keylist_get_recipients (lv, &force_trust, &n); |
rset = keylist_get_recipients (lv, &force_trust, &n); |
204 |
if (!n) { |
if (!n) { |
205 |
msg_box (dlg, _("You must select at least one key."), _("Encryption"), MB_ERR); |
msg_box (dlg, _("You must select at least one key."), |
206 |
free (rset); |
_("Encryption"), MB_ERR); |
207 |
|
safe_free (rset); |
208 |
return FALSE; |
return FALSE; |
209 |
} |
} |
210 |
err = gpg_clip_encrypt (rset, force_trust, &ctx); |
err = gpg_clip_encrypt (rset, force_trust, &ctx); |
211 |
if (err) { |
if (err) { |
212 |
if (show_invalid_recipients (dlg, ctx)) |
if (show_invalid_recipients (dlg, ctx)) |
213 |
msg_box (dlg, gpgme_strerror (err), _("Encryption"), MB_ERR); |
msg_box (dlg, gpgme_strerror (err), _("Encryption"), MB_ERR); |
|
if (ctx) |
|
|
gpgme_release (ctx); |
|
|
free (rset); |
|
|
return FALSE; |
|
214 |
} |
} |
215 |
else |
else |
216 |
show_msg( dlg, 1500, _("GnuPG Status: Finished") ); |
show_msg (dlg, 1500, _("GnuPG Status: Finished")); |
217 |
free (rset); |
safe_free (rset); |
218 |
gpgme_release (ctx); |
if (ctx) |
219 |
EndDialog (dlg, TRUE); |
gpgme_release (ctx); |
220 |
|
if (!err) |
221 |
|
EndDialog (dlg, TRUE); |
222 |
return TRUE; |
return TRUE; |
223 |
|
|
224 |
case IDCANCEL: |
case IDCANCEL: |
225 |
EndDialog( dlg, FALSE ); |
EndDialog (dlg, FALSE); |
226 |
return FALSE; |
return FALSE; |
227 |
|
|
228 |
case IDC_ENCRYPT_FNDCMD: |
case IDC_ENCRYPT_FNDCMD: |
|
{ |
|
|
char tmpbuf[64]; |
|
|
|
|
229 |
n = GetDlgItemText (dlg, IDC_ENCRYPT_FIND, tmpbuf, DIM (tmpbuf)-1); |
n = GetDlgItemText (dlg, IDC_ENCRYPT_FIND, tmpbuf, DIM (tmpbuf)-1); |
230 |
if (!n) |
if (!n) |
231 |
break; |
break; |
232 |
n = listview_find (lv, tmpbuf); |
n = listview_find (lv, tmpbuf, 0); |
233 |
if (n != -1) { |
if (n != -1) { |
234 |
int oldpos = listview_get_curr_pos (lv); |
int oldpos = listview_get_curr_pos (lv); |
235 |
listview_select_one (lv, n); |
listview_select_one (lv, n); |
236 |
listview_scroll (lv, oldpos, n); |
listview_scroll (lv, oldpos, n); |
237 |
} |
} |
238 |
else |
else |
239 |
log_box (_("Encryption"), MB_ERR, _("No recipient found with '%s'"), tmpbuf); |
log_box (_("Encryption"), MB_ERR, |
240 |
|
_("No recipient found with '%s'"), tmpbuf); |
241 |
break; |
break; |
|
} |
|
242 |
} |
} |
243 |
break; |
break; |
244 |
} |
} |