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