1 |
werner |
36 |
/* wptCommonDlg.cpp |
2 |
twoaday |
181 |
* Copyright (C) 2004, 2005, 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 |
|
|
* 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 |
twoaday |
211 |
#include <ctype.h> |
24 |
werner |
36 |
#include <windows.h> |
25 |
|
|
|
26 |
werner |
47 |
#include "resource.h" |
27 |
werner |
36 |
#include "wptTypes.h" |
28 |
|
|
#include "wptW32API.h" |
29 |
|
|
#include "wptVersion.h" |
30 |
|
|
#include "wptGPG.h" |
31 |
|
|
#include "wptCommonCtl.h" |
32 |
|
|
#include "wptContext.h" |
33 |
twoaday |
193 |
#include "wptRegistry.h" |
34 |
werner |
36 |
|
35 |
|
|
|
36 |
|
|
/* check if the given buf contains a valid URL. */ |
37 |
|
|
static int |
38 |
|
|
check_URL (const char *buf) |
39 |
|
|
{ |
40 |
twoaday |
181 |
size_t i; |
41 |
twoaday |
211 |
const char *allowed = "-./#_:~&?="; |
42 |
twoaday |
181 |
|
43 |
werner |
36 |
if (!strchr (buf, ':')) |
44 |
|
|
return -1; |
45 |
|
|
if (!strstr (buf, "//")) |
46 |
|
|
return -1; |
47 |
twoaday |
181 |
if (strstr (buf, " ")) |
48 |
|
|
return -1; |
49 |
|
|
buf += 7; |
50 |
|
|
for (i=0; i < strlen (buf); i++) { |
51 |
|
|
if (isalpha (buf[i]) || isdigit (buf[i])) |
52 |
|
|
continue; |
53 |
|
|
if (!strchr (allowed, buf[i])) |
54 |
|
|
return -1; |
55 |
|
|
} |
56 |
|
|
|
57 |
werner |
36 |
return 0; |
58 |
|
|
} |
59 |
|
|
|
60 |
|
|
|
61 |
|
|
static BOOL CALLBACK |
62 |
|
|
http_dlg_proc (HWND dlg, UINT msg, WPARAM wparam, LPARAM lparam) |
63 |
|
|
{ |
64 |
|
|
static struct URL_ctx_s * ctx; |
65 |
|
|
int n; |
66 |
|
|
|
67 |
|
|
switch (msg) { |
68 |
|
|
case WM_INITDIALOG: |
69 |
|
|
ctx = (struct URL_ctx_s*)lparam; |
70 |
|
|
if (!ctx) |
71 |
|
|
BUG (0); |
72 |
|
|
if (ctx->desc != NULL) |
73 |
|
|
SetWindowText (dlg, ctx->desc); |
74 |
|
|
if (ctx->title != NULL) |
75 |
|
|
SetDlgItemText (dlg, IDC_HTTP_TITLE, ctx->title); |
76 |
twoaday |
123 |
SetDlgItemText (dlg, IDCANCEL, _("&Cancel")); |
77 |
werner |
36 |
SetForegroundWindow (dlg); |
78 |
|
|
break; |
79 |
|
|
|
80 |
|
|
case WM_COMMAND: |
81 |
|
|
switch (LOWORD (wparam)) { |
82 |
|
|
case IDOK: |
83 |
|
|
n = GetDlgItemText (dlg, IDC_HTTP_URL, ctx->url, DIM (ctx->url)-1); |
84 |
|
|
if (ctx->check && (!n || check_URL (ctx->url))) { |
85 |
|
|
msg_box (dlg, _("Please enter a valid URL."), "HTTP", MB_ERR); |
86 |
|
|
return FALSE; |
87 |
|
|
} |
88 |
|
|
ctx->cancel = 0; |
89 |
|
|
EndDialog (dlg, TRUE); |
90 |
|
|
break; |
91 |
|
|
|
92 |
|
|
case IDCANCEL: |
93 |
|
|
ctx->cancel = 1; |
94 |
|
|
EndDialog (dlg, FALSE); |
95 |
|
|
break; |
96 |
|
|
} |
97 |
|
|
} |
98 |
|
|
|
99 |
|
|
return FALSE; |
100 |
|
|
} |
101 |
|
|
|
102 |
|
|
|
103 |
|
|
/* Common dialog to request a URL from the user. |
104 |
|
|
Return value is the URL context. */ |
105 |
|
|
void* |
106 |
|
|
get_http_file_dlg (HWND root) |
107 |
|
|
{ |
108 |
|
|
struct URL_ctx_s *ctx; |
109 |
|
|
|
110 |
|
|
ctx = new URL_ctx_s; |
111 |
|
|
if (!ctx) |
112 |
|
|
BUG (0); |
113 |
|
|
memset (ctx, 0, sizeof *ctx); |
114 |
twoaday |
181 |
ctx->check = 1; |
115 |
|
|
ctx->desc = _("HTTP Key Import"); |
116 |
|
|
ctx->title = _("Enter URL to retrieve the public key"); |
117 |
werner |
36 |
DialogBoxParam (glob_hinst, (LPCTSTR)IDD_WINPT_HTTP, root, |
118 |
|
|
http_dlg_proc, (LPARAM)ctx); |
119 |
|
|
return ctx; |
120 |
|
|
} |
121 |
|
|
|
122 |
|
|
/* Wrapper to request a preferred keyserver from the user. |
123 |
|
|
Return value is the URL context. */ |
124 |
|
|
void* |
125 |
|
|
get_keyserver_URL_dlg (HWND root) |
126 |
|
|
{ |
127 |
|
|
struct URL_ctx_s *ctx; |
128 |
|
|
|
129 |
|
|
ctx = new URL_ctx_s; |
130 |
|
|
if (!ctx) |
131 |
|
|
BUG (0); |
132 |
|
|
memset (ctx, 0, sizeof *ctx); |
133 |
|
|
ctx->check = 1; |
134 |
|
|
ctx->desc = _("Key Edit"); |
135 |
|
|
ctx->title = _("Enter preferred keyserver URL"); |
136 |
|
|
DialogBoxParam (glob_hinst, (LPCTSTR)IDD_WINPT_HTTP, root, |
137 |
|
|
http_dlg_proc, (LPARAM)ctx); |
138 |
|
|
return ctx; |
139 |
|
|
} |
140 |
|
|
|
141 |
|
|
|
142 |
|
|
/* Generic text input dialog. */ |
143 |
|
|
char* |
144 |
|
|
get_input_dialog (HWND root, const char *title, const char *desc) |
145 |
|
|
{ |
146 |
|
|
struct URL_ctx_s ctx; |
147 |
|
|
|
148 |
|
|
memset (&ctx, 0, sizeof (ctx)); |
149 |
|
|
ctx.title = title; |
150 |
|
|
ctx.desc = desc; |
151 |
|
|
DialogBoxParam (glob_hinst, (LPCTSTR)IDD_WINPT_HTTP, root, |
152 |
|
|
http_dlg_proc, (LPARAM)&ctx); |
153 |
|
|
if (ctx.cancel || strlen (ctx.url) == 0) |
154 |
|
|
return NULL; |
155 |
|
|
return m_strdup (ctx.url); |
156 |
|
|
} |
157 |
twoaday |
150 |
|
158 |
|
|
|
159 |
twoaday |
193 |
/* Initialize the language list based on the .mo files |
160 |
|
|
found in the dirctory @modpath. */ |
161 |
|
|
static int |
162 |
|
|
nls_load_langlist (HWND dlg, const char *modpath) |
163 |
|
|
{ |
164 |
|
|
WIN32_FIND_DATA fnd; |
165 |
|
|
HWND cb; |
166 |
|
|
HANDLE hd; |
167 |
|
|
char *pattern = NULL; |
168 |
|
|
int i; |
169 |
|
|
|
170 |
|
|
cb = GetDlgItem (dlg, IDC_LANGUAGE_LIST); |
171 |
|
|
combox_add_string (cb, lang_list[0].name); |
172 |
|
|
SendMessage (cb, CB_SETCURSEL, 0, 0); |
173 |
|
|
|
174 |
|
|
memset (&fnd, 0, sizeof (fnd)); |
175 |
|
|
pattern = make_filename (modpath, "*.mo", NULL); |
176 |
|
|
hd = FindFirstFile (pattern, &fnd); |
177 |
|
|
if (hd == INVALID_HANDLE_VALUE) { |
178 |
|
|
free_if_alloc (pattern); |
179 |
|
|
return -1; |
180 |
|
|
} |
181 |
|
|
|
182 |
|
|
do { |
183 |
|
|
for (i=0; lang_list[i].id != NULL; i++) { |
184 |
|
|
if (!strnicmp (fnd.cFileName, lang_list[i].id, 2)) |
185 |
|
|
combox_add_string (cb, lang_list[i].name); |
186 |
|
|
} |
187 |
|
|
memset (&fnd, 0, sizeof (fnd)); |
188 |
|
|
} while (FindNextFile (hd, &fnd) != FALSE); |
189 |
|
|
FindClose (hd); |
190 |
|
|
free_if_alloc (pattern); |
191 |
|
|
return 0; |
192 |
|
|
} |
193 |
|
|
|
194 |
|
|
|
195 |
|
|
/* Set the default language for the program. |
196 |
|
|
The $lang.mo file is copied either to the selected |
197 |
|
|
folder or the directory specified by @modpath. |
198 |
|
|
The output name of the file is winpt.mo (example: de.mo -> winpt.mo). */ |
199 |
|
|
static int |
200 |
|
|
nls_set_language (HWND dlg, const char *modpath) |
201 |
|
|
{ |
202 |
|
|
HWND cb = GetDlgItem (dlg, IDC_LANGUAGE_LIST); |
203 |
|
|
int pos = SendMessage (cb, CB_GETCURSEL, 0, 0); |
204 |
|
|
const char *folder; |
205 |
|
|
char *src, *dst; |
206 |
|
|
|
207 |
|
|
/* default langauge is English. */ |
208 |
|
|
if (pos == 0 || pos == CB_ERR) |
209 |
|
|
return 0; |
210 |
|
|
|
211 |
|
|
folder = get_folder_dlg (dlg, _("Choose Locale Directory"), modpath); |
212 |
|
|
if (!folder) |
213 |
|
|
return 0; |
214 |
|
|
|
215 |
|
|
src = make_filename (modpath, lang_list[pos].id, "mo"); |
216 |
|
|
dst = make_filename (folder, "winpt", "mo"); |
217 |
|
|
|
218 |
|
|
if (!CopyFile (src, dst, FALSE)) { |
219 |
|
|
msg_box (dlg, _("Could not create winpt.mo file"), |
220 |
|
|
_("WinPT Error"), MB_ERR); |
221 |
|
|
pos = -1; |
222 |
|
|
} |
223 |
|
|
else |
224 |
|
|
set_reg_entry_mo (folder); |
225 |
|
|
free_if_alloc (src); |
226 |
|
|
free_if_alloc (dst); |
227 |
|
|
return pos; |
228 |
|
|
} |
229 |
|
|
|
230 |
|
|
|
231 |
twoaday |
150 |
static BOOL CALLBACK |
232 |
twoaday |
193 |
nls_dlg_proc (HWND dlg, UINT msg, WPARAM wparam, LPARAM lparam) |
233 |
twoaday |
150 |
{ |
234 |
twoaday |
193 |
static char modpath[MAX_PATH+1]; |
235 |
|
|
int langid = 0, n; |
236 |
twoaday |
150 |
|
237 |
|
|
switch (msg) { |
238 |
|
|
case WM_INITDIALOG: |
239 |
twoaday |
193 |
if ((n=GetModuleFileName (NULL, modpath, MAX_PATH-1)) > 0) { |
240 |
|
|
while (n-- > 0) { |
241 |
|
|
if (modpath[n] == '\\') { |
242 |
|
|
modpath[n] = 0; |
243 |
|
|
break; |
244 |
|
|
} |
245 |
|
|
} |
246 |
twoaday |
150 |
} |
247 |
twoaday |
193 |
n = nls_load_langlist (dlg, modpath); |
248 |
|
|
if (n == -1) |
249 |
|
|
EndDialog (dlg, -1); |
250 |
|
|
SetWindowText (dlg, _("Native Language Support")); |
251 |
|
|
SetDlgItemText (dlg, IDC_LANGUAGE_INFO, _("Please select a language")); |
252 |
|
|
SetDlgItemText (dlg, IDOK, _("&OK")); |
253 |
|
|
SetDlgItemText (dlg, IDCANCEL, _("&Cancel")); |
254 |
|
|
center_window (dlg, NULL); |
255 |
twoaday |
150 |
SetForegroundWindow (dlg); |
256 |
twoaday |
193 |
break; |
257 |
twoaday |
150 |
|
258 |
|
|
case WM_COMMAND: |
259 |
|
|
switch (LOWORD (wparam)) { |
260 |
|
|
case IDOK: |
261 |
twoaday |
193 |
langid = nls_set_language (dlg, modpath); |
262 |
|
|
EndDialog (dlg, langid); |
263 |
twoaday |
150 |
break; |
264 |
|
|
|
265 |
|
|
case IDCANCEL: |
266 |
twoaday |
193 |
EndDialog (dlg, -1); |
267 |
twoaday |
150 |
break; |
268 |
|
|
} |
269 |
|
|
break; |
270 |
|
|
} |
271 |
twoaday |
193 |
|
272 |
twoaday |
150 |
return FALSE; |
273 |
|
|
} |
274 |
|
|
|
275 |
|
|
|
276 |
|
|
int |
277 |
twoaday |
193 |
select_language (void) |
278 |
twoaday |
150 |
{ |
279 |
twoaday |
193 |
DialogBoxParam (glob_hinst, (LPCTSTR)IDD_WINPT_LANGUAGE, GetDesktopWindow (), |
280 |
|
|
nls_dlg_proc, 0); |
281 |
|
|
return 0; |
282 |
twoaday |
150 |
} |
283 |
|
|
|