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