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