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