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

Contents of /trunk/src/OEDlgEncrypt.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 12 - (show annotations)
Fri Apr 7 10:46:41 2006 UTC (19 years, 1 month ago) by twoaday
File MIME type: text/plain
File size: 5960 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 SetDlgItemText (dlg, IDOK, _("&OK"));
165 SetDlgItemText (dlg, IDCANCEL, _("&Cancel"));
166 SetDlgItemText (dlg, IDC_ENCRYPT_RECPMSG, _("Recipients which were NOT found"));
167 lb = GetDlgItem (dlg, IDC_ENCRYPT_INVLIST);
168 for (t=((plugin_ctx_t)lparam)->rset; t; t = t->next) {
169 if (t->key == NULL)
170 SendMessage (lb, LB_ADDSTRING, 0, (LPARAM)(LPCSTR)t->addr);
171 }
172 SetWindowText (dlg, _("Select Recipient for Encryption"));
173 SetForegroundWindow (dlg);
174 }
175
176
177 /* Dialog box procedure to select the recipients. */
178 BOOL CALLBACK
179 encrypt_dlg_proc (HWND dlg, UINT msg, WPARAM wparam, LPARAM lparam)
180 {
181 static plugin_ctx_t ctx;
182 HWND lb;
183 NMHDR *notify;
184 int i, ncount=0;
185 const char *email;
186 gpgme_key_t key;
187
188 switch (msg) {
189 case WM_INITDIALOG:
190 ctx = (plugin_ctx_t)lparam;
191 assert (ctx);
192 on_init_dialog (dlg, msg, wparam, lparam);
193 return TRUE;
194
195 case WM_NOTIFY:
196 notify = (NMHDR *)lparam;
197 if (notify && notify->code == NM_DBLCLK
198 && notify->idFrom == IDC_ENCRYPT_KEYLIST)
199 PostMessage (dlg, WM_COMMAND, MAKEWPARAM (IDOK, 0), 0);
200 return TRUE;
201
202 case WM_SYSCOMMAND:
203 if (LOWORD (wparam) == SC_CLOSE)
204 EndDialog (dlg, 0);
205 return FALSE;
206
207 case WM_COMMAND:
208 switch (LOWORD (wparam)) {
209 case IDOK:
210 lb = GetDlgItem (dlg, IDC_ENCRYPT_KEYLIST);
211 for (i = 0; i < ListView_GetItemCount (lb); i++) {
212 key = (gpgme_key_t)listview_get_item2 (lb, i);
213 if (key != NULL && ListView_GetCheckState (lb, i)) {
214 email = key->uids->email? key->uids->email : "none";
215 add_recipient (&ctx->rset, email, key);
216 ncount++;
217 }
218 else
219 gpgme_key_release (key);
220 }
221 EndDialog (dlg, ncount);
222 return TRUE;
223
224 case IDCANCEL:
225 lb = GetDlgItem (dlg, IDC_ENCRYPT_KEYLIST);
226 for (i=0; i < ListView_GetItemCount (lb); i++) {
227 key = (gpgme_key_t)listview_get_item2 (lb, i);
228 gpgme_key_release (key);
229 }
230 EndDialog (dlg, 0);
231 return FALSE;
232 }
233 break;
234 }
235 return FALSE;
236 }

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26