1 |
werner |
36 |
/* wptRegistry.cpp - W32 Registry access |
2 |
|
|
* Copyright (C) 2000-2005 Timo Schulz |
3 |
|
|
* |
4 |
|
|
* This file is part of WinPT. |
5 |
|
|
* |
6 |
|
|
* WinPT is free software; you can redistribute it and/or |
7 |
|
|
* modify it under the terms of the GNU General Public License |
8 |
|
|
* as published by the Free Software Foundation; either version 2 |
9 |
|
|
* of the License, or (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 GNU |
14 |
|
|
* 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 |
|
|
|
21 |
|
|
#ifdef HAVE_CONFIG_H |
22 |
|
|
#include <config.h> |
23 |
|
|
#endif |
24 |
|
|
|
25 |
|
|
#include <windows.h> |
26 |
|
|
#include <stdio.h> |
27 |
|
|
|
28 |
|
|
#include "wptErrors.h" |
29 |
|
|
#include "wptW32API.h" |
30 |
|
|
#include "wptGPG.h" |
31 |
|
|
#include "wptRegistry.h" |
32 |
|
|
#include "wptKeyserver.h" |
33 |
|
|
#include "wptTypes.h" |
34 |
|
|
#include "wptNLS.h" |
35 |
|
|
#include "wptVersion.h" |
36 |
|
|
|
37 |
|
|
#define rc_ok(rc) ((rc) == ERROR_SUCCESS) |
38 |
|
|
|
39 |
twoaday |
172 |
#define WINPT_REG "Software\\WinPT" |
40 |
werner |
48 |
|
41 |
twoaday |
179 |
/* GPG file association context. */ |
42 |
|
|
struct gpg_filetype { |
43 |
|
|
const char *descr; |
44 |
|
|
const char *ext; |
45 |
|
|
int nicon; |
46 |
|
|
}; |
47 |
|
|
|
48 |
werner |
36 |
struct reg_hotkey_s reg_hotkeys[] = { |
49 |
twoaday |
69 |
{"ClipEncrypt", "", 0}, |
50 |
|
|
{"ClipDecrypt", "", 0}, |
51 |
|
|
{"ClipSign", "", 0}, |
52 |
|
|
{"ClipSignEnc", "", 0}, |
53 |
|
|
{"CwsEncrypt", "", 0}, |
54 |
|
|
{"CwsDecrypt", "", 0}, |
55 |
|
|
{"CwsSign", "", 0}, |
56 |
|
|
{"CwsSignEnc", "", 0}, |
57 |
|
|
{NULL, "", 0} |
58 |
werner |
36 |
}; |
59 |
|
|
|
60 |
twoaday |
172 |
/* Global WinPT registry prefereneces. */ |
61 |
werner |
36 |
winpt_reg_prefs_s reg_prefs; |
62 |
|
|
|
63 |
|
|
|
64 |
twoaday |
128 |
/* Return != 0 if GPG4win is installed. */ |
65 |
|
|
int |
66 |
|
|
is_gpg4win_installed (void) |
67 |
|
|
{ |
68 |
|
|
char *p; |
69 |
|
|
|
70 |
|
|
p = get_reg_entry_gpg4win (NULL); |
71 |
|
|
if (!p) |
72 |
|
|
return 0; |
73 |
|
|
if (dir_exist_check (p)) { |
74 |
|
|
free_if_alloc (p); |
75 |
|
|
return 0; |
76 |
|
|
} |
77 |
|
|
free_if_alloc (p); |
78 |
|
|
return -1; |
79 |
|
|
} |
80 |
|
|
|
81 |
|
|
|
82 |
twoaday |
117 |
/* Return != 0 if GPGee is installed. */ |
83 |
|
|
int |
84 |
|
|
is_gpgee_installed (void) |
85 |
|
|
{ |
86 |
|
|
HKEY hk; |
87 |
|
|
LONG ec; |
88 |
|
|
|
89 |
|
|
ec = RegOpenKeyEx (HKEY_CURRENT_USER, "Software\\GPGee", 0, KEY_READ, &hk); |
90 |
|
|
if (ec == ERROR_SUCCESS) { |
91 |
|
|
RegCloseKey (hk); |
92 |
|
|
return -1; |
93 |
|
|
} |
94 |
|
|
|
95 |
|
|
return 0; |
96 |
|
|
} |
97 |
|
|
|
98 |
|
|
|
99 |
|
|
/* Free all members in the registry preference struct. */ |
100 |
werner |
36 |
void |
101 |
|
|
free_reg_prefs (void) |
102 |
|
|
{ |
103 |
|
|
free_if_alloc (reg_prefs.backup.path); |
104 |
|
|
free_if_alloc (reg_prefs.kserv_conf); |
105 |
|
|
free_if_alloc (reg_prefs.homedir); |
106 |
|
|
memset (®_prefs, 0, sizeof reg_prefs); |
107 |
twoaday |
117 |
} |
108 |
werner |
36 |
|
109 |
|
|
|
110 |
|
|
/* Register the given WinPT filetype. */ |
111 |
|
|
static int |
112 |
|
|
regist_single_filetype (gpg_filetype *gfile) |
113 |
|
|
{ |
114 |
twoaday |
128 |
char icon[256]; |
115 |
|
|
char prog[256]; |
116 |
werner |
36 |
|
117 |
|
|
memset (&icon, 0, sizeof (icon)); |
118 |
|
|
GetModuleFileName (glob_hinst, prog, sizeof (prog)-1); |
119 |
|
|
_snprintf (icon, sizeof (icon) -1, "%s,%d", prog, gfile->nicon); |
120 |
|
|
return create_file_type (prog, gfile->ext, gfile->descr, icon); |
121 |
twoaday |
117 |
} |
122 |
werner |
36 |
|
123 |
|
|
|
124 |
|
|
/* Install the GPG related into the W32 resgistry, if the entry already |
125 |
|
|
exists the function returns immediately. */ |
126 |
|
|
int |
127 |
|
|
regist_inst_gnupg (int create_mokey) |
128 |
|
|
{ |
129 |
|
|
int rc; |
130 |
|
|
HKEY reg; |
131 |
|
|
|
132 |
|
|
rc = RegOpenKeyEx( HKEY_CURRENT_USER, "Software\\GNU\\GnuPG", 0, KEY_READ, ® ); |
133 |
|
|
if( rc_ok( rc ) ) { |
134 |
|
|
RegCloseKey( reg ); |
135 |
|
|
return 0; |
136 |
|
|
} |
137 |
|
|
rc = RegCreateKey( HKEY_CURRENT_USER, "Software\\GNU\\GnuPG", ® ); |
138 |
|
|
if( !rc_ok( rc ) ) |
139 |
|
|
return WPTERR_REGISTRY; |
140 |
|
|
RegCloseKey( reg ); |
141 |
|
|
if( create_mokey ) { |
142 |
|
|
rc = RegOpenKeyEx( HKEY_CURRENT_USER, "Control Panel\\MingW32\\NLS", 0, KEY_READ, ® ); |
143 |
|
|
if( rc_ok( rc ) ) { |
144 |
|
|
RegCloseKey( reg ); |
145 |
|
|
return 0; |
146 |
|
|
} |
147 |
|
|
rc = RegCreateKey( HKEY_CURRENT_USER, "Control Panel\\MingW32\\NLS", ® ); |
148 |
|
|
if( !rc_ok( rc ) ) |
149 |
|
|
return WPTERR_REGISTRY; |
150 |
|
|
RegCloseKey( reg ); |
151 |
|
|
} |
152 |
|
|
|
153 |
|
|
return 0; |
154 |
twoaday |
128 |
} |
155 |
werner |
36 |
|
156 |
|
|
|
157 |
twoaday |
117 |
/* Install WinPT into the W32 registry, if the entry already |
158 |
twoaday |
85 |
exists the function returns immediately. @with_ext can be |
159 |
|
|
used to register some file types (if 1). @created contains |
160 |
|
|
1 if the registry key was created. |
161 |
|
|
Return value: 0 on success. */ |
162 |
werner |
36 |
int |
163 |
twoaday |
117 |
regist_inst_winpt (int with_ext, int *created) |
164 |
twoaday |
129 |
{ |
165 |
werner |
36 |
HKEY reg; |
166 |
twoaday |
80 |
char *p = NULL; |
167 |
|
|
char modpath[MAX_PATH+1]; |
168 |
twoaday |
172 |
int rc, i, id, n; |
169 |
werner |
36 |
|
170 |
twoaday |
129 |
gpg_filetype gpg_filetypes[] = { |
171 |
|
|
{_("GPG Detached Signature"), ".sig", 1}, |
172 |
|
|
{_("GPG Encrypted Data"), ".gpg", 2}, |
173 |
|
|
{_("GPG Armored Data"), ".asc", 2}, |
174 |
|
|
{0} |
175 |
|
|
}; |
176 |
|
|
|
177 |
twoaday |
85 |
if (created) |
178 |
werner |
36 |
*created = 0; |
179 |
twoaday |
172 |
if (is_gpgee_installed ()) |
180 |
werner |
36 |
with_ext = 0; |
181 |
|
|
|
182 |
twoaday |
85 |
rc = RegOpenKeyEx (HKEY_CURRENT_USER, WINPT_REG, 0, KEY_READ, ®); |
183 |
twoaday |
117 |
if (rc_ok (rc)) { |
184 |
twoaday |
85 |
RegCloseKey (reg); |
185 |
|
|
rc = RegOpenKeyEx (HKEY_CURRENT_USER, WINPT_REG"\\Keyserver", 0, KEY_READ, ®); |
186 |
twoaday |
167 |
if (!rc_ok (rc)) { |
187 |
twoaday |
85 |
RegCreateKey (HKEY_CURRENT_USER, WINPT_REG"\\Keyserver", ®); |
188 |
|
|
RegCloseKey (reg); |
189 |
werner |
36 |
} |
190 |
|
|
p = get_reg_entry_keyserver ("Default"); |
191 |
|
|
if (!p) { |
192 |
|
|
char buf[16]; |
193 |
|
|
sprintf (buf, "%d", HKP_PORT); |
194 |
|
|
set_reg_entry_keyserver ("Default_Port", buf); |
195 |
|
|
set_reg_entry_keyserver ("Default", DEF_HKP_KEYSERVER); |
196 |
|
|
} |
197 |
twoaday |
85 |
free_if_alloc (p); |
198 |
werner |
36 |
return 0; |
199 |
|
|
} |
200 |
twoaday |
117 |
rc = RegCreateKey (HKEY_CURRENT_USER, WINPT_REG, ®); |
201 |
twoaday |
85 |
if (!rc_ok (rc)) |
202 |
werner |
36 |
return WPTERR_REGISTRY; |
203 |
twoaday |
85 |
if (created) |
204 |
werner |
36 |
*created = 1; |
205 |
twoaday |
85 |
RegCloseKey (reg); |
206 |
twoaday |
172 |
if (with_ext) { |
207 |
|
|
id = msg_box (NULL, _("WinPT can register some GPG file types for you so they can " |
208 |
|
|
"be processed with a double click in the explorer.\n" |
209 |
|
|
"Do you want to continue?"), _("WinPT"), MB_YESNO|MB_INFO); |
210 |
|
|
if (id == IDYES) { |
211 |
|
|
for (i = 0; gpg_filetypes[i].ext; i++) { |
212 |
|
|
rc = RegOpenKeyEx (HKEY_CLASSES_ROOT, gpg_filetypes[i].ext, 0, KEY_READ, ®); |
213 |
|
|
if (rc_ok (rc)) { |
214 |
|
|
RegCloseKey (reg); |
215 |
|
|
id = log_box (_("WinPT WARNING"), MB_YESNO|MB_INFO, |
216 |
|
|
_("It seems there was already a '%s' file type registered by another application.\n" |
217 |
|
|
"Do you want to overwrite it?"), gpg_filetypes[i].ext); |
218 |
|
|
if (id == IDNO) |
219 |
|
|
continue; |
220 |
|
|
} |
221 |
|
|
regist_single_filetype (&gpg_filetypes[i]); |
222 |
|
|
} |
223 |
|
|
} |
224 |
|
|
} |
225 |
twoaday |
80 |
if ((n=GetModuleFileName (NULL, modpath, MAX_PATH-1)) > 0) { |
226 |
|
|
while (n-- > 0) { |
227 |
|
|
if (modpath[n] == '\\') { |
228 |
|
|
modpath[n] = 0; |
229 |
|
|
break; |
230 |
|
|
} |
231 |
|
|
} |
232 |
twoaday |
117 |
set_reg_entry (HKEY_CURRENT_USER, WINPT_REG, "Install Directory", modpath); |
233 |
twoaday |
80 |
} |
234 |
werner |
36 |
return 0; |
235 |
twoaday |
80 |
} |
236 |
werner |
36 |
|
237 |
|
|
|
238 |
|
|
/* Create a new filetype in the W32 registry. |
239 |
|
|
We should really care of errors! Otherwise we can damage the registry! */ |
240 |
|
|
int |
241 |
twoaday |
128 |
create_file_type (const char *exefile, const char *ext, |
242 |
|
|
const char *extname, char *iconfile) |
243 |
werner |
36 |
{ |
244 |
|
|
int rc; |
245 |
|
|
HKEY reg = NULL; |
246 |
|
|
char deficon[256], defexec[256], p_exefile[256]; |
247 |
|
|
|
248 |
|
|
|
249 |
|
|
rc = RegCreateKey( HKEY_CLASSES_ROOT, ext, ® ); |
250 |
|
|
if( rc_ok( rc ) ) |
251 |
|
|
rc = RegSetValueEx( reg, NULL, 0, REG_SZ, (byte *)extname, strlen( extname ) ); |
252 |
|
|
if( rc_ok( rc ) ) |
253 |
|
|
rc = RegCloseKey( reg ); |
254 |
|
|
if( rc_ok( rc ) ) |
255 |
|
|
rc = RegCreateKey( HKEY_CLASSES_ROOT, extname, ® ); |
256 |
|
|
if( rc_ok( rc ) ) |
257 |
|
|
rc = RegSetValueEx( reg, NULL, 0, REG_SZ, (byte *) extname, strlen( extname ) ); |
258 |
|
|
if( rc_ok( rc ) ) |
259 |
|
|
rc = RegCloseKey( reg ); |
260 |
|
|
if( !rc_ok( rc ) ) { |
261 |
|
|
rc = WPTERR_REGISTRY; |
262 |
|
|
goto leave; |
263 |
|
|
} |
264 |
|
|
|
265 |
|
|
memset( &deficon, 0, sizeof deficon ); |
266 |
|
|
_snprintf( deficon, sizeof deficon - 1, "%s\\DefaultIcon", extname ); |
267 |
|
|
memset( &defexec, 0, sizeof defexec ); |
268 |
|
|
_snprintf( defexec, sizeof defexec - 1, "%s\\shell\\open\\command", extname ); |
269 |
|
|
memset( &p_exefile, 0, sizeof p_exefile ); |
270 |
|
|
_snprintf( p_exefile, sizeof p_exefile - 1, "%s %%1", exefile ); |
271 |
|
|
|
272 |
|
|
rc = RegCreateKey( HKEY_CLASSES_ROOT, deficon, ® ); |
273 |
|
|
if( rc_ok( rc ) ) |
274 |
|
|
rc = RegSetValueEx(reg, NULL, 0, REG_SZ, (byte *)iconfile, strlen( iconfile ) ); |
275 |
|
|
if( rc_ok( rc ) ) |
276 |
|
|
rc = RegCloseKey( reg ); |
277 |
|
|
if( rc_ok( rc ) ) |
278 |
|
|
rc = RegCreateKey( HKEY_CLASSES_ROOT, defexec, ® ); |
279 |
|
|
if( rc_ok( rc ) ) |
280 |
|
|
rc = RegSetValueEx( reg, NULL, 0, REG_SZ, (byte *)p_exefile, strlen( exefile ) ); |
281 |
|
|
if( rc_ok( rc ) ) |
282 |
|
|
rc = RegCloseKey( reg ); |
283 |
|
|
if( !rc_ok( rc ) ) { |
284 |
|
|
rc = WPTERR_REGISTRY; |
285 |
|
|
goto leave; |
286 |
|
|
} |
287 |
|
|
|
288 |
|
|
leave: |
289 |
|
|
if( reg ) |
290 |
|
|
RegCloseKey( reg ); |
291 |
|
|
return rc; |
292 |
|
|
} /* create_file_type */ |
293 |
|
|
|
294 |
|
|
|
295 |
|
|
/* Expand a string with %foo% entries so that %foo% will |
296 |
|
|
be replaced with its actual value. */ |
297 |
|
|
static char * |
298 |
|
|
expand_path (const char *path) |
299 |
|
|
{ |
300 |
|
|
DWORD len; |
301 |
|
|
char *p; |
302 |
|
|
|
303 |
|
|
len = ExpandEnvironmentStrings (path, NULL, 0); |
304 |
|
|
if (!len) |
305 |
|
|
return NULL; |
306 |
|
|
len += 1; |
307 |
|
|
p = new char[len+1]; |
308 |
twoaday |
128 |
if (!p) |
309 |
werner |
36 |
return NULL; |
310 |
|
|
len = ExpandEnvironmentStrings (path, p, len); |
311 |
|
|
if (!len) { |
312 |
|
|
free_if_alloc (p); |
313 |
|
|
return NULL; |
314 |
|
|
} |
315 |
|
|
return p; |
316 |
twoaday |
128 |
} |
317 |
werner |
36 |
|
318 |
|
|
|
319 |
|
|
/* Retrieve a registry entry with the directory given in @dir |
320 |
|
|
and the key given in @key. |
321 |
|
|
Return value is the value or NULL otherwise. */ |
322 |
|
|
char* |
323 |
|
|
get_reg_entry (HKEY root_key, const char * dir, const char * key) |
324 |
|
|
{ |
325 |
|
|
int rc; |
326 |
|
|
char text[384] = {0}; |
327 |
twoaday |
69 |
DWORD nbytes, type; |
328 |
werner |
36 |
HKEY reg_key = NULL; |
329 |
twoaday |
128 |
char * p = NULL; |
330 |
werner |
36 |
|
331 |
|
|
rc = RegOpenKeyEx (root_key, dir, 0, KEY_QUERY_VALUE, ®_key); |
332 |
|
|
if( !rc_ok( rc ) ) |
333 |
|
|
goto leave; |
334 |
|
|
nbytes = sizeof (text) - 1; |
335 |
|
|
type = REG_SZ; |
336 |
|
|
|
337 |
|
|
rc = RegQueryValueEx (reg_key, key, 0, &type, (BYTE *)&text, &nbytes); |
338 |
|
|
if (!rc_ok (rc) || !nbytes) |
339 |
|
|
goto leave; |
340 |
|
|
|
341 |
|
|
if (type == REG_EXPAND_SZ && strchr (text, '%')) |
342 |
|
|
p = expand_path (text); |
343 |
|
|
else { |
344 |
|
|
p = new char[nbytes + 1]; |
345 |
|
|
if (!p) |
346 |
|
|
BUG (0); |
347 |
|
|
memcpy (p, text, nbytes); |
348 |
|
|
p[nbytes] = '\0'; |
349 |
|
|
} |
350 |
|
|
|
351 |
|
|
leave: |
352 |
|
|
if (reg_key) |
353 |
|
|
RegCloseKey (reg_key); |
354 |
|
|
return p; |
355 |
|
|
} |
356 |
|
|
|
357 |
|
|
|
358 |
|
|
/* XXX: use REG_EXPAND_SZ to support multi user environments. |
359 |
|
|
keep this type and do not overwrite it with REG_SZ. */ |
360 |
|
|
|
361 |
|
|
/* Set a registry entry. */ |
362 |
|
|
int |
363 |
|
|
set_reg_entry (HKEY root_key, const char *dir, const char *key, |
364 |
|
|
const char *value) |
365 |
|
|
{ |
366 |
|
|
int rc = 0; |
367 |
|
|
HKEY reg_key; |
368 |
|
|
|
369 |
|
|
rc = RegOpenKeyEx( root_key, dir, 0, KEY_WRITE, ®_key ); |
370 |
|
|
if( !rc_ok( rc ) ) |
371 |
|
|
return WPTERR_REGISTRY; |
372 |
|
|
rc = RegSetValueEx( reg_key, key, 0, REG_SZ, (BYTE *)value, strlen( value ) ); |
373 |
|
|
if( !rc_ok( rc ) ) |
374 |
|
|
rc = WPTERR_REGISTRY; |
375 |
|
|
RegCloseKey( reg_key ); |
376 |
|
|
return rc; |
377 |
|
|
} /* set_reg_entry */ |
378 |
|
|
|
379 |
|
|
|
380 |
|
|
int |
381 |
|
|
set_reg_key( HKEY root_key, const char * dir, const char * key, |
382 |
|
|
const char * value ) |
383 |
|
|
{ |
384 |
|
|
int rc = 0; |
385 |
|
|
HKEY reg_key; |
386 |
|
|
|
387 |
|
|
rc = RegOpenKeyEx( root_key, dir, 0, KEY_WRITE, ®_key ); |
388 |
|
|
if( !rc_ok( rc ) ) |
389 |
|
|
return WPTERR_REGISTRY; |
390 |
|
|
|
391 |
|
|
rc = RegSetValueEx( reg_key, key, 0, REG_SZ, (BYTE *)value, strlen( value ) ); |
392 |
|
|
if( !rc_ok( rc ) ) { |
393 |
|
|
if ( RegCreateKey( root_key, key, ®_key ) != ERROR_SUCCESS ) { |
394 |
|
|
rc = WPTERR_REGISTRY; |
395 |
|
|
goto leave; |
396 |
|
|
} |
397 |
|
|
rc = RegSetValueEx( reg_key, key, 0, REG_SZ, (BYTE *)value, strlen( value ) ); |
398 |
|
|
if ( !rc_ok( rc ) ) |
399 |
|
|
rc = WPTERR_REGISTRY; |
400 |
|
|
} |
401 |
|
|
|
402 |
|
|
leave: |
403 |
|
|
RegCloseKey( reg_key ); |
404 |
|
|
return rc; |
405 |
|
|
} /* set_reg_key */ |
406 |
|
|
|
407 |
|
|
|
408 |
|
|
int |
409 |
|
|
set_reg_entry_gpg (const char * key, const char * value) |
410 |
|
|
{ |
411 |
|
|
return set_reg_entry (HKEY_CURRENT_USER, "Software\\GNU\\GnuPG", key, value); |
412 |
|
|
} |
413 |
|
|
|
414 |
|
|
|
415 |
|
|
int |
416 |
|
|
set_reg_entry_mo (const char * value) |
417 |
|
|
{ |
418 |
|
|
return set_reg_entry (HKEY_CURRENT_USER, "Control Panel\\Mingw32\\NLS", |
419 |
|
|
"MODir", value); |
420 |
|
|
} |
421 |
|
|
|
422 |
|
|
|
423 |
twoaday |
167 |
char* |
424 |
werner |
36 |
get_reg_entry_gpg (const char *key) |
425 |
|
|
{ |
426 |
twoaday |
167 |
char *p; |
427 |
|
|
p = get_reg_entry (HKEY_CURRENT_USER, "Software\\GNU\\GnuPG", key); |
428 |
|
|
if (!p || strlen (p) == 0) { |
429 |
|
|
free_if_alloc (p); |
430 |
|
|
return NULL; |
431 |
|
|
} |
432 |
|
|
return p; |
433 |
werner |
36 |
} |
434 |
|
|
|
435 |
|
|
|
436 |
twoaday |
73 |
/* Return if possible the GPG4Win installation directory concatenated |
437 |
|
|
with the string in @path if given. */ |
438 |
|
|
char* |
439 |
|
|
get_reg_entry_gpg4win (const char *path) |
440 |
|
|
{ |
441 |
|
|
char *p, *pp; |
442 |
|
|
p = get_reg_entry (HKEY_LOCAL_MACHINE, |
443 |
|
|
"Software\\GNU\\GnuPG", "Install Directory"); |
444 |
|
|
if (!p) |
445 |
|
|
return NULL; |
446 |
|
|
if (!path) |
447 |
|
|
return p; |
448 |
|
|
pp = new char[strlen (p) + strlen (path) + 4]; |
449 |
|
|
if (!pp) |
450 |
|
|
BUG (NULL); |
451 |
|
|
sprintf (pp, "%s\\%s", p, path); |
452 |
|
|
free_if_alloc (p); |
453 |
|
|
return pp; |
454 |
|
|
} |
455 |
|
|
|
456 |
|
|
|
457 |
|
|
char* |
458 |
|
|
get_reg_entry_mo (void) |
459 |
werner |
36 |
{ |
460 |
twoaday |
76 |
char *p, *pp; |
461 |
|
|
const char *lang; |
462 |
twoaday |
117 |
|
463 |
twoaday |
73 |
p = get_reg_entry (HKEY_CURRENT_USER, |
464 |
|
|
"Control Panel\\Mingw32\\NLS", "MODir"); |
465 |
twoaday |
76 |
if (p) |
466 |
|
|
return p; |
467 |
twoaday |
117 |
|
468 |
twoaday |
76 |
lang = get_gettext_langid (); |
469 |
twoaday |
117 |
if (!lang) |
470 |
|
|
return NULL; |
471 |
twoaday |
76 |
pp = new char[strlen ("share\\xxxxx\\locale\\LC_MESSAGES")+8]; |
472 |
twoaday |
117 |
if (!pp) |
473 |
twoaday |
76 |
BUG (NULL); |
474 |
|
|
sprintf (pp, "share\\locale\\%s\\LC_MESSAGES", lang); |
475 |
|
|
p = get_reg_entry_gpg4win (pp); |
476 |
|
|
free_if_alloc (pp); |
477 |
twoaday |
73 |
return p; |
478 |
werner |
36 |
} |
479 |
|
|
|
480 |
|
|
|
481 |
|
|
/* All valid configuration commands. */ |
482 |
twoaday |
176 |
static const char *cfg [] = { |
483 |
|
|
NULL, |
484 |
|
|
"CacheTime", |
485 |
|
|
"WordWrap", |
486 |
|
|
"FastMode", |
487 |
|
|
"Viewer", |
488 |
|
|
"WipeMode", |
489 |
|
|
"AlwaysTrust", |
490 |
|
|
"AutoBackup", |
491 |
|
|
"BackupMode", |
492 |
|
|
"DisableHotkeys", |
493 |
|
|
"NoCompressMultiMedia", |
494 |
|
|
"Expert", |
495 |
|
|
"FMProgressBar", |
496 |
|
|
}; |
497 |
werner |
36 |
|
498 |
|
|
|
499 |
|
|
int |
500 |
|
|
set_reg_winpt_single (int id, int val) |
501 |
|
|
{ |
502 |
|
|
char buf[64]; |
503 |
|
|
int rc; |
504 |
|
|
|
505 |
|
|
sprintf (buf, "%d", val); |
506 |
|
|
rc = set_reg_entry (HKEY_CURRENT_USER, WINPT_REG, cfg[id], buf); |
507 |
|
|
return rc; |
508 |
|
|
} |
509 |
|
|
|
510 |
|
|
|
511 |
|
|
int |
512 |
|
|
get_reg_winpt_single (int id) |
513 |
|
|
{ |
514 |
twoaday |
128 |
char * buf = NULL; |
515 |
werner |
36 |
int val = 0; |
516 |
|
|
|
517 |
|
|
buf = get_reg_entry (HKEY_CURRENT_USER, WINPT_REG, cfg[id]); |
518 |
|
|
if (buf && *buf != ' ') |
519 |
|
|
val = 1; |
520 |
|
|
else if (!buf) |
521 |
|
|
val = -1; |
522 |
|
|
free_if_alloc (buf); |
523 |
|
|
return val; |
524 |
|
|
} |
525 |
|
|
|
526 |
|
|
|
527 |
|
|
/* Saves the winpt preferences in the registry. */ |
528 |
|
|
int |
529 |
|
|
set_reg_winpt_prefs (winpt_reg_prefs_s * opt) |
530 |
|
|
{ |
531 |
|
|
char buf[128]; |
532 |
twoaday |
73 |
size_t i; |
533 |
|
|
int rc = 0; |
534 |
werner |
36 |
|
535 |
|
|
for (i=1; i < DIM (cfg); i++) { |
536 |
|
|
switch (i) { |
537 |
|
|
case CFG_CACHETIME: |
538 |
|
|
sprintf (buf, "%d", opt->cache_time); |
539 |
|
|
break; |
540 |
|
|
case CFG_WORDWRAP: |
541 |
|
|
sprintf (buf, "%d", opt->word_wrap); |
542 |
|
|
break; |
543 |
|
|
case CFG_WIPEMODE: |
544 |
|
|
sprintf (buf, "%d", opt->wipe_mode); |
545 |
|
|
break; |
546 |
|
|
case CFG_FASTMODE: |
547 |
|
|
sprintf (buf, "%d", opt->use_tmpfiles); |
548 |
|
|
break; |
549 |
|
|
case CFG_NOZIP_MMEDIA: |
550 |
|
|
sprintf (buf, "%d", opt->no_zip_mmedia); |
551 |
|
|
break; |
552 |
|
|
case CFG_VIEWER: |
553 |
|
|
sprintf (buf, "%d", opt->use_viewer); |
554 |
|
|
break; |
555 |
|
|
case CFG_ALWAYSTRUST: |
556 |
|
|
sprintf (buf, "%d", opt->always_trust); |
557 |
|
|
break; |
558 |
|
|
case CFG_AUTOBACKUP: |
559 |
|
|
sprintf (buf, "%d", opt->auto_backup); |
560 |
|
|
break; |
561 |
|
|
case CFG_AUTOBAKMODE: |
562 |
|
|
sprintf (buf, "%d", opt->backup.mode); |
563 |
|
|
break; |
564 |
|
|
case CFG_DISHOTKEYS: |
565 |
|
|
sprintf (buf, "%d", opt->no_hotkeys); |
566 |
|
|
break; |
567 |
|
|
case CFG_EXPERT: |
568 |
|
|
sprintf (buf, "%d", opt->expert); |
569 |
|
|
break; |
570 |
|
|
|
571 |
|
|
case CFG_FM_PROGRESS: |
572 |
|
|
sprintf (buf, "%d", opt->fm.progress); |
573 |
|
|
break; |
574 |
|
|
} |
575 |
|
|
rc = set_reg_entry (HKEY_CURRENT_USER, WINPT_REG, cfg[i], buf); |
576 |
|
|
if (rc) |
577 |
|
|
goto leave; |
578 |
|
|
} |
579 |
|
|
|
580 |
|
|
if (opt->backup.path) { |
581 |
|
|
rc = set_reg_entry (HKEY_CURRENT_USER, WINPT_REG, "BackupPath", |
582 |
|
|
opt->backup.path); |
583 |
|
|
if (rc) |
584 |
|
|
goto leave; |
585 |
|
|
} |
586 |
|
|
|
587 |
|
|
for (i=0; reg_hotkeys[i].reg_entry; i++) { |
588 |
|
|
strcpy (buf, " "); |
589 |
|
|
if (reg_hotkeys[i].enabled) |
590 |
|
|
strcpy (buf, reg_hotkeys[i].key); |
591 |
|
|
rc = set_reg_key (HKEY_CURRENT_USER, WINPT_REG, |
592 |
|
|
reg_hotkeys[i].reg_entry, buf); |
593 |
|
|
if (rc) |
594 |
|
|
break; |
595 |
|
|
} |
596 |
|
|
|
597 |
|
|
leave: |
598 |
|
|
if (rc) { |
599 |
|
|
msg_box (NULL, _("Could not write to Registry."), _("Preferences"), MB_ERR); |
600 |
|
|
return rc; |
601 |
|
|
} |
602 |
|
|
return 0; |
603 |
|
|
} |
604 |
|
|
|
605 |
|
|
|
606 |
|
|
int |
607 |
|
|
set_reg_winpt_flag (const char * name, int val) |
608 |
|
|
{ |
609 |
|
|
return set_reg_entry (HKEY_CURRENT_USER, WINPT_REG, name, val? "1" : "0"); |
610 |
|
|
} /* set_reg_winpt_flag */ |
611 |
|
|
|
612 |
|
|
|
613 |
|
|
int |
614 |
|
|
get_reg_winpt_flag (const char * name) |
615 |
|
|
{ |
616 |
|
|
char * buf; |
617 |
|
|
int flag = 0; |
618 |
|
|
|
619 |
|
|
buf = get_reg_entry (HKEY_CURRENT_USER, WINPT_REG, name); |
620 |
|
|
if (buf && buf[0] == '1') |
621 |
|
|
flag = 1; |
622 |
|
|
else if (!buf || buf && buf[0] != '0') |
623 |
|
|
flag = -1; |
624 |
|
|
free_if_alloc (buf); |
625 |
|
|
return flag; |
626 |
|
|
} /* get_reg_winpt_flag */ |
627 |
|
|
|
628 |
|
|
|
629 |
|
|
/* Retrieve the winpt preferences from the registry. */ |
630 |
|
|
int |
631 |
|
|
get_reg_winpt_prefs (winpt_reg_prefs_s * opt) |
632 |
|
|
{ |
633 |
twoaday |
117 |
char *val = NULL; |
634 |
twoaday |
73 |
size_t i; |
635 |
werner |
36 |
|
636 |
|
|
for (i=1; i < DIM (cfg); i++) { |
637 |
|
|
val = get_reg_entry (HKEY_CURRENT_USER, WINPT_REG, cfg[i]); |
638 |
|
|
if (!val || *val == ' ') { |
639 |
|
|
free_if_alloc (val); |
640 |
|
|
continue; |
641 |
|
|
} |
642 |
|
|
switch (i) { |
643 |
|
|
case CFG_CACHETIME: |
644 |
|
|
opt->cache_time = atol (val); |
645 |
|
|
/* We do NOT support passphrase caching for more than an hour. |
646 |
|
|
* Perhaps we should allow it, but for now we silently drop this. |
647 |
|
|
*/ |
648 |
|
|
if (opt->cache_time > 3600) |
649 |
|
|
opt->cache_time = 3600; |
650 |
|
|
break; |
651 |
|
|
case CFG_WORDWRAP: |
652 |
|
|
opt->word_wrap = atol (val); |
653 |
|
|
break; |
654 |
|
|
case CFG_FASTMODE: |
655 |
|
|
opt->use_tmpfiles = atol (val); |
656 |
|
|
break; |
657 |
|
|
case CFG_NOZIP_MMEDIA: |
658 |
|
|
opt->no_zip_mmedia = atol (val); |
659 |
|
|
break; |
660 |
|
|
case CFG_VIEWER: |
661 |
|
|
opt->use_viewer = atol (val); |
662 |
|
|
break; |
663 |
|
|
case CFG_WIPEMODE: |
664 |
|
|
opt->wipe_mode = atol (val); |
665 |
|
|
break; |
666 |
|
|
case CFG_DISHOTKEYS: |
667 |
|
|
opt->no_hotkeys = atol (val); |
668 |
|
|
break; |
669 |
|
|
case CFG_ALWAYSTRUST: |
670 |
|
|
opt->always_trust = atol (val); |
671 |
|
|
break; |
672 |
|
|
case CFG_AUTOBACKUP: |
673 |
|
|
opt->auto_backup = atol (val); |
674 |
|
|
break; |
675 |
|
|
case CFG_AUTOBAKMODE: |
676 |
|
|
opt->backup.mode = atol (val); |
677 |
|
|
break; |
678 |
|
|
case CFG_EXPERT: |
679 |
|
|
opt->expert = atol (val); |
680 |
|
|
break; |
681 |
|
|
|
682 |
|
|
case CFG_FM_PROGRESS: |
683 |
|
|
opt->fm.progress = atol (val); |
684 |
|
|
break; |
685 |
|
|
} |
686 |
|
|
free_if_alloc (val); |
687 |
|
|
} |
688 |
|
|
|
689 |
|
|
val = get_reg_entry (HKEY_CURRENT_USER, WINPT_REG, "BackupPath"); |
690 |
|
|
if (val && val[0] != ' ') |
691 |
|
|
opt->backup.path = m_strdup (val); |
692 |
|
|
free_if_alloc (val); |
693 |
|
|
|
694 |
|
|
for (i=0; reg_hotkeys[i].reg_entry; i++) { |
695 |
|
|
val = get_reg_entry (HKEY_CURRENT_USER, WINPT_REG, reg_hotkeys[i].reg_entry); |
696 |
|
|
if (val && val[0] != ' ') { |
697 |
|
|
reg_hotkeys[i].key[0] = *val; |
698 |
|
|
reg_hotkeys[i].enabled = 1; |
699 |
|
|
} |
700 |
|
|
else |
701 |
|
|
reg_hotkeys[i].enabled = 0; |
702 |
|
|
free_if_alloc (val); |
703 |
|
|
} |
704 |
|
|
return 0; |
705 |
|
|
} /* get_reg_winpt_prefs */ |
706 |
|
|
|
707 |
|
|
|
708 |
|
|
char* |
709 |
|
|
get_reg_entry_keyserver (const char *name) |
710 |
|
|
{ |
711 |
|
|
char * p = get_reg_entry (HKEY_CURRENT_USER, WINPT_REG"\\Keyserver", name); |
712 |
|
|
if (p && !strcmp (p, "")) { |
713 |
|
|
free_if_alloc (p); |
714 |
|
|
return NULL; |
715 |
|
|
} |
716 |
|
|
return p; |
717 |
|
|
} |
718 |
|
|
|
719 |
|
|
|
720 |
|
|
void |
721 |
|
|
get_reg_proxy_prefs (char ** host, int * port, char ** user, char ** pass) |
722 |
|
|
{ |
723 |
|
|
if (host) |
724 |
|
|
*host = get_reg_entry_keyserver ("Host"); |
725 |
|
|
if (user) |
726 |
|
|
*user = get_reg_entry_keyserver ("User"); |
727 |
|
|
if (pass) |
728 |
|
|
*pass = get_reg_entry_keyserver ("Pass"); |
729 |
|
|
if (port) { |
730 |
|
|
char * p = get_reg_entry_keyserver ("Port"); |
731 |
|
|
if (p) { |
732 |
|
|
*port = atol (p); |
733 |
|
|
free_if_alloc (p); |
734 |
|
|
} |
735 |
|
|
} |
736 |
|
|
} /* get_reg_proxy_prefs */ |
737 |
|
|
|
738 |
|
|
|
739 |
|
|
int |
740 |
|
|
set_reg_entry_keyserver( const char * name, const char * val ) |
741 |
|
|
{ |
742 |
|
|
return set_reg_entry( HKEY_CURRENT_USER, WINPT_REG"\\Keyserver", name, val ); |
743 |
|
|
} /* set_reg_entry_keyserver */ |
744 |
|
|
|
745 |
|
|
|
746 |
|
|
int |
747 |
|
|
set_reg_proxy_prefs( const char * host, int port, const char * user, const char * pass ) |
748 |
|
|
{ |
749 |
|
|
int rc; |
750 |
|
|
|
751 |
|
|
rc = set_reg_entry_keyserver( "Host", host? host : "" ); |
752 |
|
|
if( !rc ) { |
753 |
|
|
char buf[32]; |
754 |
|
|
sprintf( buf, "%d", port ); |
755 |
|
|
rc = set_reg_entry_keyserver( "Port", buf ); |
756 |
|
|
} |
757 |
|
|
if( !rc ) |
758 |
|
|
rc = set_reg_entry_keyserver( "User", user? user : "" ); |
759 |
|
|
if( !rc ) |
760 |
|
|
rc = set_reg_entry_keyserver( "Pass", pass? pass : "" ); |
761 |
|
|
return rc; |
762 |
|
|
} /* set_reg_proxy_prefs */ |