Implement MsiSequenceA/W.
[wine] / dlls / msi / action.c
1 /*
2  * Implementation of the Microsoft Installer (msi.dll)
3  *
4  * Copyright 2004,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 /*
22  * Pages I need
23  *
24 http://msdn.microsoft.com/library/default.asp?url=/library/en-us/msi/setup/installexecutesequence_table.asp
25
26 http://msdn.microsoft.com/library/default.asp?url=/library/en-us/msi/setup/standard_actions_reference.asp
27  */
28
29 #include <stdarg.h>
30
31 #define COBJMACROS
32
33 #include "windef.h"
34 #include "winbase.h"
35 #include "winerror.h"
36 #include "winreg.h"
37 #include "wine/debug.h"
38 #include "msidefs.h"
39 #include "msipriv.h"
40 #include "winuser.h"
41 #include "shlobj.h"
42 #include "wine/unicode.h"
43 #include "winver.h"
44 #include "action.h"
45
46 #define REG_PROGRESS_VALUE 13200
47 #define COMPONENT_PROGRESS_VALUE 24000
48
49 WINE_DEFAULT_DEBUG_CHANNEL(msi);
50
51 /*
52  * Prototypes
53  */
54 static UINT ACTION_ProcessExecSequence(MSIPACKAGE *package, BOOL UIran);
55 static UINT ACTION_ProcessUISequence(MSIPACKAGE *package);
56 static UINT ACTION_PerformActionSequence(MSIPACKAGE *package, UINT seq, BOOL UI);
57
58 /* 
59  * action handlers
60  */
61 typedef UINT (*STANDARDACTIONHANDLER)(MSIPACKAGE*);
62
63 static UINT ACTION_LaunchConditions(MSIPACKAGE *package);
64 static UINT ACTION_CostInitialize(MSIPACKAGE *package);
65 static UINT ACTION_CreateFolders(MSIPACKAGE *package);
66 static UINT ACTION_CostFinalize(MSIPACKAGE *package);
67 static UINT ACTION_FileCost(MSIPACKAGE *package);
68 static UINT ACTION_WriteRegistryValues(MSIPACKAGE *package);
69 static UINT ACTION_InstallInitialize(MSIPACKAGE *package);
70 static UINT ACTION_InstallValidate(MSIPACKAGE *package);
71 static UINT ACTION_ProcessComponents(MSIPACKAGE *package);
72 static UINT ACTION_RegisterTypeLibraries(MSIPACKAGE *package);
73 static UINT ACTION_RegisterUser(MSIPACKAGE *package);
74 static UINT ACTION_CreateShortcuts(MSIPACKAGE *package);
75 static UINT ACTION_PublishProduct(MSIPACKAGE *package);
76 static UINT ACTION_WriteIniValues(MSIPACKAGE *package);
77 static UINT ACTION_SelfRegModules(MSIPACKAGE *package);
78 static UINT ACTION_PublishFeatures(MSIPACKAGE *package);
79 static UINT ACTION_RegisterProduct(MSIPACKAGE *package);
80 static UINT ACTION_InstallExecute(MSIPACKAGE *package);
81 static UINT ACTION_InstallFinalize(MSIPACKAGE *package);
82 static UINT ACTION_ForceReboot(MSIPACKAGE *package);
83 static UINT ACTION_ResolveSource(MSIPACKAGE *package);
84 static UINT ACTION_ExecuteAction(MSIPACKAGE *package);
85 static UINT ACTION_RegisterFonts(MSIPACKAGE *package);
86 static UINT ACTION_PublishComponents(MSIPACKAGE *package);
87
88 /*
89  * consts and values used
90  */
91 static const WCHAR c_colon[] = {'C',':','\\',0};
92
93 const static WCHAR szCreateFolders[] =
94     {'C','r','e','a','t','e','F','o','l','d','e','r','s',0};
95 const static WCHAR szCostFinalize[] =
96     {'C','o','s','t','F','i','n','a','l','i','z','e',0};
97 const WCHAR szInstallFiles[] =
98     {'I','n','s','t','a','l','l','F','i','l','e','s',0};
99 const WCHAR szDuplicateFiles[] =
100     {'D','u','p','l','i','c','a','t','e','F','i','l','e','s',0};
101 const static WCHAR szWriteRegistryValues[] =
102     {'W','r','i','t','e','R','e','g','i','s','t','r','y',
103             'V','a','l','u','e','s',0};
104 const static WCHAR szCostInitialize[] =
105     {'C','o','s','t','I','n','i','t','i','a','l','i','z','e',0};
106 const static WCHAR szFileCost[] = 
107     {'F','i','l','e','C','o','s','t',0};
108 const static WCHAR szInstallInitialize[] = 
109     {'I','n','s','t','a','l','l','I','n','i','t','i','a','l','i','z','e',0};
110 const static WCHAR szInstallValidate[] = 
111     {'I','n','s','t','a','l','l','V','a','l','i','d','a','t','e',0};
112 const static WCHAR szLaunchConditions[] = 
113     {'L','a','u','n','c','h','C','o','n','d','i','t','i','o','n','s',0};
114 const static WCHAR szProcessComponents[] = 
115     {'P','r','o','c','e','s','s','C','o','m','p','o','n','e','n','t','s',0};
116 const static WCHAR szRegisterTypeLibraries[] = 
117     {'R','e','g','i','s','t','e','r','T','y','p','e',
118             'L','i','b','r','a','r','i','e','s',0};
119 const WCHAR szRegisterClassInfo[] = 
120     {'R','e','g','i','s','t','e','r','C','l','a','s','s','I','n','f','o',0};
121 const WCHAR szRegisterProgIdInfo[] = 
122     {'R','e','g','i','s','t','e','r','P','r','o','g','I','d','I','n','f','o',0};
123 const static WCHAR szCreateShortcuts[] = 
124     {'C','r','e','a','t','e','S','h','o','r','t','c','u','t','s',0};
125 const static WCHAR szPublishProduct[] = 
126     {'P','u','b','l','i','s','h','P','r','o','d','u','c','t',0};
127 const static WCHAR szWriteIniValues[] = 
128     {'W','r','i','t','e','I','n','i','V','a','l','u','e','s',0};
129 const static WCHAR szSelfRegModules[] = 
130     {'S','e','l','f','R','e','g','M','o','d','u','l','e','s',0};
131 const static WCHAR szPublishFeatures[] = 
132     {'P','u','b','l','i','s','h','F','e','a','t','u','r','e','s',0};
133 const static WCHAR szRegisterProduct[] = 
134     {'R','e','g','i','s','t','e','r','P','r','o','d','u','c','t',0};
135 const static WCHAR szInstallExecute[] = 
136     {'I','n','s','t','a','l','l','E','x','e','c','u','t','e',0};
137 const static WCHAR szInstallExecuteAgain[] = 
138     {'I','n','s','t','a','l','l','E','x','e','c','u','t','e',
139             'A','g','a','i','n',0};
140 const static WCHAR szInstallFinalize[] = 
141     {'I','n','s','t','a','l','l','F','i','n','a','l','i','z','e',0};
142 const static WCHAR szForceReboot[] = 
143     {'F','o','r','c','e','R','e','b','o','o','t',0};
144 const static WCHAR szResolveSource[] =
145     {'R','e','s','o','l','v','e','S','o','u','r','c','e',0};
146 const WCHAR szAppSearch[] = 
147     {'A','p','p','S','e','a','r','c','h',0};
148 const static WCHAR szAllocateRegistrySpace[] = 
149     {'A','l','l','o','c','a','t','e','R','e','g','i','s','t','r','y',
150             'S','p','a','c','e',0};
151 const static WCHAR szBindImage[] = 
152     {'B','i','n','d','I','m','a','g','e',0};
153 const static WCHAR szCCPSearch[] = 
154     {'C','C','P','S','e','a','r','c','h',0};
155 const static WCHAR szDeleteServices[] = 
156     {'D','e','l','e','t','e','S','e','r','v','i','c','e','s',0};
157 const static WCHAR szDisableRollback[] = 
158     {'D','i','s','a','b','l','e','R','o','l','l','b','a','c','k',0};
159 const static WCHAR szExecuteAction[] = 
160     {'E','x','e','c','u','t','e','A','c','t','i','o','n',0};
161 const WCHAR szFindRelatedProducts[] = 
162     {'F','i','n','d','R','e','l','a','t','e','d',
163             'P','r','o','d','u','c','t','s',0};
164 const static WCHAR szInstallAdminPackage[] = 
165     {'I','n','s','t','a','l','l','A','d','m','i','n',
166             'P','a','c','k','a','g','e',0};
167 const static WCHAR szInstallSFPCatalogFile[] = 
168     {'I','n','s','t','a','l','l','S','F','P','C','a','t','a','l','o','g',
169             'F','i','l','e',0};
170 const static WCHAR szIsolateComponents[] = 
171     {'I','s','o','l','a','t','e','C','o','m','p','o','n','e','n','t','s',0};
172 const WCHAR szMigrateFeatureStates[] = 
173     {'M','i','g','r','a','t','e','F','e','a','t','u','r','e',
174             'S','t','a','t','e','s',0};
175 const WCHAR szMoveFiles[] = 
176     {'M','o','v','e','F','i','l','e','s',0};
177 const static WCHAR szMsiPublishAssemblies[] = 
178     {'M','s','i','P','u','b','l','i','s','h',
179             'A','s','s','e','m','b','l','i','e','s',0};
180 const static WCHAR szMsiUnpublishAssemblies[] = 
181     {'M','s','i','U','n','p','u','b','l','i','s','h',
182             'A','s','s','e','m','b','l','i','e','s',0};
183 const static WCHAR szInstallODBC[] = 
184     {'I','n','s','t','a','l','l','O','D','B','C',0};
185 const static WCHAR szInstallServices[] = 
186     {'I','n','s','t','a','l','l','S','e','r','v','i','c','e','s',0};
187 const WCHAR szPatchFiles[] = 
188     {'P','a','t','c','h','F','i','l','e','s',0};
189 const static WCHAR szPublishComponents[] = 
190     {'P','u','b','l','i','s','h','C','o','m','p','o','n','e','n','t','s',0};
191 const static WCHAR szRegisterComPlus[] =
192     {'R','e','g','i','s','t','e','r','C','o','m','P','l','u','s',0};
193 const WCHAR szRegisterExtensionInfo[] =
194     {'R','e','g','i','s','t','e','r','E','x','t','e','n','s','i','o','n',
195             'I','n','f','o',0};
196 const static WCHAR szRegisterFonts[] =
197     {'R','e','g','i','s','t','e','r','F','o','n','t','s',0};
198 const WCHAR szRegisterMIMEInfo[] =
199     {'R','e','g','i','s','t','e','r','M','I','M','E','I','n','f','o',0};
200 const static WCHAR szRegisterUser[] =
201     {'R','e','g','i','s','t','e','r','U','s','e','r',0};
202 const WCHAR szRemoveDuplicateFiles[] =
203     {'R','e','m','o','v','e','D','u','p','l','i','c','a','t','e',
204             'F','i','l','e','s',0};
205 const static WCHAR szRemoveEnvironmentStrings[] =
206     {'R','e','m','o','v','e','E','n','v','i','r','o','n','m','e','n','t',
207             'S','t','r','i','n','g','s',0};
208 const WCHAR szRemoveExistingProducts[] =
209     {'R','e','m','o','v','e','E','x','i','s','t','i','n','g',
210             'P','r','o','d','u','c','t','s',0};
211 const WCHAR szRemoveFiles[] =
212     {'R','e','m','o','v','e','F','i','l','e','s',0};
213 const static WCHAR szRemoveFolders[] =
214     {'R','e','m','o','v','e','F','o','l','d','e','r','s',0};
215 const static WCHAR szRemoveIniValues[] =
216     {'R','e','m','o','v','e','I','n','i','V','a','l','u','e','s',0};
217 const static WCHAR szRemoveODBC[] =
218     {'R','e','m','o','v','e','O','D','B','C',0};
219 const static WCHAR szRemoveRegistryValues[] =
220     {'R','e','m','o','v','e','R','e','g','i','s','t','r','y',
221             'V','a','l','u','e','s',0};
222 const static WCHAR szRemoveShortcuts[] =
223     {'R','e','m','o','v','e','S','h','o','r','t','c','u','t','s',0};
224 const static WCHAR szRMCCPSearch[] =
225     {'R','M','C','C','P','S','e','a','r','c','h',0};
226 const static WCHAR szScheduleReboot[] =
227     {'S','c','h','e','d','u','l','e','R','e','b','o','o','t',0};
228 const static WCHAR szSelfUnregModules[] =
229     {'S','e','l','f','U','n','r','e','g','M','o','d','u','l','e','s',0};
230 const static WCHAR szSetODBCFolders[] =
231     {'S','e','t','O','D','B','C','F','o','l','d','e','r','s',0};
232 const static WCHAR szStartServices[] =
233     {'S','t','a','r','t','S','e','r','v','i','c','e','s',0};
234 const static WCHAR szStopServices[] =
235     {'S','t','o','p','S','e','r','v','i','c','e','s',0};
236 const static WCHAR szUnpublishComponents[] =
237     {'U','n','p','u','b','l','i','s','h',
238             'C','o','m','p','o','n','e','n','t','s',0};
239 const static WCHAR szUnpublishFeatures[] =
240     {'U','n','p','u','b','l','i','s','h','F','e','a','t','u','r','e','s',0};
241 const WCHAR szUnregisterClassInfo[] =
242     {'U','n','r','e','g','i','s','t','e','r','C','l','a','s','s',
243             'I','n','f','o',0};
244 const static WCHAR szUnregisterComPlus[] =
245     {'U','n','r','e','g','i','s','t','e','r','C','o','m','P','l','u','s',0};
246 const WCHAR szUnregisterExtensionInfo[] =
247     {'U','n','r','e','g','i','s','t','e','r',
248             'E','x','t','e','n','s','i','o','n','I','n','f','o',0};
249 const static WCHAR szUnregisterFonts[] =
250     {'U','n','r','e','g','i','s','t','e','r','F','o','n','t','s',0};
251 const WCHAR szUnregisterMIMEInfo[] =
252     {'U','n','r','e','g','i','s','t','e','r','M','I','M','E','I','n','f','o',0};
253 const WCHAR szUnregisterProgIdInfo[] =
254     {'U','n','r','e','g','i','s','t','e','r','P','r','o','g','I','d',
255             'I','n','f','o',0};
256 const static WCHAR szUnregisterTypeLibraries[] =
257     {'U','n','r','e','g','i','s','t','e','r','T','y','p','e',
258             'L','i','b','r','a','r','i','e','s',0};
259 const static WCHAR szValidateProductID[] =
260     {'V','a','l','i','d','a','t','e','P','r','o','d','u','c','t','I','D',0};
261 const static WCHAR szWriteEnvironmentStrings[] =
262     {'W','r','i','t','e','E','n','v','i','r','o','n','m','e','n','t',
263             'S','t','r','i','n','g','s',0};
264
265 struct _actions {
266     LPCWSTR action;
267     STANDARDACTIONHANDLER handler;
268 };
269
270 static struct _actions StandardActions[] = {
271     { szAllocateRegistrySpace, NULL},
272     { szAppSearch, ACTION_AppSearch },
273     { szBindImage, NULL},
274     { szCCPSearch, NULL},
275     { szCostFinalize, ACTION_CostFinalize },
276     { szCostInitialize, ACTION_CostInitialize },
277     { szCreateFolders, ACTION_CreateFolders },
278     { szCreateShortcuts, ACTION_CreateShortcuts },
279     { szDeleteServices, NULL},
280     { szDisableRollback, NULL},
281     { szDuplicateFiles, ACTION_DuplicateFiles },
282     { szExecuteAction, ACTION_ExecuteAction },
283     { szFileCost, ACTION_FileCost },
284     { szFindRelatedProducts, ACTION_FindRelatedProducts },
285     { szForceReboot, ACTION_ForceReboot },
286     { szInstallAdminPackage, NULL},
287     { szInstallExecute, ACTION_InstallExecute },
288     { szInstallExecuteAgain, ACTION_InstallExecute },
289     { szInstallFiles, ACTION_InstallFiles},
290     { szInstallFinalize, ACTION_InstallFinalize },
291     { szInstallInitialize, ACTION_InstallInitialize },
292     { szInstallSFPCatalogFile, NULL},
293     { szInstallValidate, ACTION_InstallValidate },
294     { szIsolateComponents, NULL},
295     { szLaunchConditions, ACTION_LaunchConditions },
296     { szMigrateFeatureStates, NULL},
297     { szMoveFiles, NULL},
298     { szMsiPublishAssemblies, NULL},
299     { szMsiUnpublishAssemblies, NULL},
300     { szInstallODBC, NULL},
301     { szInstallServices, NULL},
302     { szPatchFiles, NULL},
303     { szProcessComponents, ACTION_ProcessComponents },
304     { szPublishComponents, ACTION_PublishComponents },
305     { szPublishFeatures, ACTION_PublishFeatures },
306     { szPublishProduct, ACTION_PublishProduct },
307     { szRegisterClassInfo, ACTION_RegisterClassInfo },
308     { szRegisterComPlus, NULL},
309     { szRegisterExtensionInfo, ACTION_RegisterExtensionInfo },
310     { szRegisterFonts, ACTION_RegisterFonts },
311     { szRegisterMIMEInfo, ACTION_RegisterMIMEInfo },
312     { szRegisterProduct, ACTION_RegisterProduct },
313     { szRegisterProgIdInfo, ACTION_RegisterProgIdInfo },
314     { szRegisterTypeLibraries, ACTION_RegisterTypeLibraries },
315     { szRegisterUser, ACTION_RegisterUser},
316     { szRemoveDuplicateFiles, NULL},
317     { szRemoveEnvironmentStrings, NULL},
318     { szRemoveExistingProducts, NULL},
319     { szRemoveFiles, NULL},
320     { szRemoveFolders, NULL},
321     { szRemoveIniValues, NULL},
322     { szRemoveODBC, NULL},
323     { szRemoveRegistryValues, NULL},
324     { szRemoveShortcuts, NULL},
325     { szResolveSource, ACTION_ResolveSource},
326     { szRMCCPSearch, NULL},
327     { szScheduleReboot, NULL},
328     { szSelfRegModules, ACTION_SelfRegModules },
329     { szSelfUnregModules, NULL},
330     { szSetODBCFolders, NULL},
331     { szStartServices, NULL},
332     { szStopServices, NULL},
333     { szUnpublishComponents, NULL},
334     { szUnpublishFeatures, NULL},
335     { szUnregisterClassInfo, NULL},
336     { szUnregisterComPlus, NULL},
337     { szUnregisterExtensionInfo, NULL},
338     { szUnregisterFonts, NULL},
339     { szUnregisterMIMEInfo, NULL},
340     { szUnregisterProgIdInfo, NULL},
341     { szUnregisterTypeLibraries, NULL},
342     { szValidateProductID, NULL},
343     { szWriteEnvironmentStrings, NULL},
344     { szWriteIniValues, ACTION_WriteIniValues },
345     { szWriteRegistryValues, ACTION_WriteRegistryValues},
346     { NULL, NULL},
347 };
348
349
350 /********************************************************
351  * helper functions
352  ********************************************************/
353
354 static void ce_actiontext(MSIPACKAGE* package, LPCWSTR action)
355 {
356     static const WCHAR szActionText[] = 
357         {'A','c','t','i','o','n','T','e','x','t',0};
358     MSIRECORD *row;
359
360     row = MSI_CreateRecord(1);
361     MSI_RecordSetStringW(row,1,action);
362     ControlEvent_FireSubscribedEvent(package,szActionText, row);
363     msiobj_release(&row->hdr);
364 }
365
366 static void ui_actionstart(MSIPACKAGE *package, LPCWSTR action)
367 {
368     static const WCHAR template_s[]=
369         {'A','c','t','i','o','n',' ','%','s',':',' ','%','s','.',' ', '%','s',
370          '.',0};
371     static const WCHAR format[] = 
372         {'H','H','\'',':','\'','m','m','\'',':','\'','s','s',0};
373     static const WCHAR Query_t[] = 
374         {'S','E','L','E','C','T',' ','*',' ','F','R','O','M',' ',
375          '`','A','c','t','i','o', 'n','T','e','x','t','`',' ',
376          'W','H','E','R','E', ' ','`','A','c','t','i','o','n','`',' ','=', 
377          ' ','\'','%','s','\'',0};
378     WCHAR message[1024];
379     WCHAR timet[0x100];
380     MSIRECORD * row = 0;
381     LPCWSTR ActionText;
382     LPWSTR deformated;
383
384     GetTimeFormatW(LOCALE_USER_DEFAULT, 0, NULL, format, timet, 0x100);
385
386     row = MSI_QueryGetRecord( package->db, Query_t, action );
387     if (!row)
388         return;
389
390     ActionText = MSI_RecordGetString(row,2);
391     deformat_string(package, ActionText, &deformated);
392
393     sprintfW(message,template_s,timet,action,deformated);
394     ce_actiontext(package, deformated);
395     msiobj_release(&row->hdr);
396
397     row = MSI_CreateRecord(1);
398     MSI_RecordSetStringW(row,1,message);
399  
400     MSI_ProcessMessage(package, INSTALLMESSAGE_ACTIONSTART, row);
401     msiobj_release(&row->hdr);
402     msi_free(deformated);
403 }
404
405 static void ui_actioninfo(MSIPACKAGE *package, LPCWSTR action, BOOL start, 
406                           UINT rc)
407 {
408     MSIRECORD * row;
409     static const WCHAR template_s[]=
410         {'A','c','t','i','o','n',' ','s','t','a','r','t',' ','%','s',':',' ',
411          '%','s', '.',0};
412     static const WCHAR template_e[]=
413         {'A','c','t','i','o','n',' ','e','n','d','e','d',' ','%','s',':',' ',
414          '%','s', '.',' ','R','e','t','u','r','n',' ','v','a','l','u','e',' ',
415          '%','i','.',0};
416     static const WCHAR format[] = 
417         {'H','H','\'',':','\'','m','m','\'',':','\'','s','s',0};
418     WCHAR message[1024];
419     WCHAR timet[0x100];
420
421     GetTimeFormatW(LOCALE_USER_DEFAULT, 0, NULL, format, timet, 0x100);
422     if (start)
423         sprintfW(message,template_s,timet,action);
424     else
425         sprintfW(message,template_e,timet,action,rc);
426     
427     row = MSI_CreateRecord(1);
428     MSI_RecordSetStringW(row,1,message);
429  
430     MSI_ProcessMessage(package, INSTALLMESSAGE_INFO, row);
431     msiobj_release(&row->hdr);
432 }
433
434 /****************************************************
435  * TOP level entry points 
436  *****************************************************/
437
438 UINT ACTION_DoTopLevelINSTALL(MSIPACKAGE *package, LPCWSTR szPackagePath,
439                               LPCWSTR szCommandLine, LPCWSTR msiFilePath)
440 {
441     DWORD sz;
442     WCHAR buffer[10];
443     UINT rc;
444     BOOL ui = FALSE;
445     static const WCHAR szUILevel[] = {'U','I','L','e','v','e','l',0};
446     static const WCHAR szAction[] = {'A','C','T','I','O','N',0};
447     static const WCHAR szInstall[] = {'I','N','S','T','A','L','L',0};
448
449     MSI_SetPropertyW(package, szAction, szInstall);
450
451     package->script = msi_alloc(sizeof(MSISCRIPT));
452     memset(package->script,0,sizeof(MSISCRIPT));
453
454     package->script->InWhatSequence = SEQUENCE_INSTALL;
455
456     package->msiFilePath= strdupW(msiFilePath);
457
458     if (szPackagePath)   
459     {
460         LPWSTR p, check, path;
461  
462         package->PackagePath = strdupW(szPackagePath);
463         path = strdupW(szPackagePath);
464         p = strrchrW(path,'\\');    
465         if (p)
466         {
467             p++;
468             *p=0;
469         }
470         else
471         {
472             msi_free(path);
473             path = msi_alloc(MAX_PATH*sizeof(WCHAR));
474             GetCurrentDirectoryW(MAX_PATH,path);
475             strcatW(path,cszbs);
476         }
477
478         check = msi_dup_property( package, cszSourceDir );
479         if (!check)
480             MSI_SetPropertyW(package, cszSourceDir, path);
481         msi_free(check);
482         msi_free(path);
483     }
484
485     if (szCommandLine)
486     {
487         LPWSTR ptr,ptr2;
488         ptr = (LPWSTR)szCommandLine;
489        
490         while (*ptr)
491         {
492             WCHAR *prop = NULL;
493             WCHAR *val = NULL;
494
495             TRACE("Looking at %s\n",debugstr_w(ptr));
496
497             ptr2 = strchrW(ptr,'=');
498             if (ptr2)
499             {
500                 BOOL quote=FALSE;
501                 DWORD len = 0;
502
503                 while (*ptr == ' ') ptr++;
504                 len = ptr2-ptr;
505                 prop = msi_alloc((len+1)*sizeof(WCHAR));
506                 memcpy(prop,ptr,len*sizeof(WCHAR));
507                 prop[len]=0;
508                 ptr2++;
509            
510                 len = 0; 
511                 ptr = ptr2; 
512                 while (*ptr && (quote || (!quote && *ptr!=' ')))
513                 {
514                     if (*ptr == '"')
515                         quote = !quote;
516                     ptr++;
517                     len++;
518                 }
519                
520                 if (*ptr2=='"')
521                 {
522                     ptr2++;
523                     len -= 2;
524                 }
525                 val = msi_alloc((len+1)*sizeof(WCHAR));
526                 memcpy(val,ptr2,len*sizeof(WCHAR));
527                 val[len] = 0;
528
529                 if (strlenW(prop) > 0)
530                 {
531                     TRACE("Found commandline property (%s) = (%s)\n", 
532                                        debugstr_w(prop), debugstr_w(val));
533                     MSI_SetPropertyW(package,prop,val);
534                 }
535                 msi_free(val);
536                 msi_free(prop);
537             }
538             ptr++;
539         }
540     }
541   
542     sz = 10; 
543     if (MSI_GetPropertyW(package,szUILevel,buffer,&sz) == ERROR_SUCCESS)
544     {
545         if (atoiW(buffer) >= INSTALLUILEVEL_REDUCED)
546         {
547             package->script->InWhatSequence |= SEQUENCE_UI;
548             rc = ACTION_ProcessUISequence(package);
549             ui = TRUE;
550             if (rc == ERROR_SUCCESS)
551             {
552                 package->script->InWhatSequence |= SEQUENCE_EXEC;
553                 rc = ACTION_ProcessExecSequence(package,TRUE);
554             }
555         }
556         else
557             rc = ACTION_ProcessExecSequence(package,FALSE);
558     }
559     else
560         rc = ACTION_ProcessExecSequence(package,FALSE);
561     
562     if (rc == -1)
563     {
564         /* install was halted but should be considered a success */
565         rc = ERROR_SUCCESS;
566     }
567
568     package->script->CurrentlyScripting= FALSE;
569
570     /* process the ending type action */
571     if (rc == ERROR_SUCCESS)
572         ACTION_PerformActionSequence(package,-1,ui);
573     else if (rc == ERROR_INSTALL_USEREXIT) 
574         ACTION_PerformActionSequence(package,-2,ui);
575     else if (rc == ERROR_INSTALL_SUSPEND) 
576         ACTION_PerformActionSequence(package,-4,ui);
577     else  /* failed */
578         ACTION_PerformActionSequence(package,-3,ui);
579
580     /* finish up running custom actions */
581     ACTION_FinishCustomActions(package);
582     
583     return rc;
584 }
585
586 static UINT ACTION_PerformActionSequence(MSIPACKAGE *package, UINT seq, BOOL UI)
587 {
588     UINT rc = ERROR_SUCCESS;
589     MSIRECORD * row = 0;
590     static const WCHAR ExecSeqQuery[] =
591         {'S','E','L','E','C','T',' ','*',' ','F','R','O','M',' ',
592          '`','I','n','s','t','a','l','l','E','x','e','c','u','t','e',
593          'S','e','q','u','e','n','c','e','`',' ', 'W','H','E','R','E',' ',
594          '`','S','e','q','u','e','n','c','e','`',' ', '=',' ','%','i',0};
595
596     static const WCHAR UISeqQuery[] =
597         {'S','E','L','E','C','T',' ','*',' ','F','R','O','M',' ',
598      '`','I','n','s','t','a','l','l','U','I','S','e','q','u','e','n','c','e',
599      '`', ' ', 'W','H','E','R','E',' ','`','S','e','q','u','e','n','c','e','`',
600          ' ', '=',' ','%','i',0};
601
602     if (UI)
603         row = MSI_QueryGetRecord(package->db, UISeqQuery, seq);
604     else
605         row = MSI_QueryGetRecord(package->db, ExecSeqQuery, seq);
606
607     if (row)
608     {
609         LPCWSTR action, cond;
610
611         TRACE("Running the actions\n"); 
612
613         /* check conditions */
614         cond = MSI_RecordGetString(row,2);
615         if (cond)
616         {
617             /* this is a hack to skip errors in the condition code */
618             if (MSI_EvaluateConditionW(package, cond) == MSICONDITION_FALSE)
619                 goto end;
620         }
621
622         action = MSI_RecordGetString(row,1);
623         if (!action)
624         {
625             ERR("failed to fetch action\n");
626             rc = ERROR_FUNCTION_FAILED;
627             goto end;
628         }
629
630         if (UI)
631             rc = ACTION_PerformUIAction(package,action);
632         else
633             rc = ACTION_PerformAction(package,action,FALSE);
634 end:
635         msiobj_release(&row->hdr);
636     }
637     else
638         rc = ERROR_SUCCESS;
639
640     return rc;
641 }
642
643 typedef struct {
644     MSIPACKAGE* package;
645     BOOL UI;
646 } iterate_action_param;
647
648 static UINT ITERATE_Actions(MSIRECORD *row, LPVOID param)
649 {
650     iterate_action_param *iap= (iterate_action_param*)param;
651     UINT rc;
652     LPCWSTR cond, action;
653
654     action = MSI_RecordGetString(row,1);
655     if (!action)
656     {
657         ERR("Error is retrieving action name\n");
658         return  ERROR_FUNCTION_FAILED;
659     }
660
661     /* check conditions */
662     cond = MSI_RecordGetString(row,2);
663     if (cond)
664     {
665         /* this is a hack to skip errors in the condition code */
666         if (MSI_EvaluateConditionW(iap->package, cond) == MSICONDITION_FALSE)
667         {
668             TRACE("Skipping action: %s (condition is false)\n",
669                             debugstr_w(action));
670             return ERROR_SUCCESS;
671         }
672     }
673
674     if (iap->UI)
675         rc = ACTION_PerformUIAction(iap->package,action);
676     else
677         rc = ACTION_PerformAction(iap->package,action,FALSE);
678
679     msi_dialog_check_messages( NULL );
680
681     if (iap->package->CurrentInstallState != ERROR_SUCCESS )
682         rc = iap->package->CurrentInstallState;
683
684     if (rc == ERROR_FUNCTION_NOT_CALLED)
685         rc = ERROR_SUCCESS;
686
687     if (rc != ERROR_SUCCESS)
688         ERR("Execution halted due to error (%i)\n",rc);
689
690     return rc;
691 }
692
693 UINT MSI_Sequence( MSIPACKAGE *package, LPCWSTR szTable, INT iSequenceMode )
694 {
695     MSIQUERY * view;
696     UINT r;
697     static const WCHAR query[] =
698         {'S','E','L','E','C','T',' ','*',' ','F','R','O','M',' ',
699          '`','%','s','`',
700          ' ','W','H','E','R','E',' ', 
701          '`','S','e','q','u','e','n','c','e','`',' ',
702          '>',' ','0',' ','O','R','D','E','R',' ','B','Y',' ',
703          '`','S','e','q','u','e','n','c','e','`',0};
704     iterate_action_param iap;
705
706     /*
707      * FIXME: probably should be checking UILevel in the
708      *       ACTION_PerformUIAction/ACTION_PerformAction
709      *       rather than saving the UI level here. Those
710      *       two functions can be merged too.
711      */
712     iap.package = package;
713     iap.UI = TRUE;
714
715     TRACE("%p %s %i\n", package, debugstr_w(szTable), iSequenceMode );
716
717     r = MSI_OpenQuery( package->db, &view, query, szTable );
718     if (r == ERROR_SUCCESS)
719     {
720         r = MSI_IterateRecords( view, NULL, ITERATE_Actions, &iap );
721         msiobj_release(&view->hdr);
722     }
723
724     return r;
725 }
726
727 static UINT ACTION_ProcessExecSequence(MSIPACKAGE *package, BOOL UIran)
728 {
729     MSIQUERY * view;
730     UINT rc;
731     static const WCHAR ExecSeqQuery[] =
732         {'S','E','L','E','C','T',' ','*',' ', 'F','R','O','M',' ',
733          '`','I','n','s','t','a','l','l','E','x','e','c','u','t','e',
734          'S','e','q','u','e','n','c','e','`',' ', 'W','H','E','R','E',' ',
735          '`','S','e','q','u','e','n','c','e','`',' ', '>',' ','%','i',' ',
736          'O','R','D','E','R',' ', 'B','Y',' ',
737          '`','S','e','q','u','e','n','c','e','`',0 };
738     MSIRECORD * row = 0;
739     static const WCHAR IVQuery[] =
740         {'S','E','L','E','C','T',' ','`','S','e','q','u','e','n','c','e','`',
741          ' ', 'F','R','O','M',' ','`','I','n','s','t','a','l','l',
742          'E','x','e','c','u','t','e','S','e','q','u','e','n','c','e','`',' ',
743          'W','H','E','R','E',' ','`','A','c','t','i','o','n','`',' ','=',
744          ' ','\'', 'I','n','s','t','a','l','l',
745          'V','a','l','i','d','a','t','e','\'', 0};
746     INT seq = 0;
747     iterate_action_param iap;
748
749     iap.package = package;
750     iap.UI = FALSE;
751
752     if (package->script->ExecuteSequenceRun)
753     {
754         TRACE("Execute Sequence already Run\n");
755         return ERROR_SUCCESS;
756     }
757
758     package->script->ExecuteSequenceRun = TRUE;
759
760     /* get the sequence number */
761     if (UIran)
762     {
763         row = MSI_QueryGetRecord(package->db, IVQuery);
764         if( !row )
765             return ERROR_FUNCTION_FAILED;
766         seq = MSI_RecordGetInteger(row,1);
767         msiobj_release(&row->hdr);
768     }
769
770     rc = MSI_OpenQuery(package->db, &view, ExecSeqQuery, seq);
771     if (rc == ERROR_SUCCESS)
772     {
773         TRACE("Running the actions\n");
774
775         rc = MSI_IterateRecords(view, NULL, ITERATE_Actions, &iap);
776         msiobj_release(&view->hdr);
777     }
778
779     return rc;
780 }
781
782 static UINT ACTION_ProcessUISequence(MSIPACKAGE *package)
783 {
784     MSIQUERY * view;
785     UINT rc;
786     static const WCHAR ExecSeqQuery [] =
787         {'S','E','L','E','C','T',' ','*',' ','F','R','O','M',' ',
788          '`','I','n','s','t','a','l','l',
789          'U','I','S','e','q','u','e','n','c','e','`',
790          ' ','W','H','E','R','E',' ', 
791          '`','S','e','q','u','e','n','c','e','`',' ',
792          '>',' ','0',' ','O','R','D','E','R',' ','B','Y',' ',
793          '`','S','e','q','u','e','n','c','e','`',0};
794     iterate_action_param iap;
795
796     iap.package = package;
797     iap.UI = TRUE;
798
799     rc = MSI_DatabaseOpenViewW(package->db, ExecSeqQuery, &view);
800     
801     if (rc == ERROR_SUCCESS)
802     {
803         TRACE("Running the actions \n"); 
804
805         rc = MSI_IterateRecords(view, NULL, ITERATE_Actions, &iap);
806         msiobj_release(&view->hdr);
807     }
808
809     return rc;
810 }
811
812 /********************************************************
813  * ACTION helper functions and functions that perform the actions
814  *******************************************************/
815 static BOOL ACTION_HandleStandardAction(MSIPACKAGE *package, LPCWSTR action, 
816                                         UINT* rc, BOOL force )
817 {
818     BOOL ret = FALSE; 
819     BOOL run = force;
820     int i;
821
822     if (!run && !package->script->CurrentlyScripting)
823         run = TRUE;
824    
825     if (!run)
826     {
827         if (strcmpW(action,szInstallFinalize) == 0 ||
828             strcmpW(action,szInstallExecute) == 0 ||
829             strcmpW(action,szInstallExecuteAgain) == 0) 
830                 run = TRUE;
831     }
832     
833     i = 0;
834     while (StandardActions[i].action != NULL)
835     {
836         if (strcmpW(StandardActions[i].action, action)==0)
837         {
838             if (!run)
839             {
840                 ui_actioninfo(package, action, TRUE, 0);
841                 *rc = schedule_action(package,INSTALL_SCRIPT,action);
842                 ui_actioninfo(package, action, FALSE, *rc);
843             }
844             else
845             {
846                 ui_actionstart(package, action);
847                 if (StandardActions[i].handler)
848                 {
849                     *rc = StandardActions[i].handler(package);
850                 }
851                 else
852                 {
853                     FIXME("unhandled standard action %s\n",debugstr_w(action));
854                     *rc = ERROR_SUCCESS;
855                 }
856             }
857             ret = TRUE;
858             break;
859         }
860         i++;
861     }
862     return ret;
863 }
864
865 static BOOL ACTION_HandleCustomAction( MSIPACKAGE* package, LPCWSTR action,
866                                        UINT* rc, BOOL force )
867 {
868     BOOL ret=FALSE;
869     UINT arc;
870
871     arc = ACTION_CustomAction(package,action, force);
872
873     if (arc != ERROR_CALL_NOT_IMPLEMENTED)
874     {
875         *rc = arc;
876         ret = TRUE;
877     }
878     return ret;
879 }
880
881 /* 
882  * A lot of actions are really important even if they don't do anything
883  * explicit... Lots of properties are set at the beginning of the installation
884  * CostFinalize does a bunch of work to translate the directories and such
885  * 
886  * But until I get write access to the database that is hard, so I am going to
887  * hack it to see if I can get something to run.
888  */
889 UINT ACTION_PerformAction(MSIPACKAGE *package, const WCHAR *action, BOOL force)
890 {
891     UINT rc = ERROR_SUCCESS; 
892     BOOL handled;
893
894     TRACE("Performing action (%s)\n",debugstr_w(action));
895
896     handled = ACTION_HandleStandardAction(package, action, &rc, force);
897
898     if (!handled)
899         handled = ACTION_HandleCustomAction(package, action, &rc, force);
900
901     if (!handled)
902     {
903         FIXME("unhandled msi action %s\n",debugstr_w(action));
904         rc = ERROR_FUNCTION_NOT_CALLED;
905     }
906
907     return rc;
908 }
909
910 UINT ACTION_PerformUIAction(MSIPACKAGE *package, const WCHAR *action)
911 {
912     UINT rc = ERROR_SUCCESS;
913     BOOL handled = FALSE;
914
915     TRACE("Performing action (%s)\n",debugstr_w(action));
916
917     handled = ACTION_HandleStandardAction(package, action, &rc,TRUE);
918
919     if (!handled)
920         handled = ACTION_HandleCustomAction(package, action, &rc, FALSE);
921
922     if( !handled && ACTION_DialogBox(package,action) == ERROR_SUCCESS )
923         handled = TRUE;
924
925     if (!handled)
926     {
927         FIXME("unhandled msi action %s\n",debugstr_w(action));
928         rc = ERROR_FUNCTION_NOT_CALLED;
929     }
930
931     return rc;
932 }
933
934
935 /*
936  * Actual Action Handlers
937  */
938
939 static UINT ITERATE_CreateFolders(MSIRECORD *row, LPVOID param)
940 {
941     MSIPACKAGE *package = (MSIPACKAGE*)param;
942     LPCWSTR dir;
943     LPWSTR full_path;
944     MSIRECORD *uirow;
945     MSIFOLDER *folder;
946
947     dir = MSI_RecordGetString(row,1);
948     if (!dir)
949     {
950         ERR("Unable to get folder id \n");
951         return ERROR_SUCCESS;
952     }
953
954     full_path = resolve_folder(package,dir,FALSE,FALSE,&folder);
955     if (!full_path)
956     {
957         ERR("Unable to resolve folder id %s\n",debugstr_w(dir));
958         return ERROR_SUCCESS;
959     }
960
961     TRACE("Folder is %s\n",debugstr_w(full_path));
962
963     /* UI stuff */
964     uirow = MSI_CreateRecord(1);
965     MSI_RecordSetStringW(uirow,1,full_path);
966     ui_actiondata(package,szCreateFolders,uirow);
967     msiobj_release( &uirow->hdr );
968
969     if (folder->State == 0)
970         create_full_pathW(full_path);
971
972     folder->State = 3;
973
974     msi_free(full_path);
975     return ERROR_SUCCESS;
976 }
977
978
979 /*
980  * Also we cannot enable/disable components either, so for now I am just going 
981  * to do all the directories for all the components.
982  */
983 static UINT ACTION_CreateFolders(MSIPACKAGE *package)
984 {
985     static const WCHAR ExecSeqQuery[] =
986         {'S','E','L','E','C','T',' ',
987          '`','D','i','r','e','c','t','o','r','y','_','`',
988          ' ','F','R','O','M',' ',
989          '`','C','r','e','a','t','e','F','o','l','d','e','r','`',0 };
990     UINT rc;
991     MSIQUERY *view;
992
993     rc = MSI_DatabaseOpenViewW(package->db, ExecSeqQuery, &view );
994     if (rc != ERROR_SUCCESS)
995         return ERROR_SUCCESS;
996
997     rc = MSI_IterateRecords(view, NULL, ITERATE_CreateFolders, package);
998     msiobj_release(&view->hdr);
999    
1000     return rc;
1001 }
1002
1003 static MSICOMPONENT* load_component( MSIRECORD * row )
1004 {
1005     MSICOMPONENT *comp;
1006
1007     comp = msi_alloc_zero( sizeof(MSICOMPONENT) );
1008     if (!comp)
1009         return comp;
1010
1011     /* fill in the data */
1012     comp->Component = load_dynamic_stringW( row, 1 );
1013
1014     TRACE("Loading Component %s\n", debugstr_w(comp->Component));
1015
1016     comp->ComponentId = load_dynamic_stringW( row, 2 );
1017     comp->Directory = load_dynamic_stringW( row, 3 );
1018     comp->Attributes = MSI_RecordGetInteger(row,4);
1019     comp->Condition = load_dynamic_stringW( row, 5 );
1020     comp->KeyPath = load_dynamic_stringW( row, 6 );
1021
1022     comp->Installed = INSTALLSTATE_ABSENT;
1023     comp->Action = INSTALLSTATE_UNKNOWN;
1024     comp->ActionRequest = INSTALLSTATE_UNKNOWN;
1025
1026     comp->Enabled = TRUE;
1027
1028     return comp;
1029 }
1030
1031 typedef struct {
1032     MSIPACKAGE *package;
1033     MSIFEATURE *feature;
1034 } _ilfs;
1035
1036 static UINT add_feature_component( MSIFEATURE *feature, MSICOMPONENT *comp )
1037 {
1038     ComponentList *cl;
1039
1040     cl = msi_alloc( sizeof (*cl) );
1041     if ( !cl )
1042         return ERROR_NOT_ENOUGH_MEMORY;
1043     cl->component = comp;
1044     list_add_tail( &feature->Components, &cl->entry );
1045
1046     return ERROR_SUCCESS;
1047 }
1048
1049 static UINT iterate_component_check( MSIRECORD *row, LPVOID param )
1050 {
1051     _ilfs* ilfs= (_ilfs*)param;
1052     MSIPACKAGE *package = ilfs->package;
1053     MSIFEATURE *feature = ilfs->feature;
1054     MSICOMPONENT *comp;
1055
1056     comp = load_component( row );
1057     if (!comp)
1058         return ERROR_FUNCTION_FAILED;
1059
1060     list_add_tail( &package->components, &comp->entry );
1061     add_feature_component( feature, comp );
1062
1063     TRACE("Loaded new component %p\n", comp);
1064
1065     return ERROR_SUCCESS;
1066 }
1067
1068 static UINT iterate_load_featurecomponents(MSIRECORD *row, LPVOID param)
1069 {
1070     _ilfs* ilfs= (_ilfs*)param;
1071     LPCWSTR component;
1072     DWORD rc;
1073     MSICOMPONENT *comp;
1074     MSIQUERY * view;
1075     static const WCHAR Query[] = 
1076         {'S','E','L','E','C','T',' ','*',' ','F','R', 'O','M',' ', 
1077          '`','C','o','m','p','o','n','e','n','t','`',' ',
1078          'W','H','E','R','E',' ', 
1079          '`','C','o','m','p','o','n','e','n','t','`',' ',
1080          '=','\'','%','s','\'',0};
1081
1082     component = MSI_RecordGetString(row,1);
1083
1084     /* check to see if the component is already loaded */
1085     comp = get_loaded_component( ilfs->package, component );
1086     if (comp)
1087     {
1088         TRACE("Component %s already loaded\n", debugstr_w(component) );
1089         add_feature_component( ilfs->feature, comp );
1090         return ERROR_SUCCESS;
1091     }
1092
1093     rc = MSI_OpenQuery(ilfs->package->db, &view, Query, component);
1094     if (rc != ERROR_SUCCESS)
1095         return ERROR_SUCCESS;
1096
1097     rc = MSI_IterateRecords(view, NULL, iterate_component_check, ilfs);
1098     msiobj_release( &view->hdr );
1099
1100     return ERROR_SUCCESS;
1101 }
1102
1103 static UINT load_feature(MSIRECORD * row, LPVOID param)
1104 {
1105     MSIPACKAGE* package = (MSIPACKAGE*)param;
1106     MSIFEATURE* feature;
1107     static const WCHAR Query1[] = 
1108         {'S','E','L','E','C','T',' ',
1109          '`','C','o','m','p','o','n','e','n','t','_','`',
1110          ' ','F','R','O','M',' ','`','F','e','a','t','u','r','e',
1111          'C','o','m','p','o','n','e','n','t','s','`',' ',
1112          'W','H','E','R','E',' ',
1113          '`','F','e', 'a','t','u','r','e','_','`',' ','=','\'','%','s','\'',0};
1114     MSIQUERY * view;
1115     UINT    rc;
1116     _ilfs ilfs;
1117
1118     /* fill in the data */
1119
1120     feature = msi_alloc_zero( sizeof (MSIFEATURE) );
1121     if (!feature)
1122         return ERROR_NOT_ENOUGH_MEMORY;
1123
1124     list_init( &feature->Components );
1125     
1126     feature->Feature = load_dynamic_stringW( row, 1 );
1127
1128     TRACE("Loading feature %s\n",debugstr_w(feature->Feature));
1129
1130     feature->Feature_Parent = load_dynamic_stringW( row, 2 );
1131     feature->Title = load_dynamic_stringW( row, 3 );
1132     feature->Description = load_dynamic_stringW( row, 4 );
1133
1134     if (!MSI_RecordIsNull(row,5))
1135         feature->Display = MSI_RecordGetInteger(row,5);
1136   
1137     feature->Level= MSI_RecordGetInteger(row,6);
1138     feature->Directory = load_dynamic_stringW( row, 7 );
1139     feature->Attributes = MSI_RecordGetInteger(row,8);
1140
1141     feature->Installed = INSTALLSTATE_ABSENT;
1142     feature->Action = INSTALLSTATE_UNKNOWN;
1143     feature->ActionRequest = INSTALLSTATE_UNKNOWN;
1144
1145     list_add_tail( &package->features, &feature->entry );
1146
1147     /* load feature components */
1148
1149     rc = MSI_OpenQuery( package->db, &view, Query1, feature->Feature );
1150     if (rc != ERROR_SUCCESS)
1151         return ERROR_SUCCESS;
1152
1153     ilfs.package = package;
1154     ilfs.feature = feature;
1155
1156     MSI_IterateRecords(view, NULL, iterate_load_featurecomponents , &ilfs);
1157     msiobj_release(&view->hdr);
1158
1159     return ERROR_SUCCESS;
1160 }
1161
1162 static UINT load_file(MSIRECORD *row, LPVOID param)
1163 {
1164     MSIPACKAGE* package = (MSIPACKAGE*)param;
1165     LPCWSTR component;
1166     MSIFILE *file;
1167
1168     /* fill in the data */
1169
1170     file = msi_alloc_zero( sizeof (MSIFILE) );
1171     if (!file)
1172         return ERROR_NOT_ENOUGH_MEMORY;
1173  
1174     file->File = load_dynamic_stringW( row, 1 );
1175
1176     component = MSI_RecordGetString( row, 2 );
1177     file->Component = get_loaded_component( package, component );
1178
1179     if (!file->Component)
1180         ERR("Unfound Component %s\n",debugstr_w(component));
1181
1182     file->FileName = load_dynamic_stringW( row, 3 );
1183     reduce_to_longfilename( file->FileName );
1184
1185     file->ShortName = load_dynamic_stringW( row, 3 );
1186     reduce_to_shortfilename( file->ShortName );
1187     
1188     file->FileSize = MSI_RecordGetInteger( row, 4 );
1189     file->Version = load_dynamic_stringW( row, 5 );
1190     file->Language = load_dynamic_stringW( row, 6 );
1191     file->Attributes = MSI_RecordGetInteger( row, 7 );
1192     file->Sequence = MSI_RecordGetInteger( row, 8 );
1193
1194     file->State = 0;
1195
1196     TRACE("File Loaded (%s)\n",debugstr_w(file->File));  
1197
1198     list_add_tail( &package->files, &file->entry );
1199  
1200     return ERROR_SUCCESS;
1201 }
1202
1203 static UINT load_all_files(MSIPACKAGE *package)
1204 {
1205     MSIQUERY * view;
1206     UINT rc;
1207     static const WCHAR Query[] =
1208         {'S','E','L','E','C','T',' ','*',' ', 'F','R','O','M',' ',
1209          '`','F','i','l','e','`',' ', 'O','R','D','E','R',' ','B','Y',' ',
1210          '`','S','e','q','u','e','n','c','e','`', 0};
1211
1212     if (!package)
1213         return ERROR_INVALID_HANDLE;
1214
1215     rc = MSI_DatabaseOpenViewW(package->db, Query, &view);
1216     if (rc != ERROR_SUCCESS)
1217         return ERROR_SUCCESS;
1218
1219     rc = MSI_IterateRecords(view, NULL, load_file, package);
1220     msiobj_release(&view->hdr);
1221
1222     return ERROR_SUCCESS;
1223 }
1224
1225
1226 /*
1227  * I am not doing any of the costing functionality yet. 
1228  * Mostly looking at doing the Component and Feature loading
1229  *
1230  * The native MSI does A LOT of modification to tables here. Mostly adding
1231  * a lot of temporary columns to the Feature and Component tables. 
1232  *
1233  *    note: Native msi also tracks the short filename. But I am only going to
1234  *          track the long ones.  Also looking at this directory table
1235  *          it appears that the directory table does not get the parents
1236  *          resolved base on property only based on their entries in the 
1237  *          directory table.
1238  */
1239 static UINT ACTION_CostInitialize(MSIPACKAGE *package)
1240 {
1241     MSIQUERY * view;
1242     UINT rc;
1243     static const WCHAR Query_all[] =
1244         {'S','E','L','E','C','T',' ','*',' ', 'F','R','O','M',' ',
1245          '`','F','e','a','t','u','r','e','`',0};
1246     static const WCHAR szCosting[] =
1247         {'C','o','s','t','i','n','g','C','o','m','p','l','e','t','e',0 };
1248     static const WCHAR szZero[] = { '0', 0 };
1249     WCHAR buffer[3];
1250     DWORD sz = 3;
1251
1252     MSI_GetPropertyW(package, szCosting, buffer, &sz);
1253     if (buffer[0]=='1')
1254         return ERROR_SUCCESS;
1255     
1256     MSI_SetPropertyW(package, szCosting, szZero);
1257     MSI_SetPropertyW(package, cszRootDrive , c_colon);
1258
1259     rc = MSI_DatabaseOpenViewW(package->db,Query_all,&view);
1260     if (rc != ERROR_SUCCESS)
1261         return rc;
1262
1263     rc = MSI_IterateRecords(view, NULL, load_feature, package);
1264     msiobj_release(&view->hdr);
1265
1266     load_all_files(package);
1267
1268     return ERROR_SUCCESS;
1269 }
1270
1271 static UINT execute_script(MSIPACKAGE *package, UINT script )
1272 {
1273     int i;
1274     UINT rc = ERROR_SUCCESS;
1275
1276     TRACE("Executing Script %i\n",script);
1277
1278     for (i = 0; i < package->script->ActionCount[script]; i++)
1279     {
1280         LPWSTR action;
1281         action = package->script->Actions[script][i];
1282         ui_actionstart(package, action);
1283         TRACE("Executing Action (%s)\n",debugstr_w(action));
1284         rc = ACTION_PerformAction(package, action, TRUE);
1285         msi_free(package->script->Actions[script][i]);
1286         if (rc != ERROR_SUCCESS)
1287             break;
1288     }
1289     msi_free(package->script->Actions[script]);
1290
1291     package->script->ActionCount[script] = 0;
1292     package->script->Actions[script] = NULL;
1293     return rc;
1294 }
1295
1296 static UINT ACTION_FileCost(MSIPACKAGE *package)
1297 {
1298     return ERROR_SUCCESS;
1299 }
1300
1301
1302 static MSIFOLDER *load_folder( MSIPACKAGE *package, LPCWSTR dir )
1303 {
1304     static const WCHAR Query[] =
1305         {'S','E','L','E','C','T',' ','*',' ','F','R','O','M',' ',
1306          '`','D','i','r','e','c', 't','o','r','y','`',' ',
1307          'W','H','E','R','E',' ', '`', 'D','i','r','e','c','t', 'o','r','y','`',
1308          ' ','=',' ','\'','%','s','\'',
1309          0};
1310     LPWSTR ptargetdir, targetdir, srcdir;
1311     LPCWSTR parent;
1312     LPWSTR shortname = NULL;
1313     MSIRECORD * row = 0;
1314     MSIFOLDER *folder;
1315
1316     TRACE("Looking for dir %s\n",debugstr_w(dir));
1317
1318     folder = get_loaded_folder( package, dir );
1319     if (folder)
1320         return folder;
1321
1322     TRACE("Working to load %s\n",debugstr_w(dir));
1323
1324     folder = msi_alloc_zero( sizeof (MSIFOLDER) );
1325     if (!folder)
1326         return NULL;
1327
1328     folder->Directory = strdupW(dir);
1329
1330     row = MSI_QueryGetRecord(package->db, Query, dir);
1331     if (!row)
1332         return NULL;
1333
1334     ptargetdir = targetdir = load_dynamic_stringW(row,3);
1335
1336     /* split src and target dir */
1337     if (strchrW(targetdir,':'))
1338     {
1339         srcdir=strchrW(targetdir,':');
1340         *srcdir=0;
1341         srcdir ++;
1342     }
1343     else
1344         srcdir=NULL;
1345
1346     /* for now only pick long filename versions */
1347     if (strchrW(targetdir,'|'))
1348     {
1349         shortname = targetdir;
1350         targetdir = strchrW(targetdir,'|'); 
1351         *targetdir = 0;
1352         targetdir ++;
1353     }
1354     /* for the sourcedir pick the short filename */
1355     if (srcdir && strchrW(srcdir,'|'))
1356     {
1357         LPWSTR p = strchrW(srcdir,'|'); 
1358         *p = 0;
1359     }
1360
1361     /* now check for root dirs */
1362     if (targetdir[0] == '.' && targetdir[1] == 0)
1363         targetdir = NULL;
1364         
1365     if (targetdir)
1366     {
1367         TRACE("   TargetDefault = %s\n",debugstr_w(targetdir));
1368         msi_free( folder->TargetDefault);
1369         folder->TargetDefault = strdupW(targetdir);
1370     }
1371
1372     if (srcdir)
1373         folder->SourceDefault = strdupW(srcdir);
1374     else if (shortname)
1375         folder->SourceDefault = strdupW(shortname);
1376     else if (targetdir)
1377         folder->SourceDefault = strdupW(targetdir);
1378     msi_free(ptargetdir);
1379         TRACE("   SourceDefault = %s\n", debugstr_w( folder->SourceDefault ));
1380
1381     parent = MSI_RecordGetString(row,2);
1382     if (parent) 
1383     {
1384         folder->Parent = load_folder( package, parent );
1385         if ( folder->Parent )
1386             TRACE("loaded parent %p %s\n", folder->Parent,
1387                   debugstr_w(folder->Parent->Directory));
1388         else
1389             ERR("failed to load parent folder %s\n", debugstr_w(parent));
1390     }
1391
1392     folder->Property = msi_dup_property( package, dir );
1393
1394     msiobj_release(&row->hdr);
1395
1396     list_add_tail( &package->folders, &folder->entry );
1397
1398     TRACE("%s returning %p\n",debugstr_w(dir),folder);
1399
1400     return folder;
1401 }
1402
1403 /* scan for and update current install states */
1404 static void ACTION_UpdateInstallStates(MSIPACKAGE *package)
1405 {
1406     MSICOMPONENT *comp;
1407     MSIFEATURE *feature;
1408
1409     LIST_FOR_EACH_ENTRY( comp, &package->components, MSICOMPONENT, entry )
1410     {
1411         INSTALLSTATE res;
1412         res = MsiGetComponentPathW( package->ProductCode, 
1413                                     comp->ComponentId, NULL, NULL);
1414         if (res < 0)
1415             res = INSTALLSTATE_ABSENT;
1416         comp->Installed = res;
1417     }
1418
1419     LIST_FOR_EACH_ENTRY( feature, &package->features, MSIFEATURE, entry )
1420     {
1421         ComponentList *cl;
1422         INSTALLSTATE res = -10;
1423
1424         LIST_FOR_EACH_ENTRY( cl, &feature->Components, ComponentList, entry )
1425         {
1426             comp= cl->component;
1427
1428             if (res == -10)
1429                 res = comp->Installed;
1430             else
1431             {
1432                 if (res == comp->Installed)
1433                     continue;
1434
1435                 if (res != comp->Installed)
1436                         res = INSTALLSTATE_INCOMPLETE;
1437             }
1438         }
1439         feature->Installed = res;
1440     }
1441 }
1442
1443 static BOOL process_state_property (MSIPACKAGE* package, LPCWSTR property, 
1444                                     INSTALLSTATE state)
1445 {
1446     static const WCHAR all[]={'A','L','L',0};
1447     LPWSTR override;
1448     MSIFEATURE *feature;
1449
1450     override = msi_dup_property( package, property );
1451     if (!override)
1452         return FALSE;
1453  
1454     LIST_FOR_EACH_ENTRY( feature, &package->features, MSIFEATURE, entry )
1455     {
1456         if (strcmpiW(override,all)==0)
1457         {
1458             feature->ActionRequest= state;
1459             feature->Action = state;
1460         }
1461         else
1462         {
1463             LPWSTR ptr = override;
1464             LPWSTR ptr2 = strchrW(override,',');
1465
1466             while (ptr)
1467             {
1468                 if ((ptr2 && strncmpW(ptr,feature->Feature, ptr2-ptr)==0)
1469                     || (!ptr2 && strcmpW(ptr,feature->Feature)==0))
1470                 {
1471                     feature->ActionRequest= state;
1472                     feature->Action = state;
1473                     break;
1474                 }
1475                 if (ptr2)
1476                 {
1477                     ptr=ptr2+1;
1478                     ptr2 = strchrW(ptr,',');
1479                 }
1480                 else
1481                     break;
1482             }
1483         }
1484     } 
1485     msi_free(override);
1486
1487     return TRUE;
1488 }
1489
1490 static UINT SetFeatureStates(MSIPACKAGE *package)
1491 {
1492     LPWSTR level;
1493     INT install_level;
1494     static const WCHAR szlevel[] =
1495         {'I','N','S','T','A','L','L','L','E','V','E','L',0};
1496     static const WCHAR szAddLocal[] =
1497         {'A','D','D','L','O','C','A','L',0};
1498     static const WCHAR szRemove[] =
1499         {'R','E','M','O','V','E',0};
1500     BOOL override = FALSE;
1501     MSICOMPONENT* component;
1502     MSIFEATURE *feature;
1503
1504
1505     /* I do not know if this is where it should happen.. but */
1506
1507     TRACE("Checking Install Level\n");
1508
1509     level = msi_dup_property( package, szlevel );
1510     if (level)
1511     {
1512         install_level = atoiW(level);
1513         msi_free(level);
1514     }
1515     else
1516         install_level = 1;
1517
1518     /* ok hereis the _real_ rub
1519      * all these activation/deactivation things happen in order and things
1520      * later on the list override things earlier on the list.
1521      * 1) INSTALLLEVEL processing
1522      * 2) ADDLOCAL
1523      * 3) REMOVE
1524      * 4) ADDSOURCE
1525      * 5) ADDDEFAULT
1526      * 6) REINSTALL
1527      * 7) COMPADDLOCAL
1528      * 8) COMPADDSOURCE
1529      * 9) FILEADDLOCAL
1530      * 10) FILEADDSOURCE
1531      * 11) FILEADDDEFAULT
1532      * I have confirmed that if ADDLOCAL is stated then the INSTALLLEVEL is
1533      * ignored for all the features. seems strange, especially since it is not
1534      * documented anywhere, but it is how it works. 
1535      *
1536      * I am still ignoring a lot of these. But that is ok for now, ADDLOCAL and
1537      * REMOVE are the big ones, since we don't handle administrative installs
1538      * yet anyway.
1539      */
1540     override |= process_state_property(package,szAddLocal,INSTALLSTATE_LOCAL);
1541     override |= process_state_property(package,szRemove,INSTALLSTATE_ABSENT);
1542
1543     if (!override)
1544     {
1545         LIST_FOR_EACH_ENTRY( feature, &package->features, MSIFEATURE, entry )
1546         {
1547             BOOL feature_state = ((feature->Level > 0) &&
1548                              (feature->Level <= install_level));
1549
1550             if ((feature_state) && (feature->Action == INSTALLSTATE_UNKNOWN))
1551             {
1552                 if (feature->Attributes & msidbFeatureAttributesFavorSource)
1553                 {
1554                     feature->ActionRequest = INSTALLSTATE_SOURCE;
1555                     feature->Action = INSTALLSTATE_SOURCE;
1556                 }
1557                 else if (feature->Attributes & msidbFeatureAttributesFavorAdvertise)
1558                 {
1559                     feature->ActionRequest = INSTALLSTATE_ADVERTISED;
1560                     feature->Action = INSTALLSTATE_ADVERTISED;
1561                 }
1562                 else
1563                 {
1564                     feature->ActionRequest = INSTALLSTATE_LOCAL;
1565                     feature->Action = INSTALLSTATE_LOCAL;
1566                 }
1567             }
1568         }
1569     }
1570     else
1571     {
1572         /* set the Preselected Property */
1573         static const WCHAR szPreselected[] = {'P','r','e','s','e','l','e','c','t','e','d',0};
1574         static const WCHAR szOne[] = { '1', 0 };
1575
1576         MSI_SetPropertyW(package,szPreselected,szOne);
1577     }
1578
1579     /*
1580      * now we want to enable or disable components base on feature 
1581     */
1582
1583     LIST_FOR_EACH_ENTRY( feature, &package->features, MSIFEATURE, entry )
1584     {
1585         ComponentList *cl;
1586
1587         TRACE("Examining Feature %s (Installed %i, Action %i, Request %i)\n",
1588             debugstr_w(feature->Feature), feature->Installed, feature->Action,
1589             feature->ActionRequest);
1590
1591         LIST_FOR_EACH_ENTRY( cl, &feature->Components, ComponentList, entry )
1592         {
1593             component = cl->component;
1594
1595             if (!component->Enabled)
1596             {
1597                 component->Action = INSTALLSTATE_UNKNOWN;
1598                 component->ActionRequest = INSTALLSTATE_UNKNOWN;
1599             }
1600             else
1601             {
1602                 if (feature->Action == INSTALLSTATE_LOCAL)
1603                 {
1604                     component->Action = INSTALLSTATE_LOCAL;
1605                     component->ActionRequest = INSTALLSTATE_LOCAL;
1606                 }
1607                 else if (feature->ActionRequest == INSTALLSTATE_SOURCE)
1608                 {
1609                     if ((component->Action == INSTALLSTATE_UNKNOWN) ||
1610                         (component->Action == INSTALLSTATE_ABSENT) ||
1611                         (component->Action == INSTALLSTATE_ADVERTISED))
1612                            
1613                     {
1614                         component->Action = INSTALLSTATE_SOURCE;
1615                         component->ActionRequest = INSTALLSTATE_SOURCE;
1616                     }
1617                 }
1618                 else if (feature->ActionRequest == INSTALLSTATE_ADVERTISED)
1619                 {
1620                     if ((component->Action == INSTALLSTATE_UNKNOWN) ||
1621                         (component->Action == INSTALLSTATE_ABSENT))
1622                            
1623                     {
1624                         component->Action = INSTALLSTATE_ADVERTISED;
1625                         component->ActionRequest = INSTALLSTATE_ADVERTISED;
1626                     }
1627                 }
1628                 else if (feature->ActionRequest == INSTALLSTATE_ABSENT)
1629                 {
1630                     if (component->Action == INSTALLSTATE_UNKNOWN)
1631                     {
1632                         component->Action = INSTALLSTATE_ABSENT;
1633                         component->ActionRequest = INSTALLSTATE_ABSENT;
1634                     }
1635                 }
1636             }
1637         }
1638     } 
1639
1640     LIST_FOR_EACH_ENTRY( component, &package->components, MSICOMPONENT, entry )
1641     {
1642         TRACE("Result: Component %s (Installed %i, Action %i, Request %i)\n",
1643             debugstr_w(component->Component), component->Installed, 
1644             component->Action, component->ActionRequest);
1645     }
1646
1647
1648     return ERROR_SUCCESS;
1649 }
1650
1651 static UINT ITERATE_CostFinalizeDirectories(MSIRECORD *row, LPVOID param)
1652 {
1653     MSIPACKAGE *package = (MSIPACKAGE*)param;
1654     LPCWSTR name;
1655     LPWSTR path;
1656
1657     name = MSI_RecordGetString(row,1);
1658
1659     /* This helper function now does ALL the work */
1660     TRACE("Dir %s ...\n",debugstr_w(name));
1661     load_folder(package,name);
1662     path = resolve_folder(package,name,FALSE,TRUE,NULL);
1663     TRACE("resolves to %s\n",debugstr_w(path));
1664     msi_free(path);
1665
1666     return ERROR_SUCCESS;
1667 }
1668
1669 static UINT ITERATE_CostFinalizeConditions(MSIRECORD *row, LPVOID param)
1670 {
1671     MSIPACKAGE *package = (MSIPACKAGE*)param;
1672     LPCWSTR name;
1673     MSIFEATURE *feature;
1674
1675     name = MSI_RecordGetString( row, 1 );
1676
1677     feature = get_loaded_feature( package, name );
1678     if (!feature)
1679         ERR("FAILED to find loaded feature %s\n",debugstr_w(name));
1680     else
1681     {
1682         LPCWSTR Condition;
1683         Condition = MSI_RecordGetString(row,3);
1684
1685         if (MSI_EvaluateConditionW(package,Condition) == MSICONDITION_TRUE)
1686         {
1687             int level = MSI_RecordGetInteger(row,2);
1688             TRACE("Reseting feature %s to level %i\n", debugstr_w(name), level);
1689             feature->Level = level;
1690         }
1691     }
1692     return ERROR_SUCCESS;
1693 }
1694
1695
1696 /* 
1697  * A lot is done in this function aside from just the costing.
1698  * The costing needs to be implemented at some point but for now I am going
1699  * to focus on the directory building
1700  *
1701  */
1702 static UINT ACTION_CostFinalize(MSIPACKAGE *package)
1703 {
1704     static const WCHAR ExecSeqQuery[] =
1705         {'S','E','L','E','C','T',' ','*',' ','F','R','O','M',' ',
1706          '`','D','i','r','e','c','t','o','r','y','`',0};
1707     static const WCHAR ConditionQuery[] =
1708         {'S','E','L','E','C','T',' ','*',' ','F','R','O','M',' ',
1709          '`','C','o','n','d','i','t','i','o','n','`',0};
1710     static const WCHAR szCosting[] =
1711         {'C','o','s','t','i','n','g','C','o','m','p','l','e','t','e',0 };
1712     static const WCHAR szlevel[] =
1713         {'I','N','S','T','A','L','L','L','E','V','E','L',0};
1714     static const WCHAR szOne[] = { '1', 0 };
1715     MSICOMPONENT *comp;
1716     MSIFILE *file;
1717     UINT rc;
1718     MSIQUERY * view;
1719     LPWSTR level;
1720     DWORD sz = 3;
1721     WCHAR buffer[3];
1722
1723     MSI_GetPropertyW(package, szCosting, buffer, &sz);
1724     if (buffer[0]=='1')
1725         return ERROR_SUCCESS;
1726
1727     TRACE("Building Directory properties\n");
1728
1729     rc = MSI_DatabaseOpenViewW(package->db, ExecSeqQuery, &view);
1730     if (rc == ERROR_SUCCESS)
1731     {
1732         rc = MSI_IterateRecords(view, NULL, ITERATE_CostFinalizeDirectories,
1733                         package);
1734         msiobj_release(&view->hdr);
1735     }
1736
1737     TRACE("File calculations\n");
1738
1739     LIST_FOR_EACH_ENTRY( file, &package->files, MSIFILE, entry )
1740     {
1741         MSICOMPONENT* comp = file->Component;
1742         LPWSTR p;
1743
1744         if (!comp)
1745             continue;
1746
1747         /* calculate target */
1748         p = resolve_folder(package, comp->Directory, FALSE, FALSE, NULL);
1749
1750         msi_free(file->TargetPath);
1751
1752         TRACE("file %s is named %s\n",
1753                debugstr_w(file->File),debugstr_w(file->FileName));       
1754
1755         file->TargetPath = build_directory_name(2, p, file->FileName);
1756
1757         msi_free(p);
1758
1759         TRACE("file %s resolves to %s\n",
1760                debugstr_w(file->File),debugstr_w(file->TargetPath));       
1761
1762         if (GetFileAttributesW(file->TargetPath) == INVALID_FILE_ATTRIBUTES)
1763         {
1764             file->State = 1;
1765             comp->Cost += file->FileSize;
1766             continue;
1767         }
1768
1769         if (file->Version)
1770         {
1771             DWORD handle;
1772             DWORD versize;
1773             UINT sz;
1774             LPVOID version;
1775             static const WCHAR name[] = 
1776                 {'\\',0};
1777             static const WCHAR name_fmt[] = 
1778                 {'%','u','.','%','u','.','%','u','.','%','u',0};
1779             WCHAR filever[0x100];
1780             VS_FIXEDFILEINFO *lpVer;
1781
1782             TRACE("Version comparison.. \n");
1783             versize = GetFileVersionInfoSizeW(file->TargetPath,&handle);
1784             version = msi_alloc(versize);
1785             GetFileVersionInfoW(file->TargetPath, 0, versize, version);
1786
1787             VerQueryValueW(version, name, (LPVOID*)&lpVer, &sz);
1788
1789             sprintfW(filever,name_fmt,
1790                 HIWORD(lpVer->dwFileVersionMS),
1791                 LOWORD(lpVer->dwFileVersionMS),
1792                 HIWORD(lpVer->dwFileVersionLS),
1793                 LOWORD(lpVer->dwFileVersionLS));
1794
1795             TRACE("new %s old %s\n", debugstr_w(file->Version),
1796                   debugstr_w(filever));
1797             if (strcmpiW(filever,file->Version)<0)
1798             {
1799                 file->State = 2;
1800                 FIXME("cost should be diff in size\n");
1801                 comp->Cost += file->FileSize;
1802             }
1803             else
1804                 file->State = 3;
1805             msi_free(version);
1806         }
1807         else
1808             file->State = 3;
1809     }
1810
1811     TRACE("Evaluating Condition Table\n");
1812
1813     rc = MSI_DatabaseOpenViewW(package->db, ConditionQuery, &view);
1814     if (rc == ERROR_SUCCESS)
1815     {
1816         rc = MSI_IterateRecords(view, NULL, ITERATE_CostFinalizeConditions,
1817                     package);
1818         msiobj_release(&view->hdr);
1819     }
1820
1821     TRACE("Enabling or Disabling Components\n");
1822     LIST_FOR_EACH_ENTRY( comp, &package->components, MSICOMPONENT, entry )
1823     {
1824         if (comp->Condition)
1825         {
1826             if (MSI_EvaluateConditionW(package,
1827                 comp->Condition) == MSICONDITION_FALSE)
1828             {
1829                 TRACE("Disabling component %s\n", debugstr_w(comp->Component));
1830                 comp->Enabled = FALSE;
1831             }
1832         }
1833     }
1834
1835     MSI_SetPropertyW(package,szCosting,szOne);
1836     /* set default run level if not set */
1837     level = msi_dup_property( package, szlevel );
1838     if (!level)
1839         MSI_SetPropertyW(package,szlevel, szOne);
1840     msi_free(level);
1841
1842     ACTION_UpdateInstallStates(package);
1843
1844     return SetFeatureStates(package);
1845 }
1846
1847 /* OK this value is "interpreted" and then formatted based on the 
1848    first few characters */
1849 static LPSTR parse_value(MSIPACKAGE *package, LPCWSTR value, DWORD *type, 
1850                          DWORD *size)
1851 {
1852     LPSTR data = NULL;
1853     if (value[0]=='#' && value[1]!='#' && value[1]!='%')
1854     {
1855         if (value[1]=='x')
1856         {
1857             LPWSTR ptr;
1858             CHAR byte[5];
1859             LPWSTR deformated = NULL;
1860             int count;
1861
1862             deformat_string(package, &value[2], &deformated);
1863
1864             /* binary value type */
1865             ptr = deformated;
1866             *type = REG_BINARY;
1867             if (strlenW(ptr)%2)
1868                 *size = (strlenW(ptr)/2)+1;
1869             else
1870                 *size = strlenW(ptr)/2;
1871
1872             data = msi_alloc(*size);
1873
1874             byte[0] = '0'; 
1875             byte[1] = 'x'; 
1876             byte[4] = 0; 
1877             count = 0;
1878             /* if uneven pad with a zero in front */
1879             if (strlenW(ptr)%2)
1880             {
1881                 byte[2]= '0';
1882                 byte[3]= *ptr;
1883                 ptr++;
1884                 data[count] = (BYTE)strtol(byte,NULL,0);
1885                 count ++;
1886                 TRACE("Uneven byte count\n");
1887             }
1888             while (*ptr)
1889             {
1890                 byte[2]= *ptr;
1891                 ptr++;
1892                 byte[3]= *ptr;
1893                 ptr++;
1894                 data[count] = (BYTE)strtol(byte,NULL,0);
1895                 count ++;
1896             }
1897             msi_free(deformated);
1898
1899             TRACE("Data %li bytes(%i)\n",*size,count);
1900         }
1901         else
1902         {
1903             LPWSTR deformated;
1904             LPWSTR p;
1905             DWORD d = 0;
1906             deformat_string(package, &value[1], &deformated);
1907
1908             *type=REG_DWORD; 
1909             *size = sizeof(DWORD);
1910             data = msi_alloc(*size);
1911             p = deformated;
1912             if (*p == '-')
1913                 p++;
1914             while (*p)
1915             {
1916                 if ( (*p < '0') || (*p > '9') )
1917                     break;
1918                 d *= 10;
1919                 d += (*p - '0');
1920                 p++;
1921             }
1922             if (deformated[0] == '-')
1923                 d = -d;
1924             *(LPDWORD)data = d;
1925             TRACE("DWORD %li\n",*(LPDWORD)data);
1926
1927             msi_free(deformated);
1928         }
1929     }
1930     else
1931     {
1932         static const WCHAR szMulti[] = {'[','~',']',0};
1933         LPCWSTR ptr;
1934         *type=REG_SZ;
1935
1936         if (value[0]=='#')
1937         {
1938             if (value[1]=='%')
1939             {
1940                 ptr = &value[2];
1941                 *type=REG_EXPAND_SZ;
1942             }
1943             else
1944                 ptr = &value[1];
1945          }
1946          else
1947             ptr=value;
1948
1949         if (strstrW(value,szMulti))
1950             *type = REG_MULTI_SZ;
1951
1952         *size = deformat_string(package, ptr,(LPWSTR*)&data);
1953     }
1954     return data;
1955 }
1956
1957 static UINT ITERATE_WriteRegistryValues(MSIRECORD *row, LPVOID param)
1958 {
1959     MSIPACKAGE *package = (MSIPACKAGE*)param;
1960     static const WCHAR szHCR[] = 
1961         {'H','K','E','Y','_','C','L','A','S','S','E','S','_',
1962          'R','O','O','T','\\',0};
1963     static const WCHAR szHCU[] =
1964         {'H','K','E','Y','_','C','U','R','R','E','N','T','_',
1965          'U','S','E','R','\\',0};
1966     static const WCHAR szHLM[] =
1967         {'H','K','E','Y','_','L','O','C','A','L','_',
1968          'M','A','C','H','I','N','E','\\',0};
1969     static const WCHAR szHU[] =
1970         {'H','K','E','Y','_','U','S','E','R','S','\\',0};
1971
1972     LPSTR value_data = NULL;
1973     HKEY  root_key, hkey;
1974     DWORD type,size;
1975     LPWSTR  deformated;
1976     LPCWSTR szRoot, component, name, key, value;
1977     MSICOMPONENT *comp;
1978     MSIRECORD * uirow;
1979     LPWSTR uikey;
1980     INT   root;
1981     BOOL check_first = FALSE;
1982     UINT rc;
1983
1984     ui_progress(package,2,0,0,0);
1985
1986     value = NULL;
1987     key = NULL;
1988     uikey = NULL;
1989     name = NULL;
1990
1991     component = MSI_RecordGetString(row, 6);
1992     comp = get_loaded_component(package,component);
1993     if (!comp)
1994         return ERROR_SUCCESS;
1995
1996     if (!ACTION_VerifyComponentForAction(package, comp, INSTALLSTATE_LOCAL))
1997     {
1998         TRACE("Skipping write due to disabled component %s\n",
1999                         debugstr_w(component));
2000
2001         comp->Action = comp->Installed;
2002
2003         return ERROR_SUCCESS;
2004     }
2005
2006     comp->Action = INSTALLSTATE_LOCAL;
2007
2008     name = MSI_RecordGetString(row, 4);
2009     if( MSI_RecordIsNull(row,5) && name )
2010     {
2011         /* null values can have special meanings */
2012         if (name[0]=='-' && name[1] == 0)
2013                 return ERROR_SUCCESS;
2014         else if ((name[0]=='+' && name[1] == 0) || 
2015                  (name[0] == '*' && name[1] == 0))
2016                 name = NULL;
2017         check_first = TRUE;
2018     }
2019
2020     root = MSI_RecordGetInteger(row,2);
2021     key = MSI_RecordGetString(row, 3);
2022
2023     /* get the root key */
2024     switch (root)
2025     {
2026         case -1: 
2027             {
2028                 static const WCHAR szALLUSER[] = {'A','L','L','U','S','E','R','S',0};
2029                 LPWSTR all_users = msi_dup_property( package, szALLUSER );
2030                 if (all_users && all_users[0] == '1')
2031                 {
2032                     root_key = HKEY_LOCAL_MACHINE;
2033                     szRoot = szHLM;
2034                 }
2035                 else
2036                 {
2037                     root_key = HKEY_CURRENT_USER;
2038                     szRoot = szHCU;
2039                 }
2040                 msi_free(all_users);
2041             }
2042                  break;
2043         case 0:  root_key = HKEY_CLASSES_ROOT; 
2044                  szRoot = szHCR;
2045                  break;
2046         case 1:  root_key = HKEY_CURRENT_USER;
2047                  szRoot = szHCU;
2048                  break;
2049         case 2:  root_key = HKEY_LOCAL_MACHINE;
2050                  szRoot = szHLM;
2051                  break;
2052         case 3:  root_key = HKEY_USERS; 
2053                  szRoot = szHU;
2054                  break;
2055         default:
2056                  ERR("Unknown root %i\n",root);
2057                  root_key=NULL;
2058                  szRoot = NULL;
2059                  break;
2060     }
2061     if (!root_key)
2062         return ERROR_SUCCESS;
2063
2064     deformat_string(package, key , &deformated);
2065     size = strlenW(deformated) + strlenW(szRoot) + 1;
2066     uikey = msi_alloc(size*sizeof(WCHAR));
2067     strcpyW(uikey,szRoot);
2068     strcatW(uikey,deformated);
2069
2070     if (RegCreateKeyW( root_key, deformated, &hkey))
2071     {
2072         ERR("Could not create key %s\n",debugstr_w(deformated));
2073         msi_free(deformated);
2074         msi_free(uikey);
2075         return ERROR_SUCCESS;
2076     }
2077     msi_free(deformated);
2078
2079     value = MSI_RecordGetString(row,5);
2080     if (value)
2081         value_data = parse_value(package, value, &type, &size); 
2082     else
2083     {
2084         static const WCHAR szEmpty[] = {0};
2085         value_data = (LPSTR)strdupW(szEmpty);
2086         size = 0;
2087         type = REG_SZ;
2088     }
2089
2090     deformat_string(package, name, &deformated);
2091
2092     /* get the double nulls to terminate SZ_MULTI */
2093     if (type == REG_MULTI_SZ)
2094         size +=sizeof(WCHAR);
2095
2096     if (!check_first)
2097     {
2098         TRACE("Setting value %s of %s\n",debugstr_w(deformated),
2099                         debugstr_w(uikey));
2100         RegSetValueExW(hkey, deformated, 0, type, (LPBYTE)value_data, size);
2101     }
2102     else
2103     {
2104         DWORD sz = 0;
2105         rc = RegQueryValueExW(hkey, deformated, NULL, NULL, NULL, &sz);
2106         if (rc == ERROR_SUCCESS || rc == ERROR_MORE_DATA)
2107         {
2108             TRACE("value %s of %s checked already exists\n",
2109                             debugstr_w(deformated), debugstr_w(uikey));
2110         }
2111         else
2112         {
2113             TRACE("Checked and setting value %s of %s\n",
2114                             debugstr_w(deformated), debugstr_w(uikey));
2115             if (deformated || size)
2116                 RegSetValueExW(hkey, deformated, 0, type, (LPBYTE) value_data, size);
2117         }
2118     }
2119     RegCloseKey(hkey);
2120
2121     uirow = MSI_CreateRecord(3);
2122     MSI_RecordSetStringW(uirow,2,deformated);
2123     MSI_RecordSetStringW(uirow,1,uikey);
2124
2125     if (type == REG_SZ)
2126         MSI_RecordSetStringW(uirow,3,(LPWSTR)value_data);
2127     else
2128         MSI_RecordSetStringW(uirow,3,value);
2129
2130     ui_actiondata(package,szWriteRegistryValues,uirow);
2131     msiobj_release( &uirow->hdr );
2132
2133     msi_free(value_data);
2134     msi_free(deformated);
2135     msi_free(uikey);
2136
2137     return ERROR_SUCCESS;
2138 }
2139
2140 static UINT ACTION_WriteRegistryValues(MSIPACKAGE *package)
2141 {
2142     UINT rc;
2143     MSIQUERY * view;
2144     static const WCHAR ExecSeqQuery[] =
2145         {'S','E','L','E','C','T',' ','*',' ','F','R','O','M',' ',
2146          '`','R','e','g','i','s','t','r','y','`',0 };
2147
2148     if (!package)
2149         return ERROR_INVALID_HANDLE;
2150
2151     rc = MSI_DatabaseOpenViewW(package->db, ExecSeqQuery, &view);
2152     if (rc != ERROR_SUCCESS)
2153         return ERROR_SUCCESS;
2154
2155     /* increment progress bar each time action data is sent */
2156     ui_progress(package,1,REG_PROGRESS_VALUE,1,0);
2157
2158     rc = MSI_IterateRecords(view, NULL, ITERATE_WriteRegistryValues, package);
2159
2160     msiobj_release(&view->hdr);
2161     return rc;
2162 }
2163
2164 static UINT ACTION_InstallInitialize(MSIPACKAGE *package)
2165 {
2166     package->script->CurrentlyScripting = TRUE;
2167
2168     return ERROR_SUCCESS;
2169 }
2170
2171
2172 static UINT ACTION_InstallValidate(MSIPACKAGE *package)
2173 {
2174     MSICOMPONENT *comp;
2175     DWORD progress = 0;
2176     DWORD total = 0;
2177     static const WCHAR q1[]=
2178         {'S','E','L','E','C','T',' ','*',' ', 'F','R','O','M',' ',
2179          '`','R','e','g','i','s','t','r','y','`',0};
2180     UINT rc;
2181     MSIQUERY * view;
2182     MSIFEATURE *feature;
2183     MSIFILE *file;
2184
2185     TRACE("InstallValidate\n");
2186
2187     rc = MSI_DatabaseOpenViewW(package->db, q1, &view);
2188     if (rc == ERROR_SUCCESS)
2189     {
2190         MSI_IterateRecords( view, &progress, NULL, package );
2191         msiobj_release( &view->hdr );
2192         total += progress * REG_PROGRESS_VALUE;
2193     }
2194
2195     LIST_FOR_EACH_ENTRY( comp, &package->components, MSICOMPONENT, entry )
2196         total += COMPONENT_PROGRESS_VALUE;
2197
2198     LIST_FOR_EACH_ENTRY( file, &package->files, MSIFILE, entry )
2199         total += file->FileSize;
2200
2201     ui_progress(package,0,total,0,0);
2202
2203     LIST_FOR_EACH_ENTRY( feature, &package->features, MSIFEATURE, entry )
2204     {
2205         TRACE("Feature: %s; Installed: %i; Action %i; Request %i\n",
2206             debugstr_w(feature->Feature), feature->Installed, feature->Action,
2207             feature->ActionRequest);
2208     }
2209     
2210     return ERROR_SUCCESS;
2211 }
2212
2213 static UINT ITERATE_LaunchConditions(MSIRECORD *row, LPVOID param)
2214 {
2215     MSIPACKAGE* package = (MSIPACKAGE*)param;
2216     LPCWSTR cond = NULL; 
2217     LPCWSTR message = NULL;
2218     static const WCHAR title[]=
2219         {'I','n','s','t','a','l','l',' ','F','a', 'i','l','e','d',0};
2220
2221     cond = MSI_RecordGetString(row,1);
2222
2223     if (MSI_EvaluateConditionW(package,cond) != MSICONDITION_TRUE)
2224     {
2225         LPWSTR deformated;
2226         message = MSI_RecordGetString(row,2);
2227         deformat_string(package,message,&deformated); 
2228         MessageBoxW(NULL,deformated,title,MB_OK);
2229         msi_free(deformated);
2230         return ERROR_FUNCTION_FAILED;
2231     }
2232
2233     return ERROR_SUCCESS;
2234 }
2235
2236 static UINT ACTION_LaunchConditions(MSIPACKAGE *package)
2237 {
2238     UINT rc;
2239     MSIQUERY * view = NULL;
2240     static const WCHAR ExecSeqQuery[] =
2241         {'S','E','L','E','C','T',' ','*',' ','F','R','O','M',' ',
2242          '`','L','a','u','n','c','h','C','o','n','d','i','t','i','o','n','`',0};
2243
2244     TRACE("Checking launch conditions\n");
2245
2246     rc = MSI_DatabaseOpenViewW(package->db, ExecSeqQuery, &view);
2247     if (rc != ERROR_SUCCESS)
2248         return ERROR_SUCCESS;
2249
2250     rc = MSI_IterateRecords(view, NULL, ITERATE_LaunchConditions, package);
2251     msiobj_release(&view->hdr);
2252
2253     return rc;
2254 }
2255
2256 static LPWSTR resolve_keypath( MSIPACKAGE* package, MSICOMPONENT *cmp )
2257 {
2258
2259     if (!cmp->KeyPath)
2260         return resolve_folder(package,cmp->Directory,FALSE,FALSE,NULL);
2261
2262     if (cmp->Attributes & msidbComponentAttributesRegistryKeyPath)
2263     {
2264         MSIRECORD * row = 0;
2265         UINT root,len;
2266         LPWSTR deformated,buffer,deformated_name;
2267         LPCWSTR key,name;
2268         static const WCHAR ExecSeqQuery[] =
2269             {'S','E','L','E','C','T',' ','*',' ','F','R','O','M',' ',
2270              '`','R','e','g','i','s','t','r','y','`',' ',
2271              'W','H','E','R','E',' ', '`','R','e','g','i','s','t','r','y','`',
2272              ' ','=',' ' ,'\'','%','s','\'',0 };
2273         static const WCHAR fmt[]={'%','0','2','i',':','\\','%','s','\\',0};
2274         static const WCHAR fmt2[]=
2275             {'%','0','2','i',':','\\','%','s','\\','%','s',0};
2276
2277         row = MSI_QueryGetRecord(package->db, ExecSeqQuery,cmp->KeyPath);
2278         if (!row)
2279             return NULL;
2280
2281         root = MSI_RecordGetInteger(row,2);
2282         key = MSI_RecordGetString(row, 3);
2283         name = MSI_RecordGetString(row, 4);
2284         deformat_string(package, key , &deformated);
2285         deformat_string(package, name, &deformated_name);
2286
2287         len = strlenW(deformated) + 6;
2288         if (deformated_name)
2289             len+=strlenW(deformated_name);
2290
2291         buffer = msi_alloc( len *sizeof(WCHAR));
2292
2293         if (deformated_name)
2294             sprintfW(buffer,fmt2,root,deformated,deformated_name);
2295         else
2296             sprintfW(buffer,fmt,root,deformated);
2297
2298         msi_free(deformated);
2299         msi_free(deformated_name);
2300         msiobj_release(&row->hdr);
2301
2302         return buffer;
2303     }
2304     else if (cmp->Attributes & msidbComponentAttributesODBCDataSource)
2305     {
2306         FIXME("UNIMPLEMENTED keypath as ODBC Source\n");
2307         return NULL;
2308     }
2309     else
2310     {
2311         MSIFILE *file = get_loaded_file( package, cmp->KeyPath );
2312
2313         if (file)
2314             return strdupW( file->TargetPath );
2315     }
2316     return NULL;
2317 }
2318
2319 static HKEY openSharedDLLsKey(void)
2320 {
2321     HKEY hkey=0;
2322     static const WCHAR path[] =
2323         {'S','o','f','t','w','a','r','e','\\',
2324          'M','i','c','r','o','s','o','f','t','\\',
2325          'W','i','n','d','o','w','s','\\',
2326          'C','u','r','r','e','n','t','V','e','r','s','i','o','n','\\',
2327          'S','h','a','r','e','d','D','L','L','s',0};
2328
2329     RegCreateKeyW(HKEY_LOCAL_MACHINE,path,&hkey);
2330     return hkey;
2331 }
2332
2333 static UINT ACTION_GetSharedDLLsCount(LPCWSTR dll)
2334 {
2335     HKEY hkey;
2336     DWORD count=0;
2337     DWORD type;
2338     DWORD sz = sizeof(count);
2339     DWORD rc;
2340     
2341     hkey = openSharedDLLsKey();
2342     rc = RegQueryValueExW(hkey, dll, NULL, &type, (LPBYTE)&count, &sz);
2343     if (rc != ERROR_SUCCESS)
2344         count = 0;
2345     RegCloseKey(hkey);
2346     return count;
2347 }
2348
2349 static UINT ACTION_WriteSharedDLLsCount(LPCWSTR path, UINT count)
2350 {
2351     HKEY hkey;
2352
2353     hkey = openSharedDLLsKey();
2354     if (count > 0)
2355         msi_reg_set_val_dword( hkey, path, count );
2356     else
2357         RegDeleteValueW(hkey,path);
2358     RegCloseKey(hkey);
2359     return count;
2360 }
2361
2362 /*
2363  * Return TRUE if the count should be written out and FALSE if not
2364  */
2365 static void ACTION_RefCountComponent( MSIPACKAGE* package, MSICOMPONENT *comp )
2366 {
2367     MSIFEATURE *feature;
2368     INT count = 0;
2369     BOOL write = FALSE;
2370
2371     /* only refcount DLLs */
2372     if (comp->KeyPath == NULL || 
2373         comp->Attributes & msidbComponentAttributesRegistryKeyPath || 
2374         comp->Attributes & msidbComponentAttributesODBCDataSource)
2375         write = FALSE;
2376     else
2377     {
2378         count = ACTION_GetSharedDLLsCount( comp->FullKeypath);
2379         write = (count > 0);
2380
2381         if (comp->Attributes & msidbComponentAttributesSharedDllRefCount)
2382             write = TRUE;
2383     }
2384
2385     /* increment counts */
2386     LIST_FOR_EACH_ENTRY( feature, &package->features, MSIFEATURE, entry )
2387     {
2388         ComponentList *cl;
2389
2390         if (!ACTION_VerifyFeatureForAction( feature, INSTALLSTATE_LOCAL ))
2391             continue;
2392
2393         LIST_FOR_EACH_ENTRY( cl, &feature->Components, ComponentList, entry )
2394         {
2395             if ( cl->component == comp )
2396                 count++;
2397         }
2398     }
2399
2400     /* decrement counts */
2401     LIST_FOR_EACH_ENTRY( feature, &package->features, MSIFEATURE, entry )
2402     {
2403         ComponentList *cl;
2404
2405         if (!ACTION_VerifyFeatureForAction( feature, INSTALLSTATE_ABSENT ))
2406             continue;
2407
2408         LIST_FOR_EACH_ENTRY( cl, &feature->Components, ComponentList, entry )
2409         {
2410             if ( cl->component == comp )
2411                 count--;
2412         }
2413     }
2414
2415     /* ref count all the files in the component */
2416     if (write)
2417     {
2418         MSIFILE *file;
2419
2420         LIST_FOR_EACH_ENTRY( file, &package->files, MSIFILE, entry )
2421         {
2422             if (file->Component == comp)
2423                 ACTION_WriteSharedDLLsCount( file->TargetPath, count );
2424         }
2425     }
2426     
2427     /* add a count for permenent */
2428     if (comp->Attributes & msidbComponentAttributesPermanent)
2429         count ++;
2430     
2431     comp->RefCount = count;
2432
2433     if (write)
2434         ACTION_WriteSharedDLLsCount( comp->FullKeypath, comp->RefCount );
2435 }
2436
2437 /*
2438  * Ok further analysis makes me think that this work is
2439  * actually done in the PublishComponents and PublishFeatures
2440  * step, and not here.  It appears like the keypath and all that is
2441  * resolved in this step, however actually written in the Publish steps.
2442  * But we will leave it here for now because it is unclear
2443  */
2444 static UINT ACTION_ProcessComponents(MSIPACKAGE *package)
2445 {
2446     WCHAR squished_pc[GUID_SIZE];
2447     WCHAR squished_cc[GUID_SIZE];
2448     UINT rc;
2449     MSICOMPONENT *comp;
2450     HKEY hkey=0,hkey2=0;
2451
2452     if (!package)
2453         return ERROR_INVALID_HANDLE;
2454
2455     /* writes the Component and Features values to the registry */
2456
2457     rc = MSIREG_OpenComponents(&hkey);
2458     if (rc != ERROR_SUCCESS)
2459         goto end;
2460       
2461     squash_guid(package->ProductCode,squished_pc);
2462     ui_progress(package,1,COMPONENT_PROGRESS_VALUE,1,0);
2463
2464     LIST_FOR_EACH_ENTRY( comp, &package->components, MSICOMPONENT, entry )
2465     {
2466         ui_progress(package,2,0,0,0);
2467         if (comp->ComponentId)
2468         {
2469             WCHAR *keypath = NULL;
2470             MSIRECORD * uirow;
2471
2472             squash_guid(comp->ComponentId,squished_cc);
2473            
2474             keypath = resolve_keypath( package, comp );
2475             comp->FullKeypath = keypath;
2476
2477             /* do the refcounting */
2478             ACTION_RefCountComponent( package, comp );
2479
2480             TRACE("Component %s (%s), Keypath=%s, RefCount=%i\n", 
2481                             debugstr_w(comp->Component),
2482                             debugstr_w(squished_cc),
2483                             debugstr_w(comp->FullKeypath), 
2484                             comp->RefCount);
2485             /*
2486             * Write the keypath out if the component is to be registered
2487             * and delete the key if the component is to be deregistered
2488             */
2489             if (ACTION_VerifyComponentForAction(package, comp,
2490                                     INSTALLSTATE_LOCAL))
2491             {
2492                 rc = RegCreateKeyW(hkey,squished_cc,&hkey2);
2493                 if (rc != ERROR_SUCCESS)
2494                     continue;
2495
2496                 if (keypath)
2497                 {
2498                     msi_reg_set_val_str( hkey2, squished_pc, keypath );
2499
2500                     if (comp->Attributes & msidbComponentAttributesPermanent)
2501                     {
2502                         static const WCHAR szPermKey[] =
2503                             { '0','0','0','0','0','0','0','0','0','0','0','0',
2504                               '0','0','0','0','0','0','0','0','0','0','0','0',
2505                               '0','0','0','0','0','0','0','0',0};
2506
2507                         msi_reg_set_val_str( hkey2, szPermKey, keypath );
2508                     }
2509                     
2510                     RegCloseKey(hkey2);
2511         
2512                     /* UI stuff */
2513                     uirow = MSI_CreateRecord(3);
2514                     MSI_RecordSetStringW(uirow,1,package->ProductCode);
2515                     MSI_RecordSetStringW(uirow,2,comp->ComponentId);
2516                     MSI_RecordSetStringW(uirow,3,keypath);
2517                     ui_actiondata(package,szProcessComponents,uirow);
2518                     msiobj_release( &uirow->hdr );
2519                }
2520             }
2521             else if (ACTION_VerifyComponentForAction(package, comp,
2522                                     INSTALLSTATE_ABSENT))
2523             {
2524                 DWORD res;
2525
2526                 rc = RegOpenKeyW(hkey,squished_cc,&hkey2);
2527                 if (rc != ERROR_SUCCESS)
2528                     continue;
2529
2530                 RegDeleteValueW(hkey2,squished_pc);
2531
2532                 /* if the key is empty delete it */
2533                 res = RegEnumKeyExW(hkey2,0,NULL,0,0,NULL,0,NULL);
2534                 RegCloseKey(hkey2);
2535                 if (res == ERROR_NO_MORE_ITEMS)
2536                     RegDeleteKeyW(hkey,squished_cc);
2537         
2538                 /* UI stuff */
2539                 uirow = MSI_CreateRecord(2);
2540                 MSI_RecordSetStringW(uirow,1,package->ProductCode);
2541                 MSI_RecordSetStringW(uirow,2,comp->ComponentId);
2542                 ui_actiondata(package,szProcessComponents,uirow);
2543                 msiobj_release( &uirow->hdr );
2544             }
2545         }
2546     } 
2547 end:
2548     RegCloseKey(hkey);
2549     return rc;
2550 }
2551
2552 typedef struct {
2553     CLSID       clsid;
2554     LPWSTR      source;
2555
2556     LPWSTR      path;
2557     ITypeLib    *ptLib;
2558 } typelib_struct;
2559
2560 static BOOL CALLBACK Typelib_EnumResNameProc( HMODULE hModule, LPCWSTR lpszType, 
2561                                        LPWSTR lpszName, LONG_PTR lParam)
2562 {
2563     TLIBATTR *attr;
2564     typelib_struct *tl_struct = (typelib_struct*) lParam;
2565     static const WCHAR fmt[] = {'%','s','\\','%','i',0};
2566     int sz; 
2567     HRESULT res;
2568
2569     if (!IS_INTRESOURCE(lpszName))
2570     {
2571         ERR("Not Int Resource Name %s\n",debugstr_w(lpszName));
2572         return TRUE;
2573     }
2574
2575     sz = strlenW(tl_struct->source)+4;
2576     sz *= sizeof(WCHAR);
2577
2578     if ((INT)lpszName == 1)
2579         tl_struct->path = strdupW(tl_struct->source);
2580     else
2581     {
2582         tl_struct->path = msi_alloc(sz);
2583         sprintfW(tl_struct->path,fmt,tl_struct->source, lpszName);
2584     }
2585
2586     TRACE("trying %s\n", debugstr_w(tl_struct->path));
2587     res = LoadTypeLib(tl_struct->path,&tl_struct->ptLib);
2588     if (!SUCCEEDED(res))
2589     {
2590         msi_free(tl_struct->path);
2591         tl_struct->path = NULL;
2592
2593         return TRUE;
2594     }
2595
2596     ITypeLib_GetLibAttr(tl_struct->ptLib, &attr);
2597     if (IsEqualGUID(&(tl_struct->clsid),&(attr->guid)))
2598     {
2599         ITypeLib_ReleaseTLibAttr(tl_struct->ptLib, attr);
2600         return FALSE;
2601     }
2602
2603     msi_free(tl_struct->path);
2604     tl_struct->path = NULL;
2605
2606     ITypeLib_ReleaseTLibAttr(tl_struct->ptLib, attr);
2607     ITypeLib_Release(tl_struct->ptLib);
2608
2609     return TRUE;
2610 }
2611
2612 static UINT ITERATE_RegisterTypeLibraries(MSIRECORD *row, LPVOID param)
2613 {
2614     MSIPACKAGE* package = (MSIPACKAGE*)param;
2615     LPCWSTR component;
2616     MSICOMPONENT *comp;
2617     MSIFILE *file;
2618     typelib_struct tl_struct;
2619     HMODULE module;
2620     static const WCHAR szTYPELIB[] = {'T','Y','P','E','L','I','B',0};
2621
2622     component = MSI_RecordGetString(row,3);
2623     comp = get_loaded_component(package,component);
2624     if (!comp)
2625         return ERROR_SUCCESS;
2626
2627     if (!ACTION_VerifyComponentForAction(package, comp, INSTALLSTATE_LOCAL))
2628     {
2629         TRACE("Skipping typelib reg due to disabled component\n");
2630
2631         comp->Action = comp->Installed;
2632
2633         return ERROR_SUCCESS;
2634     }
2635
2636     comp->Action = INSTALLSTATE_LOCAL;
2637
2638     file = get_loaded_file( package, comp->KeyPath ); 
2639     if (!file)
2640         return ERROR_SUCCESS;
2641
2642     module = LoadLibraryExW( file->TargetPath, NULL, LOAD_LIBRARY_AS_DATAFILE );
2643     if (module != NULL)
2644     {
2645         LPWSTR guid;
2646         guid = load_dynamic_stringW(row,1);
2647         CLSIDFromString(guid, &tl_struct.clsid);
2648         msi_free(guid);
2649         tl_struct.source = strdupW( file->TargetPath );
2650         tl_struct.path = NULL;
2651
2652         EnumResourceNamesW(module, szTYPELIB, Typelib_EnumResNameProc,
2653                         (LONG_PTR)&tl_struct);
2654
2655         if (tl_struct.path != NULL)
2656         {
2657             LPWSTR help = NULL;
2658             LPCWSTR helpid;
2659             HRESULT res;
2660
2661             helpid = MSI_RecordGetString(row,6);
2662
2663             if (helpid)
2664                 help = resolve_folder(package,helpid,FALSE,FALSE,NULL);
2665             res = RegisterTypeLib(tl_struct.ptLib,tl_struct.path,help);
2666             msi_free(help);
2667
2668             if (!SUCCEEDED(res))
2669                 ERR("Failed to register type library %s\n",
2670                         debugstr_w(tl_struct.path));
2671             else
2672             {
2673                 ui_actiondata(package,szRegisterTypeLibraries,row);
2674
2675                 TRACE("Registered %s\n", debugstr_w(tl_struct.path));
2676             }
2677
2678             ITypeLib_Release(tl_struct.ptLib);
2679             msi_free(tl_struct.path);
2680         }
2681         else
2682             ERR("Failed to load type library %s\n",
2683                     debugstr_w(tl_struct.source));
2684
2685         FreeLibrary(module);
2686         msi_free(tl_struct.source);
2687     }
2688     else
2689         ERR("Could not load file! %s\n", debugstr_w(file->TargetPath));
2690
2691     return ERROR_SUCCESS;
2692 }
2693
2694 static UINT ACTION_RegisterTypeLibraries(MSIPACKAGE *package)
2695 {
2696     /* 
2697      * OK this is a bit confusing.. I am given a _Component key and I believe
2698      * that the file that is being registered as a type library is the "key file
2699      * of that component" which I interpret to mean "The file in the KeyPath of
2700      * that component".
2701      */
2702     UINT rc;
2703     MSIQUERY * view;
2704     static const WCHAR Query[] =
2705         {'S','E','L','E','C','T',' ','*',' ','F','R','O','M',' ',
2706          '`','T','y','p','e','L','i','b','`',0};
2707
2708     if (!package)
2709         return ERROR_INVALID_HANDLE;
2710
2711     rc = MSI_DatabaseOpenViewW(package->db, Query, &view);
2712     if (rc != ERROR_SUCCESS)
2713         return ERROR_SUCCESS;
2714
2715     rc = MSI_IterateRecords(view, NULL, ITERATE_RegisterTypeLibraries, package);
2716     msiobj_release(&view->hdr);
2717     return rc;
2718 }
2719
2720 static UINT ITERATE_CreateShortcuts(MSIRECORD *row, LPVOID param)
2721 {
2722     MSIPACKAGE *package = (MSIPACKAGE*)param;
2723     LPWSTR target_file, target_folder;
2724     LPCWSTR buffer;
2725     WCHAR filename[0x100];
2726     DWORD sz;
2727     MSICOMPONENT *comp;
2728     static const WCHAR szlnk[]={'.','l','n','k',0};
2729     IShellLinkW *sl;
2730     IPersistFile *pf;
2731     HRESULT res;
2732
2733     buffer = MSI_RecordGetString(row,4);
2734     comp = get_loaded_component(package,buffer);
2735     if (!comp)
2736         return ERROR_SUCCESS;
2737
2738     if (!ACTION_VerifyComponentForAction(package, comp, INSTALLSTATE_LOCAL))
2739     {
2740         TRACE("Skipping shortcut creation due to disabled component\n");
2741
2742         comp->Action = comp->Installed;
2743
2744         return ERROR_SUCCESS;
2745     }
2746
2747     comp->Action = INSTALLSTATE_LOCAL;
2748
2749     ui_actiondata(package,szCreateShortcuts,row);
2750
2751     res = CoCreateInstance( &CLSID_ShellLink, NULL, CLSCTX_INPROC_SERVER,
2752                     &IID_IShellLinkW, (LPVOID *) &sl );
2753
2754     if (FAILED(res))
2755     {
2756         ERR("Is IID_IShellLink\n");
2757         return ERROR_SUCCESS;
2758     }
2759
2760     res = IShellLinkW_QueryInterface( sl, &IID_IPersistFile,(LPVOID*) &pf );
2761     if( FAILED( res ) )
2762     {
2763         ERR("Is IID_IPersistFile\n");
2764         return ERROR_SUCCESS;
2765     }
2766
2767     buffer = MSI_RecordGetString(row,2);
2768     target_folder = resolve_folder(package, buffer,FALSE,FALSE,NULL);
2769
2770     /* may be needed because of a bug somehwere else */
2771     create_full_pathW(target_folder);
2772
2773     sz = 0x100;
2774     MSI_RecordGetStringW(row,3,filename,&sz);
2775     reduce_to_longfilename(filename);
2776     if (!strchrW(filename,'.') || strcmpiW(strchrW(filename,'.'),szlnk))
2777         strcatW(filename,szlnk);
2778     target_file = build_directory_name(2, target_folder, filename);
2779     msi_free(target_folder);
2780
2781     buffer = MSI_RecordGetString(row,5);
2782     if (strchrW(buffer,'['))
2783     {
2784         LPWSTR deformated;
2785         deformat_string(package,buffer,&deformated);
2786         IShellLinkW_SetPath(sl,deformated);
2787         msi_free(deformated);
2788     }
2789     else
2790     {
2791         LPWSTR keypath;
2792         FIXME("poorly handled shortcut format, advertised shortcut\n");
2793         keypath = strdupW( comp->FullKeypath );
2794         IShellLinkW_SetPath(sl,keypath);
2795         msi_free(keypath);
2796     }
2797
2798     if (!MSI_RecordIsNull(row,6))
2799     {
2800         LPWSTR deformated;
2801         buffer = MSI_RecordGetString(row,6);
2802         deformat_string(package,buffer,&deformated);
2803         IShellLinkW_SetArguments(sl,deformated);
2804         msi_free(deformated);
2805     }
2806
2807     if (!MSI_RecordIsNull(row,7))
2808     {
2809         buffer = MSI_RecordGetString(row,7);
2810         IShellLinkW_SetDescription(sl,buffer);
2811     }
2812
2813     if (!MSI_RecordIsNull(row,8))
2814         IShellLinkW_SetHotkey(sl,MSI_RecordGetInteger(row,8));
2815
2816     if (!MSI_RecordIsNull(row,9))
2817     {
2818         WCHAR *Path = NULL;
2819         INT index; 
2820
2821         buffer = MSI_RecordGetString(row,9);
2822
2823         build_icon_path(package,buffer,&Path);
2824         index = MSI_RecordGetInteger(row,10);
2825
2826         IShellLinkW_SetIconLocation(sl,Path,index);
2827         msi_free(Path);
2828     }
2829
2830     if (!MSI_RecordIsNull(row,11))
2831         IShellLinkW_SetShowCmd(sl,MSI_RecordGetInteger(row,11));
2832
2833     if (!MSI_RecordIsNull(row,12))
2834     {
2835         LPWSTR Path;
2836         buffer = MSI_RecordGetString(row,12);
2837         Path = resolve_folder(package, buffer, FALSE, FALSE, NULL);
2838         IShellLinkW_SetWorkingDirectory(sl,Path);
2839         msi_free(Path);
2840     }
2841
2842     TRACE("Writing shortcut to %s\n",debugstr_w(target_file));
2843     IPersistFile_Save(pf,target_file,FALSE);
2844
2845     msi_free(target_file);    
2846
2847     IPersistFile_Release( pf );
2848     IShellLinkW_Release( sl );
2849
2850     return ERROR_SUCCESS;
2851 }
2852
2853 static UINT ACTION_CreateShortcuts(MSIPACKAGE *package)
2854 {
2855     UINT rc;
2856     HRESULT res;
2857     MSIQUERY * view;
2858     static const WCHAR Query[] =
2859         {'S','E','L','E','C','T',' ','*',' ','F','R','O','M',' ',
2860          '`','S','h','o','r','t','c','u','t','`',0};
2861
2862     if (!package)
2863         return ERROR_INVALID_HANDLE;
2864
2865     rc = MSI_DatabaseOpenViewW(package->db, Query, &view);
2866     if (rc != ERROR_SUCCESS)
2867         return ERROR_SUCCESS;
2868
2869     res = CoInitialize( NULL );
2870     if (FAILED (res))
2871     {
2872         ERR("CoInitialize failed\n");
2873         return ERROR_FUNCTION_FAILED;
2874     }
2875
2876     rc = MSI_IterateRecords(view, NULL, ITERATE_CreateShortcuts, package);
2877     msiobj_release(&view->hdr);
2878
2879     CoUninitialize();
2880
2881     return rc;
2882 }
2883
2884 static UINT ITERATE_PublishProduct(MSIRECORD *row, LPVOID param)
2885 {
2886     MSIPACKAGE* package = (MSIPACKAGE*)param;
2887     HANDLE the_file;
2888     LPWSTR FilePath=NULL;
2889     LPCWSTR FileName=NULL;
2890     CHAR buffer[1024];
2891     DWORD sz;
2892     UINT rc;
2893
2894     FileName = MSI_RecordGetString(row,1);
2895     if (!FileName)
2896     {
2897         ERR("Unable to get FileName\n");
2898         return ERROR_SUCCESS;
2899     }
2900
2901     build_icon_path(package,FileName,&FilePath);
2902
2903     TRACE("Creating icon file at %s\n",debugstr_w(FilePath));
2904
2905     the_file = CreateFileW(FilePath, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS,
2906                         FILE_ATTRIBUTE_NORMAL, NULL);
2907
2908     if (the_file == INVALID_HANDLE_VALUE)
2909     {
2910         ERR("Unable to create file %s\n",debugstr_w(FilePath));
2911         msi_free(FilePath);
2912         return ERROR_SUCCESS;
2913     }
2914
2915     do 
2916     {
2917         DWORD write;
2918         sz = 1024;
2919         rc = MSI_RecordReadStream(row,2,buffer,&sz);
2920         if (rc != ERROR_SUCCESS)
2921         {
2922             ERR("Failed to get stream\n");
2923             CloseHandle(the_file);  
2924             DeleteFileW(FilePath);
2925             break;
2926         }
2927         WriteFile(the_file,buffer,sz,&write,NULL);
2928     } while (sz == 1024);
2929
2930     msi_free(FilePath);
2931
2932     CloseHandle(the_file);
2933     return ERROR_SUCCESS;
2934 }
2935
2936 /*
2937  * 99% of the work done here is only done for 
2938  * advertised installs. However this is where the
2939  * Icon table is processed and written out
2940  * so that is what I am going to do here.
2941  */
2942 static UINT ACTION_PublishProduct(MSIPACKAGE *package)
2943 {
2944     UINT rc;
2945     MSIQUERY * view;
2946     static const WCHAR Query[]=
2947         {'S','E','L','E','C','T',' ','*',' ','F','R','O','M',' ',
2948          '`','I','c','o','n','`',0};
2949     /* for registry stuff */
2950     HKEY hkey=0;
2951     HKEY hukey=0;
2952     static const WCHAR szProductLanguage[] =
2953         {'P','r','o','d','u','c','t','L','a','n','g','u','a','g','e',0};
2954     static const WCHAR szARPProductIcon[] =
2955         {'A','R','P','P','R','O','D','U','C','T','I','C','O','N',0};
2956     static const WCHAR szProductVersion[] =
2957         {'P','r','o','d','u','c','t','V','e','r','s','i','o','n',0};
2958     DWORD langid;
2959     LPWSTR buffer;
2960     DWORD size;
2961     MSIHANDLE hDb, hSumInfo;
2962
2963     if (!package)
2964         return ERROR_INVALID_HANDLE;
2965
2966     /* write out icon files */
2967
2968     rc = MSI_DatabaseOpenViewW(package->db, Query, &view);
2969     if (rc == ERROR_SUCCESS)
2970     {
2971         MSI_IterateRecords(view, NULL, ITERATE_PublishProduct, package);
2972         msiobj_release(&view->hdr);
2973     }
2974
2975     /* ok there is a lot more done here but i need to figure out what */
2976
2977     rc = MSIREG_OpenProductsKey(package->ProductCode,&hkey,TRUE);
2978     if (rc != ERROR_SUCCESS)
2979         goto end;
2980
2981     rc = MSIREG_OpenUserProductsKey(package->ProductCode,&hukey,TRUE);
2982     if (rc != ERROR_SUCCESS)
2983         goto end;
2984
2985
2986     buffer = msi_dup_property( package, INSTALLPROPERTY_PRODUCTNAMEW );
2987     msi_reg_set_val_str( hukey, INSTALLPROPERTY_PRODUCTNAMEW, buffer );
2988     msi_free(buffer);
2989
2990     buffer = msi_dup_property( package, szProductLanguage );
2991     langid = atoiW(buffer);
2992     msi_reg_set_val_dword( hkey, INSTALLPROPERTY_LANGUAGEW, langid );
2993     msi_free(buffer);
2994
2995     buffer = msi_dup_property( package, szARPProductIcon );
2996     if (buffer)
2997     {
2998         LPWSTR path;
2999         build_icon_path(package,buffer,&path);
3000         msi_reg_set_val_str( hukey, INSTALLPROPERTY_PRODUCTICONW, path );
3001     }
3002     msi_free(buffer);
3003
3004     buffer = msi_dup_property( package, szProductVersion );
3005     if (buffer)
3006     {
3007         DWORD verdword = build_version_dword(buffer);
3008         msi_reg_set_val_dword( hkey, INSTALLPROPERTY_VERSIONW, verdword );
3009     }
3010     msi_free(buffer);
3011     
3012     FIXME("Need to write more keys to the user registry\n");
3013   
3014     hDb= alloc_msihandle( &package->db->hdr );
3015     rc = MsiGetSummaryInformationW(hDb, NULL, 0, &hSumInfo); 
3016     MsiCloseHandle(hDb);
3017     if (rc == ERROR_SUCCESS)
3018     {
3019         WCHAR guidbuffer[0x200];
3020         size = 0x200;
3021         rc = MsiSummaryInfoGetPropertyW(hSumInfo, 9, NULL, NULL, NULL,
3022                                         guidbuffer, &size);
3023         if (rc == ERROR_SUCCESS)
3024         {
3025             WCHAR squashed[GUID_SIZE];
3026             /* for now we only care about the first guid */
3027             LPWSTR ptr = strchrW(guidbuffer,';');
3028             if (ptr) *ptr = 0;
3029             squash_guid(guidbuffer,squashed);
3030             msi_reg_set_val_str( hukey, INSTALLPROPERTY_PACKAGECODEW, squashed );
3031         }
3032         else
3033         {
3034             ERR("Unable to query Revision_Number... \n");
3035             rc = ERROR_SUCCESS;
3036         }
3037         MsiCloseHandle(hSumInfo);
3038     }
3039     else
3040     {
3041         ERR("Unable to open Summary Information\n");
3042         rc = ERROR_SUCCESS;
3043     }
3044
3045 end:
3046
3047     RegCloseKey(hkey);
3048     RegCloseKey(hukey);
3049
3050     return rc;
3051 }
3052
3053 static UINT ITERATE_WriteIniValues(MSIRECORD *row, LPVOID param)
3054 {
3055     MSIPACKAGE *package = (MSIPACKAGE*)param;
3056     LPCWSTR component,section,key,value,identifier,filename,dirproperty;
3057     LPWSTR deformated_section, deformated_key, deformated_value;
3058     LPWSTR folder, fullname = NULL;
3059     MSIRECORD * uirow;
3060     INT action;
3061     MSICOMPONENT *comp;
3062     static const WCHAR szWindowsFolder[] =
3063           {'W','i','n','d','o','w','s','F','o','l','d','e','r',0};
3064
3065     component = MSI_RecordGetString(row, 8);
3066     comp = get_loaded_component(package,component);
3067
3068     if (!ACTION_VerifyComponentForAction(package, comp, INSTALLSTATE_LOCAL))
3069     {
3070         TRACE("Skipping ini file due to disabled component %s\n",
3071                         debugstr_w(component));
3072
3073         comp->Action = comp->Installed;
3074
3075         return ERROR_SUCCESS;
3076     }
3077
3078     comp->Action = INSTALLSTATE_LOCAL;
3079
3080     identifier = MSI_RecordGetString(row,1); 
3081     filename = MSI_RecordGetString(row,2);
3082     dirproperty = MSI_RecordGetString(row,3);
3083     section = MSI_RecordGetString(row,4);
3084     key = MSI_RecordGetString(row,5);
3085     value = MSI_RecordGetString(row,6);
3086     action = MSI_RecordGetInteger(row,7);
3087
3088     deformat_string(package,section,&deformated_section);
3089     deformat_string(package,key,&deformated_key);
3090     deformat_string(package,value,&deformated_value);
3091
3092     if (dirproperty)
3093     {
3094         folder = resolve_folder(package, dirproperty, FALSE, FALSE, NULL);
3095         if (!folder)
3096             folder = msi_dup_property( package, dirproperty );
3097     }
3098     else
3099         folder = msi_dup_property( package, szWindowsFolder );
3100
3101     if (!folder)
3102     {
3103         ERR("Unable to resolve folder! (%s)\n",debugstr_w(dirproperty));
3104         goto cleanup;
3105     }
3106
3107     fullname = build_directory_name(2, folder, filename);
3108
3109     if (action == 0)
3110     {
3111         TRACE("Adding value %s to section %s in %s\n",
3112                 debugstr_w(deformated_key), debugstr_w(deformated_section),
3113                 debugstr_w(fullname));
3114         WritePrivateProfileStringW(deformated_section, deformated_key,
3115                                    deformated_value, fullname);
3116     }
3117     else if (action == 1)
3118     {
3119         WCHAR returned[10];
3120         GetPrivateProfileStringW(deformated_section, deformated_key, NULL,
3121                                  returned, 10, fullname);
3122         if (returned[0] == 0)
3123         {
3124             TRACE("Adding value %s to section %s in %s\n",
3125                     debugstr_w(deformated_key), debugstr_w(deformated_section),
3126                     debugstr_w(fullname));
3127
3128             WritePrivateProfileStringW(deformated_section, deformated_key,
3129                                        deformated_value, fullname);
3130         }
3131     }
3132     else if (action == 3)
3133         FIXME("Append to existing section not yet implemented\n");
3134
3135     uirow = MSI_CreateRecord(4);
3136     MSI_RecordSetStringW(uirow,1,identifier);
3137     MSI_RecordSetStringW(uirow,2,deformated_section);
3138     MSI_RecordSetStringW(uirow,3,deformated_key);
3139     MSI_RecordSetStringW(uirow,4,deformated_value);
3140     ui_actiondata(package,szWriteIniValues,uirow);
3141     msiobj_release( &uirow->hdr );
3142 cleanup:
3143     msi_free(fullname);
3144     msi_free(folder);
3145     msi_free(deformated_key);
3146     msi_free(deformated_value);
3147     msi_free(deformated_section);
3148     return ERROR_SUCCESS;
3149 }
3150
3151 static UINT ACTION_WriteIniValues(MSIPACKAGE *package)
3152 {
3153     UINT rc;
3154     MSIQUERY * view;
3155     static const WCHAR ExecSeqQuery[] = 
3156         {'S','E','L','E','C','T',' ','*',' ','F','R','O','M',' ',
3157          '`','I','n','i','F','i','l','e','`',0};
3158
3159     rc = MSI_DatabaseOpenViewW(package->db, ExecSeqQuery, &view);
3160     if (rc != ERROR_SUCCESS)
3161     {
3162         TRACE("no IniFile table\n");
3163         return ERROR_SUCCESS;
3164     }
3165
3166     rc = MSI_IterateRecords(view, NULL, ITERATE_WriteIniValues, package);
3167     msiobj_release(&view->hdr);
3168     return rc;
3169 }
3170
3171 static UINT ITERATE_SelfRegModules(MSIRECORD *row, LPVOID param)
3172 {
3173     MSIPACKAGE *package = (MSIPACKAGE*)param;
3174     LPCWSTR filename;
3175     LPWSTR FullName;
3176     MSIFILE *file;
3177     DWORD len;
3178     static const WCHAR ExeStr[] =
3179         {'r','e','g','s','v','r','3','2','.','e','x','e',' ','\"',0};
3180     static const WCHAR close[] =  {'\"',0};
3181     STARTUPINFOW si;
3182     PROCESS_INFORMATION info;
3183     BOOL brc;
3184
3185     memset(&si,0,sizeof(STARTUPINFOW));
3186
3187     filename = MSI_RecordGetString(row,1);
3188     file = get_loaded_file( package, filename );
3189
3190     if (!file)
3191     {
3192         ERR("Unable to find file id %s\n",debugstr_w(filename));
3193         return ERROR_SUCCESS;
3194     }
3195
3196     len = strlenW(ExeStr) + strlenW( file->TargetPath ) + 2;
3197
3198     FullName = msi_alloc(len*sizeof(WCHAR));
3199     strcpyW(FullName,ExeStr);
3200     strcatW( FullName, file->TargetPath );
3201     strcatW(FullName,close);
3202
3203     TRACE("Registering %s\n",debugstr_w(FullName));
3204     brc = CreateProcessW(NULL, FullName, NULL, NULL, FALSE, 0, NULL, c_colon,
3205                     &si, &info);
3206
3207     if (brc)
3208         msi_dialog_check_messages(info.hProcess);
3209
3210     msi_free(FullName);
3211     return ERROR_SUCCESS;
3212 }
3213
3214 static UINT ACTION_SelfRegModules(MSIPACKAGE *package)
3215 {
3216     UINT rc;
3217     MSIQUERY * view;
3218     static const WCHAR ExecSeqQuery[] = 
3219         {'S','E','L','E','C','T',' ','*',' ','F','R','O','M',' ',
3220          '`','S','e','l','f','R','e','g','`',0};
3221
3222     rc = MSI_DatabaseOpenViewW(package->db, ExecSeqQuery, &view);
3223     if (rc != ERROR_SUCCESS)
3224     {
3225         TRACE("no SelfReg table\n");
3226         return ERROR_SUCCESS;
3227     }
3228
3229     MSI_IterateRecords(view, NULL, ITERATE_SelfRegModules, package);
3230     msiobj_release(&view->hdr);
3231
3232     return ERROR_SUCCESS;
3233 }
3234
3235 static UINT ACTION_PublishFeatures(MSIPACKAGE *package)
3236 {
3237     MSIFEATURE *feature;
3238     UINT rc;
3239     HKEY hkey=0;
3240     HKEY hukey=0;
3241     
3242     if (!package)
3243         return ERROR_INVALID_HANDLE;
3244
3245     rc = MSIREG_OpenFeaturesKey(package->ProductCode,&hkey,TRUE);
3246     if (rc != ERROR_SUCCESS)
3247         goto end;
3248
3249     rc = MSIREG_OpenUserFeaturesKey(package->ProductCode,&hukey,TRUE);
3250     if (rc != ERROR_SUCCESS)
3251         goto end;
3252
3253     /* here the guids are base 85 encoded */
3254     LIST_FOR_EACH_ENTRY( feature, &package->features, MSIFEATURE, entry )
3255     {
3256         ComponentList *cl;
3257         LPWSTR data = NULL;
3258         GUID clsid;
3259         INT size;
3260         BOOL absent = FALSE;
3261
3262         if (!ACTION_VerifyFeatureForAction( feature, INSTALLSTATE_LOCAL ) &&
3263             !ACTION_VerifyFeatureForAction( feature, INSTALLSTATE_SOURCE ) &&
3264             !ACTION_VerifyFeatureForAction( feature, INSTALLSTATE_ADVERTISED ))
3265             absent = TRUE;
3266
3267         size = 1;
3268         LIST_FOR_EACH_ENTRY( cl, &feature->Components, ComponentList, entry )
3269         {
3270             size += 21;
3271         }
3272         if (feature->Feature_Parent)
3273             size += strlenW( feature->Feature_Parent )+2;
3274
3275         data = msi_alloc(size * sizeof(WCHAR));
3276
3277         data[0] = 0;
3278         LIST_FOR_EACH_ENTRY( cl, &feature->Components, ComponentList, entry )
3279         {
3280             MSICOMPONENT* component = cl->component;
3281             WCHAR buf[21];
3282
3283             memset(buf,0,sizeof(buf));
3284             if (component->ComponentId)
3285             {
3286                 TRACE("From %s\n",debugstr_w(component->ComponentId));
3287                 CLSIDFromString(component->ComponentId, &clsid);
3288                 encode_base85_guid(&clsid,buf);
3289                 TRACE("to %s\n",debugstr_w(buf));
3290                 strcatW(data,buf);
3291             }
3292         }
3293         if (feature->Feature_Parent)
3294         {
3295             static const WCHAR sep[] = {'\2',0};
3296             strcatW(data,sep);
3297             strcatW(data,feature->Feature_Parent);
3298         }
3299
3300         msi_reg_set_val_str( hkey, feature->Feature, data );
3301         msi_free(data);
3302
3303         size = 0;
3304         if (feature->Feature_Parent)
3305             size = strlenW(feature->Feature_Parent)*sizeof(WCHAR);
3306         if (!absent)
3307         {
3308             RegSetValueExW(hukey,feature->Feature,0,REG_SZ,
3309                        (LPBYTE)feature->Feature_Parent,size);
3310         }
3311         else
3312         {
3313             size += 2*sizeof(WCHAR);
3314             data = msi_alloc(size);
3315             data[0] = 0x6;
3316             data[1] = 0;
3317             if (feature->Feature_Parent)
3318                 strcpyW( &data[1], feature->Feature_Parent );
3319             RegSetValueExW(hukey,feature->Feature,0,REG_SZ,
3320                        (LPBYTE)data,size);
3321             msi_free(data);
3322         }
3323     }
3324
3325 end:
3326     RegCloseKey(hkey);
3327     RegCloseKey(hukey);
3328     return rc;
3329 }
3330
3331 static UINT ACTION_RegisterProduct(MSIPACKAGE *package)
3332 {
3333     HKEY hkey=0;
3334     LPWSTR buffer = NULL;
3335     UINT rc,i;
3336     DWORD size;
3337     static const WCHAR szWindowsInstaller[] = 
3338     {'W','i','n','d','o','w','s','I','n','s','t','a','l','l','e','r',0};
3339     static const WCHAR szPropKeys[][80] = 
3340     {
3341 {'A','R','P','A','U','T','H','O','R','I','Z','E','D','C','D','F','P','R','E','F','I','X',0},
3342 {'A','R','P','C','O','N','T','A','C','T',0},
3343 {'A','R','P','C','O','M','M','E','N','T','S',0},
3344 {'P','r','o','d','u','c','t','N','a','m','e',0},
3345 {'P','r','o','d','u','c','t','V','e','r','s','i','o','n',0},
3346 {'A','R','P','H','E','L','P','L','I','N','K',0},
3347 {'A','R','P','H','E','L','P','T','E','L','E','P','H','O','N','E',0},
3348 {'A','R','P','I','N','S','T','A','L','L','L','O','C','A','T','I','O','N',0},
3349 {'S','o','u','r','c','e','D','i','r',0},
3350 {'M','a','n','u','f','a','c','t','u','r','e','r',0},
3351 {'A','R','P','R','E','A','D','M','E',0},
3352 {'A','R','P','S','I','Z','E',0},
3353 {'A','R','P','U','R','L','I','N','F','O','A','B','O','U','T',0},
3354 {'A','R','P','U','R','L','U','P','D','A','T','E','I','N','F','O',0},
3355 {0},
3356     };
3357
3358     static const WCHAR szRegKeys[][80] = 
3359     {
3360 {'A','u','t','h','o','r','i','z','e','d','C','D','F','P','r','e','f','i','x',0},
3361 {'C','o','n','t','a','c','t',0},
3362 {'C','o','m','m','e','n','t','s',0},
3363 {'D','i','s','p','l','a','y','N','a','m','e',0},
3364 {'D','i','s','p','l','a','y','V','e','r','s','i','o','n',0},
3365 {'H','e','l','p','L','i','n','k',0},
3366 {'H','e','l','p','T','e','l','e','p','h','o','n','e',0},
3367 {'I','n','s','t','a','l','l','L','o','c','a','t','i','o','n',0},
3368 {'I','n','s','t','a','l','l','S','o','u','r','c','e',0},
3369 {'P','u','b','l','i','s','h','e','r',0},
3370 {'R','e','a','d','m','e',0},
3371 {'S','i','z','e',0},
3372 {'U','R','L','I','n','f','o','A','b','o','u','t',0},
3373 {'U','R','L','U','p','d','a','t','e','I','n','f','o',0},
3374 {0},
3375     };
3376
3377     static const WCHAR installerPathFmt[] = {
3378     '%','s','\\',
3379     'I','n','s','t','a','l','l','e','r','\\',0};
3380     static const WCHAR fmt[] = {
3381     '%','s','\\',
3382     'I','n','s','t','a','l','l','e','r','\\',
3383     '%','x','.','m','s','i',0};
3384     static const WCHAR szUpgradeCode[] = 
3385         {'U','p','g','r','a','d','e','C','o','d','e',0};
3386     static const WCHAR modpath_fmt[] = 
3387         {'M','s','i','E','x','e','c','.','e','x','e',' ','/','I','[','P','r','o','d','u','c','t','C','o','d','e',']',0};
3388     static const WCHAR szModifyPath[] = 
3389         {'M','o','d','i','f','y','P','a','t','h',0};
3390     static const WCHAR szUninstallString[] = 
3391         {'U','n','i','n','s','t','a','l','l','S','t','r','i','n','g',0};
3392     static const WCHAR szEstimatedSize[] = 
3393         {'E','s','t','i','m','a','t','e','d','S','i','z','e',0};
3394     static const WCHAR szProductLanguage[] =
3395         {'P','r','o','d','u','c','t','L','a','n','g','u','a','g','e',0};
3396     static const WCHAR szProductVersion[] =
3397         {'P','r','o','d','u','c','t','V','e','r','s','i','o','n',0};
3398
3399     SYSTEMTIME systime;
3400     static const WCHAR date_fmt[] = {'%','i','%','i','%','i',0};
3401     LPWSTR upgrade_code;
3402     WCHAR windir[MAX_PATH], path[MAX_PATH], packagefile[MAX_PATH];
3403     INT num,start;
3404
3405     if (!package)
3406         return ERROR_INVALID_HANDLE;
3407
3408     rc = MSIREG_OpenUninstallKey(package->ProductCode,&hkey,TRUE);
3409     if (rc != ERROR_SUCCESS)
3410         goto end;
3411
3412     /* dump all the info i can grab */
3413     FIXME("Flesh out more information \n");
3414
3415     for( i=0; szPropKeys[i][0]; i++ )
3416     {
3417         buffer = msi_dup_property( package, szPropKeys[i] );
3418         msi_reg_set_val_str( hkey, szRegKeys[i], buffer );
3419         msi_free(buffer);
3420     }
3421
3422     msi_reg_set_val_dword( hkey, szWindowsInstaller, 1 );
3423     
3424     /* copy the package locally */
3425     num = GetTickCount() & 0xffff;
3426     if (!num) 
3427         num = 1;
3428     start = num;
3429     GetWindowsDirectoryW(windir, sizeof(windir) / sizeof(windir[0]));
3430     snprintfW(packagefile,sizeof(packagefile)/sizeof(packagefile[0]),fmt,
3431      windir,num);
3432     do 
3433     {
3434         HANDLE handle = CreateFileW(packagefile,GENERIC_WRITE, 0, NULL,
3435                                   CREATE_NEW, FILE_ATTRIBUTE_NORMAL, 0 );
3436         if (handle != INVALID_HANDLE_VALUE)
3437         {
3438             CloseHandle(handle);
3439             break;
3440         }
3441         if (GetLastError() != ERROR_FILE_EXISTS &&
3442             GetLastError() != ERROR_SHARING_VIOLATION)
3443             break;
3444         if (!(++num & 0xffff)) num = 1;
3445         sprintfW(packagefile,fmt,num);
3446     } while (num != start);
3447
3448     snprintfW(path,sizeof(path)/sizeof(path[0]),installerPathFmt,windir);
3449     create_full_pathW(path);
3450     TRACE("Copying to local package %s\n",debugstr_w(packagefile));
3451     if (!CopyFileW(package->msiFilePath,packagefile,FALSE))
3452         ERR("Unable to copy package (%s -> %s) (error %ld)\n",
3453             debugstr_w(package->msiFilePath), debugstr_w(packagefile),
3454             GetLastError());
3455     msi_reg_set_val_str( hkey, INSTALLPROPERTY_LOCALPACKAGEW, packagefile );
3456
3457     /* do ModifyPath and UninstallString */
3458     size = deformat_string(package,modpath_fmt,&buffer);
3459     RegSetValueExW(hkey,szModifyPath,0,REG_EXPAND_SZ,(LPBYTE)buffer,size);
3460     RegSetValueExW(hkey,szUninstallString,0,REG_EXPAND_SZ,(LPBYTE)buffer,size);
3461     msi_free(buffer);
3462
3463     FIXME("Write real Estimated Size when we have it\n");
3464     msi_reg_set_val_dword( hkey, szEstimatedSize, 0 );
3465    
3466     GetLocalTime(&systime);
3467     size = 9*sizeof(WCHAR);
3468     buffer= msi_alloc(size);
3469     sprintfW(buffer,date_fmt,systime.wYear,systime.wMonth,systime.wDay);
3470     msi_reg_set_val_str( hkey, INSTALLPROPERTY_INSTALLDATEW, buffer );
3471     msi_free(buffer);
3472    
3473     buffer = msi_dup_property( package, szProductLanguage );
3474     msi_reg_set_val_dword( hkey, INSTALLPROPERTY_LANGUAGEW, atoiW(buffer) );
3475     msi_free(buffer);
3476
3477     buffer = msi_dup_property( package, szProductVersion );
3478     if (buffer)
3479     {
3480         DWORD verdword = build_version_dword(buffer);
3481
3482         msi_reg_set_val_dword( hkey, INSTALLPROPERTY_VERSIONW, verdword );
3483         msi_reg_set_val_dword( hkey, INSTALLPROPERTY_VERSIONMAJORW, verdword>>24 );
3484         msi_reg_set_val_dword( hkey, INSTALLPROPERTY_VERSIONMINORW, (verdword>>16)&0x00FF );
3485     }
3486     msi_free(buffer);
3487     
3488     /* Handle Upgrade Codes */
3489     upgrade_code = msi_dup_property( package, szUpgradeCode );
3490     if (upgrade_code)
3491     {
3492         HKEY hkey2;
3493         WCHAR squashed[33];
3494         MSIREG_OpenUpgradeCodesKey(upgrade_code, &hkey2, TRUE);
3495         squash_guid(package->ProductCode,squashed);
3496         msi_reg_set_val_str( hkey2, squashed, NULL );
3497         RegCloseKey(hkey2);
3498         MSIREG_OpenUserUpgradeCodesKey(upgrade_code, &hkey2, TRUE);
3499         squash_guid(package->ProductCode,squashed);
3500         msi_reg_set_val_str( hkey2, squashed, NULL );
3501         RegCloseKey(hkey2);
3502
3503         msi_free(upgrade_code);
3504     }
3505     
3506 end:
3507     RegCloseKey(hkey);
3508
3509     return ERROR_SUCCESS;
3510 }
3511
3512 static UINT ACTION_InstallExecute(MSIPACKAGE *package)
3513 {
3514     UINT rc;
3515
3516     if (!package)
3517         return ERROR_INVALID_HANDLE;
3518
3519     rc = execute_script(package,INSTALL_SCRIPT);
3520
3521     return rc;
3522 }
3523
3524 static UINT ACTION_InstallFinalize(MSIPACKAGE *package)
3525 {
3526     UINT rc;
3527
3528     if (!package)
3529         return ERROR_INVALID_HANDLE;
3530
3531     /* turn off scheduleing */
3532     package->script->CurrentlyScripting= FALSE;
3533
3534     /* first do the same as an InstallExecute */
3535     rc = ACTION_InstallExecute(package);
3536     if (rc != ERROR_SUCCESS)
3537         return rc;
3538
3539     /* then handle Commit Actions */
3540     rc = execute_script(package,COMMIT_SCRIPT);
3541
3542     return rc;
3543 }
3544
3545 static UINT ACTION_ForceReboot(MSIPACKAGE *package)
3546 {
3547     static const WCHAR RunOnce[] = {
3548     'S','o','f','t','w','a','r','e','\\',
3549     'M','i','c','r','o','s','o','f','t','\\',
3550     'W','i','n','d','o','w','s','\\',
3551     'C','u','r','r','e','n','t','V','e','r','s','i','o','n','\\',
3552     'R','u','n','O','n','c','e',0};
3553     static const WCHAR InstallRunOnce[] = {
3554     'S','o','f','t','w','a','r','e','\\',
3555     'M','i','c','r','o','s','o','f','t','\\',
3556     'W','i','n','d','o','w','s','\\',
3557     'C','u','r','r','e','n','t','V','e','r','s','i','o','n','\\',
3558     'I','n','s','t','a','l','l','e','r','\\',
3559     'R','u','n','O','n','c','e','E','n','t','r','i','e','s',0};
3560
3561     static const WCHAR msiexec_fmt[] = {
3562     '%','s',
3563     '\\','M','s','i','E','x','e','c','.','e','x','e',' ','/','@',' ',
3564     '\"','%','s','\"',0};
3565     static const WCHAR install_fmt[] = {
3566     '/','I',' ','\"','%','s','\"',' ',
3567     'A','F','T','E','R','R','E','B','O','O','T','=','1',' ',
3568     'R','U','N','O','N','C','E','E','N','T','R','Y','=','\"','%','s','\"',0};
3569     WCHAR buffer[256], sysdir[MAX_PATH];
3570     HKEY hkey;
3571     WCHAR squished_pc[100];
3572
3573     if (!package)
3574         return ERROR_INVALID_HANDLE;
3575
3576     squash_guid(package->ProductCode,squished_pc);
3577
3578     GetSystemDirectoryW(sysdir, sizeof(sysdir)/sizeof(sysdir[0]));
3579     RegCreateKeyW(HKEY_LOCAL_MACHINE,RunOnce,&hkey);
3580     snprintfW(buffer,sizeof(buffer)/sizeof(buffer[0]),msiexec_fmt,sysdir,
3581      squished_pc);
3582
3583     msi_reg_set_val_str( hkey, squished_pc, buffer );
3584     RegCloseKey(hkey);
3585
3586     TRACE("Reboot command %s\n",debugstr_w(buffer));
3587
3588     RegCreateKeyW(HKEY_LOCAL_MACHINE,InstallRunOnce,&hkey);
3589     sprintfW(buffer,install_fmt,package->ProductCode,squished_pc);
3590
3591     msi_reg_set_val_str( hkey, squished_pc, buffer );
3592     RegCloseKey(hkey);
3593
3594     return ERROR_INSTALL_SUSPEND;
3595 }
3596
3597 UINT ACTION_ResolveSource(MSIPACKAGE* package)
3598 {
3599     DWORD attrib;
3600     UINT rc;
3601     /*
3602      * we are currently doing what should be done here in the top level Install
3603      * however for Adminastrative and uninstalls this step will be needed
3604      */
3605     if (!package->PackagePath)
3606         return ERROR_SUCCESS;
3607
3608     attrib = GetFileAttributesW(package->PackagePath);
3609     if (attrib == INVALID_FILE_ATTRIBUTES)
3610     {
3611         LPWSTR prompt;
3612         LPWSTR msg;
3613         DWORD size = 0;
3614
3615         rc = MsiSourceListGetInfoW(package->ProductCode, NULL, 
3616                 MSIINSTALLCONTEXT_USERMANAGED, MSICODE_PRODUCT,
3617                 INSTALLPROPERTY_DISKPROMPTW,NULL,&size);
3618         if (rc == ERROR_MORE_DATA)
3619         {
3620             prompt = msi_alloc(size * sizeof(WCHAR));
3621             MsiSourceListGetInfoW(package->ProductCode, NULL, 
3622                     MSIINSTALLCONTEXT_USERMANAGED, MSICODE_PRODUCT,
3623                     INSTALLPROPERTY_DISKPROMPTW,prompt,&size);
3624         }
3625         else
3626             prompt = strdupW(package->PackagePath);
3627
3628         msg = generate_error_string(package,1302,1,prompt);
3629         while(attrib == INVALID_FILE_ATTRIBUTES)
3630         {
3631             rc = MessageBoxW(NULL,msg,NULL,MB_OKCANCEL);
3632             if (rc == IDCANCEL)
3633             {
3634                 rc = ERROR_INSTALL_USEREXIT;
3635                 break;
3636             }
3637             attrib = GetFileAttributesW(package->PackagePath);
3638         }
3639         msi_free(prompt);
3640         rc = ERROR_SUCCESS;
3641     }
3642     else
3643         return ERROR_SUCCESS;
3644
3645     return rc;
3646 }
3647
3648 static UINT ACTION_RegisterUser(MSIPACKAGE *package)
3649 {
3650     HKEY hkey=0;
3651     LPWSTR buffer;
3652     LPWSTR productid;
3653     UINT rc,i;
3654
3655     static const WCHAR szPropKeys[][80] = 
3656     {
3657         {'P','r','o','d','u','c','t','I','D',0},
3658         {'U','S','E','R','N','A','M','E',0},
3659         {'C','O','M','P','A','N','Y','N','A','M','E',0},
3660         {0},
3661     };
3662
3663     static const WCHAR szRegKeys[][80] = 
3664     {
3665         {'P','r','o','d','u','c','t','I','D',0},
3666         {'R','e','g','O','w','n','e','r',0},
3667         {'R','e','g','C','o','m','p','a','n','y',0},
3668         {0},
3669     };
3670
3671     if (!package)
3672         return ERROR_INVALID_HANDLE;
3673
3674     productid = msi_dup_property( package, INSTALLPROPERTY_PRODUCTIDW );
3675     if (!productid)
3676         return ERROR_SUCCESS;
3677
3678     rc = MSIREG_OpenUninstallKey(package->ProductCode,&hkey,TRUE);
3679     if (rc != ERROR_SUCCESS)
3680         goto end;
3681
3682     for( i = 0; szPropKeys[i][0]; i++ )
3683     {
3684         buffer = msi_dup_property( package, szPropKeys[i] );
3685         msi_reg_set_val_str( hkey, szRegKeys[i], buffer );
3686         msi_free( buffer );
3687     }
3688
3689 end:
3690     msi_free(productid);
3691     RegCloseKey(hkey);
3692
3693     return ERROR_SUCCESS;
3694 }
3695
3696
3697 static UINT ACTION_ExecuteAction(MSIPACKAGE *package)
3698 {
3699     static const WCHAR szUILevel[] = {'U','I','L','e','v','e','l',0};
3700     static const WCHAR szTwo[] = {'2',0};
3701     UINT rc;
3702     LPWSTR level;
3703     level = msi_dup_property( package, szUILevel );
3704
3705     MSI_SetPropertyW(package,szUILevel,szTwo);
3706     package->script->InWhatSequence |= SEQUENCE_EXEC;
3707     rc = ACTION_ProcessExecSequence(package,FALSE);
3708     MSI_SetPropertyW(package,szUILevel,level);
3709     msi_free(level);
3710     return rc;
3711 }
3712
3713
3714 /*
3715  * Code based off of code located here
3716  * http://www.codeproject.com/gdi/fontnamefromfile.asp
3717  *
3718  * Using string index 4 (full font name) instead of 1 (family name)
3719  */
3720 static LPWSTR load_ttfname_from(LPCWSTR filename)
3721 {
3722     HANDLE handle;
3723     LPWSTR ret = NULL;
3724     int i;
3725
3726     typedef struct _tagTT_OFFSET_TABLE{
3727         USHORT uMajorVersion;
3728         USHORT uMinorVersion;
3729         USHORT uNumOfTables;
3730         USHORT uSearchRange;
3731         USHORT uEntrySelector;
3732         USHORT uRangeShift;
3733     }TT_OFFSET_TABLE;
3734
3735     typedef struct _tagTT_TABLE_DIRECTORY{
3736         char szTag[4]; /* table name */
3737         ULONG uCheckSum; /* Check sum */
3738         ULONG uOffset; /* Offset from beginning of file */
3739         ULONG uLength; /* length of the table in bytes */
3740     }TT_TABLE_DIRECTORY;
3741
3742     typedef struct _tagTT_NAME_TABLE_HEADER{
3743     USHORT uFSelector; /* format selector. Always 0 */
3744     USHORT uNRCount; /* Name Records count */
3745     USHORT uStorageOffset; /* Offset for strings storage, 
3746                             * from start of the table */
3747     }TT_NAME_TABLE_HEADER;
3748    
3749     typedef struct _tagTT_NAME_RECORD{
3750         USHORT uPlatformID;
3751         USHORT uEncodingID;
3752         USHORT uLanguageID;
3753         USHORT uNameID;
3754         USHORT uStringLength;
3755         USHORT uStringOffset; /* from start of storage area */
3756     }TT_NAME_RECORD;
3757
3758 #define SWAPWORD(x) MAKEWORD(HIBYTE(x), LOBYTE(x))
3759 #define SWAPLONG(x) MAKELONG(SWAPWORD(HIWORD(x)), SWAPWORD(LOWORD(x)))
3760
3761     handle = CreateFileW(filename ,GENERIC_READ, 0, NULL, OPEN_EXISTING,
3762                     FILE_ATTRIBUTE_NORMAL, 0 );
3763     if (handle != INVALID_HANDLE_VALUE)
3764     {
3765         TT_TABLE_DIRECTORY tblDir;
3766         BOOL bFound = FALSE;
3767         TT_OFFSET_TABLE ttOffsetTable;
3768
3769         ReadFile(handle,&ttOffsetTable, sizeof(TT_OFFSET_TABLE),NULL,NULL);
3770         ttOffsetTable.uNumOfTables = SWAPWORD(ttOffsetTable.uNumOfTables);
3771         ttOffsetTable.uMajorVersion = SWAPWORD(ttOffsetTable.uMajorVersion);
3772         ttOffsetTable.uMinorVersion = SWAPWORD(ttOffsetTable.uMinorVersion);
3773         
3774         if (ttOffsetTable.uMajorVersion != 1 || 
3775                         ttOffsetTable.uMinorVersion != 0)
3776             return NULL;
3777
3778         for (i=0; i< ttOffsetTable.uNumOfTables; i++)
3779         {
3780             ReadFile(handle,&tblDir, sizeof(TT_TABLE_DIRECTORY),NULL,NULL);
3781             if (strncmp(tblDir.szTag,"name",4)==0)
3782             {
3783                 bFound = TRUE;
3784                 tblDir.uLength = SWAPLONG(tblDir.uLength);
3785                 tblDir.uOffset = SWAPLONG(tblDir.uOffset);
3786                 break;
3787             }
3788         }
3789
3790         if (bFound)
3791         {
3792             TT_NAME_TABLE_HEADER ttNTHeader;
3793             TT_NAME_RECORD ttRecord;
3794
3795             SetFilePointer(handle, tblDir.uOffset, NULL, FILE_BEGIN);
3796             ReadFile(handle,&ttNTHeader, sizeof(TT_NAME_TABLE_HEADER),
3797                             NULL,NULL);
3798
3799             ttNTHeader.uNRCount = SWAPWORD(ttNTHeader.uNRCount);
3800             ttNTHeader.uStorageOffset = SWAPWORD(ttNTHeader.uStorageOffset);
3801             bFound = FALSE;
3802             for(i=0; i<ttNTHeader.uNRCount; i++)
3803             {
3804                 ReadFile(handle,&ttRecord, sizeof(TT_NAME_RECORD),NULL,NULL);
3805                 ttRecord.uNameID = SWAPWORD(ttRecord.uNameID);
3806                 /* 4 is the Full Font Name */
3807                 if(ttRecord.uNameID == 4)
3808                 {
3809                     int nPos;
3810                     LPSTR buf;
3811                     static LPCSTR tt = " (TrueType)";
3812
3813                     ttRecord.uStringLength = SWAPWORD(ttRecord.uStringLength);
3814                     ttRecord.uStringOffset = SWAPWORD(ttRecord.uStringOffset);
3815                     nPos = SetFilePointer(handle, 0, NULL, FILE_CURRENT);
3816                     SetFilePointer(handle, tblDir.uOffset + 
3817                                     ttRecord.uStringOffset + 
3818                                     ttNTHeader.uStorageOffset,
3819                                     NULL, FILE_BEGIN);
3820                     buf = msi_alloc( ttRecord.uStringLength + 1 + strlen(tt) );
3821                     memset(buf, 0, ttRecord.uStringLength + 1 + strlen(tt));
3822                     ReadFile(handle, buf, ttRecord.uStringLength, NULL, NULL);
3823                     if (strlen(buf) > 0)
3824                     {
3825                         strcat(buf,tt);
3826                         ret = strdupAtoW(buf);
3827                         msi_free(buf);
3828                         break;
3829                     }
3830
3831                     msi_free(buf);
3832                     SetFilePointer(handle,nPos, NULL, FILE_BEGIN);
3833                 }
3834             }
3835         }
3836         CloseHandle(handle);
3837     }
3838     else
3839         ERR("Unable to open font file %s\n", debugstr_w(filename));
3840
3841     TRACE("Returning fontname %s\n",debugstr_w(ret));
3842     return ret;
3843 }
3844
3845 static UINT ITERATE_RegisterFonts(MSIRECORD *row, LPVOID param)
3846 {
3847     MSIPACKAGE *package = (MSIPACKAGE*)param;
3848     LPWSTR name;
3849     LPCWSTR filename;
3850     MSIFILE *file;
3851     static const WCHAR regfont1[] =
3852         {'S','o','f','t','w','a','r','e','\\',
3853          'M','i','c','r','o','s','o','f','t','\\',
3854          'W','i','n','d','o','w','s',' ','N','T','\\',
3855          'C','u','r','r','e','n','t','V','e','r','s','i','o','n','\\',
3856          'F','o','n','t','s',0};
3857     static const WCHAR regfont2[] =
3858         {'S','o','f','t','w','a','r','e','\\',
3859          'M','i','c','r','o','s','o','f','t','\\',
3860          'W','i','n','d','o','w','s','\\',
3861          'C','u','r','r','e','n','t','V','e','r','s','i','o','n','\\',
3862          'F','o','n','t','s',0};
3863     HKEY hkey1;
3864     HKEY hkey2;
3865
3866     filename = MSI_RecordGetString( row, 1 );
3867     file = get_loaded_file( package, filename );
3868     if (!file)
3869     {
3870         ERR("Unable to load file\n");
3871         return ERROR_SUCCESS;
3872     }
3873
3874     /* check to make sure that component is installed */
3875     if (!ACTION_VerifyComponentForAction(package, 
3876                 file->Component, INSTALLSTATE_LOCAL))
3877     {
3878         TRACE("Skipping: Component not scheduled for install\n");
3879         return ERROR_SUCCESS;
3880     }
3881
3882     RegCreateKeyW(HKEY_LOCAL_MACHINE,regfont1,&hkey1);
3883     RegCreateKeyW(HKEY_LOCAL_MACHINE,regfont2,&hkey2);
3884
3885     if (MSI_RecordIsNull(row,2))
3886         name = load_ttfname_from( file->TargetPath );
3887     else
3888         name = load_dynamic_stringW(row,2);
3889
3890     if (name)
3891     {
3892         msi_reg_set_val_str( hkey1, name, file->FileName );
3893         msi_reg_set_val_str( hkey2, name, file->FileName );
3894     }
3895
3896     msi_free(name);
3897     RegCloseKey(hkey1);
3898     RegCloseKey(hkey2);
3899     return ERROR_SUCCESS;
3900 }
3901
3902 static UINT ACTION_RegisterFonts(MSIPACKAGE *package)
3903 {
3904     UINT rc;
3905     MSIQUERY * view;
3906     static const WCHAR ExecSeqQuery[] =
3907         {'S','E','L','E','C','T',' ','*',' ','F','R','O','M',' ',
3908          '`','F','o','n','t','`',0};
3909
3910     rc = MSI_DatabaseOpenViewW(package->db, ExecSeqQuery, &view);
3911     if (rc != ERROR_SUCCESS)
3912     {
3913         TRACE("MSI_DatabaseOpenViewW failed: %d\n", rc);
3914         return ERROR_SUCCESS;
3915     }
3916
3917     MSI_IterateRecords(view, NULL, ITERATE_RegisterFonts, package);
3918     msiobj_release(&view->hdr);
3919
3920     return ERROR_SUCCESS;
3921 }
3922
3923 static UINT ITERATE_PublishComponent(MSIRECORD *rec, LPVOID param)
3924 {
3925     MSIPACKAGE *package = (MSIPACKAGE*)param;
3926     LPCWSTR compgroupid=NULL;
3927     LPCWSTR feature=NULL;
3928     LPCWSTR text = NULL;
3929     LPCWSTR qualifier = NULL;
3930     LPCWSTR component = NULL;
3931     LPWSTR advertise = NULL;
3932     LPWSTR output = NULL;
3933     HKEY hkey;
3934     UINT rc = ERROR_SUCCESS;
3935     MSICOMPONENT *comp;
3936     DWORD sz = 0;
3937
3938     component = MSI_RecordGetString(rec,3);
3939     comp = get_loaded_component(package,component);
3940
3941     if (!ACTION_VerifyComponentForAction(package, comp, INSTALLSTATE_LOCAL) && 
3942        !ACTION_VerifyComponentForAction(package, comp, INSTALLSTATE_SOURCE) &&
3943        !ACTION_VerifyComponentForAction(package, comp, INSTALLSTATE_ADVERTISED))
3944     {
3945         TRACE("Skipping: Component %s not scheduled for install\n",
3946                         debugstr_w(component));
3947
3948         return ERROR_SUCCESS;
3949     }
3950
3951     compgroupid = MSI_RecordGetString(rec,1);
3952
3953     rc = MSIREG_OpenUserComponentsKey(compgroupid, &hkey, TRUE);
3954     if (rc != ERROR_SUCCESS)
3955         goto end;
3956     
3957     text = MSI_RecordGetString(rec,4);
3958     qualifier = MSI_RecordGetString(rec,2);
3959     feature = MSI_RecordGetString(rec,5);
3960   
3961     advertise = create_component_advertise_string(package, comp, feature);
3962
3963     sz = strlenW(advertise);
3964
3965     if (text)
3966         sz += lstrlenW(text);
3967
3968     sz+=3;
3969     sz *= sizeof(WCHAR);
3970            
3971     output = msi_alloc(sz);
3972     memset(output,0,sz);
3973     strcpyW(output,advertise);
3974
3975     if (text)
3976         strcatW(output,text);
3977
3978     msi_reg_set_val_multi_str( hkey, qualifier, output );
3979     
3980 end:
3981     RegCloseKey(hkey);
3982     msi_free(output);
3983     
3984     return rc;
3985 }
3986
3987 /*
3988  * At present I am ignorning the advertised components part of this and only
3989  * focusing on the qualified component sets
3990  */
3991 static UINT ACTION_PublishComponents(MSIPACKAGE *package)
3992 {
3993     UINT rc;
3994     MSIQUERY * view;
3995     static const WCHAR ExecSeqQuery[] =
3996         {'S','E','L','E','C','T',' ','*',' ','F','R','O','M',' ',
3997          '`','P','u','b','l','i','s','h',
3998          'C','o','m','p','o','n','e','n','t','`',0};
3999     
4000     rc = MSI_DatabaseOpenViewW(package->db, ExecSeqQuery, &view);
4001     if (rc != ERROR_SUCCESS)
4002         return ERROR_SUCCESS;
4003
4004     rc = MSI_IterateRecords(view, NULL, ITERATE_PublishComponent, package);
4005     msiobj_release(&view->hdr);
4006
4007     return rc;
4008 }