- rewrite the transaction system to be based on a settings overlay,
[wine] / programs / winecfg / winecfg.c
1 /*
2  * WineCfg configuration management
3  *
4  * Copyright 2002 Jaco Greeff
5  * Copyright 2003 Dimitrie O. Paun
6  * Copyright 2003-2004 Mike Hearn
7  *
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  *
22  */
23
24 #include <assert.h>
25 #include <stdio.h>
26 #include <limits.h>
27 #include <windows.h>
28 #include <winreg.h>
29 #include <wine/debug.h>
30 #include <wine/list.h>
31
32 WINE_DEFAULT_DEBUG_CHANNEL(winecfg);
33
34 #include "winecfg.h"
35
36 HKEY config_key = NULL;
37
38
39
40 /* this is called from the WM_SHOWWINDOW handlers of each tab page.
41  *
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.
44  */
45 void set_window_title(HWND dialog)
46 {
47     char *newtitle;
48
49     /* update the window title  */
50     if (currentApp)
51     {
52         char *template = "Wine Configuration for %s";
53         newtitle = HeapAlloc(GetProcessHeap(), 0, strlen(template) + strlen(currentApp) + 1);
54         sprintf(newtitle, template, currentApp);
55     }
56     else
57     {
58         newtitle = strdupA("Wine Configuration");
59     }
60
61     WINE_TRACE("setting title to %s\n", newtitle);
62     SendMessage(GetParent(dialog), PSM_SETTITLE, 0, (LPARAM) newtitle);
63     HeapFree(GetProcessHeap(), 0, newtitle);
64 }
65
66
67 /**
68  * getkey: Retrieves a configuration value from the registry
69  *
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
73  *
74  * Returns a buffer holding the value if successful, NULL if
75  * not. Caller is responsible for releasing the result.
76  *
77  */
78 static char *getkey (char *subkey, char *name, char *def)
79 {
80     LPBYTE buffer = NULL;
81     DWORD len;
82     HKEY hSubKey = NULL;
83     DWORD res;
84
85     WINE_TRACE("subkey=%s, name=%s, def=%s\n", subkey, name, def);
86
87     res = RegOpenKeyEx(config_key, subkey, 0, KEY_READ, &hSubKey);
88     if (res != ERROR_SUCCESS)
89     {
90         if (res == ERROR_FILE_NOT_FOUND)
91         {
92             WINE_TRACE("Section key not present - using default\n");
93             return def ? strdupA(def) : NULL;
94         }
95         else
96         {
97             WINE_ERR("RegOpenKey failed on wine config key (res=%ld)\n", res);
98         }
99         goto end;
100     }
101
102     res = RegQueryValueExA(hSubKey, name, NULL, NULL, NULL, &len);
103     if (res == ERROR_FILE_NOT_FOUND)
104     {
105         WINE_TRACE("Value not present - using default\n");
106         buffer = def ? strdupA(def) : NULL;
107         goto end;
108     } else if (res != ERROR_SUCCESS)
109     {
110         WINE_ERR("Couldn't query value's length (res=%ld)\n", res);
111         goto end;
112     }
113
114     buffer = HeapAlloc(GetProcessHeap(), 0, len + 1);
115
116     RegQueryValueEx(hSubKey, name, NULL, NULL, buffer, &len);
117
118     WINE_TRACE("buffer=%s\n", buffer);
119 end:
120     if (hSubKey) RegCloseKey(hSubKey);
121
122     return buffer;
123 }
124
125 /**
126  * setkey: convenience wrapper to set a key/value pair
127  *
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
131  *
132  * Returns 0 on success, non-zero otherwise
133  *
134  * If valueName or value is NULL, an empty section will be created
135  */
136 int setkey(const char *subkey, const char *name, const char *value) {
137     DWORD res = 1;
138     HKEY key = NULL;
139
140     WINE_TRACE("subkey=%s: name=%s, value=%s\n", subkey, name, value);
141
142     assert( subkey != NULL );
143
144     res = RegCreateKey(config_key, subkey, &key);
145     if (res != ERROR_SUCCESS) goto end;
146     if (name == NULL || value == NULL) goto end;
147
148     res = RegSetValueEx(key, name, 0, REG_SZ, value, strlen(value) + 1);
149     if (res != ERROR_SUCCESS) goto end;
150
151     res = 0;
152 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);
155     return res;
156 }
157
158 /* removes the requested value from the registry, however, does not
159  * remove the section if empty. Returns S_OK (0) on success.
160  */
161 static HRESULT remove_value(const char *subkey, const char *name)
162 {
163     HRESULT hr;
164     HKEY key;
165
166     WINE_TRACE("subkey=%s, name=%s\n", subkey, name);
167
168     hr = RegOpenKeyEx(config_key, subkey, 0, KEY_READ, &key);
169     if (hr != S_OK) return hr;
170
171     hr = RegDeleteValue(key, name);
172     if (hr != ERROR_SUCCESS) return hr;
173
174     return S_OK;
175 }
176
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);
180
181     return RegDeleteKey(config_key, section);
182 }
183
184
185 /* ========================================================================= */
186
187 /* This code exists for the following reasons:
188  *
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.
193  *
194  * Rather than model a tree in memory, we simply store each absolute (rooted
195  * at the config key) path.
196  *
197  */
198
199 struct setting
200 {
201     struct list entry;
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  */
205 };
206
207 struct list *settings;
208
209 static void free_setting(struct setting *setting)
210 {
211     assert( setting != NULL );
212
213     WINE_TRACE("destroying %p\n", setting);
214
215     assert( setting->path && setting->name );
216
217     HeapFree(GetProcessHeap(), 0, setting->path);
218     HeapFree(GetProcessHeap(), 0, setting->name);
219     if (setting->value) HeapFree(GetProcessHeap(), 0, setting->value);
220
221     list_remove(&setting->entry);
222
223     HeapFree(GetProcessHeap(), 0, setting);
224 }
225
226 /**
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.
230  *
231  * If already in the list, the contents as given there will be
232  * returned. You are expected to HeapFree the result.
233  */
234 char *get(char *path, char *name, char *def)
235 {
236     struct list *cursor;
237     struct setting *s;
238     char *val;
239
240     WINE_TRACE("path=%s, name=%s, def=%s\n", path, name, def);
241
242     /* check if it's in the list */
243     LIST_FOR_EACH( cursor, settings )
244     {
245         s = LIST_ENTRY(cursor, struct setting, entry);
246
247         if (strcasecmp(path, s->path) != 0) continue;
248         if (strcasecmp(name, s->name) != 0) continue;
249
250         WINE_TRACE("found %s:%s in settings list, returning %s\n", path, name, s->value);
251         return strdupA(s->value);
252     }
253
254     /* no, so get from the registry */
255     val = getkey(path, name, def);
256
257     WINE_TRACE("returning %s\n", val);
258
259     return val;
260 }
261
262 /**
263  * Used to set a registry key.
264  *
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.
268  *
269  * name is the value name, it must not be null (you cannot create
270  * empty groups, sorry ...)
271  *
272  * value is what to set the value to, or NULL to delete it.
273  *
274  * These values will be copied when necessary.
275  */
276 void set(char *path, char *name, char *value)
277 {
278     struct list *cursor;
279     struct setting *s;
280
281     assert( path != NULL );
282     assert( name != NULL );
283
284     WINE_TRACE("path=%s, name=%s, value=%s\n", path, name, value);
285
286     /* firstly, see if we already set this setting  */
287     LIST_FOR_EACH( cursor, settings )
288     {
289         struct setting *s = LIST_ENTRY(cursor, struct setting, entry);
290
291         if (strcasecmp(s->path, path) != 0) continue;
292         if (strcasecmp(s->name, name) != 0) continue;
293
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;
297
298         return;
299     }
300
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;
306
307     list_add_tail(settings, &s->entry);
308 }
309
310 /**
311  * enumerates the value names at the given path, taking into account
312  * the changes in the settings list.
313  *
314  * you are expected to HeapFree each element of the array, which is null
315  * terminated, as well as the array itself.
316  */
317 char **enumerate_values(char *path)
318 {
319     HKEY key;
320     DWORD res, i = 0;
321     char **values = NULL;
322     int valueslen = 0;
323     struct list *cursor;
324
325     res = RegOpenKeyEx(config_key, path, 0, KEY_READ, &key);
326     if (res == ERROR_SUCCESS)
327     {
328         while (TRUE)
329         {
330             char name[1024];
331             DWORD namesize = sizeof(name);
332             BOOL removed = FALSE;
333
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)
336                 break;
337
338             WINE_TRACE("name=%s\n", name);
339
340             /* check if this value name has been removed in the settings list  */
341             LIST_FOR_EACH( cursor, settings )
342             {
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;
346
347                 if (!s->value)
348                 {
349                     WINE_TRACE("this key has been removed, so skipping\n");
350                     removed = TRUE;
351                     break;
352                 }
353             }
354
355             if (removed)            /* this value was deleted by the user, so don't include it */
356             {
357                 HeapFree(GetProcessHeap(), 0, name);
358                 i++;
359                 continue;
360             }
361
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*));
365
366             values[valueslen++] = strdupA(name);
367             WINE_TRACE("valueslen is now %d\n", valueslen);
368             i++;
369         }
370     }
371     else
372     {
373         WINE_WARN("failed opening registry key %s, res=0x%lx\n", path, res);
374     }
375
376     WINE_TRACE("adding settings in list but not registry\n");
377
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 )
380     {
381         struct setting *setting = LIST_ENTRY(cursor, struct setting, entry);
382         BOOL found = FALSE;
383
384         if (strcasecmp(setting->path, path) != 0) continue;
385
386         if (!setting->value) continue;
387
388         for (i = 0; i < valueslen; i++)
389         {
390             if (strcasecmp(setting->name, values[i]) == 0)
391             {
392                 found = TRUE;
393                 break;
394             }
395         }
396
397         if (found) continue;
398
399         WINE_TRACE("%s in list but not registry\n", setting->name);
400
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*));
404
405         values[valueslen++] = strdupA(setting->name);
406     }
407
408     WINE_TRACE("adding null terminator\n");
409     if (values)
410     {
411         values = HeapReAlloc(GetProcessHeap(), 0, values, sizeof(char*) * (valueslen + 1));
412         values[valueslen] = NULL;
413     }
414
415     RegCloseKey(key);
416
417     return values;
418 }
419
420 /**
421  * returns true if the given key/value pair exists in the registry or
422  * has been written to.
423  */
424 BOOL exists(char *path, char *name)
425 {
426     char *val = get(path, name, NULL);
427
428     if (val)
429     {
430         HeapFree(GetProcessHeap(), 0, val);
431         return TRUE;
432     }
433
434     return FALSE;
435 }
436
437 static void process_setting(struct setting *s)
438 {
439     if (s->value)
440     {
441         WINE_TRACE("Setting %s:%s to '%s'\n", s->path, s->name, s->value);
442         setkey(s->path, s->name, s->value);
443     }
444     else
445     {
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);
449     }
450 }
451
452 void apply(void)
453 {
454     if (list_empty(settings)) return; /* we will be called for each page when the user clicks OK */
455
456     WINE_TRACE("()\n");
457
458     while (!list_empty(settings))
459     {
460         struct setting *s = (struct setting *) list_head(settings);
461         process_setting(s);
462         free_setting(s);
463     }
464 }
465
466 /* ================================== utility functions ============================ */
467
468 char *currentApp = NULL; /* the app we are currently editing, or NULL if editing global */
469
470 /* returns a registry key path suitable for passing to addTransaction  */
471 char *keypath(char *section)
472 {
473     static char *result = NULL;
474
475     if (result) HeapFree(GetProcessHeap(), 0, result);
476
477     if (currentApp)
478     {
479         result = HeapAlloc(GetProcessHeap(), 0, strlen("AppDefaults\\") + strlen(currentApp) + 2 /* \\ */ + strlen(section) + 1 /* terminator */);
480         sprintf(result, "AppDefaults\\%s\\%s", currentApp, section);
481     }
482     else
483     {
484         result = strdupA(section);
485     }
486
487     return result;
488 }
489
490 /* returns a string with the window text of the dialog item. user is responsible for freeing the result */
491 char *getDialogItemText(HWND hDlg, WORD controlID) {
492     HWND item = GetDlgItem(hDlg, controlID);
493     int len = GetWindowTextLength(item) + 1;
494     char *result = malloc(len);
495     if (GetWindowText(item, result, len) == 0) return NULL;
496     return result;
497 }
498
499 void PRINTERROR(void)
500 {
501         LPSTR msg;
502
503         FormatMessageA(FORMAT_MESSAGE_ALLOCATE_BUFFER|FORMAT_MESSAGE_FROM_SYSTEM,
504                        0, GetLastError(), MAKELANGID(LANG_NEUTRAL,SUBLANG_DEFAULT),
505                        (LPSTR)&msg, 0, NULL);
506         WINE_TRACE("error: '%s'\n", msg);
507 }
508
509 int initialize(void) {
510     DWORD res = RegCreateKey(HKEY_LOCAL_MACHINE, WINE_KEY_ROOT, &config_key);
511
512     if (res != ERROR_SUCCESS) {
513         WINE_ERR("RegOpenKey failed on wine config key (%ld)\n", res);
514         return 1;
515     }
516
517     /* we could probably just have the list as static data  */
518     settings = HeapAlloc(GetProcessHeap(), 0, sizeof(struct list));
519     list_init(settings);
520
521     return 0;
522 }