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