/[gpgoe]/trunk/src/OEDlgEncrypt.c
ViewVC logotype

Contents of /trunk/src/OEDlgEncrypt.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 23 - (show annotations)
Sat Aug 18 10:55:14 2007 UTC (17 years, 8 months ago) by twoaday
File MIME type: text/plain
File size: 6168 byte(s)


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 /* Declare Explorer version to 5.0 for extended listview capabilities. */
25 #ifndef _WIN32_IE
26 #define _WIN32_IE 0x0500
27 #endif
28 #include <windows.h>
29 #include <commctrl.h>
30 #include <assert.h>
31 #include "resource.h"
32 #include "gpgme.h"
33 #include "GPGOE.h"
34
35
36 void add_recipient (recip_list_t *rset, const char *addr, gpgme_key_t key);
37
38
39 /* Add a column to the given list view @ctl. */
40 int
41 listview_add_column (HWND ctl, const char *fieldname, int width, int pos)
42 {
43 LV_COLUMN lvc;
44
45 memset (&lvc, 0, sizeof (lvc));
46 lvc.mask = LVCF_TEXT | LVCF_WIDTH | LVCF_SUBITEM;
47 lvc.pszText = (char*)fieldname;
48 lvc.cx = width;
49 lvc.fmt = LVCFMT_LEFT;
50 lvc.iSubItem = pos;
51 if (ListView_InsertColumn (ctl, pos, &lvc) == -1)
52 return -1;
53 return 0;
54 }
55
56
57
58 /* Return a human readable key information from @key. */
59 const char*
60 get_key_info (gpgme_key_t key)
61 {
62 static char buf[64];
63 gpgme_subkey_t s;
64
65 if (key->subkeys->can_encrypt)
66 s = key->subkeys;
67 else
68 s = key->subkeys->next;
69
70 switch (s->pubkey_algo) {
71 case GPGME_PK_RSA:
72 sprintf (buf, "RSA/%d", s->length);
73 break;
74 case GPGME_PK_ELG_E:
75 sprintf (buf, "ELG/%d", s->length);
76 break;
77
78 default:
79 strcpy (buf, "UNKNOWN/0");
80 }
81 return buf;
82 }
83
84
85 /* Add the given key @key to the list view @ctl. */
86 int
87 listview_add_key_item (HWND ctl, gpgme_key_t key)
88 {
89 LVITEM lvi;
90 char *p;
91
92 if (!key || key->can_encrypt == 0)
93 return 0;
94 if (key->disabled || key->expired || key->revoked)
95 return 0;
96
97 p = utf8_to_native (key->uids->uid);
98 memset (&lvi, 0, sizeof(lvi));
99 lvi.mask = LVIF_TEXT|LVIF_PARAM;
100 lvi.lParam = (LPARAM)key;
101 lvi.pszText = p;
102 if (ListView_InsertItem (ctl, &lvi) == -1) {
103 free (p);
104 return -1;
105 }
106 ListView_SetItemText (ctl, 0, 1, key->subkeys->keyid+8);
107 ListView_SetItemText (ctl, 0, 2, (char*)get_key_info (key));
108 free_if_alloc (p);
109 return 0;
110 }
111
112
113 /* Return the stored LPARAM from the item at position @pos. */
114 LPARAM
115 listview_get_item2 (HWND ctrl, int pos)
116 {
117 LVITEM lvi;
118
119 memset (&lvi, 0, sizeof lvi);
120 lvi.mask = LVIF_PARAM;
121 lvi.iItem = pos;
122 ListView_GetItem (ctrl, &lvi);
123 return lvi.lParam;
124 }
125
126
127 /* Load the keylist in the list view @ctl. */
128 static int
129 keylist_load (HWND ctl)
130 {
131 gpgme_ctx_t ctx;
132 gpgme_key_t key;
133 gpgme_error_t err;
134 int nkeys = 0;
135
136 ListView_SetExtendedListViewStyle(ctl, LVS_EX_FULLROWSELECT);
137 ListView_SetExtendedListViewStyle(ctl, LVS_EX_CHECKBOXES);
138 listview_add_column (ctl, _("User ID"), 240, 0);
139 listview_add_column (ctl, _("Key ID"), 70, 1);
140 listview_add_column (ctl, _("Algorithm"), 70, 2);
141
142 err = gpgme_new (&ctx);
143 if (err)
144 return 0;
145 err = gpgme_op_keylist_start (ctx, NULL, 0);
146 while (!err) {
147 err = gpgme_op_keylist_next (ctx, &key);
148 if (err)
149 break;
150 listview_add_key_item (ctl, key);
151 nkeys++;
152 }
153 gpgme_release (ctx);
154 return nkeys;
155 }
156
157
158 /* Dialog initialisation. */
159 static void
160 on_init_dialog (HWND dlg, UINT msg, WPARAM wparam, LPARAM lparam)
161 {
162 HWND lb;
163 recip_list_t t;
164
165 lb = GetDlgItem (dlg, IDC_ENCRYPT_KEYLIST);
166 if (keylist_load (lb) == 0) {
167 MessageBox (dlg, _("No keys found in the keyring"),
168 _("GPG Plug-in Error"), MB_ICONERROR|MB_OK);
169 EndDialog (dlg, 0);
170 }
171 SetDlgItemText (dlg, IDOK, _("&OK"));
172 SetDlgItemText (dlg, IDCANCEL, _("&Cancel"));
173 SetDlgItemText (dlg, IDC_ENCRYPT_RECPMSG, _("Recipients which were NOT found"));
174 lb = GetDlgItem (dlg, IDC_ENCRYPT_INVLIST);
175 for (t=((plugin_ctx_t)lparam)->rset; t; t = t->next) {
176 if (t->key == NULL)
177 SendMessage (lb, LB_ADDSTRING, 0, (LPARAM)(LPCSTR)t->addr);
178 }
179 SetWindowText (dlg, _("Select Recipient for Encryption"));
180 SetForegroundWindow (dlg);
181 }
182
183
184 /* Dialog box procedure to select the recipients. */
185 BOOL CALLBACK
186 encrypt_dlg_proc (HWND dlg, UINT msg, WPARAM wparam, LPARAM lparam)
187 {
188 static plugin_ctx_t ctx;
189 HWND lb;
190 NMHDR *notify;
191 int i, ncount=0;
192 const char *email;
193 gpgme_key_t key;
194
195 switch (msg) {
196 case WM_INITDIALOG:
197 ctx = (plugin_ctx_t)lparam;
198 assert (ctx);
199 on_init_dialog (dlg, msg, wparam, lparam);
200 center_window (dlg, ctx->main_wnd);
201 return TRUE;
202
203 case WM_NOTIFY:
204 notify = (NMHDR *)lparam;
205 if (notify && notify->code == NM_DBLCLK
206 && notify->idFrom == IDC_ENCRYPT_KEYLIST)
207 PostMessage (dlg, WM_COMMAND, MAKEWPARAM (IDOK, 0), 0);
208 return TRUE;
209
210 case WM_SYSCOMMAND:
211 if (LOWORD (wparam) == SC_CLOSE)
212 EndDialog (dlg, 0);
213 return FALSE;
214
215 case WM_COMMAND:
216 switch (LOWORD (wparam)) {
217 case IDOK:
218 lb = GetDlgItem (dlg, IDC_ENCRYPT_KEYLIST);
219 for (i = 0; i < ListView_GetItemCount (lb); i++) {
220 key = (gpgme_key_t)listview_get_item2 (lb, i);
221 if (key != NULL && ListView_GetCheckState (lb, i)) {
222 email = key->uids->email? key->uids->email : "none";
223 add_recipient (&ctx->rset, email, key);
224 ncount++;
225 }
226 else
227 gpgme_key_release (key);
228 }
229 EndDialog (dlg, ncount);
230 return TRUE;
231
232 case IDCANCEL:
233 lb = GetDlgItem (dlg, IDC_ENCRYPT_KEYLIST);
234 for (i=0; i < ListView_GetItemCount (lb); i++) {
235 key = (gpgme_key_t)listview_get_item2 (lb, i);
236 gpgme_key_release (key);
237 }
238 EndDialog (dlg, 0);
239 return FALSE;
240 }
241 break;
242 }
243 return FALSE;
244 }

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26