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