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

Contents of /trunk/Src/wptProxySettingsDlg.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 271 - (show annotations)
Sun Nov 5 08:57:45 2006 UTC (18 years, 3 months ago) by twoaday
File size: 6989 byte(s)


1 /* wptProxySettingsDlg.cpp - Dialog for the proxy settings
2 * Copyright (C) 2002-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 <ctype.h>
26 #include <stdio.h>
27
28 #include "resource.h"
29 #include "wptTypes.h"
30 #include "wptGPG.h"
31 #include "wptKeyserver.h"
32 #include "wptW32API.h"
33 #include "wptErrors.h"
34 #include "wptNLS.h"
35 #include "wptRegistry.h"
36
37 #define combo_get_sel(dlg, id) \
38 SendDlgItemMessage ((dlg), (id), CB_GETCURSEL, 0, 0)
39
40
41 static int
42 set_reg_entry_keyserver_int (const char *key, int val)
43 {
44 char buf[32];
45
46 _snprintf (buf, DIM (buf)-1, "%d", val);
47 return set_reg_entry_keyserver (key, buf);
48 }
49
50
51 /* Save the proxy settings in the registry. */
52 int
53 set_reg_proxy_prefs (keyserver_proxy_t prox)
54 {
55 int rc;
56
57 rc = set_reg_entry_keyserver_int ("Proto", prox->proto);
58 if (!rc)
59 rc = set_reg_entry_keyserver ("Host", prox->host? prox->host : "");
60 if (!rc)
61 rc = set_reg_entry_keyserver_int ("Port", prox->port);
62 if (!rc)
63 rc = set_reg_entry_keyserver ("User", prox->user? prox->user : "");
64 if (!rc)
65 rc = set_reg_entry_keyserver ("Pass", prox->pass? prox->pass : "");
66 return rc;
67 }
68
69
70 /* Check if the edit control (@id) contains a valid number. */
71 static int
72 check_number (HWND dlg, int id)
73 {
74 char buf[32];
75 size_t i;
76
77 i = GetDlgItemText (dlg, id, buf, DIM (buf)-1);
78 if (!i)
79 return -1;
80 for (i = 0; i < strlen (buf); i++) {
81 if (!isdigit (buf[i]))
82 return -1;
83 }
84 return 0;
85 }
86
87
88 static int
89 read_proxy (HWND dlg, keyserver_proxy_t ctx)
90 {
91 char t[512];
92 int ncount = 0, pos = 0;
93
94 ncount = GetDlgItemText (dlg, IDC_PROXY_PWD, t, DIM (t)-1);
95 if (ncount > 0)
96 ctx->pass = m_strdup (t);
97 ncount = GetDlgItemText (dlg, IDC_PROXY_USER, t, DIM (t)-1);
98 if (ncount > 0)
99 ctx->user = m_strdup (t);
100 ncount = GetDlgItemText (dlg, IDC_PROXY_HOST, t, DIM (t)-1);
101 if (ncount > 0) {
102 if (check_IP_or_hostname (t)) {
103 msg_box (dlg, _("Invalid host/IP address."), _("Proxy Settings"), MB_ERR);
104 return -1;
105 }
106 /* XXX: check prefix */
107 if (!strncmp (t, "http://", 7))
108 pos = 7;
109 ctx->host = m_strdup (t + pos);
110 }
111 else {
112 msg_box (dlg, _("Please enter the proxy hostname."),
113 _("Proxy Settings"), MB_ERR);
114 return -1;
115 }
116
117 if (check_number (dlg, IDC_PROXY_PORT)) {
118 msg_box (dlg, _("Invalid port number."), _("Proxy Settings"), MB_ERR);
119 return -1;
120 }
121
122 ctx->port = GetDlgItemInt (dlg, IDC_PROXY_PORT, NULL, FALSE);
123 if (ctx->port < 0 || ctx->port > 65535) {
124 msg_box (dlg, _("Please select a value from 0-65535 for the port"),
125 _("Proxy Settings"), MB_INFO);
126 return -1;
127 }
128 if (ctx->user && !ctx->pass || !ctx->user && ctx->pass) {
129 msg_box (dlg, _("When you want to use authentication, "
130 "please fill out both fields."), _("Proxy Settings"), MB_ERR);
131 return -1;
132 }
133 if (ctx->host && !ctx->port || !ctx->host && ctx->port) {
134 msg_box (dlg, _("Please enter a host name and a port."),
135 _("Proxy Settings"), MB_INFO);
136 return -1;
137 }
138 ctx->proto = combo_get_sel (dlg, IDC_PROXY_PROTO);
139 return 0;
140 }
141
142
143 /* Enable or disable the authentication elements based on
144 the value @val. */
145 static void
146 enable_proxy_auth (HWND dlg, int val)
147 {
148 int mode = val? TRUE : FALSE;
149 EnableWindow (GetDlgItem (dlg, IDC_PROXY_PWD), mode);
150 EnableWindow (GetDlgItem (dlg, IDC_PROXY_USER), mode);
151 }
152
153
154 /* Fill in all valid proxy protocols. */
155 static void
156 init_proxy_protocols (HWND dlg)
157 {
158 HWND cb = GetDlgItem (dlg, IDC_PROXY_PROTO);
159 combox_add_string (cb, (char *)"NONE");
160 combox_add_string (cb, (char *)"HTTP");
161 /*XXX: combox_add_string (cb, (char *)"SOCKS5");*/
162 SendMessage (cb, CB_SETCURSEL, (WPARAM)0, 0);
163 }
164
165
166 /* Dialog box procedure for proxy authentication. */
167 BOOL CALLBACK
168 keyserver_proxy_dlg_proc (HWND dlg, UINT msg, WPARAM wparam, LPARAM lparam)
169 {
170 int auth = 0;
171
172 switch (msg) {
173 case WM_INITDIALOG:
174 if (proxy.host != NULL) {
175 SetDlgItemText (dlg, IDC_PROXY_HOST, proxy.host);
176 SetDlgItemInt( dlg, IDC_PROXY_PORT, proxy.port, FALSE );
177 if (proxy.user != NULL) {
178 SetDlgItemText (dlg, IDC_PROXY_USER, proxy.user);
179 CheckDlgButton (dlg, IDC_PROXY_AUTH, BST_CHECKED);
180 auth++;
181 }
182 if (proxy.pass != NULL) {
183 SetDlgItemText (dlg, IDC_PROXY_PWD, proxy.pass);
184 auth++;
185 }
186 }
187 enable_proxy_auth (dlg, auth);
188 SetDlgItemText (dlg, IDC_PROXY_HOSTINF, _("Proxy host name or IP address"));
189 SetDlgItemText (dlg, IDC_PROXY_AUTH, _("Server requires &authentication"));
190 SetDlgItemText (dlg, IDC_PROXY_USERINF, _("User name"));
191 SetDlgItemText (dlg, IDC_PROXY_PWDINF, _("Password"));
192 SetDlgItemText (dlg, IDC_PROXY_PROTOINF, _("Proxy type"));
193 SetDlgItemText (dlg, IDC_PROXY_AUTHINF, _("Authentication"));
194 SetWindowText (dlg, _("Proxy Settings"));
195 init_proxy_protocols (dlg);
196 SendDlgItemMessage (dlg, IDC_PROXY_PROTO, CB_SETCURSEL,
197 (WPARAM)proxy.proto, 0);
198 center_window (dlg, NULL);
199 SetForegroundWindow (dlg);
200 break;
201
202 case WM_COMMAND:
203 switch (HIWORD (wparam)) {
204 case CBN_SELCHANGE:
205 if (combo_get_sel (dlg, (int)LOWORD (wparam))) {
206 }
207 else {
208 }
209 break;
210
211 case BN_CLICKED:
212 switch ((int)LOWORD (wparam)) {
213 case IDC_PROXY_AUTH:
214 if (IsDlgButtonChecked (dlg, IDC_PROXY_AUTH))
215 enable_proxy_auth (dlg, 1);
216 else
217 enable_proxy_auth (dlg, 0);
218 break;
219 }
220 break;
221 }
222
223 switch (LOWORD (wparam)) {
224 case IDOK:
225 if (IsDlgButtonChecked (dlg, IDC_PROXY_AUTH) &&
226 item_get_text_length (dlg, IDC_PROXY_USER) == 0 &&
227 item_get_text_length (dlg, IDC_PROXY_PWD) == 0) {
228 msg_box (dlg, _("Please fill out all required fields for authentication."),
229 _("Proxy Settings"), MB_WARN);
230 return TRUE;
231 }
232 kserver_proxy_release (&proxy);
233 if (combo_get_sel (dlg, IDC_PROXY_PROTO) != PROXY_PROTO_NONE) {
234 if (read_proxy (dlg, &proxy))
235 return FALSE;
236 }
237 set_reg_proxy_prefs (&proxy);
238 EndDialog (dlg, TRUE);
239 return TRUE;
240
241 case IDCANCEL:
242 EndDialog (dlg, FALSE);
243 return FALSE;
244 }
245 break;
246 }
247
248 return FALSE;
249 }

Properties

Name Value
svn:eol-style native

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26