1 |
/* wptRegistryMobile.cpp - Registry emulation for the mobile mode |
2 |
* Copyright (C) 2006, 2007 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 |
#ifdef HAVE_CONFIG_H |
17 |
#include <config.h> |
18 |
#endif |
19 |
|
20 |
#ifdef WINPT_MOBILE |
21 |
#include <windows.h> |
22 |
|
23 |
#include "wptTypes.h" |
24 |
#include "wptGPG.h" |
25 |
#include "wptRegistry.h" |
26 |
#include "wptHotkey.h" |
27 |
#include "wptW32API.h" |
28 |
#include "wptErrors.h" |
29 |
|
30 |
|
31 |
static config_file_t cfg_file = NULL; |
32 |
|
33 |
struct winpt_prefs_s reg_prefs; |
34 |
|
35 |
/* Because we do not use any existing software, we hardcode |
36 |
the return to FALSE. */ |
37 |
int is_gpg4win_installed (void) |
38 |
{ |
39 |
return 0; |
40 |
} |
41 |
|
42 |
/* XXX: the settings should be stored as 'winpt_prefs.txt' |
43 |
because in mobile mode all files should be localted |
44 |
in the same directory. */ |
45 |
|
46 |
void |
47 |
free_reg_prefs (void) |
48 |
{ |
49 |
if (cfg_file != NULL) { |
50 |
commit_config ("c:\\winpt_prefs.txt", &cfg_file); |
51 |
release_config (cfg_file); |
52 |
} |
53 |
cfg_file = NULL; |
54 |
} |
55 |
|
56 |
|
57 |
static gpg_option_t |
58 |
reg_find_option (const char *key) |
59 |
{ |
60 |
if (cfg_file == NULL) |
61 |
parse_config ("c:\\winpt_prefs.txt", &pr); |
62 |
return find_option (pr, key); |
63 |
} |
64 |
|
65 |
|
66 |
int |
67 |
set_reg_winpt_prefs (winpt_prefs_t opt) |
68 |
{ |
69 |
char buf[128]; |
70 |
size_t i; |
71 |
|
72 |
if (cfg_file == NULL) |
73 |
new_config (&cfg_file); |
74 |
|
75 |
for (i=1; i < DIM (cfg); i++) { |
76 |
switch (i) { |
77 |
case CFG_CACHETIME: |
78 |
sprintf (buf, "%d", opt->cache_time); |
79 |
break; |
80 |
case CFG_WORDWRAP: |
81 |
sprintf (buf, "%d", opt->word_wrap); |
82 |
break; |
83 |
case CFG_WIPEMODE: |
84 |
sprintf (buf, "%d", opt->wipe_mode); |
85 |
break; |
86 |
case CFG_FILEEXT: |
87 |
sprintf (buf, "%d", opt->default_ext); |
88 |
break; |
89 |
case CFG_NOZIP_MMEDIA: |
90 |
sprintf (buf, "%d", opt->no_zip_mmedia); |
91 |
break; |
92 |
case CFG_VIEWER: |
93 |
sprintf (buf, "%d", opt->use_viewer); |
94 |
break; |
95 |
case CFG_ALWAYSTRUST: |
96 |
sprintf (buf, "%d", opt->always_trust); |
97 |
break; |
98 |
case CFG_AUTOBACKUP: |
99 |
sprintf (buf, "%d", opt->auto_backup); |
100 |
break; |
101 |
case CFG_AUTOBAKMODE: |
102 |
sprintf (buf, "%d", opt->backup.mode); |
103 |
break; |
104 |
case CFG_DISHOTKEYS: |
105 |
sprintf (buf, "%d", opt->no_hotkeys); |
106 |
break; |
107 |
case CFG_EXPERT: |
108 |
sprintf (buf, "%d", opt->expert); |
109 |
break; |
110 |
|
111 |
case CFG_FM_PROGRESS: |
112 |
sprintf (buf, "%d", opt->fm.progress); |
113 |
break; |
114 |
|
115 |
case CFG_BACKUP_INC_SKR: |
116 |
sprintf (buf, "%d", opt->backup.include_secr); |
117 |
break; |
118 |
} |
119 |
add_entry (pr, ENTRY_MULTI, cfg[i], buf); |
120 |
} |
121 |
|
122 |
if (opt->backup.path != NULL) |
123 |
add_entry (pr, ENTRY_MULTI, "BackupPath", opt->backup.path); |
124 |
|
125 |
for (i=0; wpt_hotkeys[i].name; i++) { |
126 |
if (wpt_hotkeys[i].enabled) { |
127 |
buf[0] = wpt_hotkeys[i].key; |
128 |
buf[1] = 0; |
129 |
add_entry (pr, ENTRY_MULTI, wpt_hotkeys[i].name, buf); |
130 |
} |
131 |
} |
132 |
|
133 |
return 0; |
134 |
} |
135 |
|
136 |
|
137 |
/* Retrieve the winpt preferences from the registry. */ |
138 |
int |
139 |
get_reg_winpt_prefs (winpt_prefs_t opt) |
140 |
{ |
141 |
gpg_option_t val; |
142 |
size_t i; |
143 |
|
144 |
for (i=1; i < DIM (cfg); i++) { |
145 |
val = reg_find_option (cfg[i]); |
146 |
if (!val) |
147 |
continue; |
148 |
switch (i) { |
149 |
case CFG_CACHETIME: |
150 |
opt->cache_time = atoi (val->val); |
151 |
break; |
152 |
case CFG_WORDWRAP: |
153 |
opt->word_wrap = atoi (val->val); |
154 |
break; |
155 |
case CFG_FILEEXT: |
156 |
opt->default_ext = atoi (val->val); |
157 |
break; |
158 |
case CFG_NOZIP_MMEDIA: |
159 |
opt->no_zip_mmedia = atoi (val->val); |
160 |
break; |
161 |
case CFG_VIEWER: |
162 |
opt->use_viewer = atoi (val->val); |
163 |
break; |
164 |
case CFG_WIPEMODE: |
165 |
opt->wipe_mode = atoi (val->val); |
166 |
break; |
167 |
case CFG_DISHOTKEYS: |
168 |
opt->no_hotkeys = atoi (val->val); |
169 |
break; |
170 |
case CFG_ALWAYSTRUST: |
171 |
opt->always_trust = atoi (val->val); |
172 |
break; |
173 |
case CFG_AUTOBACKUP: |
174 |
opt->auto_backup = atoi (val->val); |
175 |
break; |
176 |
case CFG_AUTOBAKMODE: |
177 |
opt->backup.mode = atoi (val->val); |
178 |
break; |
179 |
case CFG_EXPERT: |
180 |
opt->expert = atoi (val->val); |
181 |
break; |
182 |
case CFG_FM_PROGRESS: |
183 |
opt->fm.progress = atoi (val->val); |
184 |
break; |
185 |
|
186 |
case CFG_BACKUP_INC_SKR: |
187 |
opt->backup.include_secr = atoi (val->val); |
188 |
break; |
189 |
} |
190 |
} |
191 |
|
192 |
val = reg_find_option("BackupPath"); |
193 |
if (val != NULL) |
194 |
opt->backup.path = m_strdup (val->val); |
195 |
|
196 |
for (i=0; wpt_hotkeys[i].name; i++) { |
197 |
wpt_hotkeys[i].enabled = 0; |
198 |
val = reg_find_option (wpt_hotkeys[i].name); |
199 |
if (val != NULL && val->val && val->val[0] != ' ') { |
200 |
wpt_hotkeys[i].key = *val->val; |
201 |
wpt_hotkeys[i].enabled = 1; |
202 |
} |
203 |
} |
204 |
return 0; |
205 |
} |
206 |
|
207 |
|
208 |
|
209 |
/* Just a dummy needed for 1-to-1 mapping. */ |
210 |
int |
211 |
regist_inst_gnupg (int create_mokey) |
212 |
{ |
213 |
return 0; |
214 |
} |
215 |
|
216 |
|
217 |
|
218 |
/* Just a dummy for 1-to-1 mapping. */ |
219 |
int |
220 |
regist_inst_winpt (int with_ext, int *created) |
221 |
{ |
222 |
return 0; |
223 |
} |
224 |
|
225 |
|
226 |
char* |
227 |
get_reg_entry (HKEY root_key, const char *dir, const char *key) |
228 |
{ |
229 |
gpg_option_t val; |
230 |
|
231 |
val = reg_find_option (key); |
232 |
if (!val) |
233 |
return NULL; |
234 |
return m_strdup (val->val); |
235 |
} |
236 |
|
237 |
|
238 |
int |
239 |
set_reg_entry (HKEY root_key, const char *dir, const char *key, |
240 |
const char *value) |
241 |
{ |
242 |
modify_entry (pr, ENTRY_MULTI, key, value); |
243 |
return 0; |
244 |
} |
245 |
|
246 |
|
247 |
int |
248 |
set_reg_key (HKEY root_key, const char *dir, const char *key, |
249 |
const char *value) |
250 |
{ |
251 |
|
252 |
set_reg_entry (root_key, dir, key, value); |
253 |
return 0; |
254 |
} |
255 |
|
256 |
|
257 |
int |
258 |
set_reg_entry_gpg (const char *key, const char *value) |
259 |
{ |
260 |
return set_reg_entry (HKEY_CURRENT_USER, NULL, key, value); |
261 |
} |
262 |
|
263 |
|
264 |
int |
265 |
set_reg_entry_mo (const char *value) |
266 |
{ |
267 |
return set_reg_entry (HKEY_CURRENT_USER, NULL, "MODir", value); |
268 |
} |
269 |
|
270 |
|
271 |
char* |
272 |
get_reg_entry_gpg (const char *key) |
273 |
{ |
274 |
gpg_option_t val; |
275 |
|
276 |
val = reg_find_option (key); |
277 |
if (!val) |
278 |
return NULL; |
279 |
return m_strdup (val->val); |
280 |
} |
281 |
|
282 |
|
283 |
char* |
284 |
get_reg_entry_gpg4win (const char *path) |
285 |
{ |
286 |
return NULL; |
287 |
} |
288 |
|
289 |
|
290 |
char* |
291 |
get_reg_entry_mo (void) |
292 |
{ |
293 |
gpg_option_t val; |
294 |
|
295 |
val = reg_find_option ("MODir"); |
296 |
if (val != NULL) |
297 |
return m_strdup (val->val); |
298 |
return NULL; |
299 |
} |
300 |
|
301 |
|
302 |
int |
303 |
set_reg_winpt_single (int id, int val) |
304 |
{ |
305 |
char buf[64]; |
306 |
int rc; |
307 |
|
308 |
_snprintf (buf, DIM (buf)-1, "%d", val); |
309 |
rc = set_reg_entry (HKEY_CURRENT_USER, NULL, cfg[id], buf); |
310 |
return rc; |
311 |
} |
312 |
|
313 |
|
314 |
int |
315 |
get_reg_winpt_single (int id) |
316 |
{ |
317 |
char *buf; |
318 |
int val = 0; |
319 |
|
320 |
buf = get_reg_entry (HKEY_CURRENT_USER, NULL, cfg[id]); |
321 |
if (buf && *buf != ' ') |
322 |
val = 1; |
323 |
else if (!buf) |
324 |
val = -1; |
325 |
free_if_alloc (buf); |
326 |
return val; |
327 |
} |
328 |
|
329 |
|
330 |
int |
331 |
set_reg_winpt_flag (const char *name, int val) |
332 |
{ |
333 |
return set_reg_entry (HKEY_CURRENT_USER, NULL, name, val? "1" : "0"); |
334 |
} |
335 |
|
336 |
|
337 |
int |
338 |
get_reg_winpt_flag (const char *name) |
339 |
{ |
340 |
char *buf; |
341 |
int flag = 0; |
342 |
|
343 |
buf = get_reg_entry (HKEY_CURRENT_USER, NULL, name); |
344 |
if (buf && buf[0] == '1') |
345 |
flag = 1; |
346 |
else if (!buf || buf && buf[0] != '0') |
347 |
flag = -1; |
348 |
free_if_alloc (buf); |
349 |
return flag; |
350 |
} |
351 |
|
352 |
|
353 |
char* |
354 |
get_reg_entry_keyserver (const char *name) |
355 |
{ |
356 |
return get_reg_entry (HKEY_CURRENT_USER, NULL, name); |
357 |
} |
358 |
|
359 |
|
360 |
int |
361 |
set_reg_entry_keyserver (const char *name, const char *val) |
362 |
{ |
363 |
return set_reg_entry (HKEY_CURRENT_USER, NULL, name, val); |
364 |
} |
365 |
#endif |