/[winpt]/trunk/Src/wptKeyserverSearchDlg.cpp
ViewVC logotype

Contents of /trunk/Src/wptKeyserverSearchDlg.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 328 - (show annotations)
Fri Sep 25 16:07:38 2009 UTC (15 years, 5 months ago) by twoaday
File size: 5567 byte(s)


1 /* wptKeyserverSearchDlg.cpp - Keyserver Searching
2 * Copyright (C) 2001-2007 Timo Schulz
3 *
4 * This file is part of WinPT.
5 *
6 * WinPT is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * WinPT 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 #ifdef HAVE_CONFIG_H
17 #include <config.h>
18 #endif
19
20 #include <windows.h>
21 #include <stdio.h>
22
23 #include "resource.h"
24 #include "wptCommonCtl.h"
25 #include "wptGPG.h"
26 #include "wptKeyserver.h"
27 #include "wptErrors.h"
28 #include "wptTypes.h"
29 #include "wptDlgs.h"
30 #include "wptNLS.h"
31 #include "wptKeylist.h"
32
33
34 /* Default threshold for key items. */
35 #define MAX_N_KEYS 64
36
37 /* Symbolic column IDs. */
38 enum { KSS_COL_BITS = 0, KSS_COL_ALGO,
39 KSS_COL_KEYID, KSS_COL_DATE, KSS_COL_UID };
40
41 int hkp_recv_key2 (HWND dlg, const char *kserver, WORD port,
42 const char *pattern, int proto, char **r_fpr);
43
44 /* Initialize dialog. */
45 static listview_ctrl_t
46 on_init_dialog (HWND dlg, UINT msg, WPARAM wparam, LPARAM lparam)
47 {
48 struct listview_column_s keysearch[] = {
49 {0, 48, (char *)_("Size")},
50 {1, 48, (char*)_("Algorithm")},
51 {2, 80, (char *)_("Key ID")},
52 {3, 72, (char *)_("Creation")},
53 {4, 256, (char *)_("User ID")},
54 {0, 0, NULL}
55 };
56 keyserver_ctx *parm = (keyserver_ctx*)lparam;
57 listview_ctrl_t lv = NULL;
58 char info[300];
59 int i;
60
61 SetDlgItemText (dlg, IDC_HKPSEARCH_RECV, _("&Receive"));
62 SetDlgItemText (dlg, IDOK, _("&Cancel"));
63 _snprintf (info, DIM (info) - 1,
64 _("Connect to '%s' to search for \"%s\""),
65 parm->name, parm->pattern);
66 SetDlgItemText (dlg, IDC_HKPSEARCH_INFO, info);
67 SetWindowText (dlg, _("Keyserver Searching"));
68 SetForegroundWindow (dlg);
69 listview_new (&lv, GetDlgItem (dlg, IDC_HKPSEARCH_LIST));
70 for (i = 0; keysearch[i].width; i++)
71 listview_add_column (lv, &keysearch[i]);
72 listview_set_ext_style (lv);
73 return lv;
74 }
75
76
77 /* Begin the key serach and add all results to the list view @lv.
78 Return value: 0 on success. */
79 static int
80 search_hkp_keys (HWND dlg, keyserver_ctx *ksc, listview_ctrl_t lv)
81 {
82 keyserver_key_s *key;
83 const char *uid;
84 char bits[32], algo[32], keyid[32];
85 size_t nkeys=0;
86 int conn_fd = 0;
87 int rc;
88
89 rc = kserver_search_begin (ksc->name, ksc->port, ksc->pattern,
90 &conn_fd, &nkeys);
91 if (rc) {
92 msg_box (dlg, winpt_strerror (rc), _("Keyserver Search"), MB_ERR);
93 return rc;
94 }
95
96 if (nkeys > MAX_N_KEYS) {
97 rc = log_box (_("Keyserver Search"), MB_INFO_ASK,
98 _("The search result contains a lot of keys: %d\n\n"
99 "Do you really want to continue?"), nkeys);
100 if (rc == IDNO) {
101 kserver_search_end (conn_fd);
102 return -1;
103 }
104 }
105
106 while (nkeys-- > 0) {
107 if (kserver_search_next (conn_fd, &key))
108 break;
109 if (!key)
110 continue;
111
112 _snprintf (bits, DIM (bits) - 1, "%d", key->bits);
113 _snprintf (algo, DIM (algo)-1, "%s",
114 get_key_pubalgo ((gpgme_pubkey_algo_t)key->algo));
115 _snprintf (keyid, DIM (keyid)-1, "0x%s", key->keyid);
116 uid = key->main_uid? key->main_uid->uid : _("No User ID found");
117 listview_add_item (lv, " ");
118 listview_add_sub_item (lv, 0, KSS_COL_BITS, bits);
119 listview_add_sub_item (lv, 0, KSS_COL_ALGO, algo);
120 listview_add_sub_item (lv, 0, KSS_COL_KEYID, keyid);
121 listview_add_sub_item (lv, 0, KSS_COL_DATE, get_key_created (key->creation));
122 listview_add_sub_item (lv, 0, KSS_COL_UID, uid);
123 kserver_release_key (key); key=NULL;
124 }
125
126 kserver_search_end (conn_fd);
127 return 0;
128 }
129
130
131 /* Fetch all selected keys from the keyserver in @ksc. */
132 static void
133 fetch_keys (HWND dlg, listview_ctrl_t lv, keyserver_ctx *ksc)
134 {
135 char keyid[32];
136 char *fpr;
137 int n, rc;
138
139 n = listview_count_items (lv, 1);
140 if (!n) {
141 msg_box (dlg, _("Please select a key."), _("Keyserver Search"), MB_ERR);
142 return;
143 }
144
145 rc = 0;
146 for (n=0; n < listview_count_items (lv, 0); n++) {
147 if (!listview_get_item_state (lv, n))
148 continue;
149
150 listview_get_item_text (lv, n, KSS_COL_KEYID, keyid, DIM (keyid)-1);
151 rc = hkp_recv_key2 (dlg, ksc->name, ksc->port, keyid, 0, &fpr);
152 if (!rc && fpr != NULL)
153 keycache_update (0, fpr);
154 free_if_alloc (fpr);
155 }
156 }
157
158
159 /* Dialog box procedure for keyserver searching. */
160 BOOL CALLBACK
161 hkpsearch_dlg_proc (HWND dlg, UINT msg, WPARAM wparam, LPARAM lparam)
162 {
163 static listview_ctrl_t lv = NULL;
164 static keyserver_ctx *ksc;
165
166 switch (msg) {
167 case WM_INITDIALOG:
168 ksc = (keyserver_ctx *)lparam;
169 if (!ksc)
170 BUG (0);
171 lv = on_init_dialog (dlg, msg, wparam, lparam);
172 if (search_hkp_keys (dlg, ksc, lv)) {
173 EndDialog (dlg, FALSE);
174 return TRUE;
175 }
176 return TRUE;
177
178 case WM_DESTROY:
179 if (lv) {
180 listview_release (lv);
181 lv = NULL;
182 }
183 return FALSE;
184
185 case WM_COMMAND:
186 switch (LOWORD (wparam)) {
187 case IDOK:
188 EndDialog (dlg, TRUE);
189 return TRUE;
190
191 case IDCANCEL:
192 EndDialog (dlg, FALSE);
193 return TRUE;
194
195 case IDC_HKPSEARCH_RECV:
196 fetch_keys (dlg, lv, ksc);
197 return TRUE;
198 }
199 break;
200 }
201
202 return FALSE;
203 }

Properties

Name Value
svn:eol-style native

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26