classes.c 45.9 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
/*
 * Implementation of the Microsoft Installer (msi.dll)
 *
 * Copyright 2005 Aric Stewart for CodeWeavers
 *
 * 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
18
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19 20
 */

21 22
/* Actions handled in this module:
 *
23 24 25 26
 * RegisterClassInfo
 * RegisterProgIdInfo
 * RegisterExtensionInfo
 * RegisterMIMEInfo
27 28 29
 * UnregisterClassInfo
 * UnregisterProgIdInfo
 * UnregisterExtensionInfo
30
 * UnregisterMIMEInfo
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
 */

#include <stdarg.h>

#include "windef.h"
#include "winbase.h"
#include "winerror.h"
#include "winreg.h"
#include "wine/debug.h"
#include "msipriv.h"
#include "winuser.h"
#include "wine/unicode.h"

WINE_DEFAULT_DEBUG_CHANNEL(msi);

46
static MSIAPPID *load_appid( MSIPACKAGE* package, MSIRECORD *row )
47 48
{
    LPCWSTR buffer;
49
    MSIAPPID *appid;
50 51 52

    /* fill in the data */

53
    appid = msi_alloc_zero( sizeof(MSIAPPID) );
54 55
    if (!appid)
        return NULL;
56
    
57
    appid->AppID = msi_dup_record_field( row, 1 );
58
    TRACE("loading appid %s\n", debugstr_w( appid->AppID ));
59 60

    buffer = MSI_RecordGetString(row,2);
61
    deformat_string( package, buffer, &appid->RemoteServerName );
62

63 64 65
    appid->LocalServer = msi_dup_record_field(row,3);
    appid->ServiceParameters = msi_dup_record_field(row,4);
    appid->DllSurrogate = msi_dup_record_field(row,5);
66

67 68 69 70
    appid->ActivateAtStorage = !MSI_RecordIsNull(row,6);
    appid->RunAsInteractiveUser = !MSI_RecordIsNull(row,7);

    list_add_tail( &package->appids, &appid->entry );
71
    
72
    return appid;
73 74
}

75
static MSIAPPID *load_given_appid( MSIPACKAGE *package, LPCWSTR name )
76
{
77 78 79 80
    static const WCHAR query[] = {
        'S','E','L','E','C','T',' ','*',' ','F','R','O','M',' ',
        '`','A','p','p','I','d','`',' ','W','H','E','R','E',' ',
        '`','A','p','p','I','d','`',' ','=',' ','\'','%','s','\'',0};
81
    MSIRECORD *row;
82
    MSIAPPID *appid;
83

84 85
    if (!name)
        return NULL;
86 87

    /* check for appids already loaded */
88 89
    LIST_FOR_EACH_ENTRY( appid, &package->appids, MSIAPPID, entry )
    {
90
        if (!strcmpiW( appid->AppID, name ))
91
        {
92 93
            TRACE("found appid %s %p\n", debugstr_w(name), appid);
            return appid;
94
        }
95
    }
96
    
97
    row = MSI_QueryGetRecord(package->db, query, name);
98
    if (!row)
99
        return NULL;
100

101
    appid = load_appid(package, row);
102
    msiobj_release(&row->hdr);
103
    return appid;
104 105
}

106
static MSIPROGID *load_given_progid(MSIPACKAGE *package, LPCWSTR progid);
107
static MSICLASS *load_given_class( MSIPACKAGE *package, LPCWSTR classid );
108

109
static MSIPROGID *load_progid( MSIPACKAGE* package, MSIRECORD *row )
110
{
111
    MSIPROGID *progid;
112 113 114 115
    LPCWSTR buffer;

    /* fill in the data */

116
    progid = msi_alloc_zero( sizeof(MSIPROGID) );
117 118
    if (!progid)
        return NULL;
119

120
    list_add_tail( &package->progids, &progid->entry );
121

122
    progid->ProgID = msi_dup_record_field(row,1);
123
    TRACE("loading progid %s\n",debugstr_w(progid->ProgID));
124 125

    buffer = MSI_RecordGetString(row,2);
126 127
    progid->Parent = load_given_progid(package,buffer);
    if (progid->Parent == NULL && buffer)
128 129 130
        FIXME("Unknown parent ProgID %s\n",debugstr_w(buffer));

    buffer = MSI_RecordGetString(row,3);
131 132
    progid->Class = load_given_class(package,buffer);
    if (progid->Class == NULL && buffer)
133 134
        FIXME("Unknown class %s\n",debugstr_w(buffer));

135
    progid->Description = msi_dup_record_field(row,4);
136 137 138 139

    if (!MSI_RecordIsNull(row,6))
    {
        INT icon_index = MSI_RecordGetInteger(row,6); 
140
        LPCWSTR FileName = MSI_RecordGetString(row,5);
141 142 143
        LPWSTR FilePath;
        static const WCHAR fmt[] = {'%','s',',','%','i',0};

144
        FilePath = msi_build_icon_path(package, FileName);
145
       
146
        progid->IconPath = msi_alloc( (strlenW(FilePath)+10)* sizeof(WCHAR) );
147

148
        sprintfW(progid->IconPath,fmt,FilePath,icon_index);
149

150
        msi_free(FilePath);
151 152 153 154 155
    }
    else
    {
        buffer = MSI_RecordGetString(row,5);
        if (buffer)
156
            progid->IconPath = msi_build_icon_path(package, buffer);
157 158
    }

159 160
    progid->CurVer = NULL;
    progid->VersionInd = NULL;
161 162

    /* if we have a parent then we may be that parents CurVer */
163
    if (progid->Parent && progid->Parent != progid)
164
    {
165 166
        MSIPROGID *parent = progid->Parent;

167
        while (parent->Parent && parent->Parent != parent)
168
            parent = parent->Parent;
169

Austin English's avatar
Austin English committed
170
        /* FIXME: need to determine if we are really the CurVer */
171

172
        progid->CurVer = parent;
173
        parent->VersionInd = progid;
174 175
    }
    
176
    return progid;
177 178
}

179
static MSIPROGID *load_given_progid(MSIPACKAGE *package, LPCWSTR name)
180
{
181 182 183 184
    static const WCHAR query[] = {
        'S','E','L','E','C','T',' ','*',' ','F','R','O','M',' ',
        '`','P','r','o','g','I','d','`',' ','W','H','E','R','E',' ',
        '`','P','r','o','g','I','d','`',' ','=',' ','\'','%','s','\'',0};
185
    MSIPROGID *progid;
186 187
    MSIRECORD *row;

188 189
    if (!name)
        return NULL;
190 191

    /* check for progids already loaded */
192 193
    LIST_FOR_EACH_ENTRY( progid, &package->progids, MSIPROGID, entry )
    {
194
        if (!strcmpiW( progid->ProgID, name ))
195
        {
196 197
            TRACE("found progid %s (%p)\n",debugstr_w(name), progid );
            return progid;
198
        }
199
    }
200
    
201
    row = MSI_QueryGetRecord( package->db, query, name );
202 203
    if (!row)
        return NULL;
204

205
    progid = load_progid(package, row);
206
    msiobj_release(&row->hdr);
207
    return progid;
208 209
}

210
static MSICLASS *load_class( MSIPACKAGE* package, MSIRECORD *row )
211
{
212
    MSICLASS *cls;
213
    DWORD i;
214 215 216 217
    LPCWSTR buffer;

    /* fill in the data */

218
    cls = msi_alloc_zero( sizeof(MSICLASS) );
219 220
    if (!cls)
        return NULL;
221

222 223
    list_add_tail( &package->classes, &cls->entry );

224
    cls->clsid = msi_dup_record_field( row, 1 );
225
    TRACE("loading class %s\n",debugstr_w(cls->clsid));
226
    cls->Context = msi_dup_record_field( row, 2 );
227
    buffer = MSI_RecordGetString(row,3);
228
    cls->Component = msi_get_loaded_component( package, buffer );
229

230
    cls->ProgIDText = msi_dup_record_field(row,4);
231
    cls->ProgID = load_given_progid(package, cls->ProgIDText);
232

233
    cls->Description = msi_dup_record_field(row,5);
234 235 236

    buffer = MSI_RecordGetString(row,6);
    if (buffer)
237
        cls->AppID = load_given_appid(package, buffer);
238

239
    cls->FileTypeMask = msi_dup_record_field(row,7);
240 241 242 243 244

    if (!MSI_RecordIsNull(row,9))
    {

        INT icon_index = MSI_RecordGetInteger(row,9); 
245
        LPCWSTR FileName = MSI_RecordGetString(row,8);
246 247 248
        LPWSTR FilePath;
        static const WCHAR fmt[] = {'%','s',',','%','i',0};

249
        FilePath = msi_build_icon_path(package, FileName);
250
       
251
        cls->IconPath = msi_alloc( (strlenW(FilePath)+5)* sizeof(WCHAR) );
252

253
        sprintfW(cls->IconPath,fmt,FilePath,icon_index);
254

255
        msi_free(FilePath);
256 257 258 259 260
    }
    else
    {
        buffer = MSI_RecordGetString(row,8);
        if (buffer)
261
            cls->IconPath = msi_build_icon_path(package, buffer);
262 263 264 265 266 267 268 269 270 271 272 273 274
    }

    if (!MSI_RecordIsNull(row,10))
    {
        i = MSI_RecordGetInteger(row,10);
        if (i != MSI_NULL_INTEGER && i > 0 &&  i < 4)
        {
            static const WCHAR ole2[] = {'o','l','e','2','.','d','l','l',0};
            static const WCHAR ole32[] = {'o','l','e','3','2','.','d','l','l',0};

            switch(i)
            {
                case 1:
275
                    cls->DefInprocHandler = strdupW(ole2);
276 277
                    break;
                case 2:
278
                    cls->DefInprocHandler32 = strdupW(ole32);
279 280
                    break;
                case 3:
281 282
                    cls->DefInprocHandler = strdupW(ole2);
                    cls->DefInprocHandler32 = strdupW(ole32);
283 284 285 286 287
                    break;
            }
        }
        else
        {
288 289
            cls->DefInprocHandler32 = msi_dup_record_field( row, 10 );
            msi_reduce_to_long_filename( cls->DefInprocHandler32 );
290 291 292
        }
    }
    buffer = MSI_RecordGetString(row,11);
293
    deformat_string(package,buffer,&cls->Argument);
294 295

    buffer = MSI_RecordGetString(row,12);
296
    cls->Feature = msi_get_loaded_feature(package, buffer);
297

298
    cls->Attributes = MSI_RecordGetInteger(row,13);
299
    cls->action = INSTALLSTATE_UNKNOWN;
300
    return cls;
301 302 303 304 305 306 307 308
}

/*
 * the Class table has 3 primary keys. Generally it is only 
 * referenced through the first CLSID key. However when loading
 * all of the classes we need to make sure we do not ignore rows
 * with other Context and ComponentIndexs 
 */
309
static MSICLASS *load_given_class(MSIPACKAGE *package, LPCWSTR classid)
310
{
311 312 313 314
    static const WCHAR query[] = {
        'S','E','L','E','C','T',' ','*',' ','F','R','O','M',' ',
        '`','C','l','a','s','s','`',' ','W','H','E','R','E',' ',
        '`','C','L','S','I','D','`',' ','=',' ','\'','%','s','\'',0};
315
    MSICLASS *cls;
316 317 318
    MSIRECORD *row;

    if (!classid)
319
        return NULL;
320 321
    
    /* check for classes already loaded */
322 323
    LIST_FOR_EACH_ENTRY( cls, &package->classes, MSICLASS, entry )
    {
324
        if (!strcmpiW( cls->clsid, classid ))
325
        {
326 327
            TRACE("found class %s (%p)\n",debugstr_w(classid), cls);
            return cls;
328
        }
329 330
    }

331
    row = MSI_QueryGetRecord(package->db, query, classid);
332
    if (!row)
333
        return NULL;
334

335
    cls = load_class(package, row);
336
    msiobj_release(&row->hdr);
337
    return cls;
338 339
}

340
static MSIEXTENSION *load_given_extension( MSIPACKAGE *package, LPCWSTR extension );
341

342
static MSIMIME *load_mime( MSIPACKAGE* package, MSIRECORD *row )
343
{
344
    LPCWSTR extension;
345
    MSIMIME *mt;
346 347 348

    /* fill in the data */

349
    mt = msi_alloc_zero( sizeof(MSIMIME) );
350 351
    if (!mt)
        return mt;
352

353
    mt->ContentType = msi_dup_record_field( row, 1 ); 
354
    TRACE("loading mime %s\n", debugstr_w(mt->ContentType));
355

356 357 358
    extension = MSI_RecordGetString( row, 2 );
    mt->Extension = load_given_extension( package, extension );
    mt->suffix = strdupW( extension );
359

360
    mt->clsid = msi_dup_record_field( row, 3 );
361
    mt->Class = load_given_class( package, mt->clsid );
362 363

    list_add_tail( &package->mimes, &mt->entry );
364

365
    return mt;
366 367
}

368
static MSIMIME *load_given_mime( MSIPACKAGE *package, LPCWSTR mime )
369
{
370 371 372 373
    static const WCHAR query[] = {
        'S','E','L','E','C','T',' ','*',' ','F','R','O','M',' ',
        '`','M','I','M','E','`',' ','W','H','E','R','E',' ',
        '`','C','o','n','t','e','n','t','T','y','p','e','`',' ','=',' ','\'','%','s','\'',0};
374
    MSIRECORD *row;
375
    MSIMIME *mt;
376 377

    if (!mime)
378
        return NULL;
379 380
    
    /* check for mime already loaded */
381 382
    LIST_FOR_EACH_ENTRY( mt, &package->mimes, MSIMIME, entry )
    {
383
        if (!strcmpiW( mt->ContentType, mime ))
384
        {
385 386
            TRACE("found mime %s (%p)\n",debugstr_w(mime), mt);
            return mt;
387
        }
388
    }
389
    
390
    row = MSI_QueryGetRecord(package->db, query, mime);
391
    if (!row)
392
        return NULL;
393

394
    mt = load_mime(package, row);
395
    msiobj_release(&row->hdr);
396
    return mt;
397 398
}

399
static MSIEXTENSION *load_extension( MSIPACKAGE* package, MSIRECORD *row )
400
{
401
    MSIEXTENSION *ext;
402 403 404 405
    LPCWSTR buffer;

    /* fill in the data */

406
    ext = msi_alloc_zero( sizeof(MSIEXTENSION) );
407 408 409
    if (!ext)
        return NULL;

410 411
    list_init( &ext->verbs );

412
    list_add_tail( &package->extensions, &ext->entry );
413

414
    ext->Extension = msi_dup_record_field( row, 1 );
415
    TRACE("loading extension %s\n", debugstr_w(ext->Extension));
416

417
    buffer = MSI_RecordGetString( row, 2 );
418
    ext->Component = msi_get_loaded_component( package, buffer );
419

420
    ext->ProgIDText = msi_dup_record_field( row, 3 );
421
    ext->ProgID = load_given_progid( package, ext->ProgIDText );
422

423 424
    buffer = MSI_RecordGetString( row, 4 );
    ext->Mime = load_given_mime( package, buffer );
425 426

    buffer = MSI_RecordGetString(row,5);
427
    ext->Feature = msi_get_loaded_feature( package, buffer );
428
    ext->action = INSTALLSTATE_UNKNOWN;
429
    return ext;
430 431 432 433
}

/*
 * While the extension table has 2 primary keys, this function is only looking
Austin English's avatar
Austin English committed
434
 * at the Extension key which is what is referenced as a foreign key
435
 */
436
static MSIEXTENSION *load_given_extension( MSIPACKAGE *package, LPCWSTR name )
437
{
438 439 440 441
    static const WCHAR query[] = {
        'S','E','L','E','C','T',' ','*',' ','F','R','O','M',' ',
        '`','E','x','t','e','n','s','i','o','n','`',' ','W','H','E','R','E',' ',
        '`','E','x','t','e','n','s','i','o','n','`',' ','=',' ','\'','%','s','\'',0};
442
    MSIEXTENSION *ext;
443
    MSIRECORD *row;
444

445 446
    if (!name)
        return NULL;
447

448 449 450
    if (name[0] == '.')
        name++;

451
    /* check for extensions already loaded */
452 453
    LIST_FOR_EACH_ENTRY( ext, &package->extensions, MSIEXTENSION, entry )
    {
454
        if (!strcmpiW( ext->Extension, name ))
455
        {
456 457
            TRACE("extension %s already loaded %p\n", debugstr_w(name), ext);
            return ext;
458
        }
459
    }
460
    
461
    row = MSI_QueryGetRecord( package->db, query, name );
462
    if (!row)
463
        return NULL;
464

465
    ext = load_extension(package, row);
466
    msiobj_release(&row->hdr);
467
    return ext;
468 469 470 471
}

static UINT iterate_load_verb(MSIRECORD *row, LPVOID param)
{
472
    MSIPACKAGE* package = param;
473
    MSIVERB *verb;
474
    LPCWSTR buffer;
475
    MSIEXTENSION *extension;
476 477

    buffer = MSI_RecordGetString(row,1);
478 479 480
    extension = load_given_extension( package, buffer );
    if (!extension)
    {
481
        ERR("Verb unable to find loaded extension %s\n", debugstr_w(buffer));
482 483 484 485
        return ERROR_SUCCESS;
    }

    /* fill in the data */
486

487
    verb = msi_alloc_zero( sizeof(MSIVERB) );
488 489 490
    if (!verb)
        return ERROR_OUTOFMEMORY;

491
    verb->Verb = msi_dup_record_field(row,2);
492 493
    TRACE("loading verb %s\n",debugstr_w(verb->Verb));
    verb->Sequence = MSI_RecordGetInteger(row,3);
494 495

    buffer = MSI_RecordGetString(row,4);
496
    deformat_string(package,buffer,&verb->Command);
497 498

    buffer = MSI_RecordGetString(row,5);
499
    deformat_string(package,buffer,&verb->Argument);
500

Austin English's avatar
Austin English committed
501
    /* associate the verb with the correct extension */
502
    list_add_tail( &extension->verbs, &verb->entry );
503 504 505 506 507 508
    
    return ERROR_SUCCESS;
}

static UINT iterate_all_classes(MSIRECORD *rec, LPVOID param)
{
509
    MSICOMPONENT *comp;
510 511 512
    LPCWSTR clsid;
    LPCWSTR context;
    LPCWSTR buffer;
513
    MSIPACKAGE* package = param;
514
    MSICLASS *cls;
515 516 517 518 519
    BOOL match = FALSE;

    clsid = MSI_RecordGetString(rec,1);
    context = MSI_RecordGetString(rec,2);
    buffer = MSI_RecordGetString(rec,3);
520
    comp = msi_get_loaded_component(package, buffer);
521

522
    LIST_FOR_EACH_ENTRY( cls, &package->classes, MSICLASS, entry )
523
    {
524
        if (strcmpiW( clsid, cls->clsid ))
525
            continue;
526
        if (strcmpW( context, cls->Context ))
527
            continue;
528
        if (comp == cls->Component)
529 530 531 532 533 534 535 536 537 538 539 540
        {
            match = TRUE;
            break;
        }
    }
    
    if (!match)
        load_class(package, rec);

    return ERROR_SUCCESS;
}

541
static UINT load_all_classes( MSIPACKAGE *package )
542
{
543 544
    static const WCHAR query[] = {
        'S','E','L','E','C','T',' ','*',' ', 'F','R','O','M',' ','`','C','l','a','s','s','`',0};
545
    MSIQUERY *view;
546
    UINT rc;
547

548
    rc = MSI_DatabaseOpenViewW(package->db, query, &view);
549
    if (rc != ERROR_SUCCESS)
550
        return ERROR_SUCCESS;
551 552 553

    rc = MSI_IterateRecords(view, NULL, iterate_all_classes, package);
    msiobj_release(&view->hdr);
554
    return rc;
555 556 557 558
}

static UINT iterate_all_extensions(MSIRECORD *rec, LPVOID param)
{
559
    MSICOMPONENT *comp;
560 561
    LPCWSTR buffer;
    LPCWSTR extension;
562
    MSIPACKAGE* package = param;
563
    BOOL match = FALSE;
564
    MSIEXTENSION *ext;
565 566 567

    extension = MSI_RecordGetString(rec,1);
    buffer = MSI_RecordGetString(rec,2);
568
    comp = msi_get_loaded_component(package, buffer);
569

570
    LIST_FOR_EACH_ENTRY( ext, &package->extensions, MSIEXTENSION, entry )
571
    {
572
        if (strcmpiW(extension, ext->Extension))
573
            continue;
574
        if (comp == ext->Component)
575 576 577 578 579 580 581 582 583 584 585 586
        {
            match = TRUE;
            break;
        }
    }

    if (!match)
        load_extension(package, rec);

    return ERROR_SUCCESS;
}

587
static UINT load_all_extensions( MSIPACKAGE *package )
588
{
589 590
    static const WCHAR query[] = {
        'S','E','L','E','C','T',' ','*',' ','F','R','O','M',' ','`','E','x','t','e','n','s','i','o','n','`',0};
591
    MSIQUERY *view;
592
    UINT rc;
593

594
    rc = MSI_DatabaseOpenViewW( package->db, query, &view );
595
    if (rc != ERROR_SUCCESS)
596
        return ERROR_SUCCESS;
597 598 599

    rc = MSI_IterateRecords(view, NULL, iterate_all_extensions, package);
    msiobj_release(&view->hdr);
600
    return rc;
601 602 603 604 605
}

static UINT iterate_all_progids(MSIRECORD *rec, LPVOID param)
{
    LPCWSTR buffer;
606
    MSIPACKAGE* package = param;
607 608 609 610 611 612

    buffer = MSI_RecordGetString(rec,1);
    load_given_progid(package,buffer);
    return ERROR_SUCCESS;
}

613
static UINT load_all_progids( MSIPACKAGE *package )
614
{
615 616 617
    static const WCHAR query[] = {
        'S','E','L','E','C','T',' ','`','P','r','o','g','I','d','`',' ','F','R','O','M',' ',
        '`','P','r','o','g','I','d','`',0};
618
    MSIQUERY *view;
619
    UINT rc;
620

621
    rc = MSI_DatabaseOpenViewW(package->db, query, &view);
622
    if (rc != ERROR_SUCCESS)
623
        return ERROR_SUCCESS;
624 625 626

    rc = MSI_IterateRecords(view, NULL, iterate_all_progids, package);
    msiobj_release(&view->hdr);
627
    return rc;
628 629
}

630
static UINT load_all_verbs( MSIPACKAGE *package )
631
{
632 633
    static const WCHAR query[] = {
        'S','E','L','E','C','T',' ','*',' ','F','R','O','M',' ','`','V','e','r','b','`',0};
634
    MSIQUERY *view;
635
    UINT rc;
636

637
    rc = MSI_DatabaseOpenViewW(package->db, query, &view);
638
    if (rc != ERROR_SUCCESS)
639
        return ERROR_SUCCESS;
640 641 642

    rc = MSI_IterateRecords(view, NULL, iterate_load_verb, package);
    msiobj_release(&view->hdr);
643
    return rc;
644 645 646 647 648
}

static UINT iterate_all_mimes(MSIRECORD *rec, LPVOID param)
{
    LPCWSTR buffer;
649
    MSIPACKAGE* package = param;
650 651 652 653 654 655

    buffer = MSI_RecordGetString(rec,1);
    load_given_mime(package,buffer);
    return ERROR_SUCCESS;
}

656
static UINT load_all_mimes( MSIPACKAGE *package )
657
{
658 659 660
    static const WCHAR query[] = {
        'S','E','L','E','C','T',' ','`','C','o','n','t','e','n','t','T','y','p','e','`',' ',
        'F','R','O','M',' ','`','M','I','M','E','`',0};
661
    MSIQUERY *view;
662
    UINT rc;
663

664
    rc = MSI_DatabaseOpenViewW(package->db, query, &view);
665
    if (rc != ERROR_SUCCESS)
666
        return ERROR_SUCCESS;
667 668 669

    rc = MSI_IterateRecords(view, NULL, iterate_all_mimes, package);
    msiobj_release(&view->hdr);
670
    return rc;
671 672
}

673
static UINT load_classes_and_such( MSIPACKAGE *package )
674
{
675 676
    UINT r;

677 678 679
    TRACE("Loading all the class info and related tables\n");

    /* check if already loaded */
680
    if (!list_empty( &package->classes ) ||
681 682
        !list_empty( &package->mimes ) ||
        !list_empty( &package->extensions ) ||
683 684 685 686 687 688 689 690 691 692
        !list_empty( &package->progids )) return ERROR_SUCCESS;

    r = load_all_classes( package );
    if (r != ERROR_SUCCESS) return r;

    r = load_all_extensions( package );
    if (r != ERROR_SUCCESS) return r;

    r = load_all_progids( package );
    if (r != ERROR_SUCCESS) return r;
693 694

    /* these loads must come after the other loads */
695 696 697 698
    r = load_all_verbs( package );
    if (r != ERROR_SUCCESS) return r;

    return load_all_mimes( package );
699 700
}

701
static UINT register_appid(const MSIAPPID *appid, LPCWSTR app )
702
{
703 704 705 706 707 708 709 710 711 712 713 714 715 716 717
    static const WCHAR szRemoteServerName[] =
         {'R','e','m','o','t','e','S','e','r','v','e','r','N','a','m','e',0};
    static const WCHAR szLocalService[] =
         {'L','o','c','a','l','S','e','r','v','i','c','e',0};
    static const WCHAR szService[] =
         {'S','e','r','v','i','c','e','P','a','r','a','m','e','t','e','r','s',0};
    static const WCHAR szDLL[] =
         {'D','l','l','S','u','r','r','o','g','a','t','e',0};
    static const WCHAR szActivate[] =
         {'A','c','t','i','v','a','t','e','A','s','S','t','o','r','a','g','e',0};
    static const WCHAR szY[] = {'Y',0};
    static const WCHAR szRunAs[] = {'R','u','n','A','s',0};
    static const WCHAR szUser[] = 
         {'I','n','t','e','r','a','c','t','i','v','e',' ','U','s','e','r',0};

718 719 720
    HKEY hkey2,hkey3;

    RegCreateKeyW(HKEY_CLASSES_ROOT,szAppID,&hkey2);
721
    RegCreateKeyW( hkey2, appid->AppID, &hkey3 );
722
    RegCloseKey(hkey2);
723
    msi_reg_set_val_str( hkey3, NULL, app );
724

725
    if (appid->RemoteServerName)
726
        msi_reg_set_val_str( hkey3, szRemoteServerName, appid->RemoteServerName );
727

728
    if (appid->LocalServer)
729
        msi_reg_set_val_str( hkey3, szLocalService, appid->LocalServer );
730

731
    if (appid->ServiceParameters)
732
        msi_reg_set_val_str( hkey3, szService, appid->ServiceParameters );
733

734
    if (appid->DllSurrogate)
735
        msi_reg_set_val_str( hkey3, szDLL, appid->DllSurrogate );
736

737
    if (appid->ActivateAtStorage)
738
        msi_reg_set_val_str( hkey3, szActivate, szY );
739

740
    if (appid->RunAsInteractiveUser)
741
        msi_reg_set_val_str( hkey3, szRunAs, szUser );
742 743 744 745 746 747 748 749

    RegCloseKey(hkey3);
    return ERROR_SUCCESS;
}

UINT ACTION_RegisterClassInfo(MSIPACKAGE *package)
{
    static const WCHAR szFileType_fmt[] = {'F','i','l','e','T','y','p','e','\\','%','s','\\','%','i',0};
750
    const WCHAR *keypath;
751
    MSIRECORD *uirow;
752
    HKEY hkey, hkey2, hkey3;
753
    MSICLASS *cls;
754
    UINT r;
755

756 757 758
    if (package->script == SCRIPT_NONE)
        return msi_schedule_action(package, SCRIPT_INSTALL, szRegisterClassInfo);

759 760 761
    r = load_classes_and_such( package );
    if (r != ERROR_SUCCESS)
        return r;
762 763 764 765 766 767 768

    if (is_64bit && package->platform == PLATFORM_INTEL)
        keypath = szWow6432NodeCLSID;
    else
        keypath = szCLSID;

    if (RegCreateKeyW(HKEY_CLASSES_ROOT, keypath, &hkey) != ERROR_SUCCESS)
769 770
        return ERROR_FUNCTION_FAILED;

771
    LIST_FOR_EACH_ENTRY( cls, &package->classes, MSICLASS, entry )
772
    {
773
        MSICOMPONENT *comp;
774
        MSIFILE *file;
775
        DWORD size;
776
        LPWSTR argument;
777
        MSIFEATURE *feature;
778

779
        comp = cls->Component;
780
        if ( !comp )
781 782
            continue;

783 784 785 786 787 788
        if (!comp->Enabled)
        {
            TRACE("component is disabled\n");
            continue;
        }

789
        feature = cls->Feature;
790 791
        if (!feature)
            continue;
792

793 794 795
        feature->Action = msi_get_feature_action( package, feature );
        if (feature->Action != INSTALLSTATE_LOCAL &&
            feature->Action != INSTALLSTATE_ADVERTISED )
796
        {
797
            TRACE("feature %s not scheduled for installation, skipping registration of class %s\n",
798
                  debugstr_w(feature->Feature), debugstr_w(cls->clsid));
799 800 801
            continue;
        }

802
        if (!comp->KeyPath || !(file = msi_get_loaded_file( package, comp->KeyPath )))
803 804 805 806
        {
            TRACE("COM server not provided, skipping class %s\n", debugstr_w(cls->clsid));
            continue;
        }
807
        TRACE("Registering class %s (%p)\n", debugstr_w(cls->clsid), cls);
808

809
        cls->action = INSTALLSTATE_LOCAL;
810

811
        RegCreateKeyW( hkey, cls->clsid, &hkey2 );
812

813
        if (cls->Description)
814
            msi_reg_set_val_str( hkey2, NULL, cls->Description );
815

816
        RegCreateKeyW( hkey2, cls->Context, &hkey3 );
817

818 819 820 821 822 823
        /*
         * FIXME: Implement install on demand (advertised components).
         *
         * ole32.dll should call msi.MsiProvideComponentFromDescriptor()
         *  when it needs an InProcServer that doesn't exist.
         * The component advertise string should be in the "InProcServer" value.
824
         */
825 826 827
        size = lstrlenW( file->TargetPath )+1;
        if (cls->Argument)
            size += lstrlenW(cls->Argument)+1;
828

829 830
        argument = msi_alloc( size * sizeof(WCHAR) );
        lstrcpyW( argument, file->TargetPath );
831

832
        if (cls->Argument)
833
        {
834 835
            lstrcatW( argument, szSpace );
            lstrcatW( argument, cls->Argument );
836 837
        }

838 839 840
        msi_reg_set_val_str( hkey3, NULL, argument );
        msi_free(argument);

841 842
        RegCloseKey(hkey3);

843
        if (cls->ProgID || cls->ProgIDText)
844 845 846
        {
            LPCWSTR progid;

847 848
            if (cls->ProgID)
                progid = cls->ProgID->ProgID;
849
            else
850
                progid = cls->ProgIDText;
851

852
            msi_reg_set_subkey_val( hkey2, szProgID, NULL, progid );
853

854
            if (cls->ProgID && cls->ProgID->VersionInd)
855
            {
856 857
                msi_reg_set_subkey_val( hkey2, szVIProgID, NULL, 
                                        cls->ProgID->VersionInd->ProgID );
858 859 860
            }
        }

861
        if (cls->AppID)
862
        {
863
            MSIAPPID *appid = cls->AppID;
864
            msi_reg_set_val_str( hkey2, szAppID, appid->AppID );
865
            register_appid( appid, cls->Description );
866 867
        }

868
        if (cls->IconPath)
869
            msi_reg_set_subkey_val( hkey2, szDefaultIcon, NULL, cls->IconPath );
870

871
        if (cls->DefInprocHandler)
872
            msi_reg_set_subkey_val( hkey2, szInprocHandler, NULL, cls->DefInprocHandler );
873

874
        if (cls->DefInprocHandler32)
875
            msi_reg_set_subkey_val( hkey2, szInprocHandler32, NULL, cls->DefInprocHandler32 );
876 877 878 879
        
        RegCloseKey(hkey2);

        /* if there is a FileTypeMask, register the FileType */
880
        if (cls->FileTypeMask)
881 882 883 884
        {
            LPWSTR ptr, ptr2;
            LPWSTR keyname;
            INT index = 0;
885
            ptr = cls->FileTypeMask;
886 887 888 889 890
            while (ptr && *ptr)
            {
                ptr2 = strchrW(ptr,';');
                if (ptr2)
                    *ptr2 = 0;
891
                keyname = msi_alloc( (strlenW(szFileType_fmt) + strlenW(cls->clsid) + 4) * sizeof(WCHAR));
892
                sprintfW( keyname, szFileType_fmt, cls->clsid, index );
893

894
                msi_reg_set_subkey_val( HKEY_CLASSES_ROOT, keyname, NULL, ptr );
895
                msi_free(keyname);
896 897 898 899 900 901 902 903 904 905 906

                if (ptr2)
                    ptr = ptr2+1;
                else
                    ptr = NULL;

                index ++;
            }
        }
        
        uirow = MSI_CreateRecord(1);
907
        MSI_RecordSetStringW( uirow, 1, cls->clsid );
908
        MSI_ProcessMessage(package, INSTALLMESSAGE_ACTIONDATA, uirow);
909 910 911
        msiobj_release(&uirow->hdr);
    }
    RegCloseKey(hkey);
912 913 914 915 916 917
    return ERROR_SUCCESS;
}

UINT ACTION_UnregisterClassInfo( MSIPACKAGE *package )
{
    static const WCHAR szFileType[] = {'F','i','l','e','T','y','p','e','\\',0};
918
    const WCHAR *keypath;
919 920 921
    MSIRECORD *uirow;
    MSICLASS *cls;
    HKEY hkey, hkey2;
922
    UINT r;
923

924 925 926
    if (package->script == SCRIPT_NONE)
        return msi_schedule_action(package, SCRIPT_INSTALL, szUnregisterClassInfo);

927 928 929
    r = load_classes_and_such( package );
    if (r != ERROR_SUCCESS)
        return r;
930 931 932 933 934 935 936

    if (is_64bit && package->platform == PLATFORM_INTEL)
        keypath = szWow6432NodeCLSID;
    else
        keypath = szCLSID;

    if (RegOpenKeyW( HKEY_CLASSES_ROOT, keypath, &hkey ) != ERROR_SUCCESS)
937 938 939 940 941 942 943 944 945 946 947 948 949
        return ERROR_SUCCESS;

    LIST_FOR_EACH_ENTRY( cls, &package->classes, MSICLASS, entry )
    {
        MSIFEATURE *feature;
        MSICOMPONENT *comp;
        LPWSTR filetype;
        LONG res;

        comp = cls->Component;
        if (!comp)
            continue;

950 951 952 953 954 955
        if (!comp->Enabled)
        {
            TRACE("component is disabled\n");
            continue;
        }

956 957 958 959
        feature = cls->Feature;
        if (!feature)
            continue;

960 961
        feature->Action = msi_get_feature_action( package, feature );
        if (feature->Action != INSTALLSTATE_ABSENT)
962
        {
963
            TRACE("feature %s not scheduled for removal, skipping unregistration of class %s\n",
964 965 966 967 968
                  debugstr_w(feature->Feature), debugstr_w(cls->clsid));
            continue;
        }
        TRACE("Unregistering class %s (%p)\n", debugstr_w(cls->clsid), cls);

969
        cls->action = INSTALLSTATE_ABSENT;
970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002

        res = RegDeleteTreeW( hkey, cls->clsid );
        if (res != ERROR_SUCCESS)
            WARN("Failed to delete class key %d\n", res);

        if (cls->AppID)
        {
            res = RegOpenKeyW( HKEY_CLASSES_ROOT, szAppID, &hkey2 );
            if (res == ERROR_SUCCESS)
            {
                res = RegDeleteKeyW( hkey2, cls->AppID->AppID );
                if (res != ERROR_SUCCESS)
                    WARN("Failed to delete appid key %d\n", res);
                RegCloseKey( hkey2 );
            }
        }
        if (cls->FileTypeMask)
        {
            filetype = msi_alloc( (strlenW( szFileType ) + strlenW( cls->clsid ) + 1) * sizeof(WCHAR) );
            if (filetype)
            {
                strcpyW( filetype, szFileType );
                strcatW( filetype, cls->clsid );
                res = RegDeleteTreeW( HKEY_CLASSES_ROOT, filetype );
                msi_free( filetype );

                if (res != ERROR_SUCCESS)
                    WARN("Failed to delete file type %d\n", res);
            }
        }

        uirow = MSI_CreateRecord( 1 );
        MSI_RecordSetStringW( uirow, 1, cls->clsid );
1003
        MSI_ProcessMessage(package, INSTALLMESSAGE_ACTIONDATA, uirow);
1004 1005 1006 1007
        msiobj_release( &uirow->hdr );
    }
    RegCloseKey( hkey );
    return ERROR_SUCCESS;
1008 1009
}

1010
static LPCWSTR get_clsid_of_progid( const MSIPROGID *progid )
1011
{
1012
    while (progid)
1013
    {
1014 1015
        if (progid->Class)
            return progid->Class->clsid;
1016 1017
        if (progid->Parent == progid)
            break;
1018
        progid = progid->Parent;
1019
    }
1020
    return NULL;
1021 1022
}

1023
static UINT register_progid( const MSIPROGID* progid )
1024
{
1025
    static const WCHAR szCurVer[] = {'C','u','r','V','e','r',0};
1026 1027
    HKEY hkey = 0;
    UINT rc;
1028

1029 1030
    rc = RegCreateKeyW( HKEY_CLASSES_ROOT, progid->ProgID, &hkey );
    if (rc == ERROR_SUCCESS)
1031
    {
1032
        LPCWSTR clsid = get_clsid_of_progid( progid );
1033

1034 1035 1036
        if (clsid)
            msi_reg_set_subkey_val( hkey, szCLSID, NULL, clsid );
        else
1037
            TRACE("%s has no class\n", debugstr_w( progid->ProgID ) );
1038 1039

        if (progid->Description)
1040
            msi_reg_set_val_str( hkey, NULL, progid->Description );
1041 1042

        if (progid->IconPath)
1043
            msi_reg_set_subkey_val( hkey, szDefaultIcon, NULL, progid->IconPath );
1044 1045

        /* write out the current version */
1046
        if (progid->CurVer)
1047
            msi_reg_set_subkey_val( hkey, szCurVer, NULL, progid->CurVer->ProgID );
1048 1049 1050

        RegCloseKey(hkey);
    }
1051 1052 1053
    else
        ERR("failed to create key %s\n", debugstr_w( progid->ProgID ) );

1054 1055 1056
    return rc;
}

1057 1058 1059 1060 1061 1062
static const MSICLASS *get_progid_class( const MSIPROGID *progid )
{
    while (progid)
    {
        if (progid->Parent) progid = progid->Parent;
        if (progid->Class) return progid->Class;
Vincent Povirk's avatar
Vincent Povirk committed
1063
        if (!progid->Parent || progid->Parent == progid) break;
1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085
    }
    return NULL;
}

static BOOL has_class_installed( const MSIPROGID *progid )
{
    const MSICLASS *class = get_progid_class( progid );
    if (!class || !class->ProgID) return FALSE;
    return (class->action == INSTALLSTATE_LOCAL);
}

static BOOL has_one_extension_installed( const MSIPACKAGE *package, const MSIPROGID *progid )
{
    const MSIEXTENSION *extension;
    LIST_FOR_EACH_ENTRY( extension, &package->extensions, MSIEXTENSION, entry )
    {
        if (extension->ProgID == progid && !list_empty( &extension->verbs ) &&
            extension->action == INSTALLSTATE_LOCAL) return TRUE;
    }
    return FALSE;
}

1086 1087
UINT ACTION_RegisterProgIdInfo(MSIPACKAGE *package)
{
1088
    MSIPROGID *progid;
1089
    MSIRECORD *uirow;
1090
    UINT r;
1091

1092 1093 1094
    if (package->script == SCRIPT_NONE)
        return msi_schedule_action(package, SCRIPT_INSTALL, szRegisterProgIdInfo);

1095 1096 1097
    r = load_classes_and_such( package );
    if (r != ERROR_SUCCESS)
        return r;
1098

1099
    LIST_FOR_EACH_ENTRY( progid, &package->progids, MSIPROGID, entry )
1100
    {
1101
        if (!has_class_installed( progid ) && !has_one_extension_installed( package, progid ))
1102
        {
1103
            TRACE("progid %s not scheduled to be installed\n", debugstr_w(progid->ProgID));
1104 1105
            continue;
        }
1106
        TRACE("Registering progid %s\n", debugstr_w(progid->ProgID));
1107

1108
        register_progid( progid );
1109

1110
        uirow = MSI_CreateRecord( 1 );
1111
        MSI_RecordSetStringW( uirow, 1, progid->ProgID );
1112
        MSI_ProcessMessage(package, INSTALLMESSAGE_ACTIONDATA, uirow);
1113
        msiobj_release( &uirow->hdr );
1114 1115 1116 1117
    }
    return ERROR_SUCCESS;
}

1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147
static BOOL has_class_removed( const MSIPROGID *progid )
{
    const MSICLASS *class = get_progid_class( progid );
    if (!class || !class->ProgID) return FALSE;
    return (class->action == INSTALLSTATE_ABSENT);
}

static BOOL has_extensions( const MSIPACKAGE *package, const MSIPROGID *progid )
{
    const MSIEXTENSION *extension;
    LIST_FOR_EACH_ENTRY( extension, &package->extensions, MSIEXTENSION, entry )
    {
        if (extension->ProgID == progid && !list_empty( &extension->verbs )) return TRUE;
    }
    return FALSE;
}

static BOOL has_all_extensions_removed( const MSIPACKAGE *package, const MSIPROGID *progid )
{
    BOOL ret = FALSE;
    const MSIEXTENSION *extension;
    LIST_FOR_EACH_ENTRY( extension, &package->extensions, MSIEXTENSION, entry )
    {
        if (extension->ProgID == progid && !list_empty( &extension->verbs ) &&
            extension->action == INSTALLSTATE_ABSENT) ret = TRUE;
        else ret = FALSE;
    }
    return ret;
}

1148 1149 1150 1151 1152
UINT ACTION_UnregisterProgIdInfo( MSIPACKAGE *package )
{
    MSIPROGID *progid;
    MSIRECORD *uirow;
    LONG res;
1153
    UINT r;
1154

1155 1156 1157
    if (package->script == SCRIPT_NONE)
        return msi_schedule_action(package, SCRIPT_INSTALL, szUnregisterProgIdInfo);

1158 1159 1160
    r = load_classes_and_such( package );
    if (r != ERROR_SUCCESS)
        return r;
1161 1162 1163

    LIST_FOR_EACH_ENTRY( progid, &package->progids, MSIPROGID, entry )
    {
1164 1165
        if (!has_class_removed( progid ) ||
            (has_extensions( package, progid ) && !has_all_extensions_removed( package, progid )))
1166 1167 1168 1169 1170 1171 1172 1173
        {
            TRACE("progid %s not scheduled to be removed\n", debugstr_w(progid->ProgID));
            continue;
        }
        TRACE("Unregistering progid %s\n", debugstr_w(progid->ProgID));

        res = RegDeleteTreeW( HKEY_CLASSES_ROOT, progid->ProgID );
        if (res != ERROR_SUCCESS)
1174
            TRACE("Failed to delete progid key %d\n", res);
1175 1176 1177

        uirow = MSI_CreateRecord( 1 );
        MSI_RecordSetStringW( uirow, 1, progid->ProgID );
1178
        MSI_ProcessMessage(package, INSTALLMESSAGE_ACTIONDATA, uirow);
1179 1180 1181 1182 1183
        msiobj_release( &uirow->hdr );
    }
    return ERROR_SUCCESS;
}

1184
static UINT register_verb(MSIPACKAGE *package, LPCWSTR progid, 
1185
                MSICOMPONENT* component, const MSIEXTENSION* extension,
1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197
                MSIVERB* verb, INT* Sequence )
{
    LPWSTR keyname;
    HKEY key;
    static const WCHAR szShell[] = {'s','h','e','l','l',0};
    static const WCHAR szCommand[] = {'c','o','m','m','a','n','d',0};
    static const WCHAR fmt[] = {'\"','%','s','\"',' ','%','s',0};
    static const WCHAR fmt2[] = {'\"','%','s','\"',0};
    LPWSTR command;
    DWORD size;
    LPWSTR advertise;

1198
    keyname = msi_build_directory_name(4, progid, szShell, verb->Verb, szCommand);
1199 1200 1201 1202 1203 1204 1205 1206

    TRACE("Making Key %s\n",debugstr_w(keyname));
    RegCreateKeyW(HKEY_CLASSES_ROOT, keyname, &key);
    size = strlenW(component->FullKeypath);
    if (verb->Argument)
        size += strlenW(verb->Argument);
     size += 4;

1207
     command = msi_alloc(size * sizeof (WCHAR));
1208 1209 1210 1211 1212
     if (verb->Argument)
        sprintfW(command, fmt, component->FullKeypath, verb->Argument);
     else
        sprintfW(command, fmt2, component->FullKeypath);

1213
     msi_reg_set_val_str( key, NULL, command );
1214
     msi_free(command);
1215

1216 1217
     advertise = msi_create_component_advertise_string(package, component,
                                                       extension->Feature->Feature);
1218 1219 1220
     size = strlenW(advertise);

     if (verb->Argument)
1221
         size += strlenW(verb->Argument);
1222 1223
     size += 4;

1224
     command = msi_alloc_zero(size * sizeof (WCHAR));
1225 1226 1227 1228 1229 1230 1231 1232

     strcpyW(command,advertise);
     if (verb->Argument)
     {
         strcatW(command,szSpace);
         strcatW(command,verb->Argument);
     }

1233
     msi_reg_set_val_multi_str( key, szCommand, command );
1234 1235
     
     RegCloseKey(key);
1236 1237 1238
     msi_free(keyname);
     msi_free(advertise);
     msi_free(command);
1239 1240 1241

     if (verb->Command)
     {
1242
        keyname = msi_build_directory_name( 3, progid, szShell, verb->Verb );
1243
        msi_reg_set_subkey_val( HKEY_CLASSES_ROOT, keyname, NULL, verb->Command );
1244
        msi_free(keyname);
1245 1246 1247 1248 1249 1250 1251
     }

     if (verb->Sequence != MSI_NULL_INTEGER)
     {
        if (*Sequence == MSI_NULL_INTEGER || verb->Sequence < *Sequence)
        {
            *Sequence = verb->Sequence;
1252
            keyname = msi_build_directory_name( 2, progid, szShell );
1253
            msi_reg_set_subkey_val( HKEY_CLASSES_ROOT, keyname, NULL, verb->Verb );
1254
            msi_free(keyname);
1255 1256 1257 1258 1259 1260 1261
        }
    }
    return ERROR_SUCCESS;
}

UINT ACTION_RegisterExtensionInfo(MSIPACKAGE *package)
{
1262
    static const WCHAR szContentType[] = {'C','o','n','t','e','n','t',' ','T','y','p','e',0};
1263
    HKEY hkey = NULL;
1264
    MSIEXTENSION *ext;
1265 1266
    MSIRECORD *uirow;
    BOOL install_on_demand = TRUE;
1267
    LONG res;
1268
    UINT r;
1269

1270 1271 1272
    if (package->script == SCRIPT_NONE)
        return msi_schedule_action(package, SCRIPT_INSTALL, szRegisterExtensionInfo);

1273 1274 1275
    r = load_classes_and_such( package );
    if (r != ERROR_SUCCESS)
        return r;
1276 1277 1278 1279 1280 1281

    /* We need to set install_on_demand based on if the shell handles advertised
     * shortcuts and the like. Because Mike McCormack is working on this i am
     * going to default to TRUE
     */
    
1282
    LIST_FOR_EACH_ENTRY( ext, &package->extensions, MSIEXTENSION, entry )
1283
    {
1284
        LPWSTR extension;
1285
        MSIFEATURE *feature;
1286
     
1287
        if (!ext->Component)
1288 1289
            continue;

1290 1291 1292 1293 1294 1295
        if (!ext->Component->Enabled)
        {
            TRACE("component is disabled\n");
            continue;
        }

1296
        feature = ext->Feature;
1297 1298
        if (!feature)
            continue;
1299

1300 1301 1302 1303
        /* 
         * yes. MSDN says that these are based on _Feature_ not on
         * Component.  So verify the feature is to be installed
         */
1304 1305 1306
        feature->Action = msi_get_feature_action( package, feature );
        if (feature->Action != INSTALLSTATE_LOCAL &&
            !(install_on_demand && feature->Action == INSTALLSTATE_ADVERTISED))
1307
        {
1308 1309
            TRACE("feature %s not scheduled for installation, skipping registration of extension %s\n",
                  debugstr_w(feature->Feature), debugstr_w(ext->Extension));
1310 1311
            continue;
        }
1312
        TRACE("Registering extension %s (%p)\n", debugstr_w(ext->Extension), ext);
1313

1314
        ext->action = INSTALLSTATE_LOCAL;
1315

1316 1317 1318 1319 1320 1321 1322 1323 1324 1325
        extension = msi_alloc( (strlenW( ext->Extension ) + 2) * sizeof(WCHAR) );
        if (extension)
        {
            extension[0] = '.';
            strcpyW( extension + 1, ext->Extension );
            res = RegCreateKeyW( HKEY_CLASSES_ROOT, extension, &hkey );
            msi_free( extension );
            if (res != ERROR_SUCCESS)
                WARN("Failed to create extension key %d\n", res);
        }
1326

1327
        if (ext->Mime)
1328
            msi_reg_set_val_str( hkey, szContentType, ext->Mime->ContentType );
1329

1330
        if (ext->ProgID || ext->ProgIDText)
1331 1332 1333 1334 1335 1336
        {
            static const WCHAR szSN[] = 
                {'\\','S','h','e','l','l','N','e','w',0};
            HKEY hkey2;
            LPWSTR newkey;
            LPCWSTR progid;
1337
            MSIVERB *verb;
1338 1339
            INT Sequence = MSI_NULL_INTEGER;
            
1340 1341
            if (ext->ProgID)
                progid = ext->ProgID->ProgID;
1342
            else
1343
                progid = ext->ProgIDText;
1344

1345
            msi_reg_set_val_str( hkey, NULL, progid );
1346

1347
            newkey = msi_alloc( (strlenW(progid)+strlenW(szSN)+1) * sizeof(WCHAR)); 
1348 1349 1350 1351 1352 1353

            strcpyW(newkey,progid);
            strcatW(newkey,szSN);
            RegCreateKeyW(hkey,newkey,&hkey2);
            RegCloseKey(hkey2);

1354
            msi_free(newkey);
1355 1356

            /* do all the verbs */
1357 1358
            LIST_FOR_EACH_ENTRY( verb, &ext->verbs, MSIVERB, entry )
            {
1359
                register_verb( package, progid, ext->Component,
1360 1361
                               ext, verb, &Sequence);
            }
1362 1363 1364 1365 1366
        }
        
        RegCloseKey(hkey);

        uirow = MSI_CreateRecord(1);
1367
        MSI_RecordSetStringW( uirow, 1, ext->Extension );
1368
        MSI_ProcessMessage(package, INSTALLMESSAGE_ACTIONDATA, uirow);
1369 1370 1371 1372 1373
        msiobj_release(&uirow->hdr);
    }
    return ERROR_SUCCESS;
}

1374 1375 1376 1377 1378
UINT ACTION_UnregisterExtensionInfo( MSIPACKAGE *package )
{
    MSIEXTENSION *ext;
    MSIRECORD *uirow;
    LONG res;
1379
    UINT r;
1380

1381 1382 1383
    if (package->script == SCRIPT_NONE)
        return msi_schedule_action(package, SCRIPT_INSTALL, szUnregisterExtensionInfo);

1384 1385 1386
    r = load_classes_and_such( package );
    if (r != ERROR_SUCCESS)
        return r;
1387 1388 1389 1390 1391 1392 1393 1394 1395

    LIST_FOR_EACH_ENTRY( ext, &package->extensions, MSIEXTENSION, entry )
    {
        LPWSTR extension;
        MSIFEATURE *feature;

        if (!ext->Component)
            continue;

1396 1397 1398 1399 1400 1401
        if (!ext->Component->Enabled)
        {
            TRACE("component is disabled\n");
            continue;
        }

1402 1403 1404 1405
        feature = ext->Feature;
        if (!feature)
            continue;

1406 1407
        feature->Action = msi_get_feature_action( package, feature );
        if (feature->Action != INSTALLSTATE_ABSENT)
1408
        {
1409 1410
            TRACE("feature %s not scheduled for removal, skipping unregistration of extension %s\n",
                  debugstr_w(feature->Feature), debugstr_w(ext->Extension));
1411 1412 1413 1414
            continue;
        }
        TRACE("Unregistering extension %s\n", debugstr_w(ext->Extension));

1415
        ext->action = INSTALLSTATE_ABSENT;
1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453

        extension = msi_alloc( (strlenW( ext->Extension ) + 2) * sizeof(WCHAR) );
        if (extension)
        {
            extension[0] = '.';
            strcpyW( extension + 1, ext->Extension );
            res = RegDeleteTreeW( HKEY_CLASSES_ROOT, extension );
            msi_free( extension );
            if (res != ERROR_SUCCESS)
                WARN("Failed to delete extension key %d\n", res);
        }

        if (ext->ProgID || ext->ProgIDText)
        {
            static const WCHAR shellW[] = {'\\','s','h','e','l','l',0};
            LPCWSTR progid;
            LPWSTR progid_shell;

            if (ext->ProgID)
                progid = ext->ProgID->ProgID;
            else
                progid = ext->ProgIDText;

            progid_shell = msi_alloc( (strlenW( progid ) + strlenW( shellW ) + 1) * sizeof(WCHAR) );
            if (progid_shell)
            {
                strcpyW( progid_shell, progid );
                strcatW( progid_shell, shellW );
                res = RegDeleteTreeW( HKEY_CLASSES_ROOT, progid_shell );
                msi_free( progid_shell );
                if (res != ERROR_SUCCESS)
                    WARN("Failed to delete shell key %d\n", res);
                RegDeleteKeyW( HKEY_CLASSES_ROOT, progid );
            }
        }

        uirow = MSI_CreateRecord( 1 );
        MSI_RecordSetStringW( uirow, 1, ext->Extension );
1454
        MSI_ProcessMessage(package, INSTALLMESSAGE_ACTIONDATA, uirow);
1455 1456 1457 1458 1459
        msiobj_release( &uirow->hdr );
    }
    return ERROR_SUCCESS;
}

1460 1461
UINT ACTION_RegisterMIMEInfo(MSIPACKAGE *package)
{
1462
    static const WCHAR szExtension[] = {'E','x','t','e','n','s','i','o','n',0};
1463
    MSIRECORD *uirow;
1464
    MSIMIME *mt;
1465
    UINT r;
1466

1467 1468 1469
    if (package->script == SCRIPT_NONE)
        return msi_schedule_action(package, SCRIPT_INSTALL, szRegisterMIMEInfo);

1470 1471 1472
    r = load_classes_and_such( package );
    if (r != ERROR_SUCCESS)
        return r;
1473

1474
    LIST_FOR_EACH_ENTRY( mt, &package->mimes, MSIMIME, entry )
1475
    {
1476
        LPWSTR extension = NULL, key;
1477 1478

        /* 
Austin English's avatar
Austin English committed
1479
         * check if the MIME is to be installed. Either as requested by an
1480 1481
         * extension or Class
         */
1482
        if ((!mt->Class || mt->Class->action != INSTALLSTATE_LOCAL) &&
1483
            (!mt->Extension || mt->Extension->action != INSTALLSTATE_LOCAL))
1484
        {
1485
            TRACE("MIME %s not scheduled to be installed\n", debugstr_w(mt->ContentType));
1486 1487
            continue;
        }
1488

1489
        TRACE("Registering MIME type %s\n", debugstr_w(mt->ContentType));
1490

1491
        if (mt->Extension) extension = msi_alloc( (strlenW( mt->Extension->Extension ) + 2) * sizeof(WCHAR) );
1492
        key = msi_alloc( (strlenW( mt->ContentType ) + strlenW( szMIMEDatabase ) + 1) * sizeof(WCHAR) );
1493

1494 1495 1496 1497
        if (extension && key)
        {
            extension[0] = '.';
            strcpyW( extension + 1, mt->Extension->Extension );
1498

1499 1500
            strcpyW( key, szMIMEDatabase );
            strcatW( key, mt->ContentType );
1501
            msi_reg_set_subkey_val( HKEY_CLASSES_ROOT, key, szExtension, extension );
1502

1503 1504 1505 1506 1507 1508 1509 1510
            if (mt->clsid)
                msi_reg_set_subkey_val( HKEY_CLASSES_ROOT, key, szCLSID, mt->clsid );
        }
        msi_free( extension );
        msi_free( key );

        uirow = MSI_CreateRecord( 2 );
        MSI_RecordSetStringW( uirow, 1, mt->ContentType );
1511
        MSI_RecordSetStringW( uirow, 2, mt->suffix );
1512
        MSI_ProcessMessage(package, INSTALLMESSAGE_ACTIONDATA, uirow);
1513 1514 1515 1516 1517 1518 1519 1520 1521
        msiobj_release( &uirow->hdr );
    }
    return ERROR_SUCCESS;
}

UINT ACTION_UnregisterMIMEInfo( MSIPACKAGE *package )
{
    MSIRECORD *uirow;
    MSIMIME *mime;
1522
    UINT r;
1523

1524 1525 1526
    if (package->script == SCRIPT_NONE)
        return msi_schedule_action(package, SCRIPT_INSTALL, szUnregisterMIMEInfo);

1527 1528 1529
    r = load_classes_and_such( package );
    if (r != ERROR_SUCCESS)
        return r;
1530 1531 1532 1533 1534 1535

    LIST_FOR_EACH_ENTRY( mime, &package->mimes, MSIMIME, entry )
    {
        LONG res;
        LPWSTR mime_key;

1536
        if ((!mime->Class || mime->Class->action != INSTALLSTATE_ABSENT) &&
1537
            (!mime->Extension || mime->Extension->action != INSTALLSTATE_ABSENT))
1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557
        {
            TRACE("MIME %s not scheduled to be removed\n", debugstr_w(mime->ContentType));
            continue;
        }

        TRACE("Unregistering MIME type %s\n", debugstr_w(mime->ContentType));

        mime_key = msi_alloc( (strlenW( szMIMEDatabase ) + strlenW( mime->ContentType ) + 1) * sizeof(WCHAR) );
        if (mime_key)
        {
            strcpyW( mime_key, szMIMEDatabase );
            strcatW( mime_key, mime->ContentType );
            res = RegDeleteKeyW( HKEY_CLASSES_ROOT, mime_key );
            if (res != ERROR_SUCCESS)
                WARN("Failed to delete MIME key %d\n", res);
            msi_free( mime_key );
        }

        uirow = MSI_CreateRecord( 2 );
        MSI_RecordSetStringW( uirow, 1, mime->ContentType );
1558
        MSI_RecordSetStringW( uirow, 2, mime->suffix );
1559
        MSI_ProcessMessage(package, INSTALLMESSAGE_ACTIONDATA, uirow);
1560
        msiobj_release( &uirow->hdr );
1561 1562 1563
    }
    return ERROR_SUCCESS;
}