1 |
/* OEDlgEncrypt.c - OE encrypt dialog |
2 |
* Copyright (C) 2001, 2002, 2003, 2006 Timo Schulz |
3 |
* |
4 |
* This file is part of GPGOE. |
5 |
* |
6 |
* GPGOE is free software; you can redistribute it and/or modify |
7 |
* it under the terms of the GNU Lesser General Public License as published by |
8 |
* the Free Software Foundation; either version 2.1 of the License, or |
9 |
* (at your option) any later version. |
10 |
* |
11 |
* GPGOE is distributed in the hope that it will be useful, |
12 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
14 |
* GNU General Public License for more details. |
15 |
* |
16 |
* You should have received a copy of the GNU Lesser General Public License |
17 |
* along with GPGOE; if not, write to the Free Software Foundation, |
18 |
* Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA |
19 |
*/ |
20 |
|
21 |
#ifdef HAVE_CONFIG_H |
22 |
#include <config.h> |
23 |
#endif |
24 |
#include <windows.h> |
25 |
#include <commctrl.h> |
26 |
#include <assert.h> |
27 |
#include "resource.h" |
28 |
#include "gpgme.h" |
29 |
#include "GPGOE.h" |
30 |
|
31 |
|
32 |
void add_recipient (recip_list_t *rset, const char *addr, gpgme_key_t key); |
33 |
|
34 |
|
35 |
/* Add a column to the given list view @ctl. */ |
36 |
int |
37 |
listview_add_column (HWND ctl, const char *fieldname, int width, int pos) |
38 |
{ |
39 |
int rc = 0; |
40 |
LV_COLUMN lvc; |
41 |
|
42 |
memset (&lvc, 0, sizeof (lvc)); |
43 |
lvc.mask = LVCF_TEXT | LVCF_WIDTH | LVCF_SUBITEM; |
44 |
lvc.pszText = (char*)fieldname; |
45 |
lvc.cx = width; |
46 |
lvc.fmt = LVCFMT_LEFT; |
47 |
lvc.iSubItem = pos; |
48 |
if (ListView_InsertColumn (ctl, pos, &lvc) == -1) |
49 |
return -1; |
50 |
return 0; |
51 |
} |
52 |
|
53 |
|
54 |
|
55 |
/* Return a human readable key information from @key. */ |
56 |
const char* |
57 |
get_key_info (gpgme_key_t key) |
58 |
{ |
59 |
static char buf[64]; |
60 |
gpgme_subkey_t s; |
61 |
|
62 |
if (key->subkeys->can_encrypt) |
63 |
s = key->subkeys; |
64 |
else |
65 |
s = key->subkeys->next; |
66 |
|
67 |
switch (s->pubkey_algo) { |
68 |
case GPGME_PK_RSA: |
69 |
sprintf (buf, "RSA/%d", s->length); |
70 |
break; |
71 |
case GPGME_PK_ELG_E: |
72 |
sprintf (buf, "ELG/%d", s->length); |
73 |
break; |
74 |
} |
75 |
return buf; |
76 |
} |
77 |
|
78 |
|
79 |
/* Add the given key @key to the list view @ctl. */ |
80 |
int |
81 |
listview_add_key_item (HWND ctl, gpgme_key_t key) |
82 |
{ |
83 |
LVITEM lvi; |
84 |
char *p; |
85 |
|
86 |
if (!key || key->can_encrypt == 0) |
87 |
return 0; |
88 |
if (key->disabled || key->expired || key->revoked) |
89 |
return 0; |
90 |
|
91 |
p = utf8_to_native (key->uids->uid); |
92 |
memset (&lvi, 0, sizeof(lvi)); |
93 |
lvi.mask = LVIF_TEXT|LVIF_PARAM; |
94 |
lvi.lParam = (LPARAM)key; |
95 |
lvi.pszText = p; |
96 |
if (ListView_InsertItem (ctl, &lvi) == -1) { |
97 |
free (p); |
98 |
return -1; |
99 |
} |
100 |
ListView_SetItemText (ctl, 0, 1, key->subkeys->keyid+8); |
101 |
ListView_SetItemText (ctl, 0, 2, (char*)get_key_info (key)); |
102 |
free_if_alloc (p); |
103 |
return 0; |
104 |
} |
105 |
|
106 |
|
107 |
/* Return the stored LPARAM from the item at position @pos. */ |
108 |
LPARAM |
109 |
listview_get_item2 (HWND ctrl, int pos) |
110 |
{ |
111 |
LVITEM lvi; |
112 |
|
113 |
memset (&lvi, 0, sizeof lvi); |
114 |
lvi.mask = LVIF_PARAM; |
115 |
lvi.iItem = pos; |
116 |
ListView_GetItem (ctrl, &lvi); |
117 |
return lvi.lParam; |
118 |
} |
119 |
|
120 |
|
121 |
/* Load the keylist in the list view @ctl. */ |
122 |
static void |
123 |
keylist_load (HWND ctl) |
124 |
{ |
125 |
gpgme_ctx_t ctx; |
126 |
gpgme_key_t key; |
127 |
gpgme_error_t err; |
128 |
|
129 |
ListView_SetExtendedListViewStyle(ctl, LVS_EX_FULLROWSELECT); |
130 |
ListView_SetExtendedListViewStyle(ctl, LVS_EX_CHECKBOXES); |
131 |
listview_add_column (ctl, _("User ID"), 240, 0); |
132 |
listview_add_column (ctl, _("Key ID"), 70, 1); |
133 |
listview_add_column (ctl, _("Algorithm"), 70, 2); |
134 |
|
135 |
err = gpgme_new (&ctx); |
136 |
if (err) |
137 |
return; |
138 |
err = gpgme_op_keylist_start (ctx, NULL, 0); |
139 |
while (!err) { |
140 |
err = gpgme_op_keylist_next (ctx, &key); |
141 |
if (err) |
142 |
break; |
143 |
listview_add_key_item (ctl, key); |
144 |
} |
145 |
gpgme_release (ctx); |
146 |
} |
147 |
|
148 |
|
149 |
/* Dialog initialisation. */ |
150 |
static void |
151 |
on_init_dialog (HWND dlg, UINT msg, WPARAM wparam, LPARAM lparam) |
152 |
{ |
153 |
HWND lb; |
154 |
recip_list_t t; |
155 |
|
156 |
lb = GetDlgItem (dlg, IDC_ENCRYPT_KEYLIST); |
157 |
keylist_load (lb); |
158 |
|
159 |
SetDlgItemText (dlg, IDC_ENCRYPT_RECPMSG, _("Recipients which were NOT found")); |
160 |
lb = GetDlgItem (dlg, IDC_ENCRYPT_INVLIST); |
161 |
for (t=((plugin_ctx_t)lparam)->rset; t; t = t->next) { |
162 |
if (t->key == NULL) |
163 |
SendMessage (lb, LB_ADDSTRING, 0, (LPARAM)(LPCSTR)t->addr); |
164 |
} |
165 |
SetWindowText (dlg, _("Select Recipient for Encryption")); |
166 |
SetForegroundWindow (dlg); |
167 |
} |
168 |
|
169 |
|
170 |
/* Dialog box procedure to select the recipients. */ |
171 |
BOOL CALLBACK |
172 |
encrypt_dlg_proc (HWND dlg, UINT msg, WPARAM wparam, LPARAM lparam) |
173 |
{ |
174 |
static plugin_ctx_t ctx; |
175 |
HWND lb; |
176 |
NMHDR *notify; |
177 |
int i, ncount=0; |
178 |
const char *email; |
179 |
gpgme_key_t key; |
180 |
|
181 |
switch (msg) { |
182 |
case WM_INITDIALOG: |
183 |
ctx = (plugin_ctx_t)lparam; |
184 |
assert (ctx); |
185 |
on_init_dialog (dlg, msg, wparam, lparam); |
186 |
return TRUE; |
187 |
|
188 |
case WM_DESTROY: |
189 |
return FALSE; |
190 |
|
191 |
case WM_NOTIFY: |
192 |
notify = (NMHDR *)lparam; |
193 |
if (notify && notify->code == NM_DBLCLK |
194 |
&& notify->idFrom == IDC_ENCRYPT_KEYLIST) |
195 |
PostMessage (dlg, WM_COMMAND, MAKEWPARAM (IDOK, 0), 0); |
196 |
return TRUE; |
197 |
|
198 |
case WM_SYSCOMMAND: |
199 |
if (LOWORD (wparam) == SC_CLOSE) |
200 |
EndDialog (dlg, 0); |
201 |
return FALSE; |
202 |
|
203 |
case WM_COMMAND: |
204 |
switch (LOWORD (wparam)) { |
205 |
case IDOK: |
206 |
lb = GetDlgItem (dlg, IDC_ENCRYPT_KEYLIST); |
207 |
for (i = 0; i < ListView_GetItemCount (lb); i++) { |
208 |
key = (gpgme_key_t)listview_get_item2 (lb, i); |
209 |
if (key != NULL && ListView_GetCheckState (lb, i)) { |
210 |
email = key->uids->email? key->uids->email : "none"; |
211 |
add_recipient (&ctx->rset, email, key); |
212 |
ncount++; |
213 |
} |
214 |
else |
215 |
gpgme_key_release (key); |
216 |
} |
217 |
EndDialog (dlg, ncount); |
218 |
return TRUE; |
219 |
|
220 |
case IDCANCEL: |
221 |
lb = GetDlgItem (dlg, IDC_ENCRYPT_KEYLIST); |
222 |
for (i=0; i < ListView_GetItemCount (lb); i++) { |
223 |
key = (gpgme_key_t)listview_get_item2 (lb, i); |
224 |
gpgme_key_release (key); |
225 |
} |
226 |
EndDialog (dlg, 0); |
227 |
return FALSE; |
228 |
} |
229 |
break; |
230 |
} |
231 |
return FALSE; |
232 |
} |