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