blob: 748f2146116078cd006a6ed08377e8b288495050 [file] [log] [blame]
Dimitrie O. Paun82ce2cc2003-03-31 19:41:55 +00001/*
2 * WineCfg configuration management
3 *
4 * Copyright 2002 Jaco Greeff
5 * Copyright 2003 Dimitrie O. Paun
Mike Hearna5ce4ee2004-09-28 03:16:43 +00006 * Copyright 2003-2004 Mike Hearn
Dimitrie O. Paun82ce2cc2003-03-31 19:41:55 +00007 *
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
12 *
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
17 *
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 *
Mike Hearn7d8711e2004-11-23 13:50:23 +000022 * TODO:
23 * - Use unicode
24 * - Icons in listviews/icons
25 * - Better add app dialog, scan c: for EXE files and add to list in background
26 * - Use [GNOME] HIG style groupboxes rather than win32 style (looks nicer, imho)
27 *
Dimitrie O. Paun82ce2cc2003-03-31 19:41:55 +000028 */
29
Mike McCormack55303932006-01-19 11:55:01 +010030#define WIN32_LEAN_AND_MEAN
31
Mike Hearncd0b7892003-09-08 18:58:07 +000032#include <assert.h>
Dimitrie O. Paun82ce2cc2003-03-31 19:41:55 +000033#include <stdio.h>
34#include <limits.h>
35#include <windows.h>
Matthew Davison5101dfc2003-04-27 00:33:07 +000036#include <winreg.h>
37#include <wine/debug.h>
Mike Hearn0af614e2004-09-28 03:55:16 +000038#include <wine/list.h>
Matthew Davison5101dfc2003-04-27 00:33:07 +000039
40WINE_DEFAULT_DEBUG_CHANNEL(winecfg);
Dimitrie O. Paun82ce2cc2003-03-31 19:41:55 +000041
42#include "winecfg.h"
Robert Reiff0c55e72005-12-17 12:30:06 +010043#include "resource.h"
Dimitrie O. Paun82ce2cc2003-03-31 19:41:55 +000044
Mike Hearn0af614e2004-09-28 03:55:16 +000045HKEY config_key = NULL;
Robert Reiff0c55e72005-12-17 12:30:06 +010046HMENU hPopupMenus = 0;
Dimitrie O. Paun82ce2cc2003-03-31 19:41:55 +000047
Mike Hearn0af614e2004-09-28 03:55:16 +000048
49/* this is called from the WM_SHOWWINDOW handlers of each tab page.
50 *
51 * it's a nasty hack, necessary because the property sheet insists on resetting the window title
52 * to the title of the tab, which is utterly useless. dropping the property sheet is on the todo list.
53 */
54void set_window_title(HWND dialog)
55{
Paul Vriens3c3a7722006-02-16 19:44:53 +010056 char newtitle[256];
Mike Hearn0af614e2004-09-28 03:55:16 +000057
58 /* update the window title */
Mike Hearn7d8711e2004-11-23 13:50:23 +000059 if (current_app)
Mike Hearn0af614e2004-09-28 03:55:16 +000060 {
Paul Vriens3c3a7722006-02-16 19:44:53 +010061 char apptitle[256];
62 LoadString(GetModuleHandle(NULL), IDS_WINECFG_TITLE_APP, apptitle, 256);
63 sprintf(newtitle, apptitle, current_app);
Mike Hearncd0b7892003-09-08 18:58:07 +000064 }
Mike Hearn0af614e2004-09-28 03:55:16 +000065 else
66 {
Paul Vriens3c3a7722006-02-16 19:44:53 +010067 LoadString(GetModuleHandle(NULL), IDS_WINECFG_TITLE, newtitle, 256);
Mike Hearn0af614e2004-09-28 03:55:16 +000068 }
69
70 WINE_TRACE("setting title to %s\n", newtitle);
71 SendMessage(GetParent(dialog), PSM_SETTITLE, 0, (LPARAM) newtitle);
Dimitrie O. Paun82ce2cc2003-03-31 19:41:55 +000072}
73
Mike Hearncd0b7892003-09-08 18:58:07 +000074
Mike Hearn0af614e2004-09-28 03:55:16 +000075/**
Gerald Pfeifer61f8e122004-11-30 21:03:36 +000076 * get_config_key: Retrieves a configuration value from the registry
Mike Hearn4e1afc62003-08-30 00:27:08 +000077 *
Mike Hearn0af614e2004-09-28 03:55:16 +000078 * char *subkey : the name of the config section
79 * char *name : the name of the config value
80 * char *default : if the key isn't found, return this value instead
Mike Hearn4e1afc62003-08-30 00:27:08 +000081 *
Mike Hearn0af614e2004-09-28 03:55:16 +000082 * Returns a buffer holding the value if successful, NULL if
83 * not. Caller is responsible for releasing the result.
Mike Hearn4e1afc62003-08-30 00:27:08 +000084 *
Matthew Davison5101dfc2003-04-27 00:33:07 +000085 */
Michael Jung3bbb7fd2005-06-23 11:42:54 +000086static char *get_config_key (HKEY root, const char *subkey, const char *name, const char *def)
Matthew Davison5101dfc2003-04-27 00:33:07 +000087{
Mike McCormack88bddd72005-08-19 15:19:10 +000088 LPSTR buffer = NULL;
Mike Hearn0af614e2004-09-28 03:55:16 +000089 DWORD len;
Mike Hearn4e1afc62003-08-30 00:27:08 +000090 HKEY hSubKey = NULL;
Mike Hearncd0b7892003-09-08 18:58:07 +000091 DWORD res;
Matthew Davison5101dfc2003-04-27 00:33:07 +000092
Mike Hearn0af614e2004-09-28 03:55:16 +000093 WINE_TRACE("subkey=%s, name=%s, def=%s\n", subkey, name, def);
Mike Hearncd0b7892003-09-08 18:58:07 +000094
Michael Jung3bbb7fd2005-06-23 11:42:54 +000095 res = RegOpenKey(root, subkey, &hSubKey);
Mike Hearn0af614e2004-09-28 03:55:16 +000096 if (res != ERROR_SUCCESS)
97 {
98 if (res == ERROR_FILE_NOT_FOUND)
Matthew Davison5101dfc2003-04-27 00:33:07 +000099 {
Mike Hearn4e1afc62003-08-30 00:27:08 +0000100 WINE_TRACE("Section key not present - using default\n");
Mike Hearn0af614e2004-09-28 03:55:16 +0000101 return def ? strdupA(def) : NULL;
Matthew Davison5101dfc2003-04-27 00:33:07 +0000102 }
103 else
104 {
Mike Hearn4e1afc62003-08-30 00:27:08 +0000105 WINE_ERR("RegOpenKey failed on wine config key (res=%ld)\n", res);
Matthew Davison5101dfc2003-04-27 00:33:07 +0000106 }
107 goto end;
108 }
Mike Hearn498e1ce2003-09-30 00:27:55 +0000109
Mike Hearn0af614e2004-09-28 03:55:16 +0000110 res = RegQueryValueExA(hSubKey, name, NULL, NULL, NULL, &len);
111 if (res == ERROR_FILE_NOT_FOUND)
112 {
Matthew Davison5101dfc2003-04-27 00:33:07 +0000113 WINE_TRACE("Value not present - using default\n");
Mike Hearn0af614e2004-09-28 03:55:16 +0000114 buffer = def ? strdupA(def) : NULL;
Mike Hearncd0b7892003-09-08 18:58:07 +0000115 goto end;
Mike Hearn0af614e2004-09-28 03:55:16 +0000116 } else if (res != ERROR_SUCCESS)
117 {
118 WINE_ERR("Couldn't query value's length (res=%ld)\n", res);
Matthew Davison5101dfc2003-04-27 00:33:07 +0000119 goto end;
120 }
121
Mike Hearn0af614e2004-09-28 03:55:16 +0000122 buffer = HeapAlloc(GetProcessHeap(), 0, len + 1);
123
Mike McCormack88bddd72005-08-19 15:19:10 +0000124 RegQueryValueEx(hSubKey, name, NULL, NULL, (LPBYTE) buffer, &len);
Mike Hearn0af614e2004-09-28 03:55:16 +0000125
126 WINE_TRACE("buffer=%s\n", buffer);
Matthew Davison5101dfc2003-04-27 00:33:07 +0000127end:
Michael Jung3bbb7fd2005-06-23 11:42:54 +0000128 if (hSubKey && hSubKey != root) RegCloseKey(hSubKey);
Matthew Davison5101dfc2003-04-27 00:33:07 +0000129
Mike McCormack88bddd72005-08-19 15:19:10 +0000130 return buffer;
Matthew Davison5101dfc2003-04-27 00:33:07 +0000131}
132
Mike Hearn0af614e2004-09-28 03:55:16 +0000133/**
Gerald Pfeifer61f8e122004-11-30 21:03:36 +0000134 * set_config_key: convenience wrapper to set a key/value pair
Mike Hearn4e1afc62003-08-30 00:27:08 +0000135 *
Daniel Marmier8e5bb202003-10-09 04:39:01 +0000136 * const char *subKey : the name of the config section
137 * const char *valueName : the name of the config value
138 * const char *value : the value to set the configuration key to
Mike Hearn4e1afc62003-08-30 00:27:08 +0000139 *
140 * Returns 0 on success, non-zero otherwise
Mike Hearn0af614e2004-09-28 03:55:16 +0000141 *
142 * If valueName or value is NULL, an empty section will be created
Mike Hearn4e1afc62003-08-30 00:27:08 +0000143 */
Mike McCormack88bddd72005-08-19 15:19:10 +0000144static int set_config_key(HKEY root, const char *subkey, const char *name, const void *value, DWORD type) {
Mike Hearn4e1afc62003-08-30 00:27:08 +0000145 DWORD res = 1;
146 HKEY key = NULL;
147
Mike McCormack88bddd72005-08-19 15:19:10 +0000148 WINE_TRACE("subkey=%s: name=%s, value=%p, type=%ld\n", subkey, name, value, type);
Mike Hearncd0b7892003-09-08 18:58:07 +0000149
150 assert( subkey != NULL );
Mike Hearn4e1afc62003-08-30 00:27:08 +0000151
Alexandre Julliard0d3bddc2005-06-17 21:09:07 +0000152 if (subkey[0])
153 {
Michael Jung3bbb7fd2005-06-23 11:42:54 +0000154 res = RegCreateKey(root, subkey, &key);
Alexandre Julliard0d3bddc2005-06-17 21:09:07 +0000155 if (res != ERROR_SUCCESS) goto end;
156 }
Michael Jung3bbb7fd2005-06-23 11:42:54 +0000157 else key = root;
Mike Hearn0af614e2004-09-28 03:55:16 +0000158 if (name == NULL || value == NULL) goto end;
159
Vitaliy Margolenaad2fe3b2005-08-17 11:37:34 +0000160 switch (type)
161 {
162 case REG_SZ: res = RegSetValueEx(key, name, 0, REG_SZ, value, strlen(value) + 1); break;
163 case REG_DWORD: res = RegSetValueEx(key, name, 0, REG_DWORD, value, sizeof(DWORD)); break;
164 }
Mike Hearn4e1afc62003-08-30 00:27:08 +0000165 if (res != ERROR_SUCCESS) goto end;
166
167 res = 0;
168end:
Michael Jung3bbb7fd2005-06-23 11:42:54 +0000169 if (key && key != root) RegCloseKey(key);
Mike McCormack88bddd72005-08-19 15:19:10 +0000170 if (res != 0) WINE_ERR("Unable to set configuration key %s in section %s, res=%ld\n", name, subkey, res);
Mike Hearn4e1afc62003-08-30 00:27:08 +0000171 return res;
172}
173
Mike Hearn0af614e2004-09-28 03:55:16 +0000174/* removes the requested value from the registry, however, does not
175 * remove the section if empty. Returns S_OK (0) on success.
176 */
Michael Jung3bbb7fd2005-06-23 11:42:54 +0000177static HRESULT remove_value(HKEY root, const char *subkey, const char *name)
Mike Hearn0af614e2004-09-28 03:55:16 +0000178{
Mike Hearncd0b7892003-09-08 18:58:07 +0000179 HRESULT hr;
180 HKEY key;
181
Mike Hearn0af614e2004-09-28 03:55:16 +0000182 WINE_TRACE("subkey=%s, name=%s\n", subkey, name);
Mike Hearncd0b7892003-09-08 18:58:07 +0000183
Michael Jung3bbb7fd2005-06-23 11:42:54 +0000184 hr = RegOpenKey(root, subkey, &key);
Mike Hearncd0b7892003-09-08 18:58:07 +0000185 if (hr != S_OK) return hr;
186
Mike Hearn0af614e2004-09-28 03:55:16 +0000187 hr = RegDeleteValue(key, name);
Mike Hearncd0b7892003-09-08 18:58:07 +0000188 if (hr != ERROR_SUCCESS) return hr;
189
190 return S_OK;
191}
192
Mike Hearn0af614e2004-09-28 03:55:16 +0000193/* removes the requested subkey from the registry, assuming it exists */
Vitaliy Margolenf4db5df2005-10-13 13:44:38 +0000194static LONG remove_path(HKEY root, char *section) {
195 HKEY branch_key;
196 DWORD max_sub_key_len;
197 DWORD subkeys;
198 DWORD curr_len;
199 LONG ret = ERROR_SUCCESS;
200 long int i;
201 char *buffer;
202
Mike Hearn5a2cde62003-09-17 22:40:38 +0000203 WINE_TRACE("section=%s\n", section);
204
Vitaliy Margolenf4db5df2005-10-13 13:44:38 +0000205 if ((ret = RegOpenKey(root, section, &branch_key)) != ERROR_SUCCESS)
206 return ret;
207
208 /* get size information and resize the buffers if necessary */
209 if ((ret = RegQueryInfoKey(branch_key, NULL, NULL, NULL,
210 &subkeys, &max_sub_key_len,
211 NULL, NULL, NULL, NULL, NULL, NULL
212 )) != ERROR_SUCCESS)
213 return ret;
214
215 curr_len = strlen(section);
216 buffer = HeapAlloc(GetProcessHeap(), 0, max_sub_key_len + curr_len + 1);
217 strcpy(buffer, section);
218
219 buffer[curr_len] = '\\';
220 for (i = subkeys - 1; i >= 0; i--)
221 {
222 DWORD buf_len = max_sub_key_len - curr_len - 1;
223
224 ret = RegEnumKeyEx(branch_key, i, buffer + curr_len + 1,
225 &buf_len, NULL, NULL, NULL, NULL);
226 if (ret != ERROR_SUCCESS && ret != ERROR_MORE_DATA &&
227 ret != ERROR_NO_MORE_ITEMS)
228 break;
229 else
230 remove_path(root, buffer);
231 }
232 HeapFree(GetProcessHeap(), 0, buffer);
233 RegCloseKey(branch_key);
234
Michael Jung3bbb7fd2005-06-23 11:42:54 +0000235 return RegDeleteKey(root, section);
Mike Hearn5a2cde62003-09-17 22:40:38 +0000236}
237
Mike Hearn4e1afc62003-08-30 00:27:08 +0000238
Mike Hearncd0b7892003-09-08 18:58:07 +0000239/* ========================================================================= */
Mike Hearncd0b7892003-09-08 18:58:07 +0000240
Mike Hearn0af614e2004-09-28 03:55:16 +0000241/* This code exists for the following reasons:
242 *
243 * - It makes working with the registry easier
244 * - By storing a mini cache of the registry, we can more easily implement
245 * cancel/revert and apply. The 'settings list' is an overlay on top of
246 * the actual registry data that we can write out at will.
247 *
248 * Rather than model a tree in memory, we simply store each absolute (rooted
249 * at the config key) path.
250 *
251 */
Mike Hearncd0b7892003-09-08 18:58:07 +0000252
Mike Hearn0af614e2004-09-28 03:55:16 +0000253struct setting
Mike Hearn0f4c6c92003-09-10 03:41:44 +0000254{
Mike Hearn0af614e2004-09-28 03:55:16 +0000255 struct list entry;
Michael Jung3bbb7fd2005-06-23 11:42:54 +0000256 HKEY root; /* the key on which path is rooted */
257 char *path; /* path in the registry rooted at root */
Mike Hearnc6191522005-01-03 14:45:05 +0000258 char *name; /* name of the registry value. if null, this means delete the key */
259 char *value; /* contents of the registry value. if null, this means delete the value */
Vitaliy Margolenaad2fe3b2005-08-17 11:37:34 +0000260 DWORD type; /* type of registry value. REG_SZ or REG_DWORD for now */
Mike Hearn0af614e2004-09-28 03:55:16 +0000261};
262
263struct list *settings;
264
265static void free_setting(struct setting *setting)
266{
267 assert( setting != NULL );
Mike Hearnc6191522005-01-03 14:45:05 +0000268 assert( setting->path );
Mike Hearn0af614e2004-09-28 03:55:16 +0000269
Mike Hearnc6191522005-01-03 14:45:05 +0000270 WINE_TRACE("destroying %p: %s\n", setting, setting->path);
271
Mike Hearn0af614e2004-09-28 03:55:16 +0000272 HeapFree(GetProcessHeap(), 0, setting->path);
273 HeapFree(GetProcessHeap(), 0, setting->name);
Michael Stefaniuc5ad7d852004-12-23 17:06:43 +0000274 HeapFree(GetProcessHeap(), 0, setting->value);
Mike Hearn0af614e2004-09-28 03:55:16 +0000275
276 list_remove(&setting->entry);
277
278 HeapFree(GetProcessHeap(), 0, setting);
279}
280
281/**
282 * Returns the contents of the value at path. If not in the settings
283 * list, it will be fetched from the registry - failing that, the
284 * default will be used.
285 *
286 * If already in the list, the contents as given there will be
287 * returned. You are expected to HeapFree the result.
288 */
Michael Jung3bbb7fd2005-06-23 11:42:54 +0000289char *get_reg_key(HKEY root, const char *path, const char *name, const char *def)
Mike Hearn0af614e2004-09-28 03:55:16 +0000290{
291 struct list *cursor;
292 struct setting *s;
293 char *val;
294
295 WINE_TRACE("path=%s, name=%s, def=%s\n", path, name, def);
296
297 /* check if it's in the list */
298 LIST_FOR_EACH( cursor, settings )
299 {
300 s = LIST_ENTRY(cursor, struct setting, entry);
301
Michael Jung3bbb7fd2005-06-23 11:42:54 +0000302 if (root != s->root) continue;
Mike Hearn0af614e2004-09-28 03:55:16 +0000303 if (strcasecmp(path, s->path) != 0) continue;
304 if (strcasecmp(name, s->name) != 0) continue;
305
306 WINE_TRACE("found %s:%s in settings list, returning %s\n", path, name, s->value);
Crestez Leonard3e55df32005-01-14 19:48:41 +0000307 return s->value ? strdupA(s->value) : NULL;
Mike Hearn0af614e2004-09-28 03:55:16 +0000308 }
309
310 /* no, so get from the registry */
Michael Jung3bbb7fd2005-06-23 11:42:54 +0000311 val = get_config_key(root, path, name, def);
Mike Hearn0af614e2004-09-28 03:55:16 +0000312
313 WINE_TRACE("returning %s\n", val);
314
315 return val;
316}
317
318/**
319 * Used to set a registry key.
320 *
321 * path is rooted at the config key, ie use "Version" or
322 * "AppDefaults\\fooapp.exe\\Version". You can use keypath()
323 * to get such a string.
324 *
Mike Hearnc6191522005-01-03 14:45:05 +0000325 * name is the value name, or NULL to delete the path.
Mike Hearn0af614e2004-09-28 03:55:16 +0000326 *
327 * value is what to set the value to, or NULL to delete it.
328 *
Vitaliy Margolenaad2fe3b2005-08-17 11:37:34 +0000329 * type is REG_SZ or REG_DWORD.
330 *
Mike Hearn0af614e2004-09-28 03:55:16 +0000331 * These values will be copied when necessary.
332 */
Vitaliy Margolenaad2fe3b2005-08-17 11:37:34 +0000333static void set_reg_key_ex(HKEY root, const char *path, const char *name, const void *value, DWORD type)
Mike Hearn0af614e2004-09-28 03:55:16 +0000334{
335 struct list *cursor;
336 struct setting *s;
337
338 assert( path != NULL );
Mike Hearn0af614e2004-09-28 03:55:16 +0000339
Vitaliy Margolenaad2fe3b2005-08-17 11:37:34 +0000340 WINE_TRACE("path=%s, name=%s, value=%p\n", path, name, value);
Mike Hearn0af614e2004-09-28 03:55:16 +0000341
342 /* firstly, see if we already set this setting */
343 LIST_FOR_EACH( cursor, settings )
344 {
345 struct setting *s = LIST_ENTRY(cursor, struct setting, entry);
346
Michael Jung3bbb7fd2005-06-23 11:42:54 +0000347 if (root != s->root) continue;
Mike Hearn0af614e2004-09-28 03:55:16 +0000348 if (strcasecmp(s->path, path) != 0) continue;
Mike Hearnc6191522005-01-03 14:45:05 +0000349 if ((s->name && name) && strcasecmp(s->name, name) != 0) continue;
350
351 /* are we attempting a double delete? */
352 if (!s->name && !name) return;
353
354 /* do we want to undelete this key? */
355 if (!s->name && name) s->name = strdupA(name);
Mike Hearn0af614e2004-09-28 03:55:16 +0000356
357 /* yes, we have already set it, so just replace the content and return */
Michael Stefaniuc5ad7d852004-12-23 17:06:43 +0000358 HeapFree(GetProcessHeap(), 0, s->value);
Vitaliy Margolenaad2fe3b2005-08-17 11:37:34 +0000359 s->type = type;
360 switch (type)
361 {
362 case REG_SZ:
363 s->value = value ? strdupA(value) : NULL;
364 break;
365 case REG_DWORD:
366 s->value = HeapAlloc(GetProcessHeap(), 0, sizeof(DWORD));
367 memcpy( s->value, value, sizeof(DWORD) );
368 break;
369 }
Mike Hearn0af614e2004-09-28 03:55:16 +0000370
Mike Hearnc6191522005-01-03 14:45:05 +0000371 /* are we deleting this key? this won't remove any of the
372 * children from the overlay so if the user adds it again in
373 * that session it will appear to undelete the settings, but
374 * in reality only the settings actually modified by the user
375 * in that session will be restored. we might want to fix this
376 * corner case in future by actually deleting all the children
377 * here so that once it's gone, it's gone.
378 */
379 if (!name) s->name = NULL;
380
Mike Hearn0af614e2004-09-28 03:55:16 +0000381 return;
382 }
383
384 /* otherwise add a new setting for it */
385 s = HeapAlloc(GetProcessHeap(), 0, sizeof(struct setting));
Michael Jung3bbb7fd2005-06-23 11:42:54 +0000386 s->root = root;
Mike Hearnc6191522005-01-03 14:45:05 +0000387 s->path = strdupA(path);
388 s->name = name ? strdupA(name) : NULL;
Vitaliy Margolenaad2fe3b2005-08-17 11:37:34 +0000389 s->type = type;
390 switch (type)
391 {
392 case REG_SZ:
393 s->value = value ? strdupA(value) : NULL;
394 break;
395 case REG_DWORD:
396 s->value = HeapAlloc(GetProcessHeap(), 0, sizeof(DWORD));
397 memcpy( s->value, value, sizeof(DWORD) );
398 break;
399 }
Mike Hearn0af614e2004-09-28 03:55:16 +0000400
401 list_add_tail(settings, &s->entry);
402}
403
Vitaliy Margolenaad2fe3b2005-08-17 11:37:34 +0000404void set_reg_key(HKEY root, const char *path, const char *name, const char *value)
405{
406 set_reg_key_ex(root, path, name, value, REG_SZ);
407}
408
409void set_reg_key_dword(HKEY root, const char *path, const char *name, DWORD value)
410{
411 set_reg_key_ex(root, path, name, &value, REG_DWORD);
412}
413
Mike Hearn0af614e2004-09-28 03:55:16 +0000414/**
415 * enumerates the value names at the given path, taking into account
416 * the changes in the settings list.
417 *
418 * you are expected to HeapFree each element of the array, which is null
419 * terminated, as well as the array itself.
420 */
Michael Jung3bbb7fd2005-06-23 11:42:54 +0000421char **enumerate_values(HKEY root, char *path)
Mike Hearn0af614e2004-09-28 03:55:16 +0000422{
423 HKEY key;
424 DWORD res, i = 0;
425 char **values = NULL;
426 int valueslen = 0;
427 struct list *cursor;
428
Michael Jung3bbb7fd2005-06-23 11:42:54 +0000429 res = RegOpenKey(root, path, &key);
Mike Hearn0af614e2004-09-28 03:55:16 +0000430 if (res == ERROR_SUCCESS)
431 {
432 while (TRUE)
433 {
434 char name[1024];
435 DWORD namesize = sizeof(name);
436 BOOL removed = FALSE;
437
438 /* find out the needed size, allocate a buffer, read the value */
439 if ((res = RegEnumValue(key, i, name, &namesize, NULL, NULL, NULL, NULL)) != ERROR_SUCCESS)
440 break;
441
442 WINE_TRACE("name=%s\n", name);
443
444 /* check if this value name has been removed in the settings list */
445 LIST_FOR_EACH( cursor, settings )
446 {
447 struct setting *s = LIST_ENTRY(cursor, struct setting, entry);
448 if (strcasecmp(s->path, path) != 0) continue;
449 if (strcasecmp(s->name, name) != 0) continue;
450
451 if (!s->value)
452 {
453 WINE_TRACE("this key has been removed, so skipping\n");
454 removed = TRUE;
455 break;
456 }
457 }
458
459 if (removed) /* this value was deleted by the user, so don't include it */
460 {
461 HeapFree(GetProcessHeap(), 0, name);
462 i++;
463 continue;
464 }
465
466 /* grow the array if necessary, add buffer to it, iterate */
467 if (values) values = HeapReAlloc(GetProcessHeap(), 0, values, sizeof(char*) * (valueslen + 1));
468 else values = HeapAlloc(GetProcessHeap(), 0, sizeof(char*));
469
470 values[valueslen++] = strdupA(name);
471 WINE_TRACE("valueslen is now %d\n", valueslen);
472 i++;
473 }
474 }
475 else
476 {
477 WINE_WARN("failed opening registry key %s, res=0x%lx\n", path, res);
478 }
479
480 WINE_TRACE("adding settings in list but not registry\n");
481
482 /* now we have to add the values that aren't in the registry but are in the settings list */
483 LIST_FOR_EACH( cursor, settings )
484 {
485 struct setting *setting = LIST_ENTRY(cursor, struct setting, entry);
486 BOOL found = FALSE;
487
488 if (strcasecmp(setting->path, path) != 0) continue;
489
490 if (!setting->value) continue;
491
492 for (i = 0; i < valueslen; i++)
493 {
494 if (strcasecmp(setting->name, values[i]) == 0)
495 {
496 found = TRUE;
497 break;
498 }
499 }
500
501 if (found) continue;
502
503 WINE_TRACE("%s in list but not registry\n", setting->name);
504
505 /* otherwise it's been set by the user but isn't in the registry */
506 if (values) values = HeapReAlloc(GetProcessHeap(), 0, values, sizeof(char*) * (valueslen + 1));
507 else values = HeapAlloc(GetProcessHeap(), 0, sizeof(char*));
508
509 values[valueslen++] = strdupA(setting->name);
510 }
511
512 WINE_TRACE("adding null terminator\n");
513 if (values)
514 {
515 values = HeapReAlloc(GetProcessHeap(), 0, values, sizeof(char*) * (valueslen + 1));
516 values[valueslen] = NULL;
517 }
518
519 RegCloseKey(key);
520
521 return values;
522}
523
524/**
525 * returns true if the given key/value pair exists in the registry or
526 * has been written to.
527 */
Michael Jung3bbb7fd2005-06-23 11:42:54 +0000528BOOL reg_key_exists(HKEY root, const char *path, const char *name)
Mike Hearn0af614e2004-09-28 03:55:16 +0000529{
Michael Jung3bbb7fd2005-06-23 11:42:54 +0000530 char *val = get_reg_key(root, path, name, NULL);
Mike Hearn0af614e2004-09-28 03:55:16 +0000531
532 if (val)
533 {
534 HeapFree(GetProcessHeap(), 0, val);
535 return TRUE;
536 }
537
538 return FALSE;
539}
540
541static void process_setting(struct setting *s)
542{
543 if (s->value)
544 {
545 WINE_TRACE("Setting %s:%s to '%s'\n", s->path, s->name, s->value);
Vitaliy Margolenaad2fe3b2005-08-17 11:37:34 +0000546 set_config_key(s->root, s->path, s->name, s->value, s->type);
Mike Hearn0af614e2004-09-28 03:55:16 +0000547 }
548 else
549 {
550 /* NULL name means remove that path/section entirely */
Michael Jung3bbb7fd2005-06-23 11:42:54 +0000551 if (s->path && s->name) remove_value(s->root, s->path, s->name);
552 else if (s->path && !s->name) remove_path(s->root, s->path);
Mike Hearn0af614e2004-09-28 03:55:16 +0000553 }
554}
555
556void apply(void)
557{
558 if (list_empty(settings)) return; /* we will be called for each page when the user clicks OK */
559
560 WINE_TRACE("()\n");
561
562 while (!list_empty(settings))
563 {
564 struct setting *s = (struct setting *) list_head(settings);
565 process_setting(s);
566 free_setting(s);
Mike Hearncd0b7892003-09-08 18:58:07 +0000567 }
Dimitrie O. Paun82ce2cc2003-03-31 19:41:55 +0000568}
Mike Hearn0f4c6c92003-09-10 03:41:44 +0000569
Mike Hearn0f4c6c92003-09-10 03:41:44 +0000570/* ================================== utility functions ============================ */
571
Mike Hearn7d8711e2004-11-23 13:50:23 +0000572char *current_app = NULL; /* the app we are currently editing, or NULL if editing global */
Mike Hearna5ce4ee2004-09-28 03:16:43 +0000573
574/* returns a registry key path suitable for passing to addTransaction */
Mike McCormackae511352005-06-02 15:11:32 +0000575char *keypath(const char *section)
Mike Hearna5ce4ee2004-09-28 03:16:43 +0000576{
577 static char *result = NULL;
Mike Hearn0af614e2004-09-28 03:55:16 +0000578
Michael Stefaniuc5ad7d852004-12-23 17:06:43 +0000579 HeapFree(GetProcessHeap(), 0, result);
Mike Hearna5ce4ee2004-09-28 03:16:43 +0000580
Mike Hearn7d8711e2004-11-23 13:50:23 +0000581 if (current_app)
Mike Hearna5ce4ee2004-09-28 03:16:43 +0000582 {
Mike Hearn7d8711e2004-11-23 13:50:23 +0000583 result = HeapAlloc(GetProcessHeap(), 0, strlen("AppDefaults\\") + strlen(current_app) + 2 /* \\ */ + strlen(section) + 1 /* terminator */);
Alexandre Julliard0d3bddc2005-06-17 21:09:07 +0000584 sprintf(result, "AppDefaults\\%s", current_app);
585 if (section[0]) sprintf( result + strlen(result), "\\%s", section );
Mike Hearna5ce4ee2004-09-28 03:16:43 +0000586 }
587 else
588 {
589 result = strdupA(section);
590 }
Mike Hearn0af614e2004-09-28 03:55:16 +0000591
Mike Hearna5ce4ee2004-09-28 03:16:43 +0000592 return result;
593}
594
Chris Morgan49f0dd32004-05-04 02:56:46 +0000595void PRINTERROR(void)
596{
597 LPSTR msg;
598
599 FormatMessageA(FORMAT_MESSAGE_ALLOCATE_BUFFER|FORMAT_MESSAGE_FROM_SYSTEM,
600 0, GetLastError(), MAKELANGID(LANG_NEUTRAL,SUBLANG_DEFAULT),
601 (LPSTR)&msg, 0, NULL);
Mike Hearn7d8711e2004-11-23 13:50:23 +0000602
603 /* eliminate trailing newline, is this a Wine bug? */
Mike Hearnfd14bde2005-01-10 14:26:46 +0000604 *(strrchr(msg, '\r')) = '\0';
Mike Hearn7d8711e2004-11-23 13:50:23 +0000605
Chris Morgan49f0dd32004-05-04 02:56:46 +0000606 WINE_TRACE("error: '%s'\n", msg);
607}
Mike Hearn0af614e2004-09-28 03:55:16 +0000608
Robert Reiff0c55e72005-12-17 12:30:06 +0100609int initialize(HINSTANCE hInstance)
610{
Alexandre Julliarda18f2062005-06-16 16:15:37 +0000611 DWORD res = RegCreateKey(HKEY_CURRENT_USER, WINE_KEY_ROOT, &config_key);
Mike Hearn0af614e2004-09-28 03:55:16 +0000612
613 if (res != ERROR_SUCCESS) {
614 WINE_ERR("RegOpenKey failed on wine config key (%ld)\n", res);
615 return 1;
616 }
617
Robert Reiff0c55e72005-12-17 12:30:06 +0100618 /* load any menus */
619 hPopupMenus = LoadMenu(hInstance, MAKEINTRESOURCE(IDR_WINECFG));
620
Mike Hearn0af614e2004-09-28 03:55:16 +0000621 /* we could probably just have the list as static data */
622 settings = HeapAlloc(GetProcessHeap(), 0, sizeof(struct list));
623 list_init(settings);
624
625 return 0;
626}