winecfg.c 23.2 KB
Newer Older
1 2 3 4 5
/*
 * WineCfg configuration management
 *
 * Copyright 2002 Jaco Greeff
 * Copyright 2003 Dimitrie O. Paun
6
 * Copyright 2003-2004 Mike Hearn
7 8 9 10 11 12 13 14 15 16 17 18 19
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
20
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
21
 *
Mike Hearn's avatar
Mike Hearn committed
22 23 24 25 26 27
 * TODO:
 *  - Use unicode
 *  - Icons in listviews/icons
 *  - Better add app dialog, scan c: for EXE files and add to list in background
 *  - Use [GNOME] HIG style groupboxes rather than win32 style (looks nicer, imho)
 *
28 29
 */

30 31
#define WIN32_LEAN_AND_MEAN

32
#include <assert.h>
33 34 35
#include <stdio.h>
#include <limits.h>
#include <windows.h>
36
#include <winreg.h>
37
#include <wine/unicode.h>
38
#include <wine/debug.h>
39
#include <wine/list.h>
40 41

WINE_DEFAULT_DEBUG_CHANNEL(winecfg);
42 43

#include "winecfg.h"
44
#include "resource.h"
45

46 47
static const int is_win64 = (sizeof(void *) > sizeof(int));

48
HKEY config_key = NULL;
49
HMENU hPopupMenus = 0;
50

51 52 53 54 55 56 57 58

/* this is called from the WM_SHOWWINDOW handlers of each tab page.
 *
 * it's a nasty hack, necessary because the property sheet insists on resetting the window title
 * to the title of the tab, which is utterly useless. dropping the property sheet is on the todo list.
 */
void set_window_title(HWND dialog)
{
59
    WCHAR newtitle[256];
60 61

    /* update the window title  */
Mike Hearn's avatar
Mike Hearn committed
62
    if (current_app)
63
    {
64
        WCHAR apptitle[256];
65
        LoadStringW (GetModuleHandleW(NULL), IDS_WINECFG_TITLE_APP, apptitle,
66 67
            sizeof(apptitle)/sizeof(apptitle[0]));
        wsprintfW (newtitle, apptitle, current_app);
68
    }
69 70
    else
    {
71
        LoadStringW (GetModuleHandleW(NULL), IDS_WINECFG_TITLE, newtitle,
72
            sizeof(newtitle)/sizeof(newtitle[0]));
73 74
    }

75 76
    WINE_TRACE("setting title to %s\n", wine_dbgstr_w (newtitle));
    SendMessageW (GetParent(dialog), PSM_SETTITLEW, 0, (LPARAM) newtitle);
77 78
}

79

80 81
WCHAR* load_string (UINT id)
{
82
    WCHAR buf[1024];
83 84 85
    int len;
    WCHAR* newStr;

86
    LoadStringW (GetModuleHandleW(NULL), id, buf, sizeof(buf)/sizeof(buf[0]));
87 88 89 90 91 92 93 94

    len = lstrlenW (buf);
    newStr = HeapAlloc (GetProcessHeap(), 0, (len + 1) * sizeof (WCHAR));
    memcpy (newStr, buf, len * sizeof (WCHAR));
    newStr[len] = 0;
    return newStr;
}

95
/**
96
 * get_config_key: Retrieves a configuration value from the registry
97
 *
98 99 100
 * char *subkey : the name of the config section
 * char *name : the name of the config value
 * char *default : if the key isn't found, return this value instead
101
 *
102 103
 * Returns a buffer holding the value if successful, NULL if
 * not. Caller is responsible for releasing the result.
104
 *
105
 */
106
static WCHAR *get_config_key (HKEY root, const WCHAR *subkey, const WCHAR *name, const WCHAR *def)
107
{
108
    LPWSTR buffer = NULL;
109
    DWORD len;
110
    HKEY hSubKey = NULL;
111
    DWORD res;
112

113 114
    WINE_TRACE("subkey=%s, name=%s, def=%s\n", wine_dbgstr_w(subkey),
               wine_dbgstr_w(name), wine_dbgstr_w(def));
115

116
    res = RegOpenKeyExW(root, subkey, 0, MAXIMUM_ALLOWED, &hSubKey);
117 118 119
    if (res != ERROR_SUCCESS)
    {
        if (res == ERROR_FILE_NOT_FOUND)
120
        {
121
            WINE_TRACE("Section key not present - using default\n");
122
            return def ? strdupW(def) : NULL;
123 124 125
        }
        else
        {
126
            WINE_ERR("RegOpenKey failed on wine config key (res=%d)\n", res);
127 128 129
        }
        goto end;
    }
130

131
    res = RegQueryValueExW(hSubKey, name, NULL, NULL, NULL, &len);
132 133
    if (res == ERROR_FILE_NOT_FOUND)
    {
134
        WINE_TRACE("Value not present - using default\n");
135
        buffer = def ? strdupW(def) : NULL;
136
	goto end;
137
    } else if (res != ERROR_SUCCESS)
138
    {
139
        WINE_ERR("Couldn't query value's length (res=%d)\n", res);
140 141
        goto end;
    }
142

143
    buffer = HeapAlloc(GetProcessHeap(), 0, len + sizeof(WCHAR));
144

145
    RegQueryValueExW(hSubKey, name, NULL, NULL, (LPBYTE) buffer, &len);
146

147
    WINE_TRACE("buffer=%s\n", wine_dbgstr_w(buffer));
148
end:
149
    RegCloseKey(hSubKey);
150

Mike McCormack's avatar
Mike McCormack committed
151
    return buffer;
152 153
}

154
/**
155
 * set_config_key: convenience wrapper to set a key/value pair
156
 *
157 158 159
 * const char *subKey : the name of the config section
 * const char *valueName : the name of the config value
 * const char *value : the value to set the configuration key to
160 161
 *
 * Returns 0 on success, non-zero otherwise
162 163
 *
 * If valueName or value is NULL, an empty section will be created
164
 */
165 166
static int set_config_key(HKEY root, const WCHAR *subkey, REGSAM access, const WCHAR *name, const void *value, DWORD type)
{
167 168 169
    DWORD res = 1;
    HKEY key = NULL;

170 171
    WINE_TRACE("subkey=%s: name=%s, value=%p, type=%d\n", wine_dbgstr_w(subkey),
               wine_dbgstr_w(name), value, type);
172 173

    assert( subkey != NULL );
174

175 176
    if (subkey[0])
    {
177 178
        res = RegCreateKeyExW( root, subkey, 0, NULL, REG_OPTION_NON_VOLATILE,
                               access, NULL, &key, NULL );
179 180
        if (res != ERROR_SUCCESS) goto end;
    }
181
    else key = root;
182
    if (name == NULL || value == NULL) goto end;
183

184 185
    switch (type)
    {
186 187
        case REG_SZ: res = RegSetValueExW(key, name, 0, REG_SZ, value, (lstrlenW(value)+1)*sizeof(WCHAR)); break;
        case REG_DWORD: res = RegSetValueExW(key, name, 0, REG_DWORD, value, sizeof(DWORD)); break;
188
    }
189 190 191 192
    if (res != ERROR_SUCCESS) goto end;

    res = 0;
end:
193
    if (key && key != root) RegCloseKey(key);
194 195 196
    if (res != 0)
        WINE_ERR("Unable to set configuration key %s in section %s, res=%d\n",
                 wine_dbgstr_w(name), wine_dbgstr_w(subkey), res);
197 198 199
    return res;
}

200
/* ========================================================================= */
201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216

/* This code exists for the following reasons:
 *
 * - It makes working with the registry easier
 * - By storing a mini cache of the registry, we can more easily implement
 *   cancel/revert and apply. The 'settings list' is an overlay on top of
 *   the actual registry data that we can write out at will.
 *
 * Rather than model a tree in memory, we simply store each absolute (rooted
 * at the config key) path.
 *
 */

struct setting
{
    struct list entry;
217
    HKEY root;    /* the key on which path is rooted */
218 219 220
    WCHAR *path;   /* path in the registry rooted at root  */
    WCHAR *name;   /* name of the registry value. if null, this means delete the key  */
    WCHAR *value;  /* contents of the registry value. if null, this means delete the value  */
221
    DWORD type;   /* type of registry value. REG_SZ or REG_DWORD for now */
222 223 224 225 226 227 228
};

struct list *settings;

static void free_setting(struct setting *setting)
{
    assert( setting != NULL );
229
    assert( setting->path );
230

231 232 233
    WINE_TRACE("destroying %p: %s\n", setting,
               wine_dbgstr_w(setting->path));

234 235
    HeapFree(GetProcessHeap(), 0, setting->path);
    HeapFree(GetProcessHeap(), 0, setting->name);
236
    HeapFree(GetProcessHeap(), 0, setting->value);
237 238 239 240 241 242 243 244 245 246 247 248 249 250

    list_remove(&setting->entry);

    HeapFree(GetProcessHeap(), 0, setting);
}

/**
 * Returns the contents of the value at path. If not in the settings
 * list, it will be fetched from the registry - failing that, the
 * default will be used.
 *
 * If already in the list, the contents as given there will be
 * returned. You are expected to HeapFree the result.
 */
251
WCHAR *get_reg_keyW(HKEY root, const WCHAR *path, const WCHAR *name, const WCHAR *def)
252 253 254
{
    struct list *cursor;
    struct setting *s;
255
    WCHAR *val;
256

257 258
    WINE_TRACE("path=%s, name=%s, def=%s\n", wine_dbgstr_w(path),
               wine_dbgstr_w(name), wine_dbgstr_w(def));
259 260 261 262 263 264

    /* check if it's in the list */
    LIST_FOR_EACH( cursor, settings )
    {
        s = LIST_ENTRY(cursor, struct setting, entry);

265
        if (root != s->root) continue;
266
        if (lstrcmpiW(path, s->path) != 0) continue;
267
        if (!s->name) continue;
268
        if (lstrcmpiW(name, s->name) != 0) continue;
269

270 271 272 273
        WINE_TRACE("found %s:%s in settings list, returning %s\n",
                   wine_dbgstr_w(path), wine_dbgstr_w(name),
                   wine_dbgstr_w(s->value));
        return s->value ? strdupW(s->value) : NULL;
274 275 276
    }

    /* no, so get from the registry */
277
    val = get_config_key(root, path, name, def);
278

279
    WINE_TRACE("returning %s\n", wine_dbgstr_w(val));
280 281 282 283

    return val;
}

284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305
char *get_reg_key(HKEY root, const char *path, const char *name, const char *def)
{
    WCHAR *wpath, *wname, *wdef = NULL, *wRet = NULL;
    char *szRet = NULL;
    int len;

    WINE_TRACE("path=%s, name=%s, def=%s\n", path, name, def);

    wpath = HeapAlloc(GetProcessHeap(), 0, (strlen(path)+1)*sizeof(WCHAR));
    wname = HeapAlloc(GetProcessHeap(), 0, (strlen(name)+1)*sizeof(WCHAR));

    MultiByteToWideChar(CP_ACP, 0, path, -1, wpath, strlen(path)+1);
    MultiByteToWideChar(CP_ACP, 0, name, -1, wname, strlen(name)+1);

    if (def)
    {
        wdef = HeapAlloc(GetProcessHeap(), 0, (strlen(def)+1)*sizeof(WCHAR));
        MultiByteToWideChar(CP_ACP, 0, def, -1, wdef, strlen(def)+1);
    }

    wRet = get_reg_keyW(root, wpath, wname, wdef);

306 307 308 309 310 311
    len = WideCharToMultiByte(CP_ACP, 0, wRet, -1, NULL, 0, NULL, NULL);
    if (len)
    {
        szRet = HeapAlloc(GetProcessHeap(), 0, len);
        WideCharToMultiByte(CP_ACP, 0, wRet, -1, szRet, len, NULL, NULL);
    }
312 313 314 315 316 317 318 319 320

    HeapFree(GetProcessHeap(), 0, wpath);
    HeapFree(GetProcessHeap(), 0, wname);
    HeapFree(GetProcessHeap(), 0, wdef);
    HeapFree(GetProcessHeap(), 0, wRet);

    return szRet;
}

321 322 323 324 325 326 327
/**
 * Used to set a registry key.
 *
 * path is rooted at the config key, ie use "Version" or
 * "AppDefaults\\fooapp.exe\\Version". You can use keypath()
 * to get such a string.
 *
328
 * name is the value name, or NULL to delete the path.
329 330 331
 *
 * value is what to set the value to, or NULL to delete it.
 *
332 333
 * type is REG_SZ or REG_DWORD.
 *
334 335
 * These values will be copied when necessary.
 */
336
static void set_reg_key_ex(HKEY root, const WCHAR *path, const WCHAR *name, const void *value, DWORD type)
337 338 339 340 341 342
{
    struct list *cursor;
    struct setting *s;

    assert( path != NULL );

343
    WINE_TRACE("path=%s, name=%s, value=%s\n", wine_dbgstr_w(path),
344
               wine_dbgstr_w(name), wine_dbgstr_w(value));
345 346 347 348 349 350

    /* firstly, see if we already set this setting  */
    LIST_FOR_EACH( cursor, settings )
    {
        struct setting *s = LIST_ENTRY(cursor, struct setting, entry);

351
        if (root != s->root) continue;
352 353
        if (lstrcmpiW(s->path, path) != 0) continue;
        if ((s->name && name) && lstrcmpiW(s->name, name) != 0) continue;
354 355 356 357 358

        /* are we attempting a double delete? */
        if (!s->name && !name) return;

        /* do we want to undelete this key? */
359
        if (!s->name && name) s->name = strdupW(name);
360 361

        /* yes, we have already set it, so just replace the content and return  */
362
        HeapFree(GetProcessHeap(), 0, s->value);
363 364 365 366
        s->type = type;
        switch (type)
        {
            case REG_SZ:
367
                s->value = value ? strdupW(value) : NULL;
368 369 370 371 372 373
                break;
            case REG_DWORD:
                s->value = HeapAlloc(GetProcessHeap(), 0, sizeof(DWORD));
                memcpy( s->value, value, sizeof(DWORD) );
                break;
        }
374

375 376 377 378 379 380 381 382 383 384
        /* are we deleting this key? this won't remove any of the
         * children from the overlay so if the user adds it again in
         * that session it will appear to undelete the settings, but
         * in reality only the settings actually modified by the user
         * in that session will be restored. we might want to fix this
         * corner case in future by actually deleting all the children
         * here so that once it's gone, it's gone.
         */
        if (!name) s->name = NULL;

385 386 387 388 389
        return;
    }

    /* otherwise add a new setting for it  */
    s = HeapAlloc(GetProcessHeap(), 0, sizeof(struct setting));
390
    s->root  = root;
391 392
    s->path  = strdupW(path);
    s->name  = name  ? strdupW(name)  : NULL;
393 394 395 396
    s->type  = type;
    switch (type)
    {
        case REG_SZ:
397
            s->value = value ? strdupW(value) : NULL;
398 399 400 401 402 403
            break;
        case REG_DWORD:
            s->value = HeapAlloc(GetProcessHeap(), 0, sizeof(DWORD));
            memcpy( s->value, value, sizeof(DWORD) );
            break;
    }
404 405

    list_add_tail(settings, &s->entry);
406
}
407

408 409
void set_reg_key(HKEY root, const char *path, const char *name, const char *value)
{
410
    WCHAR *wpath, *wname = NULL, *wvalue = NULL;
411 412 413

    wpath = HeapAlloc(GetProcessHeap(), 0, (strlen(path)+1)*sizeof(WCHAR));
    MultiByteToWideChar(CP_ACP, 0, path, -1, wpath, strlen(path)+1);
414 415 416 417 418 419

    if (name)
    {
        wname = HeapAlloc(GetProcessHeap(), 0, (strlen(name)+1)*sizeof(WCHAR));
        MultiByteToWideChar(CP_ACP, 0, name, -1, wname, strlen(name)+1);
    }
420 421 422 423 424 425

    if (value)
    {
        wvalue = HeapAlloc(GetProcessHeap(), 0, (strlen(value)+1)*sizeof(WCHAR));
        MultiByteToWideChar(CP_ACP, 0, value, -1, wvalue, strlen(value)+1);
    }
426 427 428 429 430 431

    set_reg_key_ex(root, wpath, wname, wvalue, REG_SZ);

    HeapFree(GetProcessHeap(), 0, wpath);
    HeapFree(GetProcessHeap(), 0, wname);
    HeapFree(GetProcessHeap(), 0, wvalue);
432 433 434 435
}

void set_reg_key_dword(HKEY root, const char *path, const char *name, DWORD value)
{
436 437 438 439 440 441 442 443 444 445 446 447
    WCHAR *wpath, *wname;

    wpath = HeapAlloc(GetProcessHeap(), 0, (strlen(path)+1)*sizeof(WCHAR));
    wname = HeapAlloc(GetProcessHeap(), 0, (strlen(name)+1)*sizeof(WCHAR));

    MultiByteToWideChar(CP_ACP, 0, path, -1, wpath, strlen(path)+1);
    MultiByteToWideChar(CP_ACP, 0, name, -1, wname, strlen(name)+1);

    set_reg_key_ex(root, wpath, wname, &value, REG_DWORD);

    HeapFree(GetProcessHeap(), 0, wpath);
    HeapFree(GetProcessHeap(), 0, wname);
448 449
}

450 451 452 453 454
void set_reg_keyW(HKEY root, const WCHAR *path, const WCHAR *name, const WCHAR *value)
{
    set_reg_key_ex(root, path, name, value, REG_SZ);
}

455 456 457 458 459
void set_reg_key_dwordW(HKEY root, const WCHAR *path, const WCHAR *name, DWORD value)
{
    set_reg_key_ex(root, path, name, &value, REG_DWORD);
}

460 461 462 463 464 465 466
/**
 * enumerates the value names at the given path, taking into account
 * the changes in the settings list.
 *
 * you are expected to HeapFree each element of the array, which is null
 * terminated, as well as the array itself.
 */
467
static WCHAR **enumerate_valuesW(HKEY root, WCHAR *path)
468 469
{
    HKEY key;
470
    DWORD res, i = 0, valueslen = 0;
471
    WCHAR **values = NULL;
472 473
    struct list *cursor;

474
    res = RegOpenKeyExW(root, path, 0, MAXIMUM_ALLOWED, &key);
475 476 477 478
    if (res == ERROR_SUCCESS)
    {
        while (TRUE)
        {
479
            WCHAR name[1024];
480
            DWORD namesize = sizeof(name)/sizeof(name[0]);
481 482 483
            BOOL removed = FALSE;

            /* find out the needed size, allocate a buffer, read the value  */
484
            if ((res = RegEnumValueW(key, i, name, &namesize, NULL, NULL, NULL, NULL)) != ERROR_SUCCESS)
485 486
                break;

487
            WINE_TRACE("name=%s\n", wine_dbgstr_w(name));
488 489 490 491 492

            /* check if this value name has been removed in the settings list  */
            LIST_FOR_EACH( cursor, settings )
            {
                struct setting *s = LIST_ENTRY(cursor, struct setting, entry);
493 494
                if (lstrcmpiW(s->path, path) != 0) continue;
                if (lstrcmpiW(s->name, name) != 0) continue;
495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511

                if (!s->value)
                {
                    WINE_TRACE("this key has been removed, so skipping\n");
                    removed = TRUE;
                    break;
                }
            }

            if (removed)            /* this value was deleted by the user, so don't include it */
            {
                HeapFree(GetProcessHeap(), 0, name);
                i++;
                continue;
            }

            /* grow the array if necessary, add buffer to it, iterate  */
512 513
            if (values) values = HeapReAlloc(GetProcessHeap(), 0, values, sizeof(WCHAR*) * (valueslen + 1));
            else values = HeapAlloc(GetProcessHeap(), 0, sizeof(WCHAR*));
514

515
            values[valueslen++] = strdupW(name);
516 517 518 519 520 521
            WINE_TRACE("valueslen is now %d\n", valueslen);
            i++;
        }
    }
    else
    {
522 523
        WINE_WARN("failed opening registry key %s, res=0x%x\n",
                  wine_dbgstr_w(path), res);
524 525 526 527 528 529 530 531 532 533
    }

    WINE_TRACE("adding settings in list but not registry\n");

    /* now we have to add the values that aren't in the registry but are in the settings list */
    LIST_FOR_EACH( cursor, settings )
    {
        struct setting *setting = LIST_ENTRY(cursor, struct setting, entry);
        BOOL found = FALSE;

534
        if (lstrcmpiW(setting->path, path) != 0) continue;
535 536 537 538 539

        if (!setting->value) continue;

        for (i = 0; i < valueslen; i++)
        {
540
            if (lstrcmpiW(setting->name, values[i]) == 0)
541 542 543 544 545 546 547 548
            {
                found = TRUE;
                break;
            }
        }

        if (found) continue;

549
        WINE_TRACE("%s in list but not registry\n", wine_dbgstr_w(setting->name));
550 551

        /* otherwise it's been set by the user but isn't in the registry */
552 553
        if (values) values = HeapReAlloc(GetProcessHeap(), 0, values, sizeof(WCHAR*) * (valueslen + 1));
        else values = HeapAlloc(GetProcessHeap(), 0, sizeof(WCHAR*));
554

555
        values[valueslen++] = strdupW(setting->name);
556 557
    }

558 559 560
    WINE_TRACE("adding null terminator\n");
    if (values)
    {
561
        values = HeapReAlloc(GetProcessHeap(), 0, values, sizeof(WCHAR*) * (valueslen + 1));
562
        values[valueslen] = NULL;
563
    }
564 565 566 567

    RegCloseKey(key);

    return values;
568 569
}

570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595
char **enumerate_values(HKEY root, char *path)
{
    WCHAR *wpath;
    WCHAR **wret;
    char **ret=NULL;
    int i=0, len=0;

    wpath = HeapAlloc(GetProcessHeap(), 0, (strlen(path)+1)*sizeof(WCHAR));
    MultiByteToWideChar(CP_ACP, 0, path, -1, wpath, strlen(path)+1);

    wret = enumerate_valuesW(root, wpath);

    if (wret)
    {
        for(len=0; wret[len]; len++);
        ret = HeapAlloc(GetProcessHeap(), 0, (len+1)*sizeof(char*));

        /* convert WCHAR ** to char ** and HeapFree each WCHAR * element on our way */
        for (i=0; i<len; i++)
        {
            ret[i] = HeapAlloc(GetProcessHeap(), 0,
                               (lstrlenW(wret[i]) + 1) * sizeof(char));
            WideCharToMultiByte(CP_ACP, 0, wret[i], -1, ret[i],
                                lstrlenW(wret[i]) + 1, NULL, NULL);
            HeapFree(GetProcessHeap(), 0, wret[i]);
        }
596
        ret[len] = NULL;
597 598 599 600 601 602 603 604
    }

    HeapFree(GetProcessHeap(), 0, wpath);
    HeapFree(GetProcessHeap(), 0, wret);

    return ret;
}

605 606 607 608
/**
 * returns true if the given key/value pair exists in the registry or
 * has been written to.
 */
609
BOOL reg_key_exists(HKEY root, const char *path, const char *name)
610
{
611
    char *val = get_reg_key(root, path, name, NULL);
612 613 614 615 616

    if (val)
    {
        HeapFree(GetProcessHeap(), 0, val);
        return TRUE;
617
    }
618 619

    return FALSE;
620 621
}

622
static void process_setting(struct setting *s)
623
{
624 625 626 627 628
    static const WCHAR softwareW[] = {'S','o','f','t','w','a','r','e','\\'};
    HKEY key;
    BOOL needs_wow64 = (is_win64 && s->root == HKEY_LOCAL_MACHINE && s->path &&
                        !strncmpiW( s->path, softwareW, sizeof(softwareW)/sizeof(WCHAR) ));

629 630
    if (s->value)
    {
631 632
	WINE_TRACE("Setting %s:%s to '%s'\n", wine_dbgstr_w(s->path),
                   wine_dbgstr_w(s->name), wine_dbgstr_w(s->value));
633 634 635 636 637 638 639
        set_config_key(s->root, s->path, MAXIMUM_ALLOWED, s->name, s->value, s->type);
        if (needs_wow64)
        {
            WINE_TRACE("Setting 32-bit %s:%s to '%s'\n", wine_dbgstr_w(s->path),
                       wine_dbgstr_w(s->name), wine_dbgstr_w(s->value));
            set_config_key(s->root, s->path, MAXIMUM_ALLOWED | KEY_WOW64_32KEY, s->name, s->value, s->type);
        }
640 641 642
    }
    else
    {
643
	WINE_TRACE("Removing %s:%s\n", wine_dbgstr_w(s->path), wine_dbgstr_w(s->name));
644
        if (!RegOpenKeyExW( s->root, s->path, 0, MAXIMUM_ALLOWED, &key ))
645 646 647
        {
            /* NULL name means remove that path/section entirely */
            if (s->name) RegDeleteValueW( key, s->name );
648 649 650 651 652
            else
            {
                RegDeleteTreeW( key, NULL );
                RegDeleteKeyW( s->root, s->path );
            }
653 654 655 656 657 658 659 660
            RegCloseKey( key );
        }
        if (needs_wow64)
        {
            WINE_TRACE("Removing 32-bit %s:%s\n", wine_dbgstr_w(s->path), wine_dbgstr_w(s->name));
            if (!RegOpenKeyExW( s->root, s->path, 0, MAXIMUM_ALLOWED | KEY_WOW64_32KEY, &key ))
            {
                if (s->name) RegDeleteValueW( key, s->name );
661 662 663 664 665
                else
                {
                    RegDeleteTreeW( key, NULL );
                    RegDeleteKeyExW( s->root, s->path, KEY_WOW64_32KEY, 0 );
                }
666 667 668
                RegCloseKey( key );
            }
        }
669 670 671 672 673 674 675 676 677 678 679 680 681 682
    }
}

void apply(void)
{
    if (list_empty(settings)) return; /* we will be called for each page when the user clicks OK */

    WINE_TRACE("()\n");

    while (!list_empty(settings))
    {
        struct setting *s = (struct setting *) list_head(settings);
        process_setting(s);
        free_setting(s);
683
    }
684
}
685 686 687

/* ================================== utility functions ============================ */

688
WCHAR* current_app = NULL; /* the app we are currently editing, or NULL if editing global */
689 690

/* returns a registry key path suitable for passing to addTransaction  */
691
char *keypath(const char *section)
692 693
{
    static char *result = NULL;
694

695
    HeapFree(GetProcessHeap(), 0, result);
696

Mike Hearn's avatar
Mike Hearn committed
697
    if (current_app)
698
    {
699
        result = HeapAlloc(GetProcessHeap(), 0, strlen("AppDefaults\\") + lstrlenW(current_app)*2 + 2 /* \\ */ + strlen(section) + 1 /* terminator */);
700
        wsprintfA(result, "AppDefaults\\%ls", current_app);
701
        if (section[0]) sprintf( result + strlen(result), "\\%s", section );
702 703 704 705 706
    }
    else
    {
        result = strdupA(section);
    }
707

708 709 710
    return result;
}

711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738
WCHAR *keypathW(const WCHAR *section)
{
    static const WCHAR appdefaultsW[] = {'A','p','p','D','e','f','a','u','l','t','s','\\',0};
    static WCHAR *result = NULL;

    HeapFree(GetProcessHeap(), 0, result);

    if (current_app)
    {
        DWORD len = sizeof(appdefaultsW) + (lstrlenW(current_app) + lstrlenW(section) + 1) * sizeof(WCHAR);
        result = HeapAlloc(GetProcessHeap(), 0, len );
        lstrcpyW( result, appdefaultsW );
        lstrcatW( result, current_app );
        if (section[0])
        {
            len = lstrlenW(result);
            result[len++] = '\\';
            lstrcpyW( result + len, section );
        }
    }
    else
    {
        result = strdupW(section);
    }

    return result;
}

739 740 741 742 743 744 745
void PRINTERROR(void)
{
        LPSTR msg;

        FormatMessageA(FORMAT_MESSAGE_ALLOCATE_BUFFER|FORMAT_MESSAGE_FROM_SYSTEM,
                       0, GetLastError(), MAKELANGID(LANG_NEUTRAL,SUBLANG_DEFAULT),
                       (LPSTR)&msg, 0, NULL);
Mike Hearn's avatar
Mike Hearn committed
746 747

        /* eliminate trailing newline, is this a Wine bug? */
748
        *(strrchr(msg, '\r')) = '\0';
Mike Hearn's avatar
Mike Hearn committed
749
        
750 751
        WINE_TRACE("error: '%s'\n", msg);
}
752

753 754
int initialize(HINSTANCE hInstance)
{
755
    DWORD res = RegCreateKeyA(HKEY_CURRENT_USER, WINE_KEY_ROOT, &config_key);
756 757

    if (res != ERROR_SUCCESS) {
758
	WINE_ERR("RegOpenKey failed on wine config key (%d)\n", res);
759 760 761 762 763 764 765 766 767
	return 1;
    }

    /* we could probably just have the list as static data  */
    settings = HeapAlloc(GetProcessHeap(), 0, sizeof(struct list));
    list_init(settings);

    return 0;
}