Fixed registry paths to edit the real config, and removed the startup
[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 (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 = RegOpenKeyEx(config_key, subkey, 0, KEY_READ, &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) 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(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     res = RegCreateKey(config_key, subkey, &key);
151     if (res != ERROR_SUCCESS) goto end;
152     if (name == NULL || value == NULL) goto end;
153
154     res = RegSetValueEx(key, name, 0, REG_SZ, value, strlen(value) + 1);
155     if (res != ERROR_SUCCESS) goto end;
156
157     res = 0;
158 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);
161     return res;
162 }
163
164 /* removes the requested value from the registry, however, does not
165  * remove the section if empty. Returns S_OK (0) on success.
166  */
167 static HRESULT remove_value(const char *subkey, const char *name)
168 {
169     HRESULT hr;
170     HKEY key;
171
172     WINE_TRACE("subkey=%s, name=%s\n", subkey, name);
173
174     hr = RegOpenKeyEx(config_key, subkey, 0, KEY_READ, &key);
175     if (hr != S_OK) return hr;
176
177     hr = RegDeleteValue(key, name);
178     if (hr != ERROR_SUCCESS) return hr;
179
180     return S_OK;
181 }
182
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);
186
187     return RegDeleteKey(config_key, section);
188 }
189
190
191 /* ========================================================================= */
192
193 /* This code exists for the following reasons:
194  *
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.
199  *
200  * Rather than model a tree in memory, we simply store each absolute (rooted
201  * at the config key) path.
202  *
203  */
204
205 struct setting
206 {
207     struct list entry;
208     char *path;   /* path in the registry rooted at the config key  */
209     char *name;   /* name of the registry value. if null, this means delete the key  */
210     char *value;  /* contents of the registry value. if null, this means delete the value  */
211 };
212
213 struct list *settings;
214
215 static void free_setting(struct setting *setting)
216 {
217     assert( setting != NULL );
218     assert( setting->path );
219
220     WINE_TRACE("destroying %p: %s\n", setting, setting->path);
221     
222     HeapFree(GetProcessHeap(), 0, setting->path);
223     HeapFree(GetProcessHeap(), 0, setting->name);
224     HeapFree(GetProcessHeap(), 0, setting->value);
225
226     list_remove(&setting->entry);
227
228     HeapFree(GetProcessHeap(), 0, setting);
229 }
230
231 /**
232  * Returns the contents of the value at path. If not in the settings
233  * list, it will be fetched from the registry - failing that, the
234  * default will be used.
235  *
236  * If already in the list, the contents as given there will be
237  * returned. You are expected to HeapFree the result.
238  */
239 char *get_reg_key(const char *path, const char *name, const char *def)
240 {
241     struct list *cursor;
242     struct setting *s;
243     char *val;
244
245     WINE_TRACE("path=%s, name=%s, def=%s\n", path, name, def);
246
247     /* check if it's in the list */
248     LIST_FOR_EACH( cursor, settings )
249     {
250         s = LIST_ENTRY(cursor, struct setting, entry);
251
252         if (strcasecmp(path, s->path) != 0) continue;
253         if (strcasecmp(name, s->name) != 0) continue;
254
255         WINE_TRACE("found %s:%s in settings list, returning %s\n", path, name, s->value);
256         return s->value ? strdupA(s->value) : NULL;
257     }
258
259     /* no, so get from the registry */
260     val = get_config_key(path, name, def);
261
262     WINE_TRACE("returning %s\n", val);
263
264     return val;
265 }
266
267 /**
268  * Used to set a registry key.
269  *
270  * path is rooted at the config key, ie use "Version" or
271  * "AppDefaults\\fooapp.exe\\Version". You can use keypath()
272  * to get such a string.
273  *
274  * name is the value name, or NULL to delete the path.
275  *
276  * value is what to set the value to, or NULL to delete it.
277  *
278  * These values will be copied when necessary.
279  */
280 void set_reg_key(const char *path, const char *name, const char *value)
281 {
282     struct list *cursor;
283     struct setting *s;
284
285     assert( path != NULL );
286
287     WINE_TRACE("path=%s, name=%s, value=%s\n", path, name, value);
288
289     /* firstly, see if we already set this setting  */
290     LIST_FOR_EACH( cursor, settings )
291     {
292         struct setting *s = LIST_ENTRY(cursor, struct setting, entry);
293
294         if (strcasecmp(s->path, path) != 0) continue;
295         if ((s->name && name) && strcasecmp(s->name, name) != 0) continue;
296
297         /* are we attempting a double delete? */
298         if (!s->name && !name) return;
299
300         /* do we want to undelete this key? */
301         if (!s->name && name) s->name = strdupA(name);
302
303         /* yes, we have already set it, so just replace the content and return  */
304         HeapFree(GetProcessHeap(), 0, s->value);
305         s->value = value ? strdupA(value) : NULL;
306
307         /* are we deleting this key? this won't remove any of the
308          * children from the overlay so if the user adds it again in
309          * that session it will appear to undelete the settings, but
310          * in reality only the settings actually modified by the user
311          * in that session will be restored. we might want to fix this
312          * corner case in future by actually deleting all the children
313          * here so that once it's gone, it's gone.
314          */
315         if (!name) s->name = NULL;
316
317         return;
318     }
319
320     /* otherwise add a new setting for it  */
321     s = HeapAlloc(GetProcessHeap(), 0, sizeof(struct setting));
322     s->path  = strdupA(path);
323     s->name  = name  ? strdupA(name)  : NULL;
324     s->value = value ? strdupA(value) : NULL;
325
326     list_add_tail(settings, &s->entry);
327 }
328
329 /**
330  * enumerates the value names at the given path, taking into account
331  * the changes in the settings list.
332  *
333  * you are expected to HeapFree each element of the array, which is null
334  * terminated, as well as the array itself.
335  */
336 char **enumerate_values(char *path)
337 {
338     HKEY key;
339     DWORD res, i = 0;
340     char **values = NULL;
341     int valueslen = 0;
342     struct list *cursor;
343
344     res = RegOpenKeyEx(config_key, path, 0, KEY_READ, &key);
345     if (res == ERROR_SUCCESS)
346     {
347         while (TRUE)
348         {
349             char name[1024];
350             DWORD namesize = sizeof(name);
351             BOOL removed = FALSE;
352
353             /* find out the needed size, allocate a buffer, read the value  */
354             if ((res = RegEnumValue(key, i, name, &namesize, NULL, NULL, NULL, NULL)) != ERROR_SUCCESS)
355                 break;
356
357             WINE_TRACE("name=%s\n", name);
358
359             /* check if this value name has been removed in the settings list  */
360             LIST_FOR_EACH( cursor, settings )
361             {
362                 struct setting *s = LIST_ENTRY(cursor, struct setting, entry);
363                 if (strcasecmp(s->path, path) != 0) continue;
364                 if (strcasecmp(s->name, name) != 0) continue;
365
366                 if (!s->value)
367                 {
368                     WINE_TRACE("this key has been removed, so skipping\n");
369                     removed = TRUE;
370                     break;
371                 }
372             }
373
374             if (removed)            /* this value was deleted by the user, so don't include it */
375             {
376                 HeapFree(GetProcessHeap(), 0, name);
377                 i++;
378                 continue;
379             }
380
381             /* grow the array if necessary, add buffer to it, iterate  */
382             if (values) values = HeapReAlloc(GetProcessHeap(), 0, values, sizeof(char*) * (valueslen + 1));
383             else values = HeapAlloc(GetProcessHeap(), 0, sizeof(char*));
384
385             values[valueslen++] = strdupA(name);
386             WINE_TRACE("valueslen is now %d\n", valueslen);
387             i++;
388         }
389     }
390     else
391     {
392         WINE_WARN("failed opening registry key %s, res=0x%lx\n", path, res);
393     }
394
395     WINE_TRACE("adding settings in list but not registry\n");
396
397     /* now we have to add the values that aren't in the registry but are in the settings list */
398     LIST_FOR_EACH( cursor, settings )
399     {
400         struct setting *setting = LIST_ENTRY(cursor, struct setting, entry);
401         BOOL found = FALSE;
402
403         if (strcasecmp(setting->path, path) != 0) continue;
404
405         if (!setting->value) continue;
406
407         for (i = 0; i < valueslen; i++)
408         {
409             if (strcasecmp(setting->name, values[i]) == 0)
410             {
411                 found = TRUE;
412                 break;
413             }
414         }
415
416         if (found) continue;
417
418         WINE_TRACE("%s in list but not registry\n", setting->name);
419
420         /* otherwise it's been set by the user but isn't in the registry */
421         if (values) values = HeapReAlloc(GetProcessHeap(), 0, values, sizeof(char*) * (valueslen + 1));
422         else values = HeapAlloc(GetProcessHeap(), 0, sizeof(char*));
423
424         values[valueslen++] = strdupA(setting->name);
425     }
426
427     WINE_TRACE("adding null terminator\n");
428     if (values)
429     {
430         values = HeapReAlloc(GetProcessHeap(), 0, values, sizeof(char*) * (valueslen + 1));
431         values[valueslen] = NULL;
432     }
433
434     RegCloseKey(key);
435
436     return values;
437 }
438
439 /**
440  * returns true if the given key/value pair exists in the registry or
441  * has been written to.
442  */
443 BOOL reg_key_exists(const char *path, const char *name)
444 {
445     char *val = get_reg_key(path, name, NULL);
446
447     if (val)
448     {
449         HeapFree(GetProcessHeap(), 0, val);
450         return TRUE;
451     }
452
453     return FALSE;
454 }
455
456 static void process_setting(struct setting *s)
457 {
458     if (s->value)
459     {
460         WINE_TRACE("Setting %s:%s to '%s'\n", s->path, s->name, s->value);
461         set_config_key(s->path, s->name, s->value);
462     }
463     else
464     {
465         /* NULL name means remove that path/section entirely */
466         if (s->path && s->name) remove_value(s->path, s->name);
467         else if (s->path && !s->name) remove_path(s->path);
468     }
469 }
470
471 void apply(void)
472 {
473     if (list_empty(settings)) return; /* we will be called for each page when the user clicks OK */
474
475     WINE_TRACE("()\n");
476
477     while (!list_empty(settings))
478     {
479         struct setting *s = (struct setting *) list_head(settings);
480         process_setting(s);
481         free_setting(s);
482     }
483 }
484
485 /* ================================== utility functions ============================ */
486
487 char *current_app = NULL; /* the app we are currently editing, or NULL if editing global */
488
489 /* returns a registry key path suitable for passing to addTransaction  */
490 char *keypath(const char *section)
491 {
492     static char *result = NULL;
493
494     HeapFree(GetProcessHeap(), 0, result);
495
496     if (current_app)
497     {
498         result = HeapAlloc(GetProcessHeap(), 0, strlen("AppDefaults\\") + strlen(current_app) + 2 /* \\ */ + strlen(section) + 1 /* terminator */);
499         sprintf(result, "AppDefaults\\%s\\%s", current_app, section);
500     }
501     else
502     {
503         result = strdupA(section);
504     }
505
506     return result;
507 }
508
509 void PRINTERROR(void)
510 {
511         LPSTR msg;
512
513         FormatMessageA(FORMAT_MESSAGE_ALLOCATE_BUFFER|FORMAT_MESSAGE_FROM_SYSTEM,
514                        0, GetLastError(), MAKELANGID(LANG_NEUTRAL,SUBLANG_DEFAULT),
515                        (LPSTR)&msg, 0, NULL);
516
517         /* eliminate trailing newline, is this a Wine bug? */
518         *(strrchr(msg, '\r')) = '\0';
519         
520         WINE_TRACE("error: '%s'\n", msg);
521 }
522
523 int initialize(void) {
524     DWORD res = RegCreateKey(HKEY_CURRENT_USER, WINE_KEY_ROOT, &config_key);
525
526     if (res != ERROR_SUCCESS) {
527         WINE_ERR("RegOpenKey failed on wine config key (%ld)\n", res);
528         return 1;
529     }
530
531     /* we could probably just have the list as static data  */
532     settings = HeapAlloc(GetProcessHeap(), 0, sizeof(struct list));
533     list_init(settings);
534
535     return 0;
536 }