1 |
/* wptKeyserverSearchDlg.cpp - Keyserver Searching |
2 |
* Copyright (C) 2001-2006 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 |
* You should have received a copy of the GNU General Public License |
17 |
* along with WinPT; if not, write to the Free Software Foundation, |
18 |
* Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA |
19 |
*/ |
20 |
#ifdef HAVE_CONFIG_H |
21 |
#include <config.h> |
22 |
#endif |
23 |
|
24 |
#include <windows.h> |
25 |
#include <stdio.h> |
26 |
|
27 |
#include "resource.h" |
28 |
#include "wptCommonCtl.h" |
29 |
#include "wptKeyserver.h" |
30 |
#include "wptErrors.h" |
31 |
#include "wptTypes.h" |
32 |
#include "wptGPG.h" |
33 |
#include "wptContext.h" /* for passphrase_s */ |
34 |
#include "wptDlgs.h" |
35 |
#include "wptNLS.h" |
36 |
|
37 |
|
38 |
enum { KSS_COL_BITS = 0, KSS_COL_KEYID, KSS_COL_DATE, KSS_COL_UID }; |
39 |
|
40 |
int |
41 |
hkp_recv_key2 (HWND dlg, const char *kserver, WORD port, |
42 |
const char *pattern, int proto, char **r_fpr); |
43 |
|
44 |
|
45 |
/* Initialize dialog. */ |
46 |
static listview_ctrl_t |
47 |
on_init_dialog (HWND dlg, UINT msg, WPARAM wparam, LPARAM lparam) |
48 |
{ |
49 |
struct listview_column_s keysearch[] = { |
50 |
{0, 48, (char *)_("Size")}, |
51 |
{1, 64, (char *)_("Key ID")}, |
52 |
{2, 72, (char *)_("Creation")}, |
53 |
{3, 256, (char *)_("User ID")}, |
54 |
{0, 0, NULL} |
55 |
}; |
56 |
char info[384]; |
57 |
int i; |
58 |
listview_ctrl_t lv = NULL; |
59 |
|
60 |
SetDlgItemText (dlg, IDC_HKPSEARCH_RECV, _("&Receive")); |
61 |
SetDlgItemText (dlg, IDOK, _("&Cancel")); |
62 |
_snprintf (info, sizeof (info) - 1, |
63 |
_("Connect to '%s' to search for \"%s\""), |
64 |
((keyserver_ctx *)lparam)->name, ((keyserver_ctx *)lparam)->pattern); |
65 |
SetDlgItemText (dlg, IDC_HKPSEARCH_INFO, info); |
66 |
SetWindowText (dlg, _("Keyserver Searching")); |
67 |
SetForegroundWindow (dlg); |
68 |
listview_new (&lv); |
69 |
lv->ctrl = 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 |
|
78 |
static int |
79 |
search_hkp_keys (HWND dlg, keyserver_ctx *ksc, listview_ctrl_t lv) |
80 |
{ |
81 |
int rc; |
82 |
int conn_fd = 0; |
83 |
char t[32]; |
84 |
keyserver_key key; |
85 |
|
86 |
rc = kserver_search_begin (ksc->name, ksc->port, ksc->pattern, &conn_fd); |
87 |
if (rc) { |
88 |
msg_box (dlg, winpt_strerror (rc), _("Keyserver Search"), MB_ERR); |
89 |
return rc; |
90 |
} |
91 |
|
92 |
while (!kserver_search_next (conn_fd, &key)) { |
93 |
if (!key.bits) |
94 |
continue; |
95 |
_snprintf (t, sizeof (t) - 1, "%d", key.bits); |
96 |
listview_add_item (lv, " "); |
97 |
listview_add_sub_item (lv, 0, KSS_COL_BITS, t); |
98 |
listview_add_sub_item (lv, 0, KSS_COL_KEYID, key.keyid); |
99 |
listview_add_sub_item (lv, 0, KSS_COL_DATE, key.date); |
100 |
listview_add_sub_item (lv, 0, KSS_COL_UID, key.uid); |
101 |
} |
102 |
|
103 |
kserver_search_end (conn_fd); |
104 |
return 0; |
105 |
} |
106 |
|
107 |
|
108 |
/* Dialog box procedure for keyserver searching. */ |
109 |
BOOL CALLBACK |
110 |
hkpsearch_dlg_proc (HWND dlg, UINT msg, WPARAM wparam, LPARAM lparam) |
111 |
{ |
112 |
static listview_ctrl_t lv = NULL; |
113 |
static keyserver_ctx *ksc; |
114 |
char *fpr = NULL, t[32]; |
115 |
int pos; |
116 |
int rc; |
117 |
|
118 |
switch (msg) { |
119 |
case WM_INITDIALOG: |
120 |
ksc = (keyserver_ctx *)lparam; |
121 |
if (!ksc) |
122 |
dlg_fatal_error (dlg, "Could not get dialog param."); |
123 |
lv = on_init_dialog (dlg, msg, wparam, lparam); |
124 |
rc = search_hkp_keys (dlg, ksc, lv); |
125 |
if (rc) { |
126 |
EndDialog (dlg, FALSE); |
127 |
return FALSE; |
128 |
} |
129 |
return TRUE; |
130 |
|
131 |
case WM_DESTROY: |
132 |
if (lv) { |
133 |
listview_release (lv); |
134 |
lv = NULL; |
135 |
} |
136 |
return FALSE; |
137 |
|
138 |
case WM_SYSCOMMAND: |
139 |
if (LOWORD (wparam) == SC_CLOSE) |
140 |
EndDialog (dlg, TRUE); |
141 |
return FALSE; |
142 |
|
143 |
case WM_COMMAND: |
144 |
switch (LOWORD (wparam)) { |
145 |
case IDOK: |
146 |
EndDialog (dlg, TRUE); |
147 |
return TRUE; |
148 |
|
149 |
case IDCANCEL: |
150 |
EndDialog (dlg, FALSE); |
151 |
return FALSE; |
152 |
|
153 |
case IDC_HKPSEARCH_RECV: |
154 |
if ((pos = listview_get_curr_pos (lv)) == -1) { |
155 |
msg_box (dlg, _("Please select a key."), _("Keyserver Search"), MB_ERR); |
156 |
return FALSE; |
157 |
} |
158 |
listview_get_item_text (lv, pos, 1, t, sizeof (t) -1); |
159 |
rc = hkp_recv_key2 (dlg, ksc->name, ksc->port, t, 0, &fpr); |
160 |
if (!rc && fpr != NULL) { |
161 |
keycache_update (0, fpr); |
162 |
free_if_alloc (fpr); |
163 |
} |
164 |
return TRUE; |
165 |
} |
166 |
break; |
167 |
} |
168 |
|
169 |
return FALSE; |
170 |
} |