Constify all needlessly non-const winecfg structs.
[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  * 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  *
28  */
29
30 #include <assert.h>
31 #include <stdio.h>
32 #include <limits.h>
33 #include <windows.h>
34 #include <winreg.h>
35 #include <wine/debug.h>
36 #include <wine/list.h>
37
38 WINE_DEFAULT_DEBUG_CHANNEL(winecfg);
39
40 #include "winecfg.h"
41
42 HKEY config_key = NULL;
43
44
45
46 /* this is called from the WM_SHOWWINDOW handlers of each tab page.
47  *
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.
50  */
51 void set_window_title(HWND dialog)
52 {
53     char *newtitle;
54
55     /* update the window title  */
56     if (current_app)
57     {
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);
61     }
62     else
63     {
64         newtitle = strdupA("Wine Configuration");
65     }
66
67     WINE_TRACE("setting title to %s\n", newtitle);
68     SendMessage(GetParent(dialog), PSM_SETTITLE, 0, (LPARAM) newtitle);
69     HeapFree(GetProcessHeap(), 0, newtitle);
70 }
71
72
73 /**
74  * get_config_key: Retrieves a configuration value from the registry
75  *
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
79  *
80  * Returns a buffer holding the value if successful, NULL if
81  * not. Caller is responsible for releasing the result.
82  *
83  */
84 static char *get_config_key (HKEY root, const char *subkey, const char *name, const char *def)
85 {
86     LPBYTE buffer = NULL;
87     DWORD len;
88     HKEY hSubKey = NULL;
89     DWORD res;
90
91     WINE_TRACE("subkey=%s, name=%s, def=%s\n", subkey, name, def);
92
93     res = RegOpenKey(root, subkey, &hSubKey);
94     if (res != ERROR_SUCCESS)
95     {
96         if (res == ERROR_FILE_NOT_FOUND)
97         {
98             WINE_TRACE("Section key not present - using default\n");
99             return def ? strdupA(def) : NULL;
100         }
101         else
102         {
103             WINE_ERR("RegOpenKey failed on wine config key (res=%ld)\n", res);
104         }
105         goto end;
106     }
107
108     res = RegQueryValueExA(hSubKey, name, NULL, NULL, NULL, &len);
109     if (res == ERROR_FILE_NOT_FOUND)
110     {
111         WINE_TRACE("Value not present - using default\n");
112         buffer = def ? strdupA(def) : NULL;
113         goto end;
114     } else if (res != ERROR_SUCCESS)
115     {
116         WINE_ERR("Couldn't query value's length (res=%ld)\n", res);
117         goto end;
118     }
119
120     buffer = HeapAlloc(GetProcessHeap(), 0, len + 1);
121
122     RegQueryValueEx(hSubKey, name, NULL, NULL, buffer, &len);
123
124     WINE_TRACE("buffer=%s\n", buffer);
125 end:
126     if (hSubKey && hSubKey != root) RegCloseKey(hSubKey);
127
128     return (char*)buffer;
129 }
130
131 /**
132  * set_config_key: convenience wrapper to set a key/value pair
133  *
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
137  *
138  * Returns 0 on success, non-zero otherwise
139  *
140  * If valueName or value is NULL, an empty section will be created
141  */
142 static int set_config_key(HKEY root, const char *subkey, const char *name, const char *value) {
143     DWORD res = 1;
144     HKEY key = NULL;
145
146     WINE_TRACE("subkey=%s: name=%s, value=%s\n", subkey, name, value);
147
148     assert( subkey != NULL );
149
150     if (subkey[0])
151     {
152         res = RegCreateKey(root, subkey, &key);
153         if (res != ERROR_SUCCESS) goto end;
154     }
155     else key = root;
156     if (name == NULL || value == NULL) goto end;
157
158     res = RegSetValueEx(key, name, 0, REG_SZ, value, strlen(value) + 1);
159     if (res != ERROR_SUCCESS) goto end;
160
161     res = 0;
162 end:
163     if (key && key != root) RegCloseKey(key);
164     if (res != 0) WINE_ERR("Unable to set configuration key %s in section %s to %s, res=%ld\n", name, subkey, value, res);
165     return res;
166 }
167
168 /* removes the requested value from the registry, however, does not
169  * remove the section if empty. Returns S_OK (0) on success.
170  */
171 static HRESULT remove_value(HKEY root, const char *subkey, const char *name)
172 {
173     HRESULT hr;
174     HKEY key;
175
176     WINE_TRACE("subkey=%s, name=%s\n", subkey, name);
177
178     hr = RegOpenKey(root, subkey, &key);
179     if (hr != S_OK) return hr;
180
181     hr = RegDeleteValue(key, name);
182     if (hr != ERROR_SUCCESS) return hr;
183
184     return S_OK;
185 }
186
187 /* removes the requested subkey from the registry, assuming it exists */
188 static HRESULT remove_path(HKEY root, char *section) {
189     WINE_TRACE("section=%s\n", section);
190
191     return RegDeleteKey(root, section);
192 }
193
194
195 /* ========================================================================= */
196
197 /* This code exists for the following reasons:
198  *
199  * - It makes working with the registry easier
200  * - By storing a mini cache of the registry, we can more easily implement
201  *   cancel/revert and apply. The 'settings list' is an overlay on top of
202  *   the actual registry data that we can write out at will.
203  *
204  * Rather than model a tree in memory, we simply store each absolute (rooted
205  * at the config key) path.
206  *
207  */
208
209 struct setting
210 {
211     struct list entry;
212     HKEY root;    /* the key on which path is rooted */
213     char *path;   /* path in the registry rooted at root  */
214     char *name;   /* name of the registry value. if null, this means delete the key  */
215     char *value;  /* contents of the registry value. if null, this means delete the value  */
216 };
217
218 struct list *settings;
219
220 static void free_setting(struct setting *setting)
221 {
222     assert( setting != NULL );
223     assert( setting->path );
224
225     WINE_TRACE("destroying %p: %s\n", setting, setting->path);
226     
227     HeapFree(GetProcessHeap(), 0, setting->path);
228     HeapFree(GetProcessHeap(), 0, setting->name);
229     HeapFree(GetProcessHeap(), 0, setting->value);
230
231     list_remove(&setting->entry);
232
233     HeapFree(GetProcessHeap(), 0, setting);
234 }
235
236 /**
237  * Returns the contents of the value at path. If not in the settings
238  * list, it will be fetched from the registry - failing that, the
239  * default will be used.
240  *
241  * If already in the list, the contents as given there will be
242  * returned. You are expected to HeapFree the result.
243  */
244 char *get_reg_key(HKEY root, const char *path, const char *name, const char *def)
245 {
246     struct list *cursor;
247     struct setting *s;
248     char *val;
249
250     WINE_TRACE("path=%s, name=%s, def=%s\n", path, name, def);
251
252     /* check if it's in the list */
253     LIST_FOR_EACH( cursor, settings )
254     {
255         s = LIST_ENTRY(cursor, struct setting, entry);
256
257         if (root != s->root) continue;
258         if (strcasecmp(path, s->path) != 0) continue;
259         if (strcasecmp(name, s->name) != 0) continue;
260
261         WINE_TRACE("found %s:%s in settings list, returning %s\n", path, name, s->value);
262         return s->value ? strdupA(s->value) : NULL;
263     }
264
265     /* no, so get from the registry */
266     val = get_config_key(root, path, name, def);
267
268     WINE_TRACE("returning %s\n", val);
269
270     return val;
271 }
272
273 /**
274  * Used to set a registry key.
275  *
276  * path is rooted at the config key, ie use "Version" or
277  * "AppDefaults\\fooapp.exe\\Version". You can use keypath()
278  * to get such a string.
279  *
280  * name is the value name, or NULL to delete the path.
281  *
282  * value is what to set the value to, or NULL to delete it.
283  *
284  * These values will be copied when necessary.
285  */
286 void set_reg_key(HKEY root, const char *path, const char *name, const char *value)
287 {
288     struct list *cursor;
289     struct setting *s;
290
291     assert( path != NULL );
292
293     WINE_TRACE("path=%s, name=%s, value=%s\n", path, name, value);
294
295     /* firstly, see if we already set this setting  */
296     LIST_FOR_EACH( cursor, settings )
297     {
298         struct setting *s = LIST_ENTRY(cursor, struct setting, entry);
299
300         if (root != s->root) continue;
301         if (strcasecmp(s->path, path) != 0) continue;
302         if ((s->name && name) && strcasecmp(s->name, name) != 0) continue;
303
304         /* are we attempting a double delete? */
305         if (!s->name && !name) return;
306
307         /* do we want to undelete this key? */
308         if (!s->name && name) s->name = strdupA(name);
309
310         /* yes, we have already set it, so just replace the content and return  */
311         HeapFree(GetProcessHeap(), 0, s->value);
312         s->value = value ? strdupA(value) : NULL;
313
314         /* are we deleting this key? this won't remove any of the
315          * children from the overlay so if the user adds it again in
316          * that session it will appear to undelete the settings, but
317          * in reality only the settings actually modified by the user
318          * in that session will be restored. we might want to fix this
319          * corner case in future by actually deleting all the children
320          * here so that once it's gone, it's gone.
321          */
322         if (!name) s->name = NULL;
323
324         return;
325     }
326
327     /* otherwise add a new setting for it  */
328     s = HeapAlloc(GetProcessHeap(), 0, sizeof(struct setting));
329     s->root  = root;
330     s->path  = strdupA(path);
331     s->name  = name  ? strdupA(name)  : NULL;
332     s->value = value ? strdupA(value) : NULL;
333
334     list_add_tail(settings, &s->entry);
335 }
336
337 /**
338  * enumerates the value names at the given path, taking into account
339  * the changes in the settings list.
340  *
341  * you are expected to HeapFree each element of the array, which is null
342  * terminated, as well as the array itself.
343  */
344 char **enumerate_values(HKEY root, char *path)
345 {
346     HKEY key;
347     DWORD res, i = 0;
348     char **values = NULL;
349     int valueslen = 0;
350     struct list *cursor;
351
352     res = RegOpenKey(root, path, &key);
353     if (res == ERROR_SUCCESS)
354     {
355         while (TRUE)
356         {
357             char name[1024];
358             DWORD namesize = sizeof(name);
359             BOOL removed = FALSE;
360
361             /* find out the needed size, allocate a buffer, read the value  */
362             if ((res = RegEnumValue(key, i, name, &namesize, NULL, NULL, NULL, NULL)) != ERROR_SUCCESS)
363                 break;
364
365             WINE_TRACE("name=%s\n", name);
366
367             /* check if this value name has been removed in the settings list  */
368             LIST_FOR_EACH( cursor, settings )
369             {
370                 struct setting *s = LIST_ENTRY(cursor, struct setting, entry);
371                 if (strcasecmp(s->path, path) != 0) continue;
372                 if (strcasecmp(s->name, name) != 0) continue;
373
374                 if (!s->value)
375                 {
376                     WINE_TRACE("this key has been removed, so skipping\n");
377                     removed = TRUE;
378                     break;
379                 }
380             }
381
382             if (removed)            /* this value was deleted by the user, so don't include it */
383             {
384                 HeapFree(GetProcessHeap(), 0, name);
385                 i++;
386                 continue;
387             }
388
389             /* grow the array if necessary, add buffer to it, iterate  */
390             if (values) values = HeapReAlloc(GetProcessHeap(), 0, values, sizeof(char*) * (valueslen + 1));
391             else values = HeapAlloc(GetProcessHeap(), 0, sizeof(char*));
392
393             values[valueslen++] = strdupA(name);
394             WINE_TRACE("valueslen is now %d\n", valueslen);
395             i++;
396         }
397     }
398     else
399     {
400         WINE_WARN("failed opening registry key %s, res=0x%lx\n", path, res);
401     }
402
403     WINE_TRACE("adding settings in list but not registry\n");
404
405     /* now we have to add the values that aren't in the registry but are in the settings list */
406     LIST_FOR_EACH( cursor, settings )
407     {
408         struct setting *setting = LIST_ENTRY(cursor, struct setting, entry);
409         BOOL found = FALSE;
410
411         if (strcasecmp(setting->path, path) != 0) continue;
412
413         if (!setting->value) continue;
414
415         for (i = 0; i < valueslen; i++)
416         {
417             if (strcasecmp(setting->name, values[i]) == 0)
418             {
419                 found = TRUE;
420                 break;
421             }
422         }
423
424         if (found) continue;
425
426         WINE_TRACE("%s in list but not registry\n", setting->name);
427
428         /* otherwise it's been set by the user but isn't in the registry */
429         if (values) values = HeapReAlloc(GetProcessHeap(), 0, values, sizeof(char*) * (valueslen + 1));
430         else values = HeapAlloc(GetProcessHeap(), 0, sizeof(char*));
431
432         values[valueslen++] = strdupA(setting->name);
433     }
434
435     WINE_TRACE("adding null terminator\n");
436     if (values)
437     {
438         values = HeapReAlloc(GetProcessHeap(), 0, values, sizeof(char*) * (valueslen + 1));
439         values[valueslen] = NULL;
440     }
441
442     RegCloseKey(key);
443
444     return values;
445 }
446
447 /**
448  * returns true if the given key/value pair exists in the registry or
449  * has been written to.
450  */
451 BOOL reg_key_exists(HKEY root, const char *path, const char *name)
452 {
453     char *val = get_reg_key(root, path, name, NULL);
454
455     if (val)
456     {
457         HeapFree(GetProcessHeap(), 0, val);
458         return TRUE;
459     }
460
461     return FALSE;
462 }
463
464 static void process_setting(struct setting *s)
465 {
466     if (s->value)
467     {
468         WINE_TRACE("Setting %s:%s to '%s'\n", s->path, s->name, s->value);
469         set_config_key(s->root, s->path, s->name, s->value);
470     }
471     else
472     {
473         /* NULL name means remove that path/section entirely */
474         if (s->path && s->name) remove_value(s->root, s->path, s->name);
475         else if (s->path && !s->name) remove_path(s->root, s->path);
476     }
477 }
478
479 void apply(void)
480 {
481     if (list_empty(settings)) return; /* we will be called for each page when the user clicks OK */
482
483     WINE_TRACE("()\n");
484
485     while (!list_empty(settings))
486     {
487         struct setting *s = (struct setting *) list_head(settings);
488         process_setting(s);
489         free_setting(s);
490     }
491 }
492
493 /* ================================== utility functions ============================ */
494
495 char *current_app = NULL; /* the app we are currently editing, or NULL if editing global */
496
497 /* returns a registry key path suitable for passing to addTransaction  */
498 char *keypath(const char *section)
499 {
500     static char *result = NULL;
501
502     HeapFree(GetProcessHeap(), 0, result);
503
504     if (current_app)
505     {
506         result = HeapAlloc(GetProcessHeap(), 0, strlen("AppDefaults\\") + strlen(current_app) + 2 /* \\ */ + strlen(section) + 1 /* terminator */);
507         sprintf(result, "AppDefaults\\%s", current_app);
508         if (section[0]) sprintf( result + strlen(result), "\\%s", section );
509     }
510     else
511     {
512         result = strdupA(section);
513     }
514
515     return result;
516 }
517
518 void PRINTERROR(void)
519 {
520         LPSTR msg;
521
522         FormatMessageA(FORMAT_MESSAGE_ALLOCATE_BUFFER|FORMAT_MESSAGE_FROM_SYSTEM,
523                        0, GetLastError(), MAKELANGID(LANG_NEUTRAL,SUBLANG_DEFAULT),
524                        (LPSTR)&msg, 0, NULL);
525
526         /* eliminate trailing newline, is this a Wine bug? */
527         *(strrchr(msg, '\r')) = '\0';
528         
529         WINE_TRACE("error: '%s'\n", msg);
530 }
531
532 int initialize(void) {
533     DWORD res = RegCreateKey(HKEY_CURRENT_USER, WINE_KEY_ROOT, &config_key);
534
535     if (res != ERROR_SUCCESS) {
536         WINE_ERR("RegOpenKey failed on wine config key (%ld)\n", res);
537         return 1;
538     }
539
540     /* we could probably just have the list as static data  */
541     settings = HeapAlloc(GetProcessHeap(), 0, sizeof(struct list));
542     list_init(settings);
543
544     return 0;
545 }