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