2 * WineCfg configuration management
4 * Copyright 2002 Jaco Greeff
5 * Copyright 2003 Dimitrie O. Paun
6 * Copyright 2003-2004 Mike Hearn
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.
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.
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
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)
35 #include <wine/debug.h>
36 #include <wine/list.h>
38 WINE_DEFAULT_DEBUG_CHANNEL(winecfg
);
42 HKEY config_key
= NULL
;
46 /* this is called from the WM_SHOWWINDOW handlers of each tab page.
48 * it's a nasty hack, necessary because the property sheet insists on resetting the window title
49 * to the title of the tab, which is utterly useless. dropping the property sheet is on the todo list.
51 void set_window_title(HWND dialog
)
55 /* update the window title */
58 const char *template = "Wine Configuration for %s";
59 newtitle
= HeapAlloc(GetProcessHeap(), 0, strlen(template) + strlen(current_app
) + 1);
60 sprintf(newtitle
, template, current_app
);
64 newtitle
= strdupA("Wine Configuration");
67 WINE_TRACE("setting title to %s\n", newtitle
);
68 SendMessage(GetParent(dialog
), PSM_SETTITLE
, 0, (LPARAM
) newtitle
);
69 HeapFree(GetProcessHeap(), 0, newtitle
);
74 * get_config_key: Retrieves a configuration value from the registry
76 * char *subkey : the name of the config section
77 * char *name : the name of the config value
78 * char *default : if the key isn't found, return this value instead
80 * Returns a buffer holding the value if successful, NULL if
81 * not. Caller is responsible for releasing the result.
84 static char *get_config_key (HKEY root
, const char *subkey
, const char *name
, const char *def
)
91 WINE_TRACE("subkey=%s, name=%s, def=%s\n", subkey
, name
, def
);
93 res
= RegOpenKey(root
, subkey
, &hSubKey
);
94 if (res
!= ERROR_SUCCESS
)
96 if (res
== ERROR_FILE_NOT_FOUND
)
98 WINE_TRACE("Section key not present - using default\n");
99 return def
? strdupA(def
) : NULL
;
103 WINE_ERR("RegOpenKey failed on wine config key (res=%ld)\n", res
);
108 res
= RegQueryValueExA(hSubKey
, name
, NULL
, NULL
, NULL
, &len
);
109 if (res
== ERROR_FILE_NOT_FOUND
)
111 WINE_TRACE("Value not present - using default\n");
112 buffer
= def
? strdupA(def
) : NULL
;
114 } else if (res
!= ERROR_SUCCESS
)
116 WINE_ERR("Couldn't query value's length (res=%ld)\n", res
);
120 buffer
= HeapAlloc(GetProcessHeap(), 0, len
+ 1);
122 RegQueryValueEx(hSubKey
, name
, NULL
, NULL
, (LPBYTE
) buffer
, &len
);
124 WINE_TRACE("buffer=%s\n", buffer
);
126 if (hSubKey
&& hSubKey
!= root
) RegCloseKey(hSubKey
);
132 * set_config_key: convenience wrapper to set a key/value pair
134 * const char *subKey : the name of the config section
135 * const char *valueName : the name of the config value
136 * const char *value : the value to set the configuration key to
138 * Returns 0 on success, non-zero otherwise
140 * If valueName or value is NULL, an empty section will be created
142 static int set_config_key(HKEY root
, const char *subkey
, const char *name
, const void *value
, DWORD type
) {
146 WINE_TRACE("subkey=%s: name=%s, value=%p, type=%ld\n", subkey
, name
, value
, type
);
148 assert( subkey
!= NULL
);
152 res
= RegCreateKey(root
, subkey
, &key
);
153 if (res
!= ERROR_SUCCESS
) goto end
;
156 if (name
== NULL
|| value
== NULL
) goto end
;
160 case REG_SZ
: res
= RegSetValueEx(key
, name
, 0, REG_SZ
, value
, strlen(value
) + 1); break;
161 case REG_DWORD
: res
= RegSetValueEx(key
, name
, 0, REG_DWORD
, value
, sizeof(DWORD
)); break;
163 if (res
!= ERROR_SUCCESS
) goto end
;
167 if (key
&& key
!= root
) RegCloseKey(key
);
168 if (res
!= 0) WINE_ERR("Unable to set configuration key %s in section %s, res=%ld\n", name
, subkey
, res
);
172 /* removes the requested value from the registry, however, does not
173 * remove the section if empty. Returns S_OK (0) on success.
175 static HRESULT
remove_value(HKEY root
, const char *subkey
, const char *name
)
180 WINE_TRACE("subkey=%s, name=%s\n", subkey
, name
);
182 hr
= RegOpenKey(root
, subkey
, &key
);
183 if (hr
!= S_OK
) return hr
;
185 hr
= RegDeleteValue(key
, name
);
186 if (hr
!= ERROR_SUCCESS
) return hr
;
191 /* removes the requested subkey from the registry, assuming it exists */
192 static HRESULT
remove_path(HKEY root
, char *section
) {
193 WINE_TRACE("section=%s\n", section
);
195 return RegDeleteKey(root
, section
);
199 /* ========================================================================= */
201 /* This code exists for the following reasons:
203 * - It makes working with the registry easier
204 * - By storing a mini cache of the registry, we can more easily implement
205 * cancel/revert and apply. The 'settings list' is an overlay on top of
206 * the actual registry data that we can write out at will.
208 * Rather than model a tree in memory, we simply store each absolute (rooted
209 * at the config key) path.
216 HKEY root
; /* the key on which path is rooted */
217 char *path
; /* path in the registry rooted at root */
218 char *name
; /* name of the registry value. if null, this means delete the key */
219 char *value
; /* contents of the registry value. if null, this means delete the value */
220 DWORD type
; /* type of registry value. REG_SZ or REG_DWORD for now */
223 struct list
*settings
;
225 static void free_setting(struct setting
*setting
)
227 assert( setting
!= NULL
);
228 assert( setting
->path
);
230 WINE_TRACE("destroying %p: %s\n", setting
, setting
->path
);
232 HeapFree(GetProcessHeap(), 0, setting
->path
);
233 HeapFree(GetProcessHeap(), 0, setting
->name
);
234 HeapFree(GetProcessHeap(), 0, setting
->value
);
236 list_remove(&setting
->entry
);
238 HeapFree(GetProcessHeap(), 0, setting
);
242 * Returns the contents of the value at path. If not in the settings
243 * list, it will be fetched from the registry - failing that, the
244 * default will be used.
246 * If already in the list, the contents as given there will be
247 * returned. You are expected to HeapFree the result.
249 char *get_reg_key(HKEY root
, const char *path
, const char *name
, const char *def
)
255 WINE_TRACE("path=%s, name=%s, def=%s\n", path
, name
, def
);
257 /* check if it's in the list */
258 LIST_FOR_EACH( cursor
, settings
)
260 s
= LIST_ENTRY(cursor
, struct setting
, entry
);
262 if (root
!= s
->root
) continue;
263 if (strcasecmp(path
, s
->path
) != 0) continue;
264 if (strcasecmp(name
, s
->name
) != 0) continue;
266 WINE_TRACE("found %s:%s in settings list, returning %s\n", path
, name
, s
->value
);
267 return s
->value
? strdupA(s
->value
) : NULL
;
270 /* no, so get from the registry */
271 val
= get_config_key(root
, path
, name
, def
);
273 WINE_TRACE("returning %s\n", val
);
279 * Used to set a registry key.
281 * path is rooted at the config key, ie use "Version" or
282 * "AppDefaults\\fooapp.exe\\Version". You can use keypath()
283 * to get such a string.
285 * name is the value name, or NULL to delete the path.
287 * value is what to set the value to, or NULL to delete it.
289 * type is REG_SZ or REG_DWORD.
291 * These values will be copied when necessary.
293 static void set_reg_key_ex(HKEY root
, const char *path
, const char *name
, const void *value
, DWORD type
)
298 assert( path
!= NULL
);
300 WINE_TRACE("path=%s, name=%s, value=%p\n", path
, name
, value
);
302 /* firstly, see if we already set this setting */
303 LIST_FOR_EACH( cursor
, settings
)
305 struct setting
*s
= LIST_ENTRY(cursor
, struct setting
, entry
);
307 if (root
!= s
->root
) continue;
308 if (strcasecmp(s
->path
, path
) != 0) continue;
309 if ((s
->name
&& name
) && strcasecmp(s
->name
, name
) != 0) continue;
311 /* are we attempting a double delete? */
312 if (!s
->name
&& !name
) return;
314 /* do we want to undelete this key? */
315 if (!s
->name
&& name
) s
->name
= strdupA(name
);
317 /* yes, we have already set it, so just replace the content and return */
318 HeapFree(GetProcessHeap(), 0, s
->value
);
323 s
->value
= value
? strdupA(value
) : NULL
;
326 s
->value
= HeapAlloc(GetProcessHeap(), 0, sizeof(DWORD
));
327 memcpy( s
->value
, value
, sizeof(DWORD
) );
331 /* are we deleting this key? this won't remove any of the
332 * children from the overlay so if the user adds it again in
333 * that session it will appear to undelete the settings, but
334 * in reality only the settings actually modified by the user
335 * in that session will be restored. we might want to fix this
336 * corner case in future by actually deleting all the children
337 * here so that once it's gone, it's gone.
339 if (!name
) s
->name
= NULL
;
344 /* otherwise add a new setting for it */
345 s
= HeapAlloc(GetProcessHeap(), 0, sizeof(struct setting
));
347 s
->path
= strdupA(path
);
348 s
->name
= name
? strdupA(name
) : NULL
;
353 s
->value
= value
? strdupA(value
) : NULL
;
356 s
->value
= HeapAlloc(GetProcessHeap(), 0, sizeof(DWORD
));
357 memcpy( s
->value
, value
, sizeof(DWORD
) );
361 list_add_tail(settings
, &s
->entry
);
364 void set_reg_key(HKEY root
, const char *path
, const char *name
, const char *value
)
366 set_reg_key_ex(root
, path
, name
, value
, REG_SZ
);
369 void set_reg_key_dword(HKEY root
, const char *path
, const char *name
, DWORD value
)
371 set_reg_key_ex(root
, path
, name
, &value
, REG_DWORD
);
375 * enumerates the value names at the given path, taking into account
376 * the changes in the settings list.
378 * you are expected to HeapFree each element of the array, which is null
379 * terminated, as well as the array itself.
381 char **enumerate_values(HKEY root
, char *path
)
385 char **values
= NULL
;
389 res
= RegOpenKey(root
, path
, &key
);
390 if (res
== ERROR_SUCCESS
)
395 DWORD namesize
= sizeof(name
);
396 BOOL removed
= FALSE
;
398 /* find out the needed size, allocate a buffer, read the value */
399 if ((res
= RegEnumValue(key
, i
, name
, &namesize
, NULL
, NULL
, NULL
, NULL
)) != ERROR_SUCCESS
)
402 WINE_TRACE("name=%s\n", name
);
404 /* check if this value name has been removed in the settings list */
405 LIST_FOR_EACH( cursor
, settings
)
407 struct setting
*s
= LIST_ENTRY(cursor
, struct setting
, entry
);
408 if (strcasecmp(s
->path
, path
) != 0) continue;
409 if (strcasecmp(s
->name
, name
) != 0) continue;
413 WINE_TRACE("this key has been removed, so skipping\n");
419 if (removed
) /* this value was deleted by the user, so don't include it */
421 HeapFree(GetProcessHeap(), 0, name
);
426 /* grow the array if necessary, add buffer to it, iterate */
427 if (values
) values
= HeapReAlloc(GetProcessHeap(), 0, values
, sizeof(char*) * (valueslen
+ 1));
428 else values
= HeapAlloc(GetProcessHeap(), 0, sizeof(char*));
430 values
[valueslen
++] = strdupA(name
);
431 WINE_TRACE("valueslen is now %d\n", valueslen
);
437 WINE_WARN("failed opening registry key %s, res=0x%lx\n", path
, res
);
440 WINE_TRACE("adding settings in list but not registry\n");
442 /* now we have to add the values that aren't in the registry but are in the settings list */
443 LIST_FOR_EACH( cursor
, settings
)
445 struct setting
*setting
= LIST_ENTRY(cursor
, struct setting
, entry
);
448 if (strcasecmp(setting
->path
, path
) != 0) continue;
450 if (!setting
->value
) continue;
452 for (i
= 0; i
< valueslen
; i
++)
454 if (strcasecmp(setting
->name
, values
[i
]) == 0)
463 WINE_TRACE("%s in list but not registry\n", setting
->name
);
465 /* otherwise it's been set by the user but isn't in the registry */
466 if (values
) values
= HeapReAlloc(GetProcessHeap(), 0, values
, sizeof(char*) * (valueslen
+ 1));
467 else values
= HeapAlloc(GetProcessHeap(), 0, sizeof(char*));
469 values
[valueslen
++] = strdupA(setting
->name
);
472 WINE_TRACE("adding null terminator\n");
475 values
= HeapReAlloc(GetProcessHeap(), 0, values
, sizeof(char*) * (valueslen
+ 1));
476 values
[valueslen
] = NULL
;
485 * returns true if the given key/value pair exists in the registry or
486 * has been written to.
488 BOOL
reg_key_exists(HKEY root
, const char *path
, const char *name
)
490 char *val
= get_reg_key(root
, path
, name
, NULL
);
494 HeapFree(GetProcessHeap(), 0, val
);
501 static void process_setting(struct setting
*s
)
505 WINE_TRACE("Setting %s:%s to '%s'\n", s
->path
, s
->name
, s
->value
);
506 set_config_key(s
->root
, s
->path
, s
->name
, s
->value
, s
->type
);
510 /* NULL name means remove that path/section entirely */
511 if (s
->path
&& s
->name
) remove_value(s
->root
, s
->path
, s
->name
);
512 else if (s
->path
&& !s
->name
) remove_path(s
->root
, s
->path
);
518 if (list_empty(settings
)) return; /* we will be called for each page when the user clicks OK */
522 while (!list_empty(settings
))
524 struct setting
*s
= (struct setting
*) list_head(settings
);
530 /* ================================== utility functions ============================ */
532 char *current_app
= NULL
; /* the app we are currently editing, or NULL if editing global */
534 /* returns a registry key path suitable for passing to addTransaction */
535 char *keypath(const char *section
)
537 static char *result
= NULL
;
539 HeapFree(GetProcessHeap(), 0, result
);
543 result
= HeapAlloc(GetProcessHeap(), 0, strlen("AppDefaults\\") + strlen(current_app
) + 2 /* \\ */ + strlen(section
) + 1 /* terminator */);
544 sprintf(result
, "AppDefaults\\%s", current_app
);
545 if (section
[0]) sprintf( result
+ strlen(result
), "\\%s", section
);
549 result
= strdupA(section
);
555 void PRINTERROR(void)
559 FormatMessageA(FORMAT_MESSAGE_ALLOCATE_BUFFER
|FORMAT_MESSAGE_FROM_SYSTEM
,
560 0, GetLastError(), MAKELANGID(LANG_NEUTRAL
,SUBLANG_DEFAULT
),
561 (LPSTR
)&msg
, 0, NULL
);
563 /* eliminate trailing newline, is this a Wine bug? */
564 *(strrchr(msg
, '\r')) = '\0';
566 WINE_TRACE("error: '%s'\n", msg
);
569 int initialize(void) {
570 DWORD res
= RegCreateKey(HKEY_CURRENT_USER
, WINE_KEY_ROOT
, &config_key
);
572 if (res
!= ERROR_SUCCESS
) {
573 WINE_ERR("RegOpenKey failed on wine config key (%ld)\n", res
);
577 /* we could probably just have the list as static data */
578 settings
= HeapAlloc(GetProcessHeap(), 0, sizeof(struct list
));