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 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 (char *subkey, char *name, char *def)
91 WINE_TRACE("subkey=%s, name=%s, def=%s\n", subkey, name, def);
93 res = RegOpenKeyEx(config_key, subkey, 0, KEY_READ, &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, buffer, &len);
124 WINE_TRACE("buffer=%s\n", buffer);
126 if (hSubKey) 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 int set_config_key(const char *subkey, const char *name, const char *value) {
146 WINE_TRACE("subkey=%s: name=%s, value=%s\n", subkey, name, value);
148 assert( subkey != NULL );
150 res = RegCreateKey(config_key, subkey, &key);
151 if (res != ERROR_SUCCESS) goto end;
152 if (name == NULL || value == NULL) goto end;
154 res = RegSetValueEx(key, name, 0, REG_SZ, value, strlen(value) + 1);
155 if (res != ERROR_SUCCESS) goto end;
159 if (key) RegCloseKey(key);
160 if (res != 0) WINE_ERR("Unable to set configuration key %s in section %s to %s, res=%ld\n", name, subkey, value, res);
164 /* removes the requested value from the registry, however, does not
165 * remove the section if empty. Returns S_OK (0) on success.
167 static HRESULT remove_value(const char *subkey, const char *name)
172 WINE_TRACE("subkey=%s, name=%s\n", subkey, name);
174 hr = RegOpenKeyEx(config_key, subkey, 0, KEY_READ, &key);
175 if (hr != S_OK) return hr;
177 hr = RegDeleteValue(key, name);
178 if (hr != ERROR_SUCCESS) return hr;
183 /* removes the requested subkey from the registry, assuming it exists */
184 static HRESULT remove_path(char *section) {
185 WINE_TRACE("section=%s\n", section);
187 return RegDeleteKey(config_key, section);
191 /* ========================================================================= */
193 /* This code exists for the following reasons:
195 * - It makes working with the registry easier
196 * - By storing a mini cache of the registry, we can more easily implement
197 * cancel/revert and apply. The 'settings list' is an overlay on top of
198 * the actual registry data that we can write out at will.
200 * Rather than model a tree in memory, we simply store each absolute (rooted
201 * at the config key) path.
208 char *path; /* path in the registry rooted at the config key */
209 char *name; /* name of the registry value */
210 char *value; /* contents of the registry value. if null, this means a deletion */
213 struct list *settings;
215 static void free_setting(struct setting *setting)
217 assert( setting != NULL );
219 WINE_TRACE("destroying %p\n", setting);
221 assert( setting->path && setting->name );
223 HeapFree(GetProcessHeap(), 0, setting->path);
224 HeapFree(GetProcessHeap(), 0, setting->name);
225 if (setting->value) HeapFree(GetProcessHeap(), 0, setting->value);
227 list_remove(&setting->entry);
229 HeapFree(GetProcessHeap(), 0, setting);
233 * Returns the contents of the value at path. If not in the settings
234 * list, it will be fetched from the registry - failing that, the
235 * default will be used.
237 * If already in the list, the contents as given there will be
238 * returned. You are expected to HeapFree the result.
240 char *get(char *path, char *name, char *def)
246 WINE_TRACE("path=%s, name=%s, def=%s\n", path, name, def);
248 /* check if it's in the list */
249 LIST_FOR_EACH( cursor, settings )
251 s = LIST_ENTRY(cursor, struct setting, entry);
253 if (strcasecmp(path, s->path) != 0) continue;
254 if (strcasecmp(name, s->name) != 0) continue;
256 WINE_TRACE("found %s:%s in settings list, returning %s\n", path, name, s->value);
257 return strdupA(s->value);
260 /* no, so get from the registry */
261 val = get_config_key(path, name, def);
263 WINE_TRACE("returning %s\n", val);
269 * Used to set a registry key.
271 * path is rooted at the config key, ie use "Version" or
272 * "AppDefaults\\fooapp.exe\\Version". You can use keypath()
273 * to get such a string.
275 * name is the value name, it must not be null (you cannot create
276 * empty groups, sorry ...)
278 * value is what to set the value to, or NULL to delete it.
280 * These values will be copied when necessary.
282 void set(char *path, char *name, char *value)
287 assert( path != NULL );
288 assert( name != NULL );
290 WINE_TRACE("path=%s, name=%s, value=%s\n", path, name, value);
292 /* firstly, see if we already set this setting */
293 LIST_FOR_EACH( cursor, settings )
295 struct setting *s = LIST_ENTRY(cursor, struct setting, entry);
297 if (strcasecmp(s->path, path) != 0) continue;
298 if (strcasecmp(s->name, name) != 0) continue;
300 /* yes, we have already set it, so just replace the content and return */
301 if (s->value) HeapFree(GetProcessHeap(), 0, s->value);
302 s->value = value ? strdupA(value) : NULL;
307 /* otherwise add a new setting for it */
308 s = HeapAlloc(GetProcessHeap(), 0, sizeof(struct setting));
309 s->path = strdupA(path);
310 s->name = strdupA(name);
311 s->value = value ? strdupA(value) : NULL;
313 list_add_tail(settings, &s->entry);
317 * enumerates the value names at the given path, taking into account
318 * the changes in the settings list.
320 * you are expected to HeapFree each element of the array, which is null
321 * terminated, as well as the array itself.
323 char **enumerate_values(char *path)
327 char **values = NULL;
331 res = RegOpenKeyEx(config_key, path, 0, KEY_READ, &key);
332 if (res == ERROR_SUCCESS)
337 DWORD namesize = sizeof(name);
338 BOOL removed = FALSE;
340 /* find out the needed size, allocate a buffer, read the value */
341 if ((res = RegEnumValue(key, i, name, &namesize, NULL, NULL, NULL, NULL)) != ERROR_SUCCESS)
344 WINE_TRACE("name=%s\n", name);
346 /* check if this value name has been removed in the settings list */
347 LIST_FOR_EACH( cursor, settings )
349 struct setting *s = LIST_ENTRY(cursor, struct setting, entry);
350 if (strcasecmp(s->path, path) != 0) continue;
351 if (strcasecmp(s->name, name) != 0) continue;
355 WINE_TRACE("this key has been removed, so skipping\n");
361 if (removed) /* this value was deleted by the user, so don't include it */
363 HeapFree(GetProcessHeap(), 0, name);
368 /* grow the array if necessary, add buffer to it, iterate */
369 if (values) values = HeapReAlloc(GetProcessHeap(), 0, values, sizeof(char*) * (valueslen + 1));
370 else values = HeapAlloc(GetProcessHeap(), 0, sizeof(char*));
372 values[valueslen++] = strdupA(name);
373 WINE_TRACE("valueslen is now %d\n", valueslen);
379 WINE_WARN("failed opening registry key %s, res=0x%lx\n", path, res);
382 WINE_TRACE("adding settings in list but not registry\n");
384 /* now we have to add the values that aren't in the registry but are in the settings list */
385 LIST_FOR_EACH( cursor, settings )
387 struct setting *setting = LIST_ENTRY(cursor, struct setting, entry);
390 if (strcasecmp(setting->path, path) != 0) continue;
392 if (!setting->value) continue;
394 for (i = 0; i < valueslen; i++)
396 if (strcasecmp(setting->name, values[i]) == 0)
405 WINE_TRACE("%s in list but not registry\n", setting->name);
407 /* otherwise it's been set by the user but isn't in the registry */
408 if (values) values = HeapReAlloc(GetProcessHeap(), 0, values, sizeof(char*) * (valueslen + 1));
409 else values = HeapAlloc(GetProcessHeap(), 0, sizeof(char*));
411 values[valueslen++] = strdupA(setting->name);
414 WINE_TRACE("adding null terminator\n");
417 values = HeapReAlloc(GetProcessHeap(), 0, values, sizeof(char*) * (valueslen + 1));
418 values[valueslen] = NULL;
427 * returns true if the given key/value pair exists in the registry or
428 * has been written to.
430 BOOL exists(char *path, char *name)
432 char *val = get(path, name, NULL);
436 HeapFree(GetProcessHeap(), 0, val);
443 static void process_setting(struct setting *s)
447 WINE_TRACE("Setting %s:%s to '%s'\n", s->path, s->name, s->value);
448 set_config_key(s->path, s->name, s->value);
452 /* NULL name means remove that path/section entirely */
453 if (s->path && s->name) remove_value(s->path, s->name);
454 else if (s->path && !s->name) remove_path(s->path);
460 if (list_empty(settings)) return; /* we will be called for each page when the user clicks OK */
464 while (!list_empty(settings))
466 struct setting *s = (struct setting *) list_head(settings);
472 /* ================================== utility functions ============================ */
474 char *current_app = NULL; /* the app we are currently editing, or NULL if editing global */
476 /* returns a registry key path suitable for passing to addTransaction */
477 char *keypath(char *section)
479 static char *result = NULL;
481 if (result) HeapFree(GetProcessHeap(), 0, result);
485 result = HeapAlloc(GetProcessHeap(), 0, strlen("AppDefaults\\") + strlen(current_app) + 2 /* \\ */ + strlen(section) + 1 /* terminator */);
486 sprintf(result, "AppDefaults\\%s\\%s", current_app, section);
490 result = strdupA(section);
496 void PRINTERROR(void)
500 FormatMessageA(FORMAT_MESSAGE_ALLOCATE_BUFFER|FORMAT_MESSAGE_FROM_SYSTEM,
501 0, GetLastError(), MAKELANGID(LANG_NEUTRAL,SUBLANG_DEFAULT),
502 (LPSTR)&msg, 0, NULL);
504 /* eliminate trailing newline, is this a Wine bug? */
505 *(strrchr(msg, '\n')) = '\0';
507 WINE_TRACE("error: '%s'\n", msg);
510 int initialize(void) {
511 DWORD res = RegCreateKey(HKEY_LOCAL_MACHINE, WINE_KEY_ROOT, &config_key);
513 if (res != ERROR_SUCCESS) {
514 WINE_ERR("RegOpenKey failed on wine config key (%ld)\n", res);
518 /* we could probably just have the list as static data */
519 settings = HeapAlloc(GetProcessHeap(), 0, sizeof(struct list));