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

Contents of /trunk/src/OEDlgEncrypt.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 11 - (show annotations)
Mon Apr 3 17:10:15 2006 UTC (19 years, 1 month ago) by twoaday
File MIME type: text/plain
File size: 5868 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 #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 LV_COLUMN lvc;
40
41 memset (&lvc, 0, sizeof (lvc));
42 lvc.mask = LVCF_TEXT | LVCF_WIDTH | LVCF_SUBITEM;
43 lvc.pszText = (char*)fieldname;
44 lvc.cx = width;
45 lvc.fmt = LVCFMT_LEFT;
46 lvc.iSubItem = pos;
47 if (ListView_InsertColumn (ctl, pos, &lvc) == -1)
48 return -1;
49 return 0;
50 }
51
52
53
54 /* Return a human readable key information from @key. */
55 const char*
56 get_key_info (gpgme_key_t key)
57 {
58 static char buf[64];
59 gpgme_subkey_t s;
60
61 if (key->subkeys->can_encrypt)
62 s = key->subkeys;
63 else
64 s = key->subkeys->next;
65
66 switch (s->pubkey_algo) {
67 case GPGME_PK_RSA:
68 sprintf (buf, "RSA/%d", s->length);
69 break;
70 case GPGME_PK_ELG_E:
71 sprintf (buf, "ELG/%d", s->length);
72 break;
73 }
74 return buf;
75 }
76
77
78 /* Add the given key @key to the list view @ctl. */
79 int
80 listview_add_key_item (HWND ctl, gpgme_key_t key)
81 {
82 LVITEM lvi;
83 char *p;
84
85 if (!key || key->can_encrypt == 0)
86 return 0;
87 if (key->disabled || key->expired || key->revoked)
88 return 0;
89
90 p = utf8_to_native (key->uids->uid);
91 memset (&lvi, 0, sizeof(lvi));
92 lvi.mask = LVIF_TEXT|LVIF_PARAM;
93 lvi.lParam = (LPARAM)key;
94 lvi.pszText = p;
95 if (ListView_InsertItem (ctl, &lvi) == -1) {
96 free (p);
97 return -1;
98 }
99 ListView_SetItemText (ctl, 0, 1, key->subkeys->keyid+8);
100 ListView_SetItemText (ctl, 0, 2, (char*)get_key_info (key));
101 free_if_alloc (p);
102 return 0;
103 }
104
105
106 /* Return the stored LPARAM from the item at position @pos. */
107 LPARAM
108 listview_get_item2 (HWND ctrl, int pos)
109 {
110 LVITEM lvi;
111
112 memset (&lvi, 0, sizeof lvi);
113 lvi.mask = LVIF_PARAM;
114 lvi.iItem = pos;
115 ListView_GetItem (ctrl, &lvi);
116 return lvi.lParam;
117 }
118
119
120 /* Load the keylist in the list view @ctl. */
121 static int
122 keylist_load (HWND ctl)
123 {
124 gpgme_ctx_t ctx;
125 gpgme_key_t key;
126 gpgme_error_t err;
127 int nkeys = 0;
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 0;
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 nkeys++;
145 }
146 gpgme_release (ctx);
147 return nkeys;
148 }
149
150
151 /* Dialog initialisation. */
152 static void
153 on_init_dialog (HWND dlg, UINT msg, WPARAM wparam, LPARAM lparam)
154 {
155 HWND lb;
156 recip_list_t t;
157
158 lb = GetDlgItem (dlg, IDC_ENCRYPT_KEYLIST);
159 if (keylist_load (lb) == 0) {
160 MessageBox (dlg, _("No keys found in the keyring"),
161 _("GPG Plug-in Error"), MB_ICONERROR|MB_OK);
162 EndDialog (dlg, 0);
163 }
164
165 SetDlgItemText (dlg, IDC_ENCRYPT_RECPMSG, _("Recipients which were NOT found"));
166 lb = GetDlgItem (dlg, IDC_ENCRYPT_INVLIST);
167 for (t=((plugin_ctx_t)lparam)->rset; t; t = t->next) {
168 if (t->key == NULL)
169 SendMessage (lb, LB_ADDSTRING, 0, (LPARAM)(LPCSTR)t->addr);
170 }
171 SetWindowText (dlg, _("Select Recipient for Encryption"));
172 SetForegroundWindow (dlg);
173 }
174
175
176 /* Dialog box procedure to select the recipients. */
177 BOOL CALLBACK
178 encrypt_dlg_proc (HWND dlg, UINT msg, WPARAM wparam, LPARAM lparam)
179 {
180 static plugin_ctx_t ctx;
181 HWND lb;
182 NMHDR *notify;
183 int i, ncount=0;
184 const char *email;
185 gpgme_key_t key;
186
187 switch (msg) {
188 case WM_INITDIALOG:
189 ctx = (plugin_ctx_t)lparam;
190 assert (ctx);
191 on_init_dialog (dlg, msg, wparam, lparam);
192 return TRUE;
193
194 case WM_NOTIFY:
195 notify = (NMHDR *)lparam;
196 if (notify && notify->code == NM_DBLCLK
197 && notify->idFrom == IDC_ENCRYPT_KEYLIST)
198 PostMessage (dlg, WM_COMMAND, MAKEWPARAM (IDOK, 0), 0);
199 return TRUE;
200
201 case WM_SYSCOMMAND:
202 if (LOWORD (wparam) == SC_CLOSE)
203 EndDialog (dlg, 0);
204 return FALSE;
205
206 case WM_COMMAND:
207 switch (LOWORD (wparam)) {
208 case IDOK:
209 lb = GetDlgItem (dlg, IDC_ENCRYPT_KEYLIST);
210 for (i = 0; i < ListView_GetItemCount (lb); i++) {
211 key = (gpgme_key_t)listview_get_item2 (lb, i);
212 if (key != NULL && ListView_GetCheckState (lb, i)) {
213 email = key->uids->email? key->uids->email : "none";
214 add_recipient (&ctx->rset, email, key);
215 ncount++;
216 }
217 else
218 gpgme_key_release (key);
219 }
220 EndDialog (dlg, ncount);
221 return TRUE;
222
223 case IDCANCEL:
224 lb = GetDlgItem (dlg, IDC_ENCRYPT_KEYLIST);
225 for (i=0; i < ListView_GetItemCount (lb); i++) {
226 key = (gpgme_key_t)listview_get_item2 (lb, i);
227 gpgme_key_release (key);
228 }
229 EndDialog (dlg, 0);
230 return FALSE;
231 }
232 break;
233 }
234 return FALSE;
235 }

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26