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