Moved midimap to the top-level dlls directory.
[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 szProductLanguage[] =
3011         {'P','r','o','d','u','c','t','L','a','n','g','u','a','g','e',0};
3012     static const WCHAR szARPProductIcon[] =
3013         {'A','R','P','P','R','O','D','U','C','T','I','C','O','N',0};
3014     static const WCHAR szProductVersion[] =
3015         {'P','r','o','d','u','c','t','V','e','r','s','i','o','n',0};
3016     DWORD langid;
3017     LPWSTR buffer;
3018     DWORD size;
3019     MSIHANDLE hDb, hSumInfo;
3020
3021     if (!package)
3022         return ERROR_INVALID_HANDLE;
3023
3024     /* write out icon files */
3025
3026     rc = MSI_DatabaseOpenViewW(package->db, Query, &view);
3027     if (rc == ERROR_SUCCESS)
3028     {
3029         MSI_IterateRecords(view, NULL, ITERATE_PublishProduct, package);
3030         msiobj_release(&view->hdr);
3031     }
3032
3033     /* ok there is a lot more done here but i need to figure out what */
3034
3035     rc = MSIREG_OpenProductsKey(package->ProductCode,&hkey,TRUE);
3036     if (rc != ERROR_SUCCESS)
3037         goto end;
3038
3039     rc = MSIREG_OpenUserProductsKey(package->ProductCode,&hukey,TRUE);
3040     if (rc != ERROR_SUCCESS)
3041         goto end;
3042
3043
3044     buffer = load_dynamic_property(package,INSTALLPROPERTY_PRODUCTNAMEW,NULL);
3045     size = strlenW(buffer)*sizeof(WCHAR);
3046     RegSetValueExW(hukey,INSTALLPROPERTY_PRODUCTNAMEW,0,REG_SZ, 
3047             (LPBYTE)buffer,size);
3048     HeapFree(GetProcessHeap(),0,buffer);
3049
3050     buffer = load_dynamic_property(package,szProductLanguage,NULL);
3051     size = sizeof(DWORD);
3052     langid = atoiW(buffer);
3053     RegSetValueExW(hukey,INSTALLPROPERTY_LANGUAGEW,0,REG_DWORD, 
3054             (LPBYTE)&langid,size);
3055     HeapFree(GetProcessHeap(),0,buffer);
3056
3057     buffer = load_dynamic_property(package,szARPProductIcon,NULL);
3058     if (buffer)
3059     {
3060         LPWSTR path;
3061         build_icon_path(package,buffer,&path);
3062         size = strlenW(path) * sizeof(WCHAR);
3063         RegSetValueExW(hukey,INSTALLPROPERTY_PRODUCTICONW,0,REG_SZ,
3064                 (LPBYTE)path,size);
3065     }
3066     HeapFree(GetProcessHeap(),0,buffer);
3067
3068     buffer = load_dynamic_property(package,szProductVersion,NULL);
3069     if (buffer)
3070     {
3071         DWORD verdword = build_version_dword(buffer);
3072         size = sizeof(DWORD);
3073         RegSetValueExW(hukey,INSTALLPROPERTY_VERSIONW,0,REG_DWORD, (LPBYTE
3074                     )&verdword,size);
3075     }
3076     HeapFree(GetProcessHeap(),0,buffer);
3077     
3078     FIXME("Need to write more keys to the user registry\n");
3079   
3080     hDb= alloc_msihandle( &package->db->hdr );
3081     rc = MsiGetSummaryInformationW(hDb, NULL, 0, &hSumInfo); 
3082     MsiCloseHandle(hDb);
3083     if (rc == ERROR_SUCCESS)
3084     {
3085         WCHAR guidbuffer[0x200];
3086         size = 0x200;
3087         rc = MsiSummaryInfoGetPropertyW(hSumInfo, 9, NULL, NULL, NULL,
3088                                         guidbuffer, &size);
3089         if (rc == ERROR_SUCCESS)
3090         {
3091             WCHAR squashed[GUID_SIZE];
3092             /* for now we only care about the first guid */
3093             LPWSTR ptr = strchrW(guidbuffer,';');
3094             if (ptr) *ptr = 0;
3095             squash_guid(guidbuffer,squashed);
3096             size = strlenW(squashed)*sizeof(WCHAR);
3097             RegSetValueExW(hukey,INSTALLPROPERTY_PACKAGECODEW,0,REG_SZ,
3098                     (LPBYTE)squashed, size);
3099         }
3100         else
3101         {
3102             ERR("Unable to query Revision_Number... \n");
3103             rc = ERROR_SUCCESS;
3104         }
3105         MsiCloseHandle(hSumInfo);
3106     }
3107     else
3108     {
3109         ERR("Unable to open Summary Information\n");
3110         rc = ERROR_SUCCESS;
3111     }
3112
3113 end:
3114
3115     RegCloseKey(hkey);
3116     RegCloseKey(hukey);
3117
3118     return rc;
3119 }
3120
3121 static UINT ITERATE_WriteIniValues(MSIRECORD *row, LPVOID param)
3122 {
3123     MSIPACKAGE *package = (MSIPACKAGE*)param;
3124     LPCWSTR component,section,key,value,identifier,filename,dirproperty;
3125     LPWSTR deformated_section, deformated_key, deformated_value;
3126     LPWSTR folder, fullname = NULL;
3127     MSIRECORD * uirow;
3128     INT component_index,action;
3129     static const WCHAR szWindowsFolder[] =
3130           {'W','i','n','d','o','w','s','F','o','l','d','e','r',0};
3131
3132     component = MSI_RecordGetString(row, 8);
3133     component_index = get_loaded_component(package,component);
3134
3135     if (!ACTION_VerifyComponentForAction(package, component_index,
3136                             INSTALLSTATE_LOCAL))
3137     {
3138         TRACE("Skipping ini file due to disabled component %s\n",
3139                         debugstr_w(component));
3140
3141         package->components[component_index].Action =
3142             package->components[component_index].Installed;
3143
3144         return ERROR_SUCCESS;
3145     }
3146
3147     package->components[component_index].Action = INSTALLSTATE_LOCAL;
3148
3149     identifier = MSI_RecordGetString(row,1); 
3150     filename = MSI_RecordGetString(row,2);
3151     dirproperty = MSI_RecordGetString(row,3);
3152     section = MSI_RecordGetString(row,4);
3153     key = MSI_RecordGetString(row,5);
3154     value = MSI_RecordGetString(row,6);
3155     action = MSI_RecordGetInteger(row,7);
3156
3157     deformat_string(package,section,&deformated_section);
3158     deformat_string(package,key,&deformated_key);
3159     deformat_string(package,value,&deformated_value);
3160
3161     if (dirproperty)
3162     {
3163         folder = resolve_folder(package, dirproperty, FALSE, FALSE, NULL);
3164         if (!folder)
3165             folder = load_dynamic_property(package,dirproperty,NULL);
3166     }
3167     else
3168         folder = load_dynamic_property(package, szWindowsFolder, NULL);
3169
3170     if (!folder)
3171     {
3172         ERR("Unable to resolve folder! (%s)\n",debugstr_w(dirproperty));
3173         goto cleanup;
3174     }
3175
3176     fullname = build_directory_name(3, folder, filename, NULL);
3177
3178     if (action == 0)
3179     {
3180         TRACE("Adding value %s to section %s in %s\n",
3181                 debugstr_w(deformated_key), debugstr_w(deformated_section),
3182                 debugstr_w(fullname));
3183         WritePrivateProfileStringW(deformated_section, deformated_key,
3184                                    deformated_value, fullname);
3185     }
3186     else if (action == 1)
3187     {
3188         WCHAR returned[10];
3189         GetPrivateProfileStringW(deformated_section, deformated_key, NULL,
3190                                  returned, 10, fullname);
3191         if (returned[0] == 0)
3192         {
3193             TRACE("Adding value %s to section %s in %s\n",
3194                     debugstr_w(deformated_key), debugstr_w(deformated_section),
3195                     debugstr_w(fullname));
3196
3197             WritePrivateProfileStringW(deformated_section, deformated_key,
3198                                        deformated_value, fullname);
3199         }
3200     }
3201     else if (action == 3)
3202         FIXME("Append to existing section not yet implemented\n");
3203
3204     uirow = MSI_CreateRecord(4);
3205     MSI_RecordSetStringW(uirow,1,identifier);
3206     MSI_RecordSetStringW(uirow,2,deformated_section);
3207     MSI_RecordSetStringW(uirow,3,deformated_key);
3208     MSI_RecordSetStringW(uirow,4,deformated_value);
3209     ui_actiondata(package,szWriteIniValues,uirow);
3210     msiobj_release( &uirow->hdr );
3211 cleanup:
3212     HeapFree(GetProcessHeap(),0,fullname);
3213     HeapFree(GetProcessHeap(),0,folder);
3214     HeapFree(GetProcessHeap(),0,deformated_key);
3215     HeapFree(GetProcessHeap(),0,deformated_value);
3216     HeapFree(GetProcessHeap(),0,deformated_section);
3217     return ERROR_SUCCESS;
3218 }
3219
3220 static UINT ACTION_WriteIniValues(MSIPACKAGE *package)
3221 {
3222     UINT rc;
3223     MSIQUERY * view;
3224     static const WCHAR ExecSeqQuery[] = 
3225         {'S','E','L','E','C','T',' ','*',' ','F','R','O','M',' ',
3226          '`','I','n','i','F','i','l','e','`',0};
3227
3228     rc = MSI_DatabaseOpenViewW(package->db, ExecSeqQuery, &view);
3229     if (rc != ERROR_SUCCESS)
3230     {
3231         TRACE("no IniFile table\n");
3232         return ERROR_SUCCESS;
3233     }
3234
3235     rc = MSI_IterateRecords(view, NULL, ITERATE_WriteIniValues, package);
3236     msiobj_release(&view->hdr);
3237     return rc;
3238 }
3239
3240 static UINT ITERATE_SelfRegModules(MSIRECORD *row, LPVOID param)
3241 {
3242     MSIPACKAGE *package = (MSIPACKAGE*)param;
3243     LPCWSTR filename;
3244     LPWSTR FullName;
3245     INT index;
3246     DWORD len;
3247     static const WCHAR ExeStr[] =
3248         {'r','e','g','s','v','r','3','2','.','e','x','e',' ','\"',0};
3249     static const WCHAR close[] =  {'\"',0};
3250     STARTUPINFOW si;
3251     PROCESS_INFORMATION info;
3252     BOOL brc;
3253
3254     memset(&si,0,sizeof(STARTUPINFOW));
3255
3256     filename = MSI_RecordGetString(row,1);
3257     index = get_loaded_file(package,filename);
3258
3259     if (index < 0)
3260     {
3261         ERR("Unable to find file id %s\n",debugstr_w(filename));
3262         return ERROR_SUCCESS;
3263     }
3264
3265     len = strlenW(ExeStr);
3266     len += strlenW(package->files[index].TargetPath);
3267     len +=2;
3268
3269     FullName = HeapAlloc(GetProcessHeap(),0,len*sizeof(WCHAR));
3270     strcpyW(FullName,ExeStr);
3271     strcatW(FullName,package->files[index].TargetPath);
3272     strcatW(FullName,close);
3273
3274     TRACE("Registering %s\n",debugstr_w(FullName));
3275     brc = CreateProcessW(NULL, FullName, NULL, NULL, FALSE, 0, NULL, c_colon,
3276                     &si, &info);
3277
3278     if (brc)
3279         msi_dialog_check_messages(info.hProcess);
3280
3281     HeapFree(GetProcessHeap(),0,FullName);
3282     return ERROR_SUCCESS;
3283 }
3284
3285 static UINT ACTION_SelfRegModules(MSIPACKAGE *package)
3286 {
3287     UINT rc;
3288     MSIQUERY * view;
3289     static const WCHAR ExecSeqQuery[] = 
3290         {'S','E','L','E','C','T',' ','*',' ','F','R','O','M',' ',
3291          '`','S','e','l','f','R','e','g','`',0};
3292
3293     rc = MSI_DatabaseOpenViewW(package->db, ExecSeqQuery, &view);
3294     if (rc != ERROR_SUCCESS)
3295     {
3296         TRACE("no SelfReg table\n");
3297         return ERROR_SUCCESS;
3298     }
3299
3300     MSI_IterateRecords(view, NULL, ITERATE_SelfRegModules, package);
3301     msiobj_release(&view->hdr);
3302
3303     return ERROR_SUCCESS;
3304 }
3305
3306 static UINT ACTION_PublishFeatures(MSIPACKAGE *package)
3307 {
3308     UINT rc;
3309     DWORD i;
3310     HKEY hkey=0;
3311     HKEY hukey=0;
3312     
3313     if (!package)
3314         return ERROR_INVALID_HANDLE;
3315
3316     rc = MSIREG_OpenFeaturesKey(package->ProductCode,&hkey,TRUE);
3317     if (rc != ERROR_SUCCESS)
3318         goto end;
3319
3320     rc = MSIREG_OpenUserFeaturesKey(package->ProductCode,&hukey,TRUE);
3321     if (rc != ERROR_SUCCESS)
3322         goto end;
3323
3324     /* here the guids are base 85 encoded */
3325     for (i = 0; i < package->loaded_features; i++)
3326     {
3327         LPWSTR data = NULL;
3328         GUID clsid;
3329         int j;
3330         INT size;
3331         BOOL absent = FALSE;
3332
3333         if (!ACTION_VerifyFeatureForAction(package,i,INSTALLSTATE_LOCAL) &&
3334             !ACTION_VerifyFeatureForAction(package,i,INSTALLSTATE_SOURCE) &&
3335             !ACTION_VerifyFeatureForAction(package,i,INSTALLSTATE_ADVERTISED))
3336             absent = TRUE;
3337
3338         size = package->features[i].ComponentCount*21;
3339         size +=1;
3340         if (package->features[i].Feature_Parent[0])
3341             size += strlenW(package->features[i].Feature_Parent)+2;
3342
3343         data = HeapAlloc(GetProcessHeap(), 0, size * sizeof(WCHAR));
3344
3345         data[0] = 0;
3346         for (j = 0; j < package->features[i].ComponentCount; j++)
3347         {
3348             WCHAR buf[21];
3349             memset(buf,0,sizeof(buf));
3350             if (package->components
3351                 [package->features[i].Components[j]].ComponentId[0]!=0)
3352             {
3353                 TRACE("From %s\n",debugstr_w(package->components
3354                             [package->features[i].Components[j]].ComponentId));
3355                 CLSIDFromString(package->components
3356                             [package->features[i].Components[j]].ComponentId,
3357                             &clsid);
3358                 encode_base85_guid(&clsid,buf);
3359                 TRACE("to %s\n",debugstr_w(buf));
3360                 strcatW(data,buf);
3361             }
3362         }
3363         if (package->features[i].Feature_Parent[0])
3364         {
3365             static const WCHAR sep[] = {'\2',0};
3366             strcatW(data,sep);
3367             strcatW(data,package->features[i].Feature_Parent);
3368         }
3369
3370         size = (strlenW(data)+1)*sizeof(WCHAR);
3371         RegSetValueExW(hkey,package->features[i].Feature,0,REG_SZ,
3372                        (LPBYTE)data,size);
3373         HeapFree(GetProcessHeap(),0,data);
3374
3375         if (!absent)
3376         {
3377             size = strlenW(package->features[i].Feature_Parent)*sizeof(WCHAR);
3378             RegSetValueExW(hukey,package->features[i].Feature,0,REG_SZ,
3379                        (LPBYTE)package->features[i].Feature_Parent,size);
3380         }
3381         else
3382         {
3383             size = (strlenW(package->features[i].Feature_Parent)+2)*
3384                     sizeof(WCHAR);
3385             data = HeapAlloc(GetProcessHeap(),0,size);
3386             data[0] = 0x6;
3387             strcpyW(&data[1],package->features[i].Feature_Parent);
3388             RegSetValueExW(hukey,package->features[i].Feature,0,REG_SZ,
3389                        (LPBYTE)data,size);
3390             HeapFree(GetProcessHeap(),0,data);
3391         }
3392     }
3393
3394 end:
3395     RegCloseKey(hkey);
3396     RegCloseKey(hukey);
3397     return rc;
3398 }
3399
3400 static UINT ACTION_RegisterProduct(MSIPACKAGE *package)
3401 {
3402     HKEY hkey=0;
3403     LPWSTR buffer = NULL;
3404     UINT rc,i;
3405     DWORD size;
3406     static WCHAR szNONE[] = {0};
3407     static const WCHAR szWindowsInstaler[] = 
3408     {'W','i','n','d','o','w','s','I','n','s','t','a','l','l','e','r',0};
3409     static const WCHAR szPropKeys[][80] = 
3410     {
3411 {'A','R','P','A','U','T','H','O','R','I','Z','E','D','C','D','F','P','R','E','F','I','X',0},
3412 {'A','R','P','C','O','N','T','A','C','T',0},
3413 {'A','R','P','C','O','M','M','E','N','T','S',0},
3414 {'P','r','o','d','u','c','t','N','a','m','e',0},
3415 {'P','r','o','d','u','c','t','V','e','r','s','i','o','n',0},
3416 {'A','R','P','H','E','L','P','L','I','N','K',0},
3417 {'A','R','P','H','E','L','P','T','E','L','E','P','H','O','N','E',0},
3418 {'A','R','P','I','N','S','T','A','L','L','L','O','C','A','T','I','O','N',0},
3419 {'S','o','u','r','c','e','D','i','r',0},
3420 {'M','a','n','u','f','a','c','t','u','r','e','r',0},
3421 {'A','R','P','R','E','A','D','M','E',0},
3422 {'A','R','P','S','I','Z','E',0},
3423 {'A','R','P','U','R','L','I','N','F','O','A','B','O','U','T',0},
3424 {'A','R','P','U','R','L','U','P','D','A','T','E','I','N','F','O',0},
3425 {0},
3426     };
3427
3428     static const WCHAR szRegKeys[][80] = 
3429     {
3430 {'A','u','t','h','o','r','i','z','e','d','C','D','F','P','r','e','f','i','x',0},
3431 {'C','o','n','t','a','c','t',0},
3432 {'C','o','m','m','e','n','t','s',0},
3433 {'D','i','s','p','l','a','y','N','a','m','e',0},
3434 {'D','i','s','p','l','a','y','V','e','r','s','i','o','n',0},
3435 {'H','e','l','p','L','i','n','k',0},
3436 {'H','e','l','p','T','e','l','e','p','h','o','n','e',0},
3437 {'I','n','s','t','a','l','l','L','o','c','a','t','i','o','n',0},
3438 {'I','n','s','t','a','l','l','S','o','u','r','c','e',0},
3439 {'P','u','b','l','i','s','h','e','r',0},
3440 {'R','e','a','d','m','e',0},
3441 {'S','i','z','e',0},
3442 {'U','R','L','I','n','f','o','A','b','o','u','t',0},
3443 {'U','R','L','U','p','d','a','t','e','I','n','f','o',0},
3444 {0},
3445     };
3446
3447     static const WCHAR installerPathFmt[] = {
3448     '%','s','\\',
3449     'I','n','s','t','a','l','l','e','r','\\',0};
3450     static const WCHAR fmt[] = {
3451     '%','s','\\',
3452     'I','n','s','t','a','l','l','e','r','\\',
3453     '%','x','.','m','s','i',0};
3454     static const WCHAR szUpgradeCode[] = 
3455         {'U','p','g','r','a','d','e','C','o','d','e',0};
3456     static const WCHAR modpath_fmt[] = 
3457         {'M','s','i','E','x','e','c','.','e','x','e',' ','/','I','[','P','r','o','d','u','c','t','C','o','d','e',']',0};
3458     static const WCHAR szModifyPath[] = 
3459         {'M','o','d','i','f','y','P','a','t','h',0};
3460     static const WCHAR szUninstallString[] = 
3461         {'U','n','i','n','s','t','a','l','l','S','t','r','i','n','g',0};
3462     static const WCHAR szEstimatedSize[] = 
3463         {'E','s','t','i','m','a','t','e','d','S','i','z','e',0};
3464     static const WCHAR szProductLanguage[] =
3465         {'P','r','o','d','u','c','t','L','a','n','g','u','a','g','e',0};
3466     static const WCHAR szProductVersion[] =
3467         {'P','r','o','d','u','c','t','V','e','r','s','i','o','n',0};
3468
3469     SYSTEMTIME systime;
3470     static const WCHAR date_fmt[] = {'%','i','%','i','%','i',0};
3471     LPWSTR upgrade_code;
3472     WCHAR windir[MAX_PATH], path[MAX_PATH], packagefile[MAX_PATH];
3473     INT num,start;
3474
3475     if (!package)
3476         return ERROR_INVALID_HANDLE;
3477
3478     rc = MSIREG_OpenUninstallKey(package->ProductCode,&hkey,TRUE);
3479     if (rc != ERROR_SUCCESS)
3480         goto end;
3481
3482     /* dump all the info i can grab */
3483     FIXME("Flesh out more information \n");
3484
3485     i = 0;
3486     while (szPropKeys[i][0]!=0)
3487     {
3488         buffer = load_dynamic_property(package,szPropKeys[i],&rc);
3489         if (rc != ERROR_SUCCESS)
3490             buffer = szNONE;
3491         size = strlenW(buffer)*sizeof(WCHAR);
3492         RegSetValueExW(hkey,szRegKeys[i],0,REG_SZ,(LPBYTE)buffer,size);
3493         HeapFree(GetProcessHeap(),0,buffer);
3494         i++;
3495     }
3496
3497     rc = 0x1;
3498     size = sizeof(rc);
3499     RegSetValueExW(hkey,szWindowsInstaler,0,REG_DWORD,(LPBYTE)&rc,size);
3500     
3501     /* copy the package locally */
3502     num = GetTickCount() & 0xffff;
3503     if (!num) 
3504         num = 1;
3505     start = num;
3506     GetWindowsDirectoryW(windir, sizeof(windir) / sizeof(windir[0]));
3507     snprintfW(packagefile,sizeof(packagefile)/sizeof(packagefile[0]),fmt,
3508      windir,num);
3509     do 
3510     {
3511         HANDLE handle = CreateFileW(packagefile,GENERIC_WRITE, 0, NULL,
3512                                   CREATE_NEW, FILE_ATTRIBUTE_NORMAL, 0 );
3513         if (handle != INVALID_HANDLE_VALUE)
3514         {
3515             CloseHandle(handle);
3516             break;
3517         }
3518         if (GetLastError() != ERROR_FILE_EXISTS &&
3519             GetLastError() != ERROR_SHARING_VIOLATION)
3520             break;
3521         if (!(++num & 0xffff)) num = 1;
3522         sprintfW(packagefile,fmt,num);
3523     } while (num != start);
3524
3525     snprintfW(path,sizeof(path)/sizeof(path[0]),installerPathFmt,windir);
3526     create_full_pathW(path);
3527     TRACE("Copying to local package %s\n",debugstr_w(packagefile));
3528     if (!CopyFileW(package->msiFilePath,packagefile,FALSE))
3529         ERR("Unable to copy package (%s -> %s) (error %ld)\n",
3530             debugstr_w(package->msiFilePath), debugstr_w(packagefile),
3531             GetLastError());
3532     size = strlenW(packagefile)*sizeof(WCHAR);
3533     RegSetValueExW(hkey,INSTALLPROPERTY_LOCALPACKAGEW,0,REG_SZ,
3534             (LPBYTE)packagefile,size);
3535
3536     /* do ModifyPath and UninstallString */
3537     size = deformat_string(package,modpath_fmt,&buffer);
3538     RegSetValueExW(hkey,szModifyPath,0,REG_EXPAND_SZ,(LPBYTE)buffer,size);
3539     RegSetValueExW(hkey,szUninstallString,0,REG_EXPAND_SZ,(LPBYTE)buffer,size);
3540     HeapFree(GetProcessHeap(),0,buffer);
3541
3542     FIXME("Write real Estimated Size when we have it\n");
3543     size = 0;
3544     RegSetValueExW(hkey,szEstimatedSize,0,REG_DWORD,(LPBYTE)&size,sizeof(DWORD));
3545    
3546     GetLocalTime(&systime);
3547     size = 9*sizeof(WCHAR);
3548     buffer= HeapAlloc(GetProcessHeap(),0,size);
3549     sprintfW(buffer,date_fmt,systime.wYear,systime.wMonth,systime.wDay);
3550     size = strlenW(buffer)*sizeof(WCHAR);
3551     RegSetValueExW(hkey,INSTALLPROPERTY_INSTALLDATEW,0,REG_SZ,
3552             (LPBYTE)buffer,size);
3553     HeapFree(GetProcessHeap(),0,buffer);
3554    
3555     buffer = load_dynamic_property(package,szProductLanguage,NULL);
3556     size = atoiW(buffer);
3557     RegSetValueExW(hkey,INSTALLPROPERTY_LANGUAGEW,0,REG_DWORD,
3558             (LPBYTE)&size,sizeof(DWORD));
3559     HeapFree(GetProcessHeap(),1,buffer);
3560
3561     buffer = load_dynamic_property(package,szProductVersion,NULL);
3562     if (buffer)
3563     {
3564         DWORD verdword = build_version_dword(buffer);
3565         DWORD vermajor = verdword>>24;
3566         DWORD verminor = (verdword>>16)&0x00FF;
3567         size = sizeof(DWORD);
3568         RegSetValueExW(hkey,INSTALLPROPERTY_VERSIONW,0,REG_DWORD,
3569                 (LPBYTE)&verdword,size);
3570         RegSetValueExW(hkey,INSTALLPROPERTY_VERSIONMAJORW,0,REG_DWORD,
3571                 (LPBYTE)&vermajor,size);
3572         RegSetValueExW(hkey,INSTALLPROPERTY_VERSIONMINORW,0,REG_DWORD,
3573                 (LPBYTE)&verminor,size);
3574     }
3575     HeapFree(GetProcessHeap(),0,buffer);
3576     
3577     /* Handle Upgrade Codes */
3578     upgrade_code = load_dynamic_property(package,szUpgradeCode, NULL);
3579     if (upgrade_code)
3580     {
3581         HKEY hkey2;
3582         WCHAR squashed[33];
3583         MSIREG_OpenUpgradeCodesKey(upgrade_code, &hkey2, TRUE);
3584         squash_guid(package->ProductCode,squashed);
3585         RegSetValueExW(hkey2, squashed, 0,REG_SZ,NULL,0);
3586         RegCloseKey(hkey2);
3587         MSIREG_OpenUserUpgradeCodesKey(upgrade_code, &hkey2, TRUE);
3588         squash_guid(package->ProductCode,squashed);
3589         RegSetValueExW(hkey2, squashed, 0,REG_SZ,NULL,0);
3590         RegCloseKey(hkey2);
3591
3592         HeapFree(GetProcessHeap(),0,upgrade_code);
3593     }
3594     
3595 end:
3596     RegCloseKey(hkey);
3597
3598     return ERROR_SUCCESS;
3599 }
3600
3601 static UINT ACTION_InstallExecute(MSIPACKAGE *package)
3602 {
3603     UINT rc;
3604
3605     if (!package)
3606         return ERROR_INVALID_HANDLE;
3607
3608     rc = execute_script(package,INSTALL_SCRIPT);
3609
3610     return rc;
3611 }
3612
3613 static UINT ACTION_InstallFinalize(MSIPACKAGE *package)
3614 {
3615     UINT rc;
3616
3617     if (!package)
3618         return ERROR_INVALID_HANDLE;
3619
3620     /* turn off scheduleing */
3621     package->script->CurrentlyScripting= FALSE;
3622
3623     /* first do the same as an InstallExecute */
3624     rc = ACTION_InstallExecute(package);
3625     if (rc != ERROR_SUCCESS)
3626         return rc;
3627
3628     /* then handle Commit Actions */
3629     rc = execute_script(package,COMMIT_SCRIPT);
3630
3631     return rc;
3632 }
3633
3634 static UINT ACTION_ForceReboot(MSIPACKAGE *package)
3635 {
3636     static const WCHAR RunOnce[] = {
3637     'S','o','f','t','w','a','r','e','\\',
3638     'M','i','c','r','o','s','o','f','t','\\',
3639     'W','i','n','d','o','w','s','\\',
3640     'C','u','r','r','e','n','t','V','e','r','s','i','o','n','\\',
3641     'R','u','n','O','n','c','e',0};
3642     static const WCHAR InstallRunOnce[] = {
3643     'S','o','f','t','w','a','r','e','\\',
3644     'M','i','c','r','o','s','o','f','t','\\',
3645     'W','i','n','d','o','w','s','\\',
3646     'C','u','r','r','e','n','t','V','e','r','s','i','o','n','\\',
3647     'I','n','s','t','a','l','l','e','r','\\',
3648     'R','u','n','O','n','c','e','E','n','t','r','i','e','s',0};
3649
3650     static const WCHAR msiexec_fmt[] = {
3651     '%','s',
3652     '\\','M','s','i','E','x','e','c','.','e','x','e',' ','/','@',' ',
3653     '\"','%','s','\"',0};
3654     static const WCHAR install_fmt[] = {
3655     '/','I',' ','\"','%','s','\"',' ',
3656     'A','F','T','E','R','R','E','B','O','O','T','=','1',' ',
3657     'R','U','N','O','N','C','E','E','N','T','R','Y','=','\"','%','s','\"',0};
3658     WCHAR buffer[256], sysdir[MAX_PATH];
3659     HKEY hkey;
3660     WCHAR  squished_pc[100];
3661     DWORD size;
3662
3663     if (!package)
3664         return ERROR_INVALID_HANDLE;
3665
3666     squash_guid(package->ProductCode,squished_pc);
3667
3668     GetSystemDirectoryW(sysdir, sizeof(sysdir)/sizeof(sysdir[0]));
3669     RegCreateKeyW(HKEY_LOCAL_MACHINE,RunOnce,&hkey);
3670     snprintfW(buffer,sizeof(buffer)/sizeof(buffer[0]),msiexec_fmt,sysdir,
3671      squished_pc);
3672
3673     size = strlenW(buffer)*sizeof(WCHAR);
3674     RegSetValueExW(hkey,squished_pc,0,REG_SZ,(LPBYTE)buffer,size);
3675     RegCloseKey(hkey);
3676
3677     TRACE("Reboot command %s\n",debugstr_w(buffer));
3678
3679     RegCreateKeyW(HKEY_LOCAL_MACHINE,InstallRunOnce,&hkey);
3680     sprintfW(buffer,install_fmt,package->ProductCode,squished_pc);
3681
3682     size = strlenW(buffer)*sizeof(WCHAR);
3683     RegSetValueExW(hkey,squished_pc,0,REG_SZ,(LPBYTE)buffer,size);
3684     RegCloseKey(hkey);
3685
3686     return ERROR_INSTALL_SUSPEND;
3687 }
3688
3689 UINT ACTION_ResolveSource(MSIPACKAGE* package)
3690 {
3691     /*
3692      * we are currently doing what should be done here in the top level Install
3693      * however for Adminastrative and uninstalls this step will be needed
3694      */
3695     return ERROR_SUCCESS;
3696 }
3697
3698 static UINT ACTION_RegisterUser(MSIPACKAGE *package)
3699 {
3700     HKEY hkey=0;
3701     LPWSTR buffer;
3702     LPWSTR productid;
3703     UINT rc,i;
3704     DWORD size;
3705
3706     static const WCHAR szPropKeys[][80] = 
3707     {
3708         {'P','r','o','d','u','c','t','I','D',0},
3709         {'U','S','E','R','N','A','M','E',0},
3710         {'C','O','M','P','A','N','Y','N','A','M','E',0},
3711         {0},
3712     };
3713
3714     static const WCHAR szRegKeys[][80] = 
3715     {
3716         {'P','r','o','d','u','c','t','I','D',0},
3717         {'R','e','g','O','w','n','e','r',0},
3718         {'R','e','g','C','o','m','p','a','n','y',0},
3719         {0},
3720     };
3721
3722     if (!package)
3723         return ERROR_INVALID_HANDLE;
3724
3725     productid = load_dynamic_property(package,INSTALLPROPERTY_PRODUCTIDW,
3726             &rc);
3727     if (!productid)
3728         return ERROR_SUCCESS;
3729
3730     rc = MSIREG_OpenUninstallKey(package->ProductCode,&hkey,TRUE);
3731     if (rc != ERROR_SUCCESS)
3732         goto end;
3733
3734     i = 0;
3735     while (szPropKeys[i][0]!=0)
3736     {
3737         buffer = load_dynamic_property(package,szPropKeys[i],&rc);
3738         if (rc == ERROR_SUCCESS)
3739         {
3740             size = strlenW(buffer)*sizeof(WCHAR);
3741             RegSetValueExW(hkey,szRegKeys[i],0,REG_SZ,(LPBYTE)buffer,size);
3742         }
3743         else
3744             RegSetValueExW(hkey,szRegKeys[i],0,REG_SZ,NULL,0);
3745         i++;
3746     }
3747
3748 end:
3749     HeapFree(GetProcessHeap(),0,productid);
3750     RegCloseKey(hkey);
3751
3752     return ERROR_SUCCESS;
3753 }
3754
3755
3756 static UINT ACTION_ExecuteAction(MSIPACKAGE *package)
3757 {
3758     static const WCHAR szUILevel[] = {'U','I','L','e','v','e','l',0};
3759     static const WCHAR szTwo[] = {'2',0};
3760     UINT rc;
3761     LPWSTR level;
3762     level = load_dynamic_property(package,szUILevel,NULL);
3763
3764     MSI_SetPropertyW(package,szUILevel,szTwo);
3765     package->script->InWhatSequence |= SEQUENCE_EXEC;
3766     rc = ACTION_ProcessExecSequence(package,FALSE);
3767     MSI_SetPropertyW(package,szUILevel,level);
3768     HeapFree(GetProcessHeap(),0,level);
3769     return rc;
3770 }
3771
3772
3773 /*
3774  * Code based off of code located here
3775  * http://www.codeproject.com/gdi/fontnamefromfile.asp
3776  *
3777  * Using string index 4 (full font name) instead of 1 (family name)
3778  */
3779 static LPWSTR load_ttfname_from(LPCWSTR filename)
3780 {
3781     HANDLE handle;
3782     LPWSTR ret = NULL;
3783     int i;
3784
3785     typedef struct _tagTT_OFFSET_TABLE{
3786         USHORT uMajorVersion;
3787         USHORT uMinorVersion;
3788         USHORT uNumOfTables;
3789         USHORT uSearchRange;
3790         USHORT uEntrySelector;
3791         USHORT uRangeShift;
3792     }TT_OFFSET_TABLE;
3793
3794     typedef struct _tagTT_TABLE_DIRECTORY{
3795         char szTag[4]; /* table name */
3796         ULONG uCheckSum; /* Check sum */
3797         ULONG uOffset; /* Offset from beginning of file */
3798         ULONG uLength; /* length of the table in bytes */
3799     }TT_TABLE_DIRECTORY;
3800
3801     typedef struct _tagTT_NAME_TABLE_HEADER{
3802     USHORT uFSelector; /* format selector. Always 0 */
3803     USHORT uNRCount; /* Name Records count */
3804     USHORT uStorageOffset; /* Offset for strings storage, 
3805                             * from start of the table */
3806     }TT_NAME_TABLE_HEADER;
3807    
3808     typedef struct _tagTT_NAME_RECORD{
3809         USHORT uPlatformID;
3810         USHORT uEncodingID;
3811         USHORT uLanguageID;
3812         USHORT uNameID;
3813         USHORT uStringLength;
3814         USHORT uStringOffset; /* from start of storage area */
3815     }TT_NAME_RECORD;
3816
3817 #define SWAPWORD(x) MAKEWORD(HIBYTE(x), LOBYTE(x))
3818 #define SWAPLONG(x) MAKELONG(SWAPWORD(HIWORD(x)), SWAPWORD(LOWORD(x)))
3819
3820     handle = CreateFileW(filename ,GENERIC_READ, 0, NULL, OPEN_EXISTING,
3821                     FILE_ATTRIBUTE_NORMAL, 0 );
3822     if (handle != INVALID_HANDLE_VALUE)
3823     {
3824         TT_TABLE_DIRECTORY tblDir;
3825         BOOL bFound = FALSE;
3826         TT_OFFSET_TABLE ttOffsetTable;
3827
3828         ReadFile(handle,&ttOffsetTable, sizeof(TT_OFFSET_TABLE),NULL,NULL);
3829         ttOffsetTable.uNumOfTables = SWAPWORD(ttOffsetTable.uNumOfTables);
3830         ttOffsetTable.uMajorVersion = SWAPWORD(ttOffsetTable.uMajorVersion);
3831         ttOffsetTable.uMinorVersion = SWAPWORD(ttOffsetTable.uMinorVersion);
3832         
3833         if (ttOffsetTable.uMajorVersion != 1 || 
3834                         ttOffsetTable.uMinorVersion != 0)
3835             return NULL;
3836
3837         for (i=0; i< ttOffsetTable.uNumOfTables; i++)
3838         {
3839             ReadFile(handle,&tblDir, sizeof(TT_TABLE_DIRECTORY),NULL,NULL);
3840             if (strncmp(tblDir.szTag,"name",4)==0)
3841             {
3842                 bFound = TRUE;
3843                 tblDir.uLength = SWAPLONG(tblDir.uLength);
3844                 tblDir.uOffset = SWAPLONG(tblDir.uOffset);
3845                 break;
3846             }
3847         }
3848
3849         if (bFound)
3850         {
3851             TT_NAME_TABLE_HEADER ttNTHeader;
3852             TT_NAME_RECORD ttRecord;
3853
3854             SetFilePointer(handle, tblDir.uOffset, NULL, FILE_BEGIN);
3855             ReadFile(handle,&ttNTHeader, sizeof(TT_NAME_TABLE_HEADER),
3856                             NULL,NULL);
3857
3858             ttNTHeader.uNRCount = SWAPWORD(ttNTHeader.uNRCount);
3859             ttNTHeader.uStorageOffset = SWAPWORD(ttNTHeader.uStorageOffset);
3860             bFound = FALSE;
3861             for(i=0; i<ttNTHeader.uNRCount; i++)
3862             {
3863                 ReadFile(handle,&ttRecord, sizeof(TT_NAME_RECORD),NULL,NULL);
3864                 ttRecord.uNameID = SWAPWORD(ttRecord.uNameID);
3865                 /* 4 is the Full Font Name */
3866                 if(ttRecord.uNameID == 4)
3867                 {
3868                     int nPos;
3869                     LPSTR buf;
3870                     static LPCSTR tt = " (TrueType)";
3871
3872                     ttRecord.uStringLength = SWAPWORD(ttRecord.uStringLength);
3873                     ttRecord.uStringOffset = SWAPWORD(ttRecord.uStringOffset);
3874                     nPos = SetFilePointer(handle, 0, NULL, FILE_CURRENT);
3875                     SetFilePointer(handle, tblDir.uOffset + 
3876                                     ttRecord.uStringOffset + 
3877                                     ttNTHeader.uStorageOffset,
3878                                     NULL, FILE_BEGIN);
3879                     buf = HeapAlloc(GetProcessHeap(), 0, 
3880                                     ttRecord.uStringLength + 1 + strlen(tt));
3881                     memset(buf, 0, ttRecord.uStringLength + 1 + strlen(tt));
3882                     ReadFile(handle, buf, ttRecord.uStringLength, NULL, NULL);
3883                     if (strlen(buf) > 0)
3884                     {
3885                         strcat(buf,tt);
3886                         ret = strdupAtoW(buf);
3887                         HeapFree(GetProcessHeap(),0,buf);
3888                         break;
3889                     }
3890
3891                     HeapFree(GetProcessHeap(),0,buf);
3892                     SetFilePointer(handle,nPos, NULL, FILE_BEGIN);
3893                 }
3894             }
3895         }
3896         CloseHandle(handle);
3897     }
3898     else
3899         ERR("Unable to open font file %s\n", debugstr_w(filename));
3900
3901     TRACE("Returning fontname %s\n",debugstr_w(ret));
3902     return ret;
3903 }
3904
3905 static UINT ITERATE_RegisterFonts(MSIRECORD *row, LPVOID param)
3906 {
3907     MSIPACKAGE *package = (MSIPACKAGE*)param;
3908     LPWSTR name;
3909     LPCWSTR file;
3910     UINT index;
3911     DWORD size;
3912     static const WCHAR regfont1[] =
3913         {'S','o','f','t','w','a','r','e','\\',
3914          'M','i','c','r','o','s','o','f','t','\\',
3915          'W','i','n','d','o','w','s',' ','N','T','\\',
3916          'C','u','r','r','e','n','t','V','e','r','s','i','o','n','\\',
3917          'F','o','n','t','s',0};
3918     static const WCHAR regfont2[] =
3919         {'S','o','f','t','w','a','r','e','\\',
3920          'M','i','c','r','o','s','o','f','t','\\',
3921          'W','i','n','d','o','w','s','\\',
3922          'C','u','r','r','e','n','t','V','e','r','s','i','o','n','\\',
3923          'F','o','n','t','s',0};
3924     HKEY hkey1;
3925     HKEY hkey2;
3926
3927     file = MSI_RecordGetString(row,1);
3928     index = get_loaded_file(package,file);
3929     if (index < 0)
3930     {
3931         ERR("Unable to load file\n");
3932         return ERROR_SUCCESS;
3933     }
3934
3935     /* check to make sure that component is installed */
3936     if (!ACTION_VerifyComponentForAction(package, 
3937                 package->files[index].ComponentIndex, INSTALLSTATE_LOCAL))
3938     {
3939         TRACE("Skipping: Component not scheduled for install\n");
3940         return ERROR_SUCCESS;
3941     }
3942
3943     RegCreateKeyW(HKEY_LOCAL_MACHINE,regfont1,&hkey1);
3944     RegCreateKeyW(HKEY_LOCAL_MACHINE,regfont2,&hkey2);
3945
3946     if (MSI_RecordIsNull(row,2))
3947         name = load_ttfname_from(package->files[index].TargetPath);
3948     else
3949         name = load_dynamic_stringW(row,2);
3950
3951     if (name)
3952     {
3953         size = strlenW(package->files[index].FileName) * sizeof(WCHAR);
3954         RegSetValueExW(hkey1,name,0,REG_SZ,
3955                     (LPBYTE)package->files[index].FileName,size);
3956         RegSetValueExW(hkey2,name,0,REG_SZ,
3957                     (LPBYTE)package->files[index].FileName,size);
3958     }
3959
3960     HeapFree(GetProcessHeap(),0,name);
3961     RegCloseKey(hkey1);
3962     RegCloseKey(hkey2);
3963     return ERROR_SUCCESS;
3964 }
3965
3966 static UINT ACTION_RegisterFonts(MSIPACKAGE *package)
3967 {
3968     UINT rc;
3969     MSIQUERY * view;
3970     static const WCHAR ExecSeqQuery[] =
3971         {'S','E','L','E','C','T',' ','*',' ','F','R','O','M',' ',
3972          '`','F','o','n','t','`',0};
3973
3974     rc = MSI_DatabaseOpenViewW(package->db, ExecSeqQuery, &view);
3975     if (rc != ERROR_SUCCESS)
3976     {
3977         TRACE("MSI_DatabaseOpenViewW failed: %d\n", rc);
3978         return ERROR_SUCCESS;
3979     }
3980
3981     MSI_IterateRecords(view, NULL, ITERATE_RegisterFonts, package);
3982     msiobj_release(&view->hdr);
3983
3984     return ERROR_SUCCESS;
3985 }
3986
3987 static UINT ITERATE_PublishComponent(MSIRECORD *rec, LPVOID param)
3988 {
3989     MSIPACKAGE *package = (MSIPACKAGE*)param;
3990     LPCWSTR compgroupid=NULL;
3991     LPCWSTR feature=NULL;
3992     LPCWSTR text = NULL;
3993     LPCWSTR qualifier = NULL;
3994     LPCWSTR component = NULL;
3995     LPWSTR advertise = NULL;
3996     LPWSTR output = NULL;
3997     HKEY hkey;
3998     UINT rc = ERROR_SUCCESS;
3999     UINT index;
4000     DWORD sz = 0;
4001
4002     component = MSI_RecordGetString(rec,3);
4003     index = get_loaded_component(package,component);
4004
4005     if (!ACTION_VerifyComponentForAction(package, index,
4006                             INSTALLSTATE_LOCAL) && 
4007        !ACTION_VerifyComponentForAction(package, index,
4008                             INSTALLSTATE_SOURCE) &&
4009        !ACTION_VerifyComponentForAction(package, index,
4010                             INSTALLSTATE_ADVERTISED))
4011     {
4012         TRACE("Skipping: Component %s not scheduled for install\n",
4013                         debugstr_w(component));
4014
4015         return ERROR_SUCCESS;
4016     }
4017
4018     compgroupid = MSI_RecordGetString(rec,1);
4019
4020     rc = MSIREG_OpenUserComponentsKey(compgroupid, &hkey, TRUE);
4021     if (rc != ERROR_SUCCESS)
4022         goto end;
4023     
4024     text = MSI_RecordGetString(rec,4);
4025     qualifier = MSI_RecordGetString(rec,2);
4026     feature = MSI_RecordGetString(rec,5);
4027   
4028     advertise = create_component_advertise_string(package, 
4029                     &package->components[index], feature);
4030
4031     sz = strlenW(advertise);
4032
4033     if (text)
4034         sz += lstrlenW(text);
4035
4036     sz+=3;
4037     sz *= sizeof(WCHAR);
4038            
4039     output = HeapAlloc(GetProcessHeap(),0,sz);
4040     memset(output,0,sz);
4041     strcpyW(output,advertise);
4042
4043     if (text)
4044         strcatW(output,text);
4045
4046     sz = (lstrlenW(output)+2) * sizeof(WCHAR);
4047     RegSetValueExW(hkey, qualifier,0,REG_MULTI_SZ, (LPBYTE)output, sz);
4048     
4049 end:
4050     RegCloseKey(hkey);
4051     HeapFree(GetProcessHeap(),0,output);
4052     
4053     return rc;
4054 }
4055
4056 /*
4057  * At present I am ignorning the advertised components part of this and only
4058  * focusing on the qualified component sets
4059  */
4060 static UINT ACTION_PublishComponents(MSIPACKAGE *package)
4061 {
4062     UINT rc;
4063     MSIQUERY * view;
4064     static const WCHAR ExecSeqQuery[] =
4065         {'S','E','L','E','C','T',' ','*',' ','F','R','O','M',' ',
4066          '`','P','u','b','l','i','s','h',
4067          'C','o','m','p','o','n','e','n','t','`',0};
4068     
4069     rc = MSI_DatabaseOpenViewW(package->db, ExecSeqQuery, &view);
4070     if (rc != ERROR_SUCCESS)
4071         return ERROR_SUCCESS;
4072
4073     rc = MSI_IterateRecords(view, NULL, ITERATE_PublishComponent, package);
4074     msiobj_release(&view->hdr);
4075
4076     return rc;
4077 }