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