blob: df2b05ffa196747fc516f1318a0f72af5263be36 [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{
56 char *newtitle;
57
58 /* update the window title */
Mike Hearn7d8711e2004-11-23 13:50:23 +000059 if (current_app)
Mike Hearn0af614e2004-09-28 03:55:16 +000060 {
Mike McCormackae511352005-06-02 15:11:32 +000061 const char *template = "Wine Configuration for %s";
Mike Hearn7d8711e2004-11-23 13:50:23 +000062 newtitle = HeapAlloc(GetProcessHeap(), 0, strlen(template) + strlen(current_app) + 1);
63 sprintf(newtitle, template, current_app);
Mike Hearncd0b7892003-09-08 18:58:07 +000064 }
Mike Hearn0af614e2004-09-28 03:55:16 +000065 else
66 {
67 newtitle = strdupA("Wine Configuration");
68 }
69
70 WINE_TRACE("setting title to %s\n", newtitle);
71 SendMessage(GetParent(dialog), PSM_SETTITLE, 0, (LPARAM) newtitle);
72 HeapFree(GetProcessHeap(), 0, newtitle);
Dimitrie O. Paun82ce2cc2003-03-31 19:41:55 +000073}
74
Mike Hearncd0b7892003-09-08 18:58:07 +000075
Mike Hearn0af614e2004-09-28 03:55:16 +000076/**
Gerald Pfeifer61f8e122004-11-30 21:03:36 +000077 * get_config_key: Retrieves a configuration value from the registry
Mike Hearn4e1afc62003-08-30 00:27:08 +000078 *
Mike Hearn0af614e2004-09-28 03:55:16 +000079 * char *subkey : the name of the config section
80 * char *name : the name of the config value
81 * char *default : if the key isn't found, return this value instead
Mike Hearn4e1afc62003-08-30 00:27:08 +000082 *
Mike Hearn0af614e2004-09-28 03:55:16 +000083 * Returns a buffer holding the value if successful, NULL if
84 * not. Caller is responsible for releasing the result.
Mike Hearn4e1afc62003-08-30 00:27:08 +000085 *
Matthew Davison5101dfc2003-04-27 00:33:07 +000086 */
Michael Jung3bbb7fd2005-06-23 11:42:54 +000087static char *get_config_key (HKEY root, const char *subkey, const char *name, const char *def)
Matthew Davison5101dfc2003-04-27 00:33:07 +000088{
Mike McCormack88bddd72005-08-19 15:19:10 +000089 LPSTR buffer = NULL;
Mike Hearn0af614e2004-09-28 03:55:16 +000090 DWORD len;
Mike Hearn4e1afc62003-08-30 00:27:08 +000091 HKEY hSubKey = NULL;
Mike Hearncd0b7892003-09-08 18:58:07 +000092 DWORD res;
Matthew Davison5101dfc2003-04-27 00:33:07 +000093
Mike Hearn0af614e2004-09-28 03:55:16 +000094 WINE_TRACE("subkey=%s, name=%s, def=%s\n", subkey, name, def);
Mike Hearncd0b7892003-09-08 18:58:07 +000095
Michael Jung3bbb7fd2005-06-23 11:42:54 +000096 res = RegOpenKey(root, subkey, &hSubKey);
Mike Hearn0af614e2004-09-28 03:55:16 +000097 if (res != ERROR_SUCCESS)
98 {
99 if (res == ERROR_FILE_NOT_FOUND)
Matthew Davison5101dfc2003-04-27 00:33:07 +0000100 {
Mike Hearn4e1afc62003-08-30 00:27:08 +0000101 WINE_TRACE("Section key not present - using default\n");
Mike Hearn0af614e2004-09-28 03:55:16 +0000102 return def ? strdupA(def) : NULL;
Matthew Davison5101dfc2003-04-27 00:33:07 +0000103 }
104 else
105 {
Mike Hearn4e1afc62003-08-30 00:27:08 +0000106 WINE_ERR("RegOpenKey failed on wine config key (res=%ld)\n", res);
Matthew Davison5101dfc2003-04-27 00:33:07 +0000107 }
108 goto end;
109 }
Mike Hearn498e1ce2003-09-30 00:27:55 +0000110
Mike Hearn0af614e2004-09-28 03:55:16 +0000111 res = RegQueryValueExA(hSubKey, name, NULL, NULL, NULL, &len);
112 if (res == ERROR_FILE_NOT_FOUND)
113 {
Matthew Davison5101dfc2003-04-27 00:33:07 +0000114 WINE_TRACE("Value not present - using default\n");
Mike Hearn0af614e2004-09-28 03:55:16 +0000115 buffer = def ? strdupA(def) : NULL;
Mike Hearncd0b7892003-09-08 18:58:07 +0000116 goto end;
Mike Hearn0af614e2004-09-28 03:55:16 +0000117 } else if (res != ERROR_SUCCESS)
118 {
119 WINE_ERR("Couldn't query value's length (res=%ld)\n", res);
Matthew Davison5101dfc2003-04-27 00:33:07 +0000120 goto end;
121 }
122
Mike Hearn0af614e2004-09-28 03:55:16 +0000123 buffer = HeapAlloc(GetProcessHeap(), 0, len + 1);
124
Mike McCormack88bddd72005-08-19 15:19:10 +0000125 RegQueryValueEx(hSubKey, name, NULL, NULL, (LPBYTE) buffer, &len);
Mike Hearn0af614e2004-09-28 03:55:16 +0000126
127 WINE_TRACE("buffer=%s\n", buffer);
Matthew Davison5101dfc2003-04-27 00:33:07 +0000128end:
Michael Jung3bbb7fd2005-06-23 11:42:54 +0000129 if (hSubKey && hSubKey != root) RegCloseKey(hSubKey);
Matthew Davison5101dfc2003-04-27 00:33:07 +0000130
Mike McCormack88bddd72005-08-19 15:19:10 +0000131 return buffer;
Matthew Davison5101dfc2003-04-27 00:33:07 +0000132}
133
Mike Hearn0af614e2004-09-28 03:55:16 +0000134/**
Gerald Pfeifer61f8e122004-11-30 21:03:36 +0000135 * set_config_key: convenience wrapper to set a key/value pair
Mike Hearn4e1afc62003-08-30 00:27:08 +0000136 *
Daniel Marmier8e5bb202003-10-09 04:39:01 +0000137 * const char *subKey : the name of the config section
138 * const char *valueName : the name of the config value
139 * const char *value : the value to set the configuration key to
Mike Hearn4e1afc62003-08-30 00:27:08 +0000140 *
141 * Returns 0 on success, non-zero otherwise
Mike Hearn0af614e2004-09-28 03:55:16 +0000142 *
143 * If valueName or value is NULL, an empty section will be created
Mike Hearn4e1afc62003-08-30 00:27:08 +0000144 */
Mike McCormack88bddd72005-08-19 15:19:10 +0000145static int set_config_key(HKEY root, const char *subkey, const char *name, const void *value, DWORD type) {
Mike Hearn4e1afc62003-08-30 00:27:08 +0000146 DWORD res = 1;
147 HKEY key = NULL;
148
Mike McCormack88bddd72005-08-19 15:19:10 +0000149 WINE_TRACE("subkey=%s: name=%s, value=%p, type=%ld\n", subkey, name, value, type);
Mike Hearncd0b7892003-09-08 18:58:07 +0000150
151 assert( subkey != NULL );
Mike Hearn4e1afc62003-08-30 00:27:08 +0000152
Alexandre Julliard0d3bddc2005-06-17 21:09:07 +0000153 if (subkey[0])
154 {
Michael Jung3bbb7fd2005-06-23 11:42:54 +0000155 res = RegCreateKey(root, subkey, &key);
Alexandre Julliard0d3bddc2005-06-17 21:09:07 +0000156 if (res != ERROR_SUCCESS) goto end;
157 }
Michael Jung3bbb7fd2005-06-23 11:42:54 +0000158 else key = root;
Mike Hearn0af614e2004-09-28 03:55:16 +0000159 if (name == NULL || value == NULL) goto end;
160
Vitaliy Margolenaad2fe3b2005-08-17 11:37:34 +0000161 switch (type)
162 {
163 case REG_SZ: res = RegSetValueEx(key, name, 0, REG_SZ, value, strlen(value) + 1); break;
164 case REG_DWORD: res = RegSetValueEx(key, name, 0, REG_DWORD, value, sizeof(DWORD)); break;
165 }
Mike Hearn4e1afc62003-08-30 00:27:08 +0000166 if (res != ERROR_SUCCESS) goto end;
167
168 res = 0;
169end:
Michael Jung3bbb7fd2005-06-23 11:42:54 +0000170 if (key && key != root) RegCloseKey(key);
Mike McCormack88bddd72005-08-19 15:19:10 +0000171 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 +0000172 return res;
173}
174
Mike Hearn0af614e2004-09-28 03:55:16 +0000175/* removes the requested value from the registry, however, does not
176 * remove the section if empty. Returns S_OK (0) on success.
177 */
Michael Jung3bbb7fd2005-06-23 11:42:54 +0000178static HRESULT remove_value(HKEY root, const char *subkey, const char *name)
Mike Hearn0af614e2004-09-28 03:55:16 +0000179{
Mike Hearncd0b7892003-09-08 18:58:07 +0000180 HRESULT hr;
181 HKEY key;
182
Mike Hearn0af614e2004-09-28 03:55:16 +0000183 WINE_TRACE("subkey=%s, name=%s\n", subkey, name);
Mike Hearncd0b7892003-09-08 18:58:07 +0000184
Michael Jung3bbb7fd2005-06-23 11:42:54 +0000185 hr = RegOpenKey(root, subkey, &key);
Mike Hearncd0b7892003-09-08 18:58:07 +0000186 if (hr != S_OK) return hr;
187
Mike Hearn0af614e2004-09-28 03:55:16 +0000188 hr = RegDeleteValue(key, name);
Mike Hearncd0b7892003-09-08 18:58:07 +0000189 if (hr != ERROR_SUCCESS) return hr;
190
191 return S_OK;
192}
193
Mike Hearn0af614e2004-09-28 03:55:16 +0000194/* removes the requested subkey from the registry, assuming it exists */
Vitaliy Margolenf4db5df2005-10-13 13:44:38 +0000195static LONG remove_path(HKEY root, char *section) {
196 HKEY branch_key;
197 DWORD max_sub_key_len;
198 DWORD subkeys;
199 DWORD curr_len;
200 LONG ret = ERROR_SUCCESS;
201 long int i;
202 char *buffer;
203
Mike Hearn5a2cde62003-09-17 22:40:38 +0000204 WINE_TRACE("section=%s\n", section);
205
Vitaliy Margolenf4db5df2005-10-13 13:44:38 +0000206 if ((ret = RegOpenKey(root, section, &branch_key)) != ERROR_SUCCESS)
207 return ret;
208
209 /* get size information and resize the buffers if necessary */
210 if ((ret = RegQueryInfoKey(branch_key, NULL, NULL, NULL,
211 &subkeys, &max_sub_key_len,
212 NULL, NULL, NULL, NULL, NULL, NULL
213 )) != ERROR_SUCCESS)
214 return ret;
215
216 curr_len = strlen(section);
217 buffer = HeapAlloc(GetProcessHeap(), 0, max_sub_key_len + curr_len + 1);
218 strcpy(buffer, section);
219
220 buffer[curr_len] = '\\';
221 for (i = subkeys - 1; i >= 0; i--)
222 {
223 DWORD buf_len = max_sub_key_len - curr_len - 1;
224
225 ret = RegEnumKeyEx(branch_key, i, buffer + curr_len + 1,
226 &buf_len, NULL, NULL, NULL, NULL);
227 if (ret != ERROR_SUCCESS && ret != ERROR_MORE_DATA &&
228 ret != ERROR_NO_MORE_ITEMS)
229 break;
230 else
231 remove_path(root, buffer);
232 }
233 HeapFree(GetProcessHeap(), 0, buffer);
234 RegCloseKey(branch_key);
235
Michael Jung3bbb7fd2005-06-23 11:42:54 +0000236 return RegDeleteKey(root, section);
Mike Hearn5a2cde62003-09-17 22:40:38 +0000237}
238
Mike Hearn4e1afc62003-08-30 00:27:08 +0000239
Mike Hearncd0b7892003-09-08 18:58:07 +0000240/* ========================================================================= */
Mike Hearncd0b7892003-09-08 18:58:07 +0000241
Mike Hearn0af614e2004-09-28 03:55:16 +0000242/* This code exists for the following reasons:
243 *
244 * - It makes working with the registry easier
245 * - By storing a mini cache of the registry, we can more easily implement
246 * cancel/revert and apply. The 'settings list' is an overlay on top of
247 * the actual registry data that we can write out at will.
248 *
249 * Rather than model a tree in memory, we simply store each absolute (rooted
250 * at the config key) path.
251 *
252 */
Mike Hearncd0b7892003-09-08 18:58:07 +0000253
Mike Hearn0af614e2004-09-28 03:55:16 +0000254struct setting
Mike Hearn0f4c6c92003-09-10 03:41:44 +0000255{
Mike Hearn0af614e2004-09-28 03:55:16 +0000256 struct list entry;
Michael Jung3bbb7fd2005-06-23 11:42:54 +0000257 HKEY root; /* the key on which path is rooted */
258 char *path; /* path in the registry rooted at root */
Mike Hearnc6191522005-01-03 14:45:05 +0000259 char *name; /* name of the registry value. if null, this means delete the key */
260 char *value; /* contents of the registry value. if null, this means delete the value */
Vitaliy Margolenaad2fe3b2005-08-17 11:37:34 +0000261 DWORD type; /* type of registry value. REG_SZ or REG_DWORD for now */
Mike Hearn0af614e2004-09-28 03:55:16 +0000262};
263
264struct list *settings;
265
266static void free_setting(struct setting *setting)
267{
268 assert( setting != NULL );
Mike Hearnc6191522005-01-03 14:45:05 +0000269 assert( setting->path );
Mike Hearn0af614e2004-09-28 03:55:16 +0000270
Mike Hearnc6191522005-01-03 14:45:05 +0000271 WINE_TRACE("destroying %p: %s\n", setting, setting->path);
272
Mike Hearn0af614e2004-09-28 03:55:16 +0000273 HeapFree(GetProcessHeap(), 0, setting->path);
274 HeapFree(GetProcessHeap(), 0, setting->name);
Michael Stefaniuc5ad7d852004-12-23 17:06:43 +0000275 HeapFree(GetProcessHeap(), 0, setting->value);
Mike Hearn0af614e2004-09-28 03:55:16 +0000276
277 list_remove(&setting->entry);
278
279 HeapFree(GetProcessHeap(), 0, setting);
280}
281
282/**
283 * Returns the contents of the value at path. If not in the settings
284 * list, it will be fetched from the registry - failing that, the
285 * default will be used.
286 *
287 * If already in the list, the contents as given there will be
288 * returned. You are expected to HeapFree the result.
289 */
Michael Jung3bbb7fd2005-06-23 11:42:54 +0000290char *get_reg_key(HKEY root, const char *path, const char *name, const char *def)
Mike Hearn0af614e2004-09-28 03:55:16 +0000291{
292 struct list *cursor;
293 struct setting *s;
294 char *val;
295
296 WINE_TRACE("path=%s, name=%s, def=%s\n", path, name, def);
297
298 /* check if it's in the list */
299 LIST_FOR_EACH( cursor, settings )
300 {
301 s = LIST_ENTRY(cursor, struct setting, entry);
302
Michael Jung3bbb7fd2005-06-23 11:42:54 +0000303 if (root != s->root) continue;
Mike Hearn0af614e2004-09-28 03:55:16 +0000304 if (strcasecmp(path, s->path) != 0) continue;
305 if (strcasecmp(name, s->name) != 0) continue;
306
307 WINE_TRACE("found %s:%s in settings list, returning %s\n", path, name, s->value);
Crestez Leonard3e55df32005-01-14 19:48:41 +0000308 return s->value ? strdupA(s->value) : NULL;
Mike Hearn0af614e2004-09-28 03:55:16 +0000309 }
310
311 /* no, so get from the registry */
Michael Jung3bbb7fd2005-06-23 11:42:54 +0000312 val = get_config_key(root, path, name, def);
Mike Hearn0af614e2004-09-28 03:55:16 +0000313
314 WINE_TRACE("returning %s\n", val);
315
316 return val;
317}
318
319/**
320 * Used to set a registry key.
321 *
322 * path is rooted at the config key, ie use "Version" or
323 * "AppDefaults\\fooapp.exe\\Version". You can use keypath()
324 * to get such a string.
325 *
Mike Hearnc6191522005-01-03 14:45:05 +0000326 * name is the value name, or NULL to delete the path.
Mike Hearn0af614e2004-09-28 03:55:16 +0000327 *
328 * value is what to set the value to, or NULL to delete it.
329 *
Vitaliy Margolenaad2fe3b2005-08-17 11:37:34 +0000330 * type is REG_SZ or REG_DWORD.
331 *
Mike Hearn0af614e2004-09-28 03:55:16 +0000332 * These values will be copied when necessary.
333 */
Vitaliy Margolenaad2fe3b2005-08-17 11:37:34 +0000334static 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 +0000335{
336 struct list *cursor;
337 struct setting *s;
338
339 assert( path != NULL );
Mike Hearn0af614e2004-09-28 03:55:16 +0000340
Vitaliy Margolenaad2fe3b2005-08-17 11:37:34 +0000341 WINE_TRACE("path=%s, name=%s, value=%p\n", path, name, value);
Mike Hearn0af614e2004-09-28 03:55:16 +0000342
343 /* firstly, see if we already set this setting */
344 LIST_FOR_EACH( cursor, settings )
345 {
346 struct setting *s = LIST_ENTRY(cursor, struct setting, entry);
347
Michael Jung3bbb7fd2005-06-23 11:42:54 +0000348 if (root != s->root) continue;
Mike Hearn0af614e2004-09-28 03:55:16 +0000349 if (strcasecmp(s->path, path) != 0) continue;
Mike Hearnc6191522005-01-03 14:45:05 +0000350 if ((s->name && name) && strcasecmp(s->name, name) != 0) continue;
351
352 /* are we attempting a double delete? */
353 if (!s->name && !name) return;
354
355 /* do we want to undelete this key? */
356 if (!s->name && name) s->name = strdupA(name);
Mike Hearn0af614e2004-09-28 03:55:16 +0000357
358 /* yes, we have already set it, so just replace the content and return */
Michael Stefaniuc5ad7d852004-12-23 17:06:43 +0000359 HeapFree(GetProcessHeap(), 0, s->value);
Vitaliy Margolenaad2fe3b2005-08-17 11:37:34 +0000360 s->type = type;
361 switch (type)
362 {
363 case REG_SZ:
364 s->value = value ? strdupA(value) : NULL;
365 break;
366 case REG_DWORD:
367 s->value = HeapAlloc(GetProcessHeap(), 0, sizeof(DWORD));
368 memcpy( s->value, value, sizeof(DWORD) );
369 break;
370 }
Mike Hearn0af614e2004-09-28 03:55:16 +0000371
Mike Hearnc6191522005-01-03 14:45:05 +0000372 /* are we deleting this key? this won't remove any of the
373 * children from the overlay so if the user adds it again in
374 * that session it will appear to undelete the settings, but
375 * in reality only the settings actually modified by the user
376 * in that session will be restored. we might want to fix this
377 * corner case in future by actually deleting all the children
378 * here so that once it's gone, it's gone.
379 */
380 if (!name) s->name = NULL;
381
Mike Hearn0af614e2004-09-28 03:55:16 +0000382 return;
383 }
384
385 /* otherwise add a new setting for it */
386 s = HeapAlloc(GetProcessHeap(), 0, sizeof(struct setting));
Michael Jung3bbb7fd2005-06-23 11:42:54 +0000387 s->root = root;
Mike Hearnc6191522005-01-03 14:45:05 +0000388 s->path = strdupA(path);
389 s->name = name ? strdupA(name) : NULL;
Vitaliy Margolenaad2fe3b2005-08-17 11:37:34 +0000390 s->type = type;
391 switch (type)
392 {
393 case REG_SZ:
394 s->value = value ? strdupA(value) : NULL;
395 break;
396 case REG_DWORD:
397 s->value = HeapAlloc(GetProcessHeap(), 0, sizeof(DWORD));
398 memcpy( s->value, value, sizeof(DWORD) );
399 break;
400 }
Mike Hearn0af614e2004-09-28 03:55:16 +0000401
402 list_add_tail(settings, &s->entry);
403}
404
Vitaliy Margolenaad2fe3b2005-08-17 11:37:34 +0000405void set_reg_key(HKEY root, const char *path, const char *name, const char *value)
406{
407 set_reg_key_ex(root, path, name, value, REG_SZ);
408}
409
410void set_reg_key_dword(HKEY root, const char *path, const char *name, DWORD value)
411{
412 set_reg_key_ex(root, path, name, &value, REG_DWORD);
413}
414
Mike Hearn0af614e2004-09-28 03:55:16 +0000415/**
416 * enumerates the value names at the given path, taking into account
417 * the changes in the settings list.
418 *
419 * you are expected to HeapFree each element of the array, which is null
420 * terminated, as well as the array itself.
421 */
Michael Jung3bbb7fd2005-06-23 11:42:54 +0000422char **enumerate_values(HKEY root, char *path)
Mike Hearn0af614e2004-09-28 03:55:16 +0000423{
424 HKEY key;
425 DWORD res, i = 0;
426 char **values = NULL;
427 int valueslen = 0;
428 struct list *cursor;
429
Michael Jung3bbb7fd2005-06-23 11:42:54 +0000430 res = RegOpenKey(root, path, &key);
Mike Hearn0af614e2004-09-28 03:55:16 +0000431 if (res == ERROR_SUCCESS)
432 {
433 while (TRUE)
434 {
435 char name[1024];
436 DWORD namesize = sizeof(name);
437 BOOL removed = FALSE;
438
439 /* find out the needed size, allocate a buffer, read the value */
440 if ((res = RegEnumValue(key, i, name, &namesize, NULL, NULL, NULL, NULL)) != ERROR_SUCCESS)
441 break;
442
443 WINE_TRACE("name=%s\n", name);
444
445 /* check if this value name has been removed in the settings list */
446 LIST_FOR_EACH( cursor, settings )
447 {
448 struct setting *s = LIST_ENTRY(cursor, struct setting, entry);
449 if (strcasecmp(s->path, path) != 0) continue;
450 if (strcasecmp(s->name, name) != 0) continue;
451
452 if (!s->value)
453 {
454 WINE_TRACE("this key has been removed, so skipping\n");
455 removed = TRUE;
456 break;
457 }
458 }
459
460 if (removed) /* this value was deleted by the user, so don't include it */
461 {
462 HeapFree(GetProcessHeap(), 0, name);
463 i++;
464 continue;
465 }
466
467 /* grow the array if necessary, add buffer to it, iterate */
468 if (values) values = HeapReAlloc(GetProcessHeap(), 0, values, sizeof(char*) * (valueslen + 1));
469 else values = HeapAlloc(GetProcessHeap(), 0, sizeof(char*));
470
471 values[valueslen++] = strdupA(name);
472 WINE_TRACE("valueslen is now %d\n", valueslen);
473 i++;
474 }
475 }
476 else
477 {
478 WINE_WARN("failed opening registry key %s, res=0x%lx\n", path, res);
479 }
480
481 WINE_TRACE("adding settings in list but not registry\n");
482
483 /* now we have to add the values that aren't in the registry but are in the settings list */
484 LIST_FOR_EACH( cursor, settings )
485 {
486 struct setting *setting = LIST_ENTRY(cursor, struct setting, entry);
487 BOOL found = FALSE;
488
489 if (strcasecmp(setting->path, path) != 0) continue;
490
491 if (!setting->value) continue;
492
493 for (i = 0; i < valueslen; i++)
494 {
495 if (strcasecmp(setting->name, values[i]) == 0)
496 {
497 found = TRUE;
498 break;
499 }
500 }
501
502 if (found) continue;
503
504 WINE_TRACE("%s in list but not registry\n", setting->name);
505
506 /* otherwise it's been set by the user but isn't in the registry */
507 if (values) values = HeapReAlloc(GetProcessHeap(), 0, values, sizeof(char*) * (valueslen + 1));
508 else values = HeapAlloc(GetProcessHeap(), 0, sizeof(char*));
509
510 values[valueslen++] = strdupA(setting->name);
511 }
512
513 WINE_TRACE("adding null terminator\n");
514 if (values)
515 {
516 values = HeapReAlloc(GetProcessHeap(), 0, values, sizeof(char*) * (valueslen + 1));
517 values[valueslen] = NULL;
518 }
519
520 RegCloseKey(key);
521
522 return values;
523}
524
525/**
526 * returns true if the given key/value pair exists in the registry or
527 * has been written to.
528 */
Michael Jung3bbb7fd2005-06-23 11:42:54 +0000529BOOL reg_key_exists(HKEY root, const char *path, const char *name)
Mike Hearn0af614e2004-09-28 03:55:16 +0000530{
Michael Jung3bbb7fd2005-06-23 11:42:54 +0000531 char *val = get_reg_key(root, path, name, NULL);
Mike Hearn0af614e2004-09-28 03:55:16 +0000532
533 if (val)
534 {
535 HeapFree(GetProcessHeap(), 0, val);
536 return TRUE;
537 }
538
539 return FALSE;
540}
541
542static void process_setting(struct setting *s)
543{
544 if (s->value)
545 {
546 WINE_TRACE("Setting %s:%s to '%s'\n", s->path, s->name, s->value);
Vitaliy Margolenaad2fe3b2005-08-17 11:37:34 +0000547 set_config_key(s->root, s->path, s->name, s->value, s->type);
Mike Hearn0af614e2004-09-28 03:55:16 +0000548 }
549 else
550 {
551 /* NULL name means remove that path/section entirely */
Michael Jung3bbb7fd2005-06-23 11:42:54 +0000552 if (s->path && s->name) remove_value(s->root, s->path, s->name);
553 else if (s->path && !s->name) remove_path(s->root, s->path);
Mike Hearn0af614e2004-09-28 03:55:16 +0000554 }
555}
556
557void apply(void)
558{
559 if (list_empty(settings)) return; /* we will be called for each page when the user clicks OK */
560
561 WINE_TRACE("()\n");
562
563 while (!list_empty(settings))
564 {
565 struct setting *s = (struct setting *) list_head(settings);
566 process_setting(s);
567 free_setting(s);
Mike Hearncd0b7892003-09-08 18:58:07 +0000568 }
Dimitrie O. Paun82ce2cc2003-03-31 19:41:55 +0000569}
Mike Hearn0f4c6c92003-09-10 03:41:44 +0000570
Mike Hearn0f4c6c92003-09-10 03:41:44 +0000571/* ================================== utility functions ============================ */
572
Mike Hearn7d8711e2004-11-23 13:50:23 +0000573char *current_app = NULL; /* the app we are currently editing, or NULL if editing global */
Mike Hearna5ce4ee2004-09-28 03:16:43 +0000574
575/* returns a registry key path suitable for passing to addTransaction */
Mike McCormackae511352005-06-02 15:11:32 +0000576char *keypath(const char *section)
Mike Hearna5ce4ee2004-09-28 03:16:43 +0000577{
578 static char *result = NULL;
Mike Hearn0af614e2004-09-28 03:55:16 +0000579
Michael Stefaniuc5ad7d852004-12-23 17:06:43 +0000580 HeapFree(GetProcessHeap(), 0, result);
Mike Hearna5ce4ee2004-09-28 03:16:43 +0000581
Mike Hearn7d8711e2004-11-23 13:50:23 +0000582 if (current_app)
Mike Hearna5ce4ee2004-09-28 03:16:43 +0000583 {
Mike Hearn7d8711e2004-11-23 13:50:23 +0000584 result = HeapAlloc(GetProcessHeap(), 0, strlen("AppDefaults\\") + strlen(current_app) + 2 /* \\ */ + strlen(section) + 1 /* terminator */);
Alexandre Julliard0d3bddc2005-06-17 21:09:07 +0000585 sprintf(result, "AppDefaults\\%s", current_app);
586 if (section[0]) sprintf( result + strlen(result), "\\%s", section );
Mike Hearna5ce4ee2004-09-28 03:16:43 +0000587 }
588 else
589 {
590 result = strdupA(section);
591 }
Mike Hearn0af614e2004-09-28 03:55:16 +0000592
Mike Hearna5ce4ee2004-09-28 03:16:43 +0000593 return result;
594}
595
Chris Morgan49f0dd32004-05-04 02:56:46 +0000596void PRINTERROR(void)
597{
598 LPSTR msg;
599
600 FormatMessageA(FORMAT_MESSAGE_ALLOCATE_BUFFER|FORMAT_MESSAGE_FROM_SYSTEM,
601 0, GetLastError(), MAKELANGID(LANG_NEUTRAL,SUBLANG_DEFAULT),
602 (LPSTR)&msg, 0, NULL);
Mike Hearn7d8711e2004-11-23 13:50:23 +0000603
604 /* eliminate trailing newline, is this a Wine bug? */
Mike Hearnfd14bde2005-01-10 14:26:46 +0000605 *(strrchr(msg, '\r')) = '\0';
Mike Hearn7d8711e2004-11-23 13:50:23 +0000606
Chris Morgan49f0dd32004-05-04 02:56:46 +0000607 WINE_TRACE("error: '%s'\n", msg);
608}
Mike Hearn0af614e2004-09-28 03:55:16 +0000609
Robert Reiff0c55e72005-12-17 12:30:06 +0100610int initialize(HINSTANCE hInstance)
611{
Alexandre Julliarda18f2062005-06-16 16:15:37 +0000612 DWORD res = RegCreateKey(HKEY_CURRENT_USER, WINE_KEY_ROOT, &config_key);
Mike Hearn0af614e2004-09-28 03:55:16 +0000613
614 if (res != ERROR_SUCCESS) {
615 WINE_ERR("RegOpenKey failed on wine config key (%ld)\n", res);
616 return 1;
617 }
618
Robert Reiff0c55e72005-12-17 12:30:06 +0100619 /* load any menus */
620 hPopupMenus = LoadMenu(hInstance, MAKEINTRESOURCE(IDR_WINECFG));
621
Mike Hearn0af614e2004-09-28 03:55:16 +0000622 /* we could probably just have the list as static data */
623 settings = HeapAlloc(GetProcessHeap(), 0, sizeof(struct list));
624 list_init(settings);
625
626 return 0;
627}