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

Annotation of /trunk/Src/wptProxySettingsDlg.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 345 - (hide annotations)
Sun Nov 27 15:14:26 2011 UTC (13 years, 3 months ago) by twoaday
File size: 6904 byte(s)


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

Properties

Name Value
svn:eol-style native

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26