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
29 #include <wine/debug.h>
30 #include <wine/list.h>
32 WINE_DEFAULT_DEBUG_CHANNEL(winecfg
);
36 HKEY config_key
= NULL
;
40 /* this is called from the WM_SHOWWINDOW handlers of each tab page.
42 * it's a nasty hack, necessary because the property sheet insists on resetting the window title
43 * to the title of the tab, which is utterly useless. dropping the property sheet is on the todo list.
45 void set_window_title(HWND dialog
)
49 /* update the window title */
52 char *template = "Wine Configuration for %s";
53 newtitle
= HeapAlloc(GetProcessHeap(), 0, strlen(template) + strlen(currentApp
) + 1);
54 sprintf(newtitle
, template, currentApp
);
58 newtitle
= strdupA("Wine Configuration");
61 WINE_TRACE("setting title to %s\n", newtitle
);
62 SendMessage(GetParent(dialog
), PSM_SETTITLE
, 0, (LPARAM
) newtitle
);
63 HeapFree(GetProcessHeap(), 0, newtitle
);
68 * getkey: Retrieves a configuration value from the registry
70 * char *subkey : the name of the config section
71 * char *name : the name of the config value
72 * char *default : if the key isn't found, return this value instead
74 * Returns a buffer holding the value if successful, NULL if
75 * not. Caller is responsible for releasing the result.
78 static char *getkey (char *subkey
, char *name
, char *def
)
85 WINE_TRACE("subkey=%s, name=%s, def=%s\n", subkey
, name
, def
);
87 res
= RegOpenKeyEx(config_key
, subkey
, 0, KEY_READ
, &hSubKey
);
88 if (res
!= ERROR_SUCCESS
)
90 if (res
== ERROR_FILE_NOT_FOUND
)
92 WINE_TRACE("Section key not present - using default\n");
93 return def
? strdupA(def
) : NULL
;
97 WINE_ERR("RegOpenKey failed on wine config key (res=%ld)\n", res
);
102 res
= RegQueryValueExA(hSubKey
, name
, NULL
, NULL
, NULL
, &len
);
103 if (res
== ERROR_FILE_NOT_FOUND
)
105 WINE_TRACE("Value not present - using default\n");
106 buffer
= def
? strdupA(def
) : NULL
;
108 } else if (res
!= ERROR_SUCCESS
)
110 WINE_ERR("Couldn't query value's length (res=%ld)\n", res
);
114 buffer
= HeapAlloc(GetProcessHeap(), 0, len
+ 1);
116 RegQueryValueEx(hSubKey
, name
, NULL
, NULL
, buffer
, &len
);
118 WINE_TRACE("buffer=%s\n", buffer
);
120 if (hSubKey
) RegCloseKey(hSubKey
);
126 * setkey: convenience wrapper to set a key/value pair
128 * const char *subKey : the name of the config section
129 * const char *valueName : the name of the config value
130 * const char *value : the value to set the configuration key to
132 * Returns 0 on success, non-zero otherwise
134 * If valueName or value is NULL, an empty section will be created
136 int setkey(const char *subkey
, const char *name
, const char *value
) {
140 WINE_TRACE("subkey=%s: name=%s, value=%s\n", subkey
, name
, value
);
142 assert( subkey
!= NULL
);
144 res
= RegCreateKey(config_key
, subkey
, &key
);
145 if (res
!= ERROR_SUCCESS
) goto end
;
146 if (name
== NULL
|| value
== NULL
) goto end
;
148 res
= RegSetValueEx(key
, name
, 0, REG_SZ
, value
, strlen(value
) + 1);
149 if (res
!= ERROR_SUCCESS
) goto end
;
153 if (key
) RegCloseKey(key
);
154 if (res
!= 0) WINE_ERR("Unable to set configuration key %s in section %s to %s, res=%ld\n", name
, subkey
, value
, res
);
158 /* removes the requested value from the registry, however, does not
159 * remove the section if empty. Returns S_OK (0) on success.
161 static HRESULT
remove_value(const char *subkey
, const char *name
)
166 WINE_TRACE("subkey=%s, name=%s\n", subkey
, name
);
168 hr
= RegOpenKeyEx(config_key
, subkey
, 0, KEY_READ
, &key
);
169 if (hr
!= S_OK
) return hr
;
171 hr
= RegDeleteValue(key
, name
);
172 if (hr
!= ERROR_SUCCESS
) return hr
;
177 /* removes the requested subkey from the registry, assuming it exists */
178 static HRESULT
remove_path(char *section
) {
179 WINE_TRACE("section=%s\n", section
);
181 return RegDeleteKey(config_key
, section
);
185 /* ========================================================================= */
187 /* This code exists for the following reasons:
189 * - It makes working with the registry easier
190 * - By storing a mini cache of the registry, we can more easily implement
191 * cancel/revert and apply. The 'settings list' is an overlay on top of
192 * the actual registry data that we can write out at will.
194 * Rather than model a tree in memory, we simply store each absolute (rooted
195 * at the config key) path.
202 char *path
; /* path in the registry rooted at the config key */
203 char *name
; /* name of the registry value */
204 char *value
; /* contents of the registry value. if null, this means a deletion */
207 struct list
*settings
;
209 static void free_setting(struct setting
*setting
)
211 assert( setting
!= NULL
);
213 WINE_TRACE("destroying %p\n", setting
);
215 assert( setting
->path
&& setting
->name
);
217 HeapFree(GetProcessHeap(), 0, setting
->path
);
218 HeapFree(GetProcessHeap(), 0, setting
->name
);
219 if (setting
->value
) HeapFree(GetProcessHeap(), 0, setting
->value
);
221 list_remove(&setting
->entry
);
223 HeapFree(GetProcessHeap(), 0, setting
);
227 * Returns the contents of the value at path. If not in the settings
228 * list, it will be fetched from the registry - failing that, the
229 * default will be used.
231 * If already in the list, the contents as given there will be
232 * returned. You are expected to HeapFree the result.
234 char *get(char *path
, char *name
, char *def
)
240 WINE_TRACE("path=%s, name=%s, def=%s\n", path
, name
, def
);
242 /* check if it's in the list */
243 LIST_FOR_EACH( cursor
, settings
)
245 s
= LIST_ENTRY(cursor
, struct setting
, entry
);
247 if (strcasecmp(path
, s
->path
) != 0) continue;
248 if (strcasecmp(name
, s
->name
) != 0) continue;
250 WINE_TRACE("found %s:%s in settings list, returning %s\n", path
, name
, s
->value
);
251 return strdupA(s
->value
);
254 /* no, so get from the registry */
255 val
= getkey(path
, name
, def
);
257 WINE_TRACE("returning %s\n", val
);
263 * Used to set a registry key.
265 * path is rooted at the config key, ie use "Version" or
266 * "AppDefaults\\fooapp.exe\\Version". You can use keypath()
267 * to get such a string.
269 * name is the value name, it must not be null (you cannot create
270 * empty groups, sorry ...)
272 * value is what to set the value to, or NULL to delete it.
274 * These values will be copied when necessary.
276 void set(char *path
, char *name
, char *value
)
281 assert( path
!= NULL
);
282 assert( name
!= NULL
);
284 WINE_TRACE("path=%s, name=%s, value=%s\n", path
, name
, value
);
286 /* firstly, see if we already set this setting */
287 LIST_FOR_EACH( cursor
, settings
)
289 struct setting
*s
= LIST_ENTRY(cursor
, struct setting
, entry
);
291 if (strcasecmp(s
->path
, path
) != 0) continue;
292 if (strcasecmp(s
->name
, name
) != 0) continue;
294 /* yes, we have already set it, so just replace the content and return */
295 if (s
->value
) HeapFree(GetProcessHeap(), 0, s
->value
);
296 s
->value
= value
? strdupA(value
) : NULL
;
301 /* otherwise add a new setting for it */
302 s
= HeapAlloc(GetProcessHeap(), 0, sizeof(struct setting
));
303 s
->path
= strdupA(path
);
304 s
->name
= strdupA(name
);
305 s
->value
= value
? strdupA(value
) : NULL
;
307 list_add_tail(settings
, &s
->entry
);
311 * enumerates the value names at the given path, taking into account
312 * the changes in the settings list.
314 * you are expected to HeapFree each element of the array, which is null
315 * terminated, as well as the array itself.
317 char **enumerate_values(char *path
)
321 char **values
= NULL
;
325 res
= RegOpenKeyEx(config_key
, path
, 0, KEY_READ
, &key
);
326 if (res
== ERROR_SUCCESS
)
331 DWORD namesize
= sizeof(name
);
332 BOOL removed
= FALSE
;
334 /* find out the needed size, allocate a buffer, read the value */
335 if ((res
= RegEnumValue(key
, i
, name
, &namesize
, NULL
, NULL
, NULL
, NULL
)) != ERROR_SUCCESS
)
338 WINE_TRACE("name=%s\n", name
);
340 /* check if this value name has been removed in the settings list */
341 LIST_FOR_EACH( cursor
, settings
)
343 struct setting
*s
= LIST_ENTRY(cursor
, struct setting
, entry
);
344 if (strcasecmp(s
->path
, path
) != 0) continue;
345 if (strcasecmp(s
->name
, name
) != 0) continue;
349 WINE_TRACE("this key has been removed, so skipping\n");
355 if (removed
) /* this value was deleted by the user, so don't include it */
357 HeapFree(GetProcessHeap(), 0, name
);
362 /* grow the array if necessary, add buffer to it, iterate */
363 if (values
) values
= HeapReAlloc(GetProcessHeap(), 0, values
, sizeof(char*) * (valueslen
+ 1));
364 else values
= HeapAlloc(GetProcessHeap(), 0, sizeof(char*));
366 values
[valueslen
++] = strdupA(name
);
367 WINE_TRACE("valueslen is now %d\n", valueslen
);
373 WINE_WARN("failed opening registry key %s, res=0x%lx\n", path
, res
);
376 WINE_TRACE("adding settings in list but not registry\n");
378 /* now we have to add the values that aren't in the registry but are in the settings list */
379 LIST_FOR_EACH( cursor
, settings
)
381 struct setting
*setting
= LIST_ENTRY(cursor
, struct setting
, entry
);
384 if (strcasecmp(setting
->path
, path
) != 0) continue;
386 if (!setting
->value
) continue;
388 for (i
= 0; i
< valueslen
; i
++)
390 if (strcasecmp(setting
->name
, values
[i
]) == 0)
399 WINE_TRACE("%s in list but not registry\n", setting
->name
);
401 /* otherwise it's been set by the user but isn't in the registry */
402 if (values
) values
= HeapReAlloc(GetProcessHeap(), 0, values
, sizeof(char*) * (valueslen
+ 1));
403 else values
= HeapAlloc(GetProcessHeap(), 0, sizeof(char*));
405 values
[valueslen
++] = strdupA(setting
->name
);
408 WINE_TRACE("adding null terminator\n");
411 values
= HeapReAlloc(GetProcessHeap(), 0, values
, sizeof(char*) * (valueslen
+ 1));
412 values
[valueslen
] = NULL
;
421 * returns true if the given key/value pair exists in the registry or
422 * has been written to.
424 BOOL
exists(char *path
, char *name
)
426 char *val
= get(path
, name
, NULL
);
430 HeapFree(GetProcessHeap(), 0, val
);
437 static void process_setting(struct setting
*s
)
441 WINE_TRACE("Setting %s:%s to '%s'\n", s
->path
, s
->name
, s
->value
);
442 setkey(s
->path
, s
->name
, s
->value
);
446 /* NULL name means remove that path/section entirely */
447 if (s
->path
&& s
->name
) remove_value(s
->path
, s
->name
);
448 else if (s
->path
&& !s
->name
) remove_path(s
->path
);
454 if (list_empty(settings
)) return; /* we will be called for each page when the user clicks OK */
458 while (!list_empty(settings
))
460 struct setting
*s
= (struct setting
*) list_head(settings
);
466 /* ================================== utility functions ============================ */
468 char *currentApp
= NULL
; /* the app we are currently editing, or NULL if editing global */
470 /* returns a registry key path suitable for passing to addTransaction */
471 char *keypath(char *section
)
473 static char *result
= NULL
;
475 if (result
) HeapFree(GetProcessHeap(), 0, result
);
479 result
= HeapAlloc(GetProcessHeap(), 0, strlen("AppDefaults\\") + strlen(currentApp
) + 2 /* \\ */ + strlen(section
) + 1 /* terminator */);
480 sprintf(result
, "AppDefaults\\%s\\%s", currentApp
, section
);
484 result
= strdupA(section
);
490 void PRINTERROR(void)
494 FormatMessageA(FORMAT_MESSAGE_ALLOCATE_BUFFER
|FORMAT_MESSAGE_FROM_SYSTEM
,
495 0, GetLastError(), MAKELANGID(LANG_NEUTRAL
,SUBLANG_DEFAULT
),
496 (LPSTR
)&msg
, 0, NULL
);
497 WINE_TRACE("error: '%s'\n", msg
);
500 int initialize(void) {
501 DWORD res
= RegCreateKey(HKEY_LOCAL_MACHINE
, WINE_KEY_ROOT
, &config_key
);
503 if (res
!= ERROR_SUCCESS
) {
504 WINE_ERR("RegOpenKey failed on wine config key (%ld)\n", res
);
508 /* we could probably just have the list as static data */
509 settings
= HeapAlloc(GetProcessHeap(), 0, sizeof(struct list
));