Dimitrie O. Paun | 82ce2cc | 2003-03-31 19:41:55 +0000 | [diff] [blame] | 1 | /* |
| 2 | * WineCfg configuration management |
| 3 | * |
| 4 | * Copyright 2002 Jaco Greeff |
| 5 | * Copyright 2003 Dimitrie O. Paun |
Mike Hearn | a5ce4ee | 2004-09-28 03:16:43 +0000 | [diff] [blame] | 6 | * Copyright 2003-2004 Mike Hearn |
Dimitrie O. Paun | 82ce2cc | 2003-03-31 19:41:55 +0000 | [diff] [blame] | 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 | * |
Mike Hearn | 7d8711e | 2004-11-23 13:50:23 +0000 | [diff] [blame] | 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 | * |
Dimitrie O. Paun | 82ce2cc | 2003-03-31 19:41:55 +0000 | [diff] [blame] | 28 | */ |
| 29 | |
Mike Hearn | cd0b789 | 2003-09-08 18:58:07 +0000 | [diff] [blame] | 30 | #include <assert.h> |
Dimitrie O. Paun | 82ce2cc | 2003-03-31 19:41:55 +0000 | [diff] [blame] | 31 | #include <stdio.h> |
| 32 | #include <limits.h> |
| 33 | #include <windows.h> |
Matthew Davison | 5101dfc | 2003-04-27 00:33:07 +0000 | [diff] [blame] | 34 | #include <winreg.h> |
| 35 | #include <wine/debug.h> |
Mike Hearn | 0af614e | 2004-09-28 03:55:16 +0000 | [diff] [blame] | 36 | #include <wine/list.h> |
Matthew Davison | 5101dfc | 2003-04-27 00:33:07 +0000 | [diff] [blame] | 37 | |
| 38 | WINE_DEFAULT_DEBUG_CHANNEL(winecfg); |
Dimitrie O. Paun | 82ce2cc | 2003-03-31 19:41:55 +0000 | [diff] [blame] | 39 | |
| 40 | #include "winecfg.h" |
| 41 | |
Mike Hearn | 0af614e | 2004-09-28 03:55:16 +0000 | [diff] [blame] | 42 | HKEY config_key = NULL; |
Dimitrie O. Paun | 82ce2cc | 2003-03-31 19:41:55 +0000 | [diff] [blame] | 43 | |
Dimitrie O. Paun | 82ce2cc | 2003-03-31 19:41:55 +0000 | [diff] [blame] | 44 | |
Mike Hearn | 0af614e | 2004-09-28 03:55:16 +0000 | [diff] [blame] | 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 */ |
Mike Hearn | 7d8711e | 2004-11-23 13:50:23 +0000 | [diff] [blame] | 56 | if (current_app) |
Mike Hearn | 0af614e | 2004-09-28 03:55:16 +0000 | [diff] [blame] | 57 | { |
Mike McCormack | ae51135 | 2005-06-02 15:11:32 +0000 | [diff] [blame] | 58 | const char *template = "Wine Configuration for %s"; |
Mike Hearn | 7d8711e | 2004-11-23 13:50:23 +0000 | [diff] [blame] | 59 | newtitle = HeapAlloc(GetProcessHeap(), 0, strlen(template) + strlen(current_app) + 1); |
| 60 | sprintf(newtitle, template, current_app); |
Mike Hearn | cd0b789 | 2003-09-08 18:58:07 +0000 | [diff] [blame] | 61 | } |
Mike Hearn | 0af614e | 2004-09-28 03:55:16 +0000 | [diff] [blame] | 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); |
Dimitrie O. Paun | 82ce2cc | 2003-03-31 19:41:55 +0000 | [diff] [blame] | 70 | } |
| 71 | |
Mike Hearn | cd0b789 | 2003-09-08 18:58:07 +0000 | [diff] [blame] | 72 | |
Mike Hearn | 0af614e | 2004-09-28 03:55:16 +0000 | [diff] [blame] | 73 | /** |
Gerald Pfeifer | 61f8e12 | 2004-11-30 21:03:36 +0000 | [diff] [blame] | 74 | * get_config_key: Retrieves a configuration value from the registry |
Mike Hearn | 4e1afc6 | 2003-08-30 00:27:08 +0000 | [diff] [blame] | 75 | * |
Mike Hearn | 0af614e | 2004-09-28 03:55:16 +0000 | [diff] [blame] | 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 |
Mike Hearn | 4e1afc6 | 2003-08-30 00:27:08 +0000 | [diff] [blame] | 79 | * |
Mike Hearn | 0af614e | 2004-09-28 03:55:16 +0000 | [diff] [blame] | 80 | * Returns a buffer holding the value if successful, NULL if |
| 81 | * not. Caller is responsible for releasing the result. |
Mike Hearn | 4e1afc6 | 2003-08-30 00:27:08 +0000 | [diff] [blame] | 82 | * |
Matthew Davison | 5101dfc | 2003-04-27 00:33:07 +0000 | [diff] [blame] | 83 | */ |
Mike McCormack | ae51135 | 2005-06-02 15:11:32 +0000 | [diff] [blame] | 84 | static char *get_config_key (const char *subkey, const char *name, const char *def) |
Matthew Davison | 5101dfc | 2003-04-27 00:33:07 +0000 | [diff] [blame] | 85 | { |
Mike Hearn | 0af614e | 2004-09-28 03:55:16 +0000 | [diff] [blame] | 86 | LPBYTE buffer = NULL; |
| 87 | DWORD len; |
Mike Hearn | 4e1afc6 | 2003-08-30 00:27:08 +0000 | [diff] [blame] | 88 | HKEY hSubKey = NULL; |
Mike Hearn | cd0b789 | 2003-09-08 18:58:07 +0000 | [diff] [blame] | 89 | DWORD res; |
Matthew Davison | 5101dfc | 2003-04-27 00:33:07 +0000 | [diff] [blame] | 90 | |
Mike Hearn | 0af614e | 2004-09-28 03:55:16 +0000 | [diff] [blame] | 91 | WINE_TRACE("subkey=%s, name=%s, def=%s\n", subkey, name, def); |
Mike Hearn | cd0b789 | 2003-09-08 18:58:07 +0000 | [diff] [blame] | 92 | |
Mike Hearn | 0af614e | 2004-09-28 03:55:16 +0000 | [diff] [blame] | 93 | res = RegOpenKeyEx(config_key, subkey, 0, KEY_READ, &hSubKey); |
| 94 | if (res != ERROR_SUCCESS) |
| 95 | { |
| 96 | if (res == ERROR_FILE_NOT_FOUND) |
Matthew Davison | 5101dfc | 2003-04-27 00:33:07 +0000 | [diff] [blame] | 97 | { |
Mike Hearn | 4e1afc6 | 2003-08-30 00:27:08 +0000 | [diff] [blame] | 98 | WINE_TRACE("Section key not present - using default\n"); |
Mike Hearn | 0af614e | 2004-09-28 03:55:16 +0000 | [diff] [blame] | 99 | return def ? strdupA(def) : NULL; |
Matthew Davison | 5101dfc | 2003-04-27 00:33:07 +0000 | [diff] [blame] | 100 | } |
| 101 | else |
| 102 | { |
Mike Hearn | 4e1afc6 | 2003-08-30 00:27:08 +0000 | [diff] [blame] | 103 | WINE_ERR("RegOpenKey failed on wine config key (res=%ld)\n", res); |
Matthew Davison | 5101dfc | 2003-04-27 00:33:07 +0000 | [diff] [blame] | 104 | } |
| 105 | goto end; |
| 106 | } |
Mike Hearn | 498e1ce | 2003-09-30 00:27:55 +0000 | [diff] [blame] | 107 | |
Mike Hearn | 0af614e | 2004-09-28 03:55:16 +0000 | [diff] [blame] | 108 | res = RegQueryValueExA(hSubKey, name, NULL, NULL, NULL, &len); |
| 109 | if (res == ERROR_FILE_NOT_FOUND) |
| 110 | { |
Matthew Davison | 5101dfc | 2003-04-27 00:33:07 +0000 | [diff] [blame] | 111 | WINE_TRACE("Value not present - using default\n"); |
Mike Hearn | 0af614e | 2004-09-28 03:55:16 +0000 | [diff] [blame] | 112 | buffer = def ? strdupA(def) : NULL; |
Mike Hearn | cd0b789 | 2003-09-08 18:58:07 +0000 | [diff] [blame] | 113 | goto end; |
Mike Hearn | 0af614e | 2004-09-28 03:55:16 +0000 | [diff] [blame] | 114 | } else if (res != ERROR_SUCCESS) |
| 115 | { |
| 116 | WINE_ERR("Couldn't query value's length (res=%ld)\n", res); |
Matthew Davison | 5101dfc | 2003-04-27 00:33:07 +0000 | [diff] [blame] | 117 | goto end; |
| 118 | } |
| 119 | |
Mike Hearn | 0af614e | 2004-09-28 03:55:16 +0000 | [diff] [blame] | 120 | buffer = HeapAlloc(GetProcessHeap(), 0, len + 1); |
| 121 | |
| 122 | RegQueryValueEx(hSubKey, name, NULL, NULL, buffer, &len); |
| 123 | |
| 124 | WINE_TRACE("buffer=%s\n", buffer); |
Matthew Davison | 5101dfc | 2003-04-27 00:33:07 +0000 | [diff] [blame] | 125 | end: |
Mike Hearn | 0af614e | 2004-09-28 03:55:16 +0000 | [diff] [blame] | 126 | if (hSubKey) RegCloseKey(hSubKey); |
Matthew Davison | 5101dfc | 2003-04-27 00:33:07 +0000 | [diff] [blame] | 127 | |
Eric Pouech | 05413fc | 2005-04-18 09:54:03 +0000 | [diff] [blame] | 128 | return (char*)buffer; |
Matthew Davison | 5101dfc | 2003-04-27 00:33:07 +0000 | [diff] [blame] | 129 | } |
| 130 | |
Mike Hearn | 0af614e | 2004-09-28 03:55:16 +0000 | [diff] [blame] | 131 | /** |
Gerald Pfeifer | 61f8e12 | 2004-11-30 21:03:36 +0000 | [diff] [blame] | 132 | * set_config_key: convenience wrapper to set a key/value pair |
Mike Hearn | 4e1afc6 | 2003-08-30 00:27:08 +0000 | [diff] [blame] | 133 | * |
Daniel Marmier | 8e5bb20 | 2003-10-09 04:39:01 +0000 | [diff] [blame] | 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 |
Mike Hearn | 4e1afc6 | 2003-08-30 00:27:08 +0000 | [diff] [blame] | 137 | * |
| 138 | * Returns 0 on success, non-zero otherwise |
Mike Hearn | 0af614e | 2004-09-28 03:55:16 +0000 | [diff] [blame] | 139 | * |
| 140 | * If valueName or value is NULL, an empty section will be created |
Mike Hearn | 4e1afc6 | 2003-08-30 00:27:08 +0000 | [diff] [blame] | 141 | */ |
Mike McCormack | ae51135 | 2005-06-02 15:11:32 +0000 | [diff] [blame] | 142 | static int set_config_key(const char *subkey, const char *name, const char *value) { |
Mike Hearn | 4e1afc6 | 2003-08-30 00:27:08 +0000 | [diff] [blame] | 143 | DWORD res = 1; |
| 144 | HKEY key = NULL; |
| 145 | |
Mike Hearn | 0af614e | 2004-09-28 03:55:16 +0000 | [diff] [blame] | 146 | WINE_TRACE("subkey=%s: name=%s, value=%s\n", subkey, name, value); |
Mike Hearn | cd0b789 | 2003-09-08 18:58:07 +0000 | [diff] [blame] | 147 | |
| 148 | assert( subkey != NULL ); |
Mike Hearn | 4e1afc6 | 2003-08-30 00:27:08 +0000 | [diff] [blame] | 149 | |
Mike Hearn | 0af614e | 2004-09-28 03:55:16 +0000 | [diff] [blame] | 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); |
Mike Hearn | 4e1afc6 | 2003-08-30 00:27:08 +0000 | [diff] [blame] | 155 | if (res != ERROR_SUCCESS) goto end; |
| 156 | |
| 157 | res = 0; |
| 158 | end: |
| 159 | if (key) RegCloseKey(key); |
Mike Hearn | 0af614e | 2004-09-28 03:55:16 +0000 | [diff] [blame] | 160 | if (res != 0) WINE_ERR("Unable to set configuration key %s in section %s to %s, res=%ld\n", name, subkey, value, res); |
Mike Hearn | 4e1afc6 | 2003-08-30 00:27:08 +0000 | [diff] [blame] | 161 | return res; |
| 162 | } |
| 163 | |
Mike Hearn | 0af614e | 2004-09-28 03:55:16 +0000 | [diff] [blame] | 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 | { |
Mike Hearn | cd0b789 | 2003-09-08 18:58:07 +0000 | [diff] [blame] | 169 | HRESULT hr; |
| 170 | HKEY key; |
| 171 | |
Mike Hearn | 0af614e | 2004-09-28 03:55:16 +0000 | [diff] [blame] | 172 | WINE_TRACE("subkey=%s, name=%s\n", subkey, name); |
Mike Hearn | cd0b789 | 2003-09-08 18:58:07 +0000 | [diff] [blame] | 173 | |
Mike Hearn | 0af614e | 2004-09-28 03:55:16 +0000 | [diff] [blame] | 174 | hr = RegOpenKeyEx(config_key, subkey, 0, KEY_READ, &key); |
Mike Hearn | cd0b789 | 2003-09-08 18:58:07 +0000 | [diff] [blame] | 175 | if (hr != S_OK) return hr; |
| 176 | |
Mike Hearn | 0af614e | 2004-09-28 03:55:16 +0000 | [diff] [blame] | 177 | hr = RegDeleteValue(key, name); |
Mike Hearn | cd0b789 | 2003-09-08 18:58:07 +0000 | [diff] [blame] | 178 | if (hr != ERROR_SUCCESS) return hr; |
| 179 | |
| 180 | return S_OK; |
| 181 | } |
| 182 | |
Mike Hearn | 0af614e | 2004-09-28 03:55:16 +0000 | [diff] [blame] | 183 | /* removes the requested subkey from the registry, assuming it exists */ |
| 184 | static HRESULT remove_path(char *section) { |
Mike Hearn | 5a2cde6 | 2003-09-17 22:40:38 +0000 | [diff] [blame] | 185 | WINE_TRACE("section=%s\n", section); |
| 186 | |
Mike Hearn | 0af614e | 2004-09-28 03:55:16 +0000 | [diff] [blame] | 187 | return RegDeleteKey(config_key, section); |
Mike Hearn | 5a2cde6 | 2003-09-17 22:40:38 +0000 | [diff] [blame] | 188 | } |
| 189 | |
Mike Hearn | 4e1afc6 | 2003-08-30 00:27:08 +0000 | [diff] [blame] | 190 | |
Mike Hearn | cd0b789 | 2003-09-08 18:58:07 +0000 | [diff] [blame] | 191 | /* ========================================================================= */ |
Mike Hearn | cd0b789 | 2003-09-08 18:58:07 +0000 | [diff] [blame] | 192 | |
Mike Hearn | 0af614e | 2004-09-28 03:55:16 +0000 | [diff] [blame] | 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 | */ |
Mike Hearn | cd0b789 | 2003-09-08 18:58:07 +0000 | [diff] [blame] | 204 | |
Mike Hearn | 0af614e | 2004-09-28 03:55:16 +0000 | [diff] [blame] | 205 | struct setting |
Mike Hearn | 0f4c6c9 | 2003-09-10 03:41:44 +0000 | [diff] [blame] | 206 | { |
Mike Hearn | 0af614e | 2004-09-28 03:55:16 +0000 | [diff] [blame] | 207 | struct list entry; |
| 208 | char *path; /* path in the registry rooted at the config key */ |
Mike Hearn | c619152 | 2005-01-03 14:45:05 +0000 | [diff] [blame] | 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 */ |
Mike Hearn | 0af614e | 2004-09-28 03:55:16 +0000 | [diff] [blame] | 211 | }; |
| 212 | |
| 213 | struct list *settings; |
| 214 | |
| 215 | static void free_setting(struct setting *setting) |
| 216 | { |
| 217 | assert( setting != NULL ); |
Mike Hearn | c619152 | 2005-01-03 14:45:05 +0000 | [diff] [blame] | 218 | assert( setting->path ); |
Mike Hearn | 0af614e | 2004-09-28 03:55:16 +0000 | [diff] [blame] | 219 | |
Mike Hearn | c619152 | 2005-01-03 14:45:05 +0000 | [diff] [blame] | 220 | WINE_TRACE("destroying %p: %s\n", setting, setting->path); |
| 221 | |
Mike Hearn | 0af614e | 2004-09-28 03:55:16 +0000 | [diff] [blame] | 222 | HeapFree(GetProcessHeap(), 0, setting->path); |
| 223 | HeapFree(GetProcessHeap(), 0, setting->name); |
Michael Stefaniuc | 5ad7d85 | 2004-12-23 17:06:43 +0000 | [diff] [blame] | 224 | HeapFree(GetProcessHeap(), 0, setting->value); |
Mike Hearn | 0af614e | 2004-09-28 03:55:16 +0000 | [diff] [blame] | 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 | */ |
Alexandre Julliard | 6e92d38 | 2005-06-13 18:49:23 +0000 | [diff] [blame] | 239 | char *get_reg_key(const char *path, const char *name, const char *def) |
Mike Hearn | 0af614e | 2004-09-28 03:55:16 +0000 | [diff] [blame] | 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); |
Crestez Leonard | 3e55df3 | 2005-01-14 19:48:41 +0000 | [diff] [blame] | 256 | return s->value ? strdupA(s->value) : NULL; |
Mike Hearn | 0af614e | 2004-09-28 03:55:16 +0000 | [diff] [blame] | 257 | } |
| 258 | |
| 259 | /* no, so get from the registry */ |
Gerald Pfeifer | 61f8e12 | 2004-11-30 21:03:36 +0000 | [diff] [blame] | 260 | val = get_config_key(path, name, def); |
Mike Hearn | 0af614e | 2004-09-28 03:55:16 +0000 | [diff] [blame] | 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 | * |
Mike Hearn | c619152 | 2005-01-03 14:45:05 +0000 | [diff] [blame] | 274 | * name is the value name, or NULL to delete the path. |
Mike Hearn | 0af614e | 2004-09-28 03:55:16 +0000 | [diff] [blame] | 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 | */ |
Alexandre Julliard | 6e92d38 | 2005-06-13 18:49:23 +0000 | [diff] [blame] | 280 | void set_reg_key(const char *path, const char *name, const char *value) |
Mike Hearn | 0af614e | 2004-09-28 03:55:16 +0000 | [diff] [blame] | 281 | { |
| 282 | struct list *cursor; |
| 283 | struct setting *s; |
| 284 | |
| 285 | assert( path != NULL ); |
Mike Hearn | 0af614e | 2004-09-28 03:55:16 +0000 | [diff] [blame] | 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; |
Mike Hearn | c619152 | 2005-01-03 14:45:05 +0000 | [diff] [blame] | 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); |
Mike Hearn | 0af614e | 2004-09-28 03:55:16 +0000 | [diff] [blame] | 302 | |
| 303 | /* yes, we have already set it, so just replace the content and return */ |
Michael Stefaniuc | 5ad7d85 | 2004-12-23 17:06:43 +0000 | [diff] [blame] | 304 | HeapFree(GetProcessHeap(), 0, s->value); |
Mike Hearn | 0af614e | 2004-09-28 03:55:16 +0000 | [diff] [blame] | 305 | s->value = value ? strdupA(value) : NULL; |
| 306 | |
Mike Hearn | c619152 | 2005-01-03 14:45:05 +0000 | [diff] [blame] | 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 | |
Mike Hearn | 0af614e | 2004-09-28 03:55:16 +0000 | [diff] [blame] | 317 | return; |
| 318 | } |
| 319 | |
| 320 | /* otherwise add a new setting for it */ |
| 321 | s = HeapAlloc(GetProcessHeap(), 0, sizeof(struct setting)); |
Mike Hearn | c619152 | 2005-01-03 14:45:05 +0000 | [diff] [blame] | 322 | s->path = strdupA(path); |
| 323 | s->name = name ? strdupA(name) : NULL; |
Mike Hearn | 0af614e | 2004-09-28 03:55:16 +0000 | [diff] [blame] | 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 | */ |
Alexandre Julliard | 6e92d38 | 2005-06-13 18:49:23 +0000 | [diff] [blame] | 443 | BOOL reg_key_exists(const char *path, const char *name) |
Mike Hearn | 0af614e | 2004-09-28 03:55:16 +0000 | [diff] [blame] | 444 | { |
Alexandre Julliard | 6e92d38 | 2005-06-13 18:49:23 +0000 | [diff] [blame] | 445 | char *val = get_reg_key(path, name, NULL); |
Mike Hearn | 0af614e | 2004-09-28 03:55:16 +0000 | [diff] [blame] | 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); |
Gerald Pfeifer | 61f8e12 | 2004-11-30 21:03:36 +0000 | [diff] [blame] | 461 | set_config_key(s->path, s->name, s->value); |
Mike Hearn | 0af614e | 2004-09-28 03:55:16 +0000 | [diff] [blame] | 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); |
Mike Hearn | cd0b789 | 2003-09-08 18:58:07 +0000 | [diff] [blame] | 482 | } |
Dimitrie O. Paun | 82ce2cc | 2003-03-31 19:41:55 +0000 | [diff] [blame] | 483 | } |
Mike Hearn | 0f4c6c9 | 2003-09-10 03:41:44 +0000 | [diff] [blame] | 484 | |
Mike Hearn | 0f4c6c9 | 2003-09-10 03:41:44 +0000 | [diff] [blame] | 485 | /* ================================== utility functions ============================ */ |
| 486 | |
Mike Hearn | 7d8711e | 2004-11-23 13:50:23 +0000 | [diff] [blame] | 487 | char *current_app = NULL; /* the app we are currently editing, or NULL if editing global */ |
Mike Hearn | a5ce4ee | 2004-09-28 03:16:43 +0000 | [diff] [blame] | 488 | |
| 489 | /* returns a registry key path suitable for passing to addTransaction */ |
Mike McCormack | ae51135 | 2005-06-02 15:11:32 +0000 | [diff] [blame] | 490 | char *keypath(const char *section) |
Mike Hearn | a5ce4ee | 2004-09-28 03:16:43 +0000 | [diff] [blame] | 491 | { |
| 492 | static char *result = NULL; |
Mike Hearn | 0af614e | 2004-09-28 03:55:16 +0000 | [diff] [blame] | 493 | |
Michael Stefaniuc | 5ad7d85 | 2004-12-23 17:06:43 +0000 | [diff] [blame] | 494 | HeapFree(GetProcessHeap(), 0, result); |
Mike Hearn | a5ce4ee | 2004-09-28 03:16:43 +0000 | [diff] [blame] | 495 | |
Mike Hearn | 7d8711e | 2004-11-23 13:50:23 +0000 | [diff] [blame] | 496 | if (current_app) |
Mike Hearn | a5ce4ee | 2004-09-28 03:16:43 +0000 | [diff] [blame] | 497 | { |
Mike Hearn | 7d8711e | 2004-11-23 13:50:23 +0000 | [diff] [blame] | 498 | result = HeapAlloc(GetProcessHeap(), 0, strlen("AppDefaults\\") + strlen(current_app) + 2 /* \\ */ + strlen(section) + 1 /* terminator */); |
| 499 | sprintf(result, "AppDefaults\\%s\\%s", current_app, section); |
Mike Hearn | a5ce4ee | 2004-09-28 03:16:43 +0000 | [diff] [blame] | 500 | } |
| 501 | else |
| 502 | { |
| 503 | result = strdupA(section); |
| 504 | } |
Mike Hearn | 0af614e | 2004-09-28 03:55:16 +0000 | [diff] [blame] | 505 | |
Mike Hearn | a5ce4ee | 2004-09-28 03:16:43 +0000 | [diff] [blame] | 506 | return result; |
| 507 | } |
| 508 | |
Chris Morgan | 49f0dd3 | 2004-05-04 02:56:46 +0000 | [diff] [blame] | 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); |
Mike Hearn | 7d8711e | 2004-11-23 13:50:23 +0000 | [diff] [blame] | 516 | |
| 517 | /* eliminate trailing newline, is this a Wine bug? */ |
Mike Hearn | fd14bde | 2005-01-10 14:26:46 +0000 | [diff] [blame] | 518 | *(strrchr(msg, '\r')) = '\0'; |
Mike Hearn | 7d8711e | 2004-11-23 13:50:23 +0000 | [diff] [blame] | 519 | |
Chris Morgan | 49f0dd3 | 2004-05-04 02:56:46 +0000 | [diff] [blame] | 520 | WINE_TRACE("error: '%s'\n", msg); |
| 521 | } |
Mike Hearn | 0af614e | 2004-09-28 03:55:16 +0000 | [diff] [blame] | 522 | |
| 523 | int initialize(void) { |
Alexandre Julliard | a18f206 | 2005-06-16 16:15:37 +0000 | [diff] [blame^] | 524 | DWORD res = RegCreateKey(HKEY_CURRENT_USER, WINE_KEY_ROOT, &config_key); |
Mike Hearn | 0af614e | 2004-09-28 03:55:16 +0000 | [diff] [blame] | 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 | } |