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