user32: Move EnumProps16 to wnd16.c.
[wine] / dlls / msi / classes.c
1 /*
2  * Implementation of the Microsoft Installer (msi.dll)
3  *
4  * Copyright 2005 Aric Stewart for CodeWeavers
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * 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 /* actions handled in this module
22  * RegisterClassInfo
23  * RegisterProgIdInfo
24  * RegisterExtensionInfo
25  * RegisterMIMEInfo
26  * UnRegisterClassInfo (TODO)
27  * UnRegisterProgIdInfo (TODO)
28  * UnRegisterExtensionInfo (TODO)
29  * UnRegisterMIMEInfo (TODO)
30  */
31
32 #include <stdarg.h>
33
34 #include "windef.h"
35 #include "winbase.h"
36 #include "winerror.h"
37 #include "winreg.h"
38 #include "wine/debug.h"
39 #include "msipriv.h"
40 #include "winuser.h"
41 #include "wine/unicode.h"
42
43 WINE_DEFAULT_DEBUG_CHANNEL(msi);
44
45 static MSIAPPID *load_appid( MSIPACKAGE* package, MSIRECORD *row )
46 {
47     LPCWSTR buffer;
48     MSIAPPID *appid;
49
50     /* fill in the data */
51
52     appid = msi_alloc_zero( sizeof(MSIAPPID) );
53     if (!appid)
54         return NULL;
55     
56     appid->AppID = msi_dup_record_field( row, 1 );
57     TRACE("loading appid %s\n", debugstr_w( appid->AppID ));
58
59     buffer = MSI_RecordGetString(row,2);
60     deformat_string( package, buffer, &appid->RemoteServerName );
61
62     appid->LocalServer = msi_dup_record_field(row,3);
63     appid->ServiceParameters = msi_dup_record_field(row,4);
64     appid->DllSurrogate = msi_dup_record_field(row,5);
65
66     appid->ActivateAtStorage = !MSI_RecordIsNull(row,6);
67     appid->RunAsInteractiveUser = !MSI_RecordIsNull(row,7);
68
69     list_add_tail( &package->appids, &appid->entry );
70     
71     return appid;
72 }
73
74 static MSIAPPID *load_given_appid( MSIPACKAGE *package, LPCWSTR name )
75 {
76     MSIRECORD *row;
77     MSIAPPID *appid;
78     static const WCHAR ExecSeqQuery[] =
79         {'S','E','L','E','C','T',' ','*',' ','F','R','O','M',' ',
80          '`','A','p','p','I','d','`',' ','W','H','E','R','E',' ',
81          '`','A','p','p','I','d','`',' ','=',' ','\'','%','s','\'',0};
82
83     if (!name)
84         return NULL;
85
86     /* check for appids already loaded */
87     LIST_FOR_EACH_ENTRY( appid, &package->appids, MSIAPPID, entry )
88     {
89         if (lstrcmpiW( appid->AppID, name )==0)
90         {
91             TRACE("found appid %s %p\n", debugstr_w(name), appid);
92             return appid;
93         }
94     }
95     
96     row = MSI_QueryGetRecord(package->db, ExecSeqQuery, name);
97     if (!row)
98         return NULL;
99
100     appid = load_appid(package, row);
101     msiobj_release(&row->hdr);
102
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     LPCWSTR buffer;
113
114     /* fill in the data */
115
116     progid = msi_alloc_zero( sizeof(MSIPROGID) );
117     if (!progid)
118         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     progid->Parent = load_given_progid(package,buffer);
127     if (progid->Parent == NULL && buffer)
128         FIXME("Unknown parent ProgID %s\n",debugstr_w(buffer));
129
130     buffer = MSI_RecordGetString(row,3);
131     progid->Class = load_given_class(package,buffer);
132     if (progid->Class == NULL && buffer)
133         FIXME("Unknown class %s\n",debugstr_w(buffer));
134
135     progid->Description = msi_dup_record_field(row,4);
136
137     if (!MSI_RecordIsNull(row,6))
138     {
139         INT icon_index = MSI_RecordGetInteger(row,6); 
140         LPCWSTR FileName = MSI_RecordGetString(row,5);
141         LPWSTR FilePath;
142         static const WCHAR fmt[] = {'%','s',',','%','i',0};
143
144         FilePath = 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     else
153     {
154         buffer = MSI_RecordGetString(row,5);
155         if (buffer)
156             progid->IconPath = build_icon_path(package,buffer);
157     }
158
159     progid->CurVer = NULL;
160     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         MSIPROGID *parent = progid->Parent;
166
167         while (parent->Parent && parent->Parent != parent)
168             parent = parent->Parent;
169
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     MSIPROGID *progid;
182     MSIRECORD *row;
183     static const WCHAR ExecSeqQuery[] =
184         {'S','E','L','E','C','T',' ','*',' ','F','R','O','M',' ',
185          '`','P','r','o','g','I','d','`',' ','W','H','E','R','E',' ',
186          '`','P','r','o','g','I','d','`',' ','=',' ','\'','%','s','\'',0};
187
188     if (!name)
189         return NULL;
190
191     /* check for progids already loaded */
192     LIST_FOR_EACH_ENTRY( progid, &package->progids, MSIPROGID, entry )
193     {
194         if (strcmpiW( progid->ProgID,name )==0)
195         {
196             TRACE("found progid %s (%p)\n",debugstr_w(name), progid );
197             return progid;
198         }
199     }
200     
201     row = MSI_QueryGetRecord( package->db, ExecSeqQuery, name );
202     if (!row)
203         return NULL;
204
205     progid = load_progid(package, row);
206     msiobj_release(&row->hdr);
207
208     return progid;
209 }
210
211 static MSICLASS *load_class( MSIPACKAGE* package, MSIRECORD *row )
212 {
213     MSICLASS *cls;
214     DWORD i;
215     LPCWSTR buffer;
216
217     /* fill in the data */
218
219     cls = msi_alloc_zero( sizeof(MSICLASS) );
220     if (!cls)
221         return NULL;
222
223     list_add_tail( &package->classes, &cls->entry );
224
225     cls->clsid = msi_dup_record_field( row, 1 );
226     TRACE("loading class %s\n",debugstr_w(cls->clsid));
227     cls->Context = msi_dup_record_field( row, 2 );
228     buffer = MSI_RecordGetString(row,3);
229     cls->Component = get_loaded_component(package, buffer);
230
231     cls->ProgIDText = msi_dup_record_field(row,4);
232     cls->ProgID = load_given_progid(package, cls->ProgIDText);
233
234     cls->Description = msi_dup_record_field(row,5);
235
236     buffer = MSI_RecordGetString(row,6);
237     if (buffer)
238         cls->AppID = load_given_appid(package, buffer);
239
240     cls->FileTypeMask = msi_dup_record_field(row,7);
241
242     if (!MSI_RecordIsNull(row,9))
243     {
244
245         INT icon_index = MSI_RecordGetInteger(row,9); 
246         LPCWSTR FileName = MSI_RecordGetString(row,8);
247         LPWSTR FilePath;
248         static const WCHAR fmt[] = {'%','s',',','%','i',0};
249
250         FilePath = build_icon_path(package,FileName);
251        
252         cls->IconPath = msi_alloc( (strlenW(FilePath)+5)* sizeof(WCHAR) );
253
254         sprintfW(cls->IconPath,fmt,FilePath,icon_index);
255
256         msi_free(FilePath);
257     }
258     else
259     {
260         buffer = MSI_RecordGetString(row,8);
261         if (buffer)
262             cls->IconPath = build_icon_path(package,buffer);
263     }
264
265     if (!MSI_RecordIsNull(row,10))
266     {
267         i = MSI_RecordGetInteger(row,10);
268         if (i != MSI_NULL_INTEGER && i > 0 &&  i < 4)
269         {
270             static const WCHAR ole2[] = {'o','l','e','2','.','d','l','l',0};
271             static const WCHAR ole32[] = {'o','l','e','3','2','.','d','l','l',0};
272
273             switch(i)
274             {
275                 case 1:
276                     cls->DefInprocHandler = strdupW(ole2);
277                     break;
278                 case 2:
279                     cls->DefInprocHandler32 = strdupW(ole32);
280                     break;
281                 case 3:
282                     cls->DefInprocHandler = strdupW(ole2);
283                     cls->DefInprocHandler32 = strdupW(ole32);
284                     break;
285             }
286         }
287         else
288         {
289             cls->DefInprocHandler32 = msi_dup_record_field( row, 10);
290             reduce_to_longfilename(cls->DefInprocHandler32);
291         }
292     }
293     buffer = MSI_RecordGetString(row,11);
294     deformat_string(package,buffer,&cls->Argument);
295
296     buffer = MSI_RecordGetString(row,12);
297     cls->Feature = get_loaded_feature(package,buffer);
298
299     cls->Attributes = MSI_RecordGetInteger(row,13);
300     
301     return cls;
302 }
303
304 /*
305  * the Class table has 3 primary keys. Generally it is only 
306  * referenced through the first CLSID key. However when loading
307  * all of the classes we need to make sure we do not ignore rows
308  * with other Context and ComponentIndexs 
309  */
310 static MSICLASS *load_given_class(MSIPACKAGE *package, LPCWSTR classid)
311 {
312     MSICLASS *cls;
313     MSIRECORD *row;
314     static const WCHAR ExecSeqQuery[] =
315         {'S','E','L','E','C','T',' ','*',' ','F','R','O','M',' ',
316          '`','C','l','a','s','s','`',' ','W','H','E','R','E',' ',
317          '`','C','L','S','I','D','`',' ','=',' ','\'','%','s','\'',0};
318
319     if (!classid)
320         return NULL;
321     
322     /* check for classes already loaded */
323     LIST_FOR_EACH_ENTRY( cls, &package->classes, MSICLASS, entry )
324     {
325         if (lstrcmpiW( cls->clsid, classid )==0)
326         {
327             TRACE("found class %s (%p)\n",debugstr_w(classid), cls);
328             return cls;
329         }
330     }
331
332     row = MSI_QueryGetRecord(package->db, ExecSeqQuery, classid);
333     if (!row)
334         return NULL;
335
336     cls = load_class(package, row);
337     msiobj_release(&row->hdr);
338
339     return cls;
340 }
341
342 static MSIEXTENSION *load_given_extension( MSIPACKAGE *package, LPCWSTR extension );
343
344 static MSIMIME *load_mime( MSIPACKAGE* package, MSIRECORD *row )
345 {
346     LPCWSTR buffer;
347     MSIMIME *mt;
348
349     /* fill in the data */
350
351     mt = msi_alloc_zero( sizeof(MSIMIME) );
352     if (!mt)
353         return mt;
354
355     mt->ContentType = msi_dup_record_field( row, 1 ); 
356     TRACE("loading mime %s\n", debugstr_w(mt->ContentType));
357
358     buffer = MSI_RecordGetString( row, 2 );
359     mt->Extension = load_given_extension( package, buffer );
360
361     mt->clsid = msi_dup_record_field( row, 3 );
362     mt->Class = load_given_class( package, mt->clsid );
363
364     list_add_tail( &package->mimes, &mt->entry );
365
366     return mt;
367 }
368
369 static MSIMIME *load_given_mime( MSIPACKAGE *package, LPCWSTR mime )
370 {
371     MSIRECORD *row;
372     static const WCHAR ExecSeqQuery[] =
373         {'S','E','L','E','C','T',' ','*',' ','F','R','O','M',' ',
374          '`','M','I','M','E','`',' ','W','H','E','R','E',' ',
375          '`','C','o','n','t','e','n','t','T','y','p','e','`',' ','=',' ',
376          '\'','%','s','\'',0};
377     MSIMIME *mt;
378
379     if (!mime)
380         return NULL;
381     
382     /* check for mime already loaded */
383     LIST_FOR_EACH_ENTRY( mt, &package->mimes, MSIMIME, entry )
384     {
385         if (strcmpiW(mt->ContentType,mime)==0)
386         {
387             TRACE("found mime %s (%p)\n",debugstr_w(mime), mt);
388             return mt;
389         }
390     }
391     
392     row = MSI_QueryGetRecord(package->db, ExecSeqQuery, mime);
393     if (!row)
394         return NULL;
395
396     mt = load_mime(package, row);
397     msiobj_release(&row->hdr);
398
399     return mt;
400 }
401
402 static MSIEXTENSION *load_extension( MSIPACKAGE* package, MSIRECORD *row )
403 {
404     MSIEXTENSION *ext;
405     LPCWSTR buffer;
406
407     /* fill in the data */
408
409     ext = msi_alloc_zero( sizeof(MSIEXTENSION) );
410     if (!ext)
411         return NULL;
412
413     list_init( &ext->verbs );
414
415     list_add_tail( &package->extensions, &ext->entry );
416
417     ext->Extension = msi_dup_record_field( row, 1 );
418     TRACE("loading extension %s\n", debugstr_w(ext->Extension));
419
420     buffer = MSI_RecordGetString( row, 2 );
421     ext->Component = get_loaded_component( package,buffer );
422
423     ext->ProgIDText = msi_dup_record_field( row, 3 );
424     ext->ProgID = load_given_progid( package, ext->ProgIDText );
425
426     buffer = MSI_RecordGetString( row, 4 );
427     ext->Mime = load_given_mime( package, buffer );
428
429     buffer = MSI_RecordGetString(row,5);
430     ext->Feature = get_loaded_feature( package, buffer );
431
432     return ext;
433 }
434
435 /*
436  * While the extension table has 2 primary keys, this function is only looking
437  * at the Extension key which is what is referenced as a foreign key
438  */
439 static MSIEXTENSION *load_given_extension( MSIPACKAGE *package, LPCWSTR name )
440 {
441     MSIRECORD *row;
442     MSIEXTENSION *ext;
443     static const WCHAR ExecSeqQuery[] =
444         {'S','E','L','E','C','T',' ','*',' ','F','R','O','M',' ',
445          '`','E','x','t','e','n','s','i','o','n','`',' ',
446          'W','H','E','R','E',' ',
447          '`','E','x','t','e','n','s','i','o','n','`',' ','=',' ',
448          '\'','%','s','\'',0};
449
450     if (!name)
451         return NULL;
452
453     if (name[0] == '.')
454         name++;
455
456     /* check for extensions already loaded */
457     LIST_FOR_EACH_ENTRY( ext, &package->extensions, MSIEXTENSION, entry )
458     {
459         if (strcmpiW( ext->Extension, name )==0)
460         {
461             TRACE("extension %s already loaded %p\n", debugstr_w(name), ext);
462             return ext;
463         }
464     }
465     
466     row = MSI_QueryGetRecord( package->db, ExecSeqQuery, name );
467     if (!row)
468         return NULL;
469
470     ext = load_extension(package, row);
471     msiobj_release(&row->hdr);
472
473     return ext;
474 }
475
476 static UINT iterate_load_verb(MSIRECORD *row, LPVOID param)
477 {
478     MSIPACKAGE* package = param;
479     MSIVERB *verb;
480     LPCWSTR buffer;
481     MSIEXTENSION *extension;
482
483     buffer = MSI_RecordGetString(row,1);
484     extension = load_given_extension( package, buffer );
485     if (!extension)
486     {
487         ERR("Verb unable to find loaded extension %s\n", debugstr_w(buffer));
488         return ERROR_SUCCESS;
489     }
490
491     /* fill in the data */
492
493     verb = msi_alloc_zero( sizeof(MSIVERB) );
494     if (!verb)
495         return ERROR_OUTOFMEMORY;
496
497     verb->Verb = msi_dup_record_field(row,2);
498     TRACE("loading verb %s\n",debugstr_w(verb->Verb));
499     verb->Sequence = MSI_RecordGetInteger(row,3);
500
501     buffer = MSI_RecordGetString(row,4);
502     deformat_string(package,buffer,&verb->Command);
503
504     buffer = MSI_RecordGetString(row,5);
505     deformat_string(package,buffer,&verb->Argument);
506
507     /* associate the verb with the correct extension */
508     list_add_tail( &extension->verbs, &verb->entry );
509     
510     return ERROR_SUCCESS;
511 }
512
513 static UINT iterate_all_classes(MSIRECORD *rec, LPVOID param)
514 {
515     MSICOMPONENT *comp;
516     LPCWSTR clsid;
517     LPCWSTR context;
518     LPCWSTR buffer;
519     MSIPACKAGE* package = param;
520     MSICLASS *cls;
521     BOOL match = FALSE;
522
523     clsid = MSI_RecordGetString(rec,1);
524     context = MSI_RecordGetString(rec,2);
525     buffer = MSI_RecordGetString(rec,3);
526     comp = get_loaded_component(package,buffer);
527
528     LIST_FOR_EACH_ENTRY( cls, &package->classes, MSICLASS, entry )
529     {
530         if (strcmpiW( clsid, cls->clsid ))
531             continue;
532         if (strcmpW( context, cls->Context ))
533             continue;
534         if (comp == cls->Component)
535         {
536             match = TRUE;
537             break;
538         }
539     }
540     
541     if (!match)
542         load_class(package, rec);
543
544     return ERROR_SUCCESS;
545 }
546
547 static VOID load_all_classes(MSIPACKAGE *package)
548 {
549     UINT rc = ERROR_SUCCESS;
550     MSIQUERY *view;
551
552     static const WCHAR ExecSeqQuery[] =
553         {'S','E','L','E','C','T',' ','*',' ', 'F','R','O','M',' ',
554          '`','C','l','a','s','s','`',0};
555
556     rc = MSI_DatabaseOpenViewW(package->db, ExecSeqQuery, &view);
557     if (rc != ERROR_SUCCESS)
558         return;
559
560     rc = MSI_IterateRecords(view, NULL, iterate_all_classes, package);
561     msiobj_release(&view->hdr);
562 }
563
564 static UINT iterate_all_extensions(MSIRECORD *rec, LPVOID param)
565 {
566     MSICOMPONENT *comp;
567     LPCWSTR buffer;
568     LPCWSTR extension;
569     MSIPACKAGE* package = param;
570     BOOL match = FALSE;
571     MSIEXTENSION *ext;
572
573     extension = MSI_RecordGetString(rec,1);
574     buffer = MSI_RecordGetString(rec,2);
575     comp = get_loaded_component(package,buffer);
576
577     LIST_FOR_EACH_ENTRY( ext, &package->extensions, MSIEXTENSION, entry )
578     {
579         if (strcmpiW(extension,ext->Extension))
580             continue;
581         if (comp == ext->Component)
582         {
583             match = TRUE;
584             break;
585         }
586     }
587
588     if (!match)
589         load_extension(package, rec);
590
591     return ERROR_SUCCESS;
592 }
593
594 static VOID load_all_extensions(MSIPACKAGE *package)
595 {
596     UINT rc = ERROR_SUCCESS;
597     MSIQUERY *view;
598
599     static const WCHAR ExecSeqQuery[] =
600         {'S','E','L','E','C','T',' ','*',' ','F','R','O','M',' ',
601          '`','E','x','t','e','n','s','i','o','n','`',0};
602
603     rc = MSI_DatabaseOpenViewW(package->db, ExecSeqQuery, &view);
604     if (rc != ERROR_SUCCESS)
605         return;
606
607     rc = MSI_IterateRecords(view, NULL, iterate_all_extensions, package);
608     msiobj_release(&view->hdr);
609 }
610
611 static UINT iterate_all_progids(MSIRECORD *rec, LPVOID param)
612 {
613     LPCWSTR buffer;
614     MSIPACKAGE* package = param;
615
616     buffer = MSI_RecordGetString(rec,1);
617     load_given_progid(package,buffer);
618     return ERROR_SUCCESS;
619 }
620
621 static VOID load_all_progids(MSIPACKAGE *package)
622 {
623     UINT rc = ERROR_SUCCESS;
624     MSIQUERY *view;
625
626     static const WCHAR ExecSeqQuery[] =
627         {'S','E','L','E','C','T',' ','`','P','r','o','g','I','d','`',' ',
628          'F','R','O','M',' ', '`','P','r','o','g','I','d','`',0};
629
630     rc = MSI_DatabaseOpenViewW(package->db, ExecSeqQuery, &view);
631     if (rc != ERROR_SUCCESS)
632         return;
633
634     rc = MSI_IterateRecords(view, NULL, iterate_all_progids, package);
635     msiobj_release(&view->hdr);
636 }
637
638 static VOID load_all_verbs(MSIPACKAGE *package)
639 {
640     UINT rc = ERROR_SUCCESS;
641     MSIQUERY *view;
642
643     static const WCHAR ExecSeqQuery[] =
644         {'S','E','L','E','C','T',' ','*',' ','F','R','O','M',' ',
645          '`','V','e','r','b','`',0};
646
647     rc = MSI_DatabaseOpenViewW(package->db, ExecSeqQuery, &view);
648     if (rc != ERROR_SUCCESS)
649         return;
650
651     rc = MSI_IterateRecords(view, NULL, iterate_load_verb, package);
652     msiobj_release(&view->hdr);
653 }
654
655 static UINT iterate_all_mimes(MSIRECORD *rec, LPVOID param)
656 {
657     LPCWSTR buffer;
658     MSIPACKAGE* package = param;
659
660     buffer = MSI_RecordGetString(rec,1);
661     load_given_mime(package,buffer);
662     return ERROR_SUCCESS;
663 }
664
665 static VOID load_all_mimes(MSIPACKAGE *package)
666 {
667     UINT rc = ERROR_SUCCESS;
668     MSIQUERY *view;
669
670     static const WCHAR ExecSeqQuery[] =
671         {'S','E','L','E','C','T',' ',
672          '`','C','o','n','t','e','n','t','T','y','p','e','`',
673          ' ','F','R','O','M',' ',
674          '`','M','I','M','E','`',0};
675
676     rc = MSI_DatabaseOpenViewW(package->db, ExecSeqQuery, &view);
677     if (rc != ERROR_SUCCESS)
678         return;
679
680     rc = MSI_IterateRecords(view, NULL, iterate_all_mimes, package);
681     msiobj_release(&view->hdr);
682 }
683
684 static void load_classes_and_such(MSIPACKAGE *package)
685 {
686     TRACE("Loading all the class info and related tables\n");
687
688     /* check if already loaded */
689     if (!list_empty( &package->classes ) ||
690         !list_empty( &package->mimes ) ||
691         !list_empty( &package->extensions ) ||
692         !list_empty( &package->progids ) )
693         return;
694
695     load_all_classes(package);
696     load_all_extensions(package);
697     load_all_progids(package);
698     /* these loads must come after the other loads */
699     load_all_verbs(package);
700     load_all_mimes(package);
701 }
702
703 static void mark_progid_for_install( MSIPACKAGE* package, MSIPROGID *progid )
704 {
705     MSIPROGID *child;
706
707     if (!progid)
708         return;
709
710     if (progid->InstallMe)
711         return;
712
713     progid->InstallMe = TRUE;
714
715     /* all children if this is a parent also install */
716     LIST_FOR_EACH_ENTRY( child, &package->progids, MSIPROGID, entry )
717     {
718         if (child->Parent == progid)
719             mark_progid_for_install( package, child );
720     }
721 }
722
723 static void mark_mime_for_install( MSIMIME *mime )
724 {
725     if (!mime)
726         return;
727     mime->InstallMe = TRUE;
728 }
729
730 static UINT register_appid(const MSIAPPID *appid, LPCWSTR app )
731 {
732     static const WCHAR szAppID[] = { 'A','p','p','I','D',0 };
733     static const WCHAR szRemoteServerName[] =
734          {'R','e','m','o','t','e','S','e','r','v','e','r','N','a','m','e',0};
735     static const WCHAR szLocalService[] =
736          {'L','o','c','a','l','S','e','r','v','i','c','e',0};
737     static const WCHAR szService[] =
738          {'S','e','r','v','i','c','e','P','a','r','a','m','e','t','e','r','s',0};
739     static const WCHAR szDLL[] =
740          {'D','l','l','S','u','r','r','o','g','a','t','e',0};
741     static const WCHAR szActivate[] =
742          {'A','c','t','i','v','a','t','e','A','s','S','t','o','r','a','g','e',0};
743     static const WCHAR szY[] = {'Y',0};
744     static const WCHAR szRunAs[] = {'R','u','n','A','s',0};
745     static const WCHAR szUser[] = 
746          {'I','n','t','e','r','a','c','t','i','v','e',' ','U','s','e','r',0};
747
748     HKEY hkey2,hkey3;
749
750     RegCreateKeyW(HKEY_CLASSES_ROOT,szAppID,&hkey2);
751     RegCreateKeyW( hkey2, appid->AppID, &hkey3 );
752     RegCloseKey(hkey2);
753     msi_reg_set_val_str( hkey3, NULL, app );
754
755     if (appid->RemoteServerName)
756         msi_reg_set_val_str( hkey3, szRemoteServerName, appid->RemoteServerName );
757
758     if (appid->LocalServer)
759         msi_reg_set_val_str( hkey3, szLocalService, appid->LocalServer );
760
761     if (appid->ServiceParameters)
762         msi_reg_set_val_str( hkey3, szService, appid->ServiceParameters );
763
764     if (appid->DllSurrogate)
765         msi_reg_set_val_str( hkey3, szDLL, appid->DllSurrogate );
766
767     if (appid->ActivateAtStorage)
768         msi_reg_set_val_str( hkey3, szActivate, szY );
769
770     if (appid->RunAsInteractiveUser)
771         msi_reg_set_val_str( hkey3, szRunAs, szUser );
772
773     RegCloseKey(hkey3);
774     return ERROR_SUCCESS;
775 }
776
777 UINT ACTION_RegisterClassInfo(MSIPACKAGE *package)
778 {
779     /* 
780      * Again I am assuming the words, "Whose key file represents" when referring
781      * to a Component as to meaning that Components KeyPath file
782      */
783     
784     UINT rc;
785     MSIRECORD *uirow;
786     static const WCHAR szCLSID[] = { 'C','L','S','I','D',0 };
787     static const WCHAR szProgID[] = { 'P','r','o','g','I','D',0 };
788     static const WCHAR szVIProgID[] = { 'V','e','r','s','i','o','n','I','n','d','e','p','e','n','d','e','n','t','P','r','o','g','I','D',0 };
789     static const WCHAR szAppID[] = { 'A','p','p','I','D',0 };
790     static const WCHAR szFileType_fmt[] = {'F','i','l','e','T','y','p','e','\\','%','s','\\','%','i',0};
791     HKEY hkey,hkey2,hkey3;
792     MSICLASS *cls;
793
794     load_classes_and_such(package);
795     rc = RegCreateKeyW(HKEY_CLASSES_ROOT,szCLSID,&hkey);
796     if (rc != ERROR_SUCCESS)
797         return ERROR_FUNCTION_FAILED;
798
799     LIST_FOR_EACH_ENTRY( cls, &package->classes, MSICLASS, entry )
800     {
801         MSICOMPONENT *comp;
802         MSIFILE *file;
803         DWORD size;
804         LPWSTR argument;
805         MSIFEATURE *feature;
806
807         comp = cls->Component;
808         if ( !comp )
809             continue;
810
811         feature = cls->Feature;
812
813         /*
814          * MSDN says that these are based on Feature not on Component.
815          */
816         if (!ACTION_VerifyFeatureForAction( feature, INSTALLSTATE_LOCAL ) &&
817             !ACTION_VerifyFeatureForAction( feature, INSTALLSTATE_ADVERTISED ))
818         {
819             TRACE("Skipping class %s reg due to disabled feature %s\n",
820                   debugstr_w(cls->clsid), debugstr_w(feature->Feature));
821
822             continue;
823         }
824
825         TRACE("Registering class %s (%p)\n", debugstr_w(cls->clsid), cls);
826
827         cls->Installed = TRUE;
828         mark_progid_for_install( package, cls->ProgID );
829
830         RegCreateKeyW( hkey, cls->clsid, &hkey2 );
831
832         if (cls->Description)
833             msi_reg_set_val_str( hkey2, NULL, cls->Description );
834
835         RegCreateKeyW( hkey2, cls->Context, &hkey3 );
836         file = get_loaded_file( package, comp->KeyPath );
837         if (!file)
838         {
839             TRACE("COM server not provided, skipping class %s\n", debugstr_w(cls->clsid));
840             continue;
841         }
842
843         /*
844          * FIXME: Implement install on demand (advertised components).
845          *
846          * ole32.dll should call msi.MsiProvideComponentFromDescriptor()
847          *  when it needs an InProcServer that doesn't exist.
848          * The component advertise string should be in the "InProcServer" value.
849          */
850         size = lstrlenW( file->TargetPath )+1;
851         if (cls->Argument)
852             size += lstrlenW(cls->Argument)+1;
853
854         argument = msi_alloc( size * sizeof(WCHAR) );
855         lstrcpyW( argument, file->TargetPath );
856
857         if (cls->Argument)
858         {
859             lstrcatW( argument, szSpace );
860             lstrcatW( argument, cls->Argument );
861         }
862
863         msi_reg_set_val_str( hkey3, NULL, argument );
864         msi_free(argument);
865
866         RegCloseKey(hkey3);
867
868         if (cls->ProgID || cls->ProgIDText)
869         {
870             LPCWSTR progid;
871
872             if (cls->ProgID)
873                 progid = cls->ProgID->ProgID;
874             else
875                 progid = cls->ProgIDText;
876
877             msi_reg_set_subkey_val( hkey2, szProgID, NULL, progid );
878
879             if (cls->ProgID && cls->ProgID->VersionInd)
880             {
881                 msi_reg_set_subkey_val( hkey2, szVIProgID, NULL, 
882                                         cls->ProgID->VersionInd->ProgID );
883             }
884         }
885
886         if (cls->AppID)
887         {
888             MSIAPPID *appid = cls->AppID;
889
890             msi_reg_set_val_str( hkey2, szAppID, appid->AppID );
891
892             register_appid( appid, cls->Description );
893         }
894
895         if (cls->IconPath)
896         {
897             static const WCHAR szDefaultIcon[] = 
898                 {'D','e','f','a','u','l','t','I','c','o','n',0};
899
900             msi_reg_set_subkey_val( hkey2, szDefaultIcon, NULL, cls->IconPath );
901         }
902
903         if (cls->DefInprocHandler)
904         {
905             static const WCHAR szInproc[] =
906                 {'I','n','p','r','o','c','H','a','n','d','l','e','r',0};
907
908             msi_reg_set_subkey_val( hkey2, szInproc, NULL, cls->DefInprocHandler );
909         }
910
911         if (cls->DefInprocHandler32)
912         {
913             static const WCHAR szInproc32[] =
914                 {'I','n','p','r','o','c','H','a','n','d','l','e','r','3','2',0};
915
916             msi_reg_set_subkey_val( hkey2, szInproc32, NULL, cls->DefInprocHandler32 );
917         }
918         
919         RegCloseKey(hkey2);
920
921         /* if there is a FileTypeMask, register the FileType */
922         if (cls->FileTypeMask)
923         {
924             LPWSTR ptr, ptr2;
925             LPWSTR keyname;
926             INT index = 0;
927             ptr = cls->FileTypeMask;
928             while (ptr && *ptr)
929             {
930                 ptr2 = strchrW(ptr,';');
931                 if (ptr2)
932                     *ptr2 = 0;
933                 keyname = msi_alloc( (strlenW(szFileType_fmt) + strlenW(cls->clsid) + 4) * sizeof(WCHAR));
934                 sprintfW( keyname, szFileType_fmt, cls->clsid, index );
935
936                 msi_reg_set_subkey_val( HKEY_CLASSES_ROOT, keyname, NULL, ptr );
937                 msi_free(keyname);
938
939                 if (ptr2)
940                     ptr = ptr2+1;
941                 else
942                     ptr = NULL;
943
944                 index ++;
945             }
946         }
947         
948         uirow = MSI_CreateRecord(1);
949
950         MSI_RecordSetStringW( uirow, 1, cls->clsid );
951         ui_actiondata(package,szRegisterClassInfo,uirow);
952         msiobj_release(&uirow->hdr);
953     }
954
955     RegCloseKey(hkey);
956     return rc;
957 }
958
959 static LPCWSTR get_clsid_of_progid( const MSIPROGID *progid )
960 {
961     while (progid)
962     {
963         if (progid->Class)
964             return progid->Class->clsid;
965         if (progid->Parent == progid)
966             break;
967         progid = progid->Parent;
968     }
969     return NULL;
970 }
971
972 static UINT register_progid( const MSIPROGID* progid )
973 {
974     static const WCHAR szCLSID[] = { 'C','L','S','I','D',0 };
975     static const WCHAR szDefaultIcon[] =
976         {'D','e','f','a','u','l','t','I','c','o','n',0};
977     static const WCHAR szCurVer[] =
978         {'C','u','r','V','e','r',0};
979     HKEY hkey = 0;
980     UINT rc;
981
982     rc = RegCreateKeyW( HKEY_CLASSES_ROOT, progid->ProgID, &hkey );
983     if (rc == ERROR_SUCCESS)
984     {
985         LPCWSTR clsid = get_clsid_of_progid( progid );
986
987         if (clsid)
988             msi_reg_set_subkey_val( hkey, szCLSID, NULL, clsid );
989         else
990             ERR("%s has no class\n", debugstr_w( progid->ProgID ) );
991
992         if (progid->Description)
993             msi_reg_set_val_str( hkey, NULL, progid->Description );
994
995         if (progid->IconPath)
996             msi_reg_set_subkey_val( hkey, szDefaultIcon, NULL, progid->IconPath );
997
998         /* write out the current version */
999         if (progid->CurVer)
1000             msi_reg_set_subkey_val( hkey, szCurVer, NULL, progid->CurVer->ProgID );
1001
1002         RegCloseKey(hkey);
1003     }
1004     else
1005         ERR("failed to create key %s\n", debugstr_w( progid->ProgID ) );
1006
1007     return rc;
1008 }
1009
1010 UINT ACTION_RegisterProgIdInfo(MSIPACKAGE *package)
1011 {
1012     MSIPROGID *progid;
1013     MSIRECORD *uirow;
1014
1015     load_classes_and_such(package);
1016
1017     LIST_FOR_EACH_ENTRY( progid, &package->progids, MSIPROGID, entry )
1018     {
1019         /* check if this progid is to be installed */
1020         if (progid->Class && progid->Class->Installed)
1021             progid->InstallMe = TRUE;
1022
1023         if (!progid->InstallMe)
1024         {
1025             TRACE("progid %s not scheduled to be installed\n",
1026                              debugstr_w(progid->ProgID));
1027             continue;
1028         }
1029        
1030         TRACE("Registering progid %s\n", debugstr_w(progid->ProgID));
1031
1032         register_progid( progid );
1033
1034         uirow = MSI_CreateRecord( 1 );
1035         MSI_RecordSetStringW( uirow, 1, progid->ProgID );
1036         ui_actiondata( package, szRegisterProgIdInfo, uirow );
1037         msiobj_release( &uirow->hdr );
1038     }
1039
1040     return ERROR_SUCCESS;
1041 }
1042
1043 static UINT register_verb(MSIPACKAGE *package, LPCWSTR progid, 
1044                 MSICOMPONENT* component, const MSIEXTENSION* extension,
1045                 MSIVERB* verb, INT* Sequence )
1046 {
1047     LPWSTR keyname;
1048     HKEY key;
1049     static const WCHAR szShell[] = {'s','h','e','l','l',0};
1050     static const WCHAR szCommand[] = {'c','o','m','m','a','n','d',0};
1051     static const WCHAR fmt[] = {'\"','%','s','\"',' ','%','s',0};
1052     static const WCHAR fmt2[] = {'\"','%','s','\"',0};
1053     LPWSTR command;
1054     DWORD size;
1055     LPWSTR advertise;
1056
1057     keyname = build_directory_name(4, progid, szShell, verb->Verb, szCommand);
1058
1059     TRACE("Making Key %s\n",debugstr_w(keyname));
1060     RegCreateKeyW(HKEY_CLASSES_ROOT, keyname, &key);
1061     size = strlenW(component->FullKeypath);
1062     if (verb->Argument)
1063         size += strlenW(verb->Argument);
1064      size += 4;
1065
1066      command = msi_alloc(size * sizeof (WCHAR));
1067      if (verb->Argument)
1068         sprintfW(command, fmt, component->FullKeypath, verb->Argument);
1069      else
1070         sprintfW(command, fmt2, component->FullKeypath);
1071
1072      msi_reg_set_val_str( key, NULL, command );
1073      msi_free(command);
1074
1075      advertise = create_component_advertise_string(package, component, 
1076                                                    extension->Feature->Feature);
1077
1078      size = strlenW(advertise);
1079
1080      if (verb->Argument)
1081          size += strlenW(verb->Argument);
1082      size += 4;
1083
1084      command = msi_alloc_zero(size * sizeof (WCHAR));
1085
1086      strcpyW(command,advertise);
1087      if (verb->Argument)
1088      {
1089          strcatW(command,szSpace);
1090          strcatW(command,verb->Argument);
1091      }
1092
1093      msi_reg_set_val_multi_str( key, szCommand, command );
1094      
1095      RegCloseKey(key);
1096      msi_free(keyname);
1097      msi_free(advertise);
1098      msi_free(command);
1099
1100      if (verb->Command)
1101      {
1102         keyname = build_directory_name(3, progid, szShell, verb->Verb);
1103         msi_reg_set_subkey_val( HKEY_CLASSES_ROOT, keyname, NULL, verb->Command );
1104         msi_free(keyname);
1105      }
1106
1107      if (verb->Sequence != MSI_NULL_INTEGER)
1108      {
1109         if (*Sequence == MSI_NULL_INTEGER || verb->Sequence < *Sequence)
1110         {
1111             *Sequence = verb->Sequence;
1112             keyname = build_directory_name(2, progid, szShell);
1113             msi_reg_set_subkey_val( HKEY_CLASSES_ROOT, keyname, NULL, verb->Verb );
1114             msi_free(keyname);
1115         }
1116     }
1117     return ERROR_SUCCESS;
1118 }
1119
1120 UINT ACTION_RegisterExtensionInfo(MSIPACKAGE *package)
1121 {
1122     static const WCHAR szContentType[] = 
1123         {'C','o','n','t','e','n','t',' ','T','y','p','e',0 };
1124     HKEY hkey;
1125     MSIEXTENSION *ext;
1126     MSIRECORD *uirow;
1127     BOOL install_on_demand = TRUE;
1128
1129     load_classes_and_such(package);
1130
1131     /* We need to set install_on_demand based on if the shell handles advertised
1132      * shortcuts and the like. Because Mike McCormack is working on this i am
1133      * going to default to TRUE
1134      */
1135     
1136     LIST_FOR_EACH_ENTRY( ext, &package->extensions, MSIEXTENSION, entry )
1137     {
1138         LPWSTR extension;
1139         MSIFEATURE *feature;
1140      
1141         if (!ext->Component)
1142             continue;
1143
1144         feature = ext->Feature;
1145
1146         /* 
1147          * yes. MSDN says that these are based on _Feature_ not on
1148          * Component.  So verify the feature is to be installed
1149          */
1150         if ((!ACTION_VerifyFeatureForAction( feature, INSTALLSTATE_LOCAL )) &&
1151              !(install_on_demand &&
1152                ACTION_VerifyFeatureForAction( feature, INSTALLSTATE_ADVERTISED )))
1153         {
1154             TRACE("Skipping extension %s reg due to disabled feature %s\n",
1155                    debugstr_w(ext->Extension), debugstr_w(feature->Feature));
1156
1157             continue;
1158         }
1159
1160         TRACE("Registering extension %s (%p)\n", debugstr_w(ext->Extension), ext);
1161
1162         ext->Installed = TRUE;
1163
1164         /* this is only registered if the extension has at least 1 verb
1165          * according to MSDN
1166          */
1167         if (ext->ProgID && !list_empty( &ext->verbs ) )
1168             mark_progid_for_install( package, ext->ProgID );
1169
1170         mark_mime_for_install(ext->Mime);
1171
1172         extension = msi_alloc( (lstrlenW( ext->Extension ) + 2)*sizeof(WCHAR) );
1173         extension[0] = '.';
1174         lstrcpyW(extension+1,ext->Extension);
1175
1176         RegCreateKeyW(HKEY_CLASSES_ROOT,extension,&hkey);
1177         msi_free( extension );
1178
1179         if (ext->Mime)
1180             msi_reg_set_val_str( hkey, szContentType, ext->Mime->ContentType );
1181
1182         if (ext->ProgID || ext->ProgIDText)
1183         {
1184             static const WCHAR szSN[] = 
1185                 {'\\','S','h','e','l','l','N','e','w',0};
1186             HKEY hkey2;
1187             LPWSTR newkey;
1188             LPCWSTR progid;
1189             MSIVERB *verb;
1190             INT Sequence = MSI_NULL_INTEGER;
1191             
1192             if (ext->ProgID)
1193                 progid = ext->ProgID->ProgID;
1194             else
1195                 progid = ext->ProgIDText;
1196
1197             msi_reg_set_val_str( hkey, NULL, progid );
1198
1199             newkey = msi_alloc( (strlenW(progid)+strlenW(szSN)+1) * sizeof(WCHAR)); 
1200
1201             strcpyW(newkey,progid);
1202             strcatW(newkey,szSN);
1203             RegCreateKeyW(hkey,newkey,&hkey2);
1204             RegCloseKey(hkey2);
1205
1206             msi_free(newkey);
1207
1208             /* do all the verbs */
1209             LIST_FOR_EACH_ENTRY( verb, &ext->verbs, MSIVERB, entry )
1210             {
1211                 register_verb( package, progid, ext->Component,
1212                                ext, verb, &Sequence);
1213             }
1214         }
1215         
1216         RegCloseKey(hkey);
1217
1218         uirow = MSI_CreateRecord(1);
1219         MSI_RecordSetStringW( uirow, 1, ext->Extension );
1220         ui_actiondata(package,szRegisterExtensionInfo,uirow);
1221         msiobj_release(&uirow->hdr);
1222     }
1223
1224     return ERROR_SUCCESS;
1225 }
1226
1227 UINT ACTION_RegisterMIMEInfo(MSIPACKAGE *package)
1228 {
1229     static const WCHAR szExten[] = 
1230         {'E','x','t','e','n','s','i','o','n',0 };
1231     MSIRECORD *uirow;
1232     MSIMIME *mt;
1233
1234     load_classes_and_such(package);
1235
1236     LIST_FOR_EACH_ENTRY( mt, &package->mimes, MSIMIME, entry )
1237     {
1238         LPWSTR extension;
1239         LPCWSTR exten;
1240         LPCWSTR mime;
1241         static const WCHAR fmt[] = 
1242             {'M','I','M','E','\\','D','a','t','a','b','a','s','e','\\',
1243              'C','o','n','t','e','n','t',' ','T','y','p','e','\\', '%','s',0};
1244         LPWSTR key;
1245
1246         /* 
1247          * check if the MIME is to be installed. Either as requested by an
1248          * extension or Class
1249          */
1250         mt->InstallMe = (mt->InstallMe ||
1251               (mt->Class && mt->Class->Installed) ||
1252               (mt->Extension && mt->Extension->Installed));
1253
1254         if (!mt->InstallMe)
1255         {
1256             TRACE("MIME %s not scheduled to be installed\n",
1257                              debugstr_w(mt->ContentType));
1258             continue;
1259         }
1260         
1261         mime = mt->ContentType;
1262         exten = mt->Extension->Extension;
1263
1264         extension = msi_alloc( (lstrlenW( exten ) + 2)*sizeof(WCHAR) );
1265         extension[0] = '.';
1266         lstrcpyW(extension+1,exten);
1267
1268         key = msi_alloc( (strlenW(mime)+strlenW(fmt)+1) * sizeof(WCHAR) );
1269         sprintfW(key,fmt,mime);
1270         msi_reg_set_subkey_val( HKEY_CLASSES_ROOT, key, szExten, extension );
1271
1272         msi_free(extension);
1273         msi_free(key);
1274
1275         if (mt->clsid)
1276             FIXME("Handle non null for field 3\n");
1277
1278         uirow = MSI_CreateRecord(2);
1279         MSI_RecordSetStringW(uirow,1,mt->ContentType);
1280         MSI_RecordSetStringW(uirow,2,exten);
1281         ui_actiondata(package,szRegisterMIMEInfo,uirow);
1282         msiobj_release(&uirow->hdr);
1283     }
1284
1285     return ERROR_SUCCESS;
1286 }