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