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