Fix a bug in a previous patch spotted by Dieter Komendera.
[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 = msi_dup_property( package, cszSourceDir );
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
973     comp = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(MSICOMPONENT) );
974     if (!comp)
975         return comp;
976
977     /* fill in the data */
978     comp->Component = load_dynamic_stringW( row, 1 );
979
980     TRACE("Loading Component %s\n", debugstr_w(comp->Component));
981
982     comp->ComponentId = load_dynamic_stringW( row, 2 );
983     comp->Directory = load_dynamic_stringW( row, 3 );
984     comp->Attributes = MSI_RecordGetInteger(row,4);
985     comp->Condition = load_dynamic_stringW( row, 5 );
986     comp->KeyPath = load_dynamic_stringW( row, 6 );
987
988     comp->Installed = INSTALLSTATE_ABSENT;
989     comp->Action = INSTALLSTATE_UNKNOWN;
990     comp->ActionRequest = INSTALLSTATE_UNKNOWN;
991
992     comp->Enabled = TRUE;
993
994     return comp;
995 }
996
997 typedef struct {
998     MSIPACKAGE *package;
999     MSIFEATURE *feature;
1000 } _ilfs;
1001
1002 static UINT add_feature_component( MSIFEATURE *feature, MSICOMPONENT *comp )
1003 {
1004     ComponentList *cl;
1005
1006     cl = HeapAlloc( GetProcessHeap(), 0, sizeof (*cl) );
1007     if ( !cl )
1008         return ERROR_NOT_ENOUGH_MEMORY;
1009     cl->component = comp;
1010     list_add_tail( &feature->Components, &cl->entry );
1011
1012     return ERROR_SUCCESS;
1013 }
1014
1015 static UINT iterate_component_check( MSIRECORD *row, LPVOID param )
1016 {
1017     _ilfs* ilfs= (_ilfs*)param;
1018     MSIPACKAGE *package = ilfs->package;
1019     MSIFEATURE *feature = ilfs->feature;
1020     MSICOMPONENT *comp;
1021
1022     comp = load_component( row );
1023     if (!comp)
1024         return ERROR_FUNCTION_FAILED;
1025
1026     list_add_tail( &package->components, &comp->entry );
1027     add_feature_component( feature, comp );
1028
1029     TRACE("Loaded new component %p\n", comp);
1030
1031     return ERROR_SUCCESS;
1032 }
1033
1034 static UINT iterate_load_featurecomponents(MSIRECORD *row, LPVOID param)
1035 {
1036     _ilfs* ilfs= (_ilfs*)param;
1037     LPCWSTR component;
1038     DWORD rc;
1039     MSICOMPONENT *comp;
1040     MSIQUERY * view;
1041     static const WCHAR Query[] = 
1042         {'S','E','L','E','C','T',' ','*',' ','F','R', 'O','M',' ', 
1043          '`','C','o','m','p','o','n','e','n','t','`',' ',
1044          'W','H','E','R','E',' ', 
1045          '`','C','o','m','p','o','n','e','n','t','`',' ',
1046          '=','\'','%','s','\'',0};
1047
1048     component = MSI_RecordGetString(row,1);
1049
1050     /* check to see if the component is already loaded */
1051     comp = get_loaded_component( ilfs->package, component );
1052     if (comp)
1053     {
1054         TRACE("Component %s already loaded\n", debugstr_w(component) );
1055         add_feature_component( ilfs->feature, comp );
1056         return ERROR_SUCCESS;
1057     }
1058
1059     rc = MSI_OpenQuery(ilfs->package->db, &view, Query, component);
1060     if (rc != ERROR_SUCCESS)
1061         return ERROR_SUCCESS;
1062
1063     rc = MSI_IterateRecords(view, NULL, iterate_component_check, ilfs);
1064     msiobj_release( &view->hdr );
1065
1066     return ERROR_SUCCESS;
1067 }
1068
1069 static UINT load_feature(MSIRECORD * row, LPVOID param)
1070 {
1071     MSIPACKAGE* package = (MSIPACKAGE*)param;
1072     MSIFEATURE* feature;
1073     static const WCHAR Query1[] = 
1074         {'S','E','L','E','C','T',' ',
1075          '`','C','o','m','p','o','n','e','n','t','_','`',
1076          ' ','F','R','O','M',' ','`','F','e','a','t','u','r','e',
1077          'C','o','m','p','o','n','e','n','t','s','`',' ',
1078          'W','H','E','R','E',' ',
1079          '`','F','e', 'a','t','u','r','e','_','`',' ','=','\'','%','s','\'',0};
1080     MSIQUERY * view;
1081     UINT    rc;
1082     _ilfs ilfs;
1083
1084     /* fill in the data */
1085
1086     feature = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof (MSIFEATURE) );
1087     if (!feature)
1088         return ERROR_NOT_ENOUGH_MEMORY;
1089
1090     list_init( &feature->Components );
1091     
1092     feature->Feature = load_dynamic_stringW( row, 1 );
1093
1094     TRACE("Loading feature %s\n",debugstr_w(feature->Feature));
1095
1096     feature->Feature_Parent = load_dynamic_stringW( row, 2 );
1097     feature->Title = load_dynamic_stringW( row, 3 );
1098     feature->Description = load_dynamic_stringW( row, 4 );
1099
1100     if (!MSI_RecordIsNull(row,5))
1101         feature->Display = MSI_RecordGetInteger(row,5);
1102   
1103     feature->Level= MSI_RecordGetInteger(row,6);
1104     feature->Directory = load_dynamic_stringW( row, 7 );
1105     feature->Attributes = MSI_RecordGetInteger(row,8);
1106
1107     feature->Installed = INSTALLSTATE_ABSENT;
1108     feature->Action = INSTALLSTATE_UNKNOWN;
1109     feature->ActionRequest = INSTALLSTATE_UNKNOWN;
1110
1111     list_add_tail( &package->features, &feature->entry );
1112
1113     /* load feature components */
1114
1115     rc = MSI_OpenQuery( package->db, &view, Query1, feature->Feature );
1116     if (rc != ERROR_SUCCESS)
1117         return ERROR_SUCCESS;
1118
1119     ilfs.package = package;
1120     ilfs.feature = feature;
1121
1122     MSI_IterateRecords(view, NULL, iterate_load_featurecomponents , &ilfs);
1123     msiobj_release(&view->hdr);
1124
1125     return ERROR_SUCCESS;
1126 }
1127
1128 static UINT load_file(MSIRECORD *row, LPVOID param)
1129 {
1130     MSIPACKAGE* package = (MSIPACKAGE*)param;
1131     LPCWSTR component;
1132     MSIFILE *file;
1133
1134     /* fill in the data */
1135
1136     file = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof (MSIFILE) );
1137     if (!file)
1138         return ERROR_NOT_ENOUGH_MEMORY;
1139  
1140     file->File = load_dynamic_stringW( row, 1 );
1141
1142     component = MSI_RecordGetString( row, 2 );
1143     file->Component = get_loaded_component( package, component );
1144
1145     if (!file->Component)
1146         ERR("Unfound Component %s\n",debugstr_w(component));
1147
1148     file->FileName = load_dynamic_stringW( row, 3 );
1149     reduce_to_longfilename( file->FileName );
1150
1151     file->ShortName = load_dynamic_stringW( row, 3 );
1152     reduce_to_shortfilename( file->ShortName );
1153     
1154     file->FileSize = MSI_RecordGetInteger( row, 4 );
1155     file->Version = load_dynamic_stringW( row, 5 );
1156     file->Language = load_dynamic_stringW( row, 6 );
1157     file->Attributes = MSI_RecordGetInteger( row, 7 );
1158     file->Sequence = MSI_RecordGetInteger( row, 8 );
1159
1160     file->State = 0;
1161
1162     TRACE("File Loaded (%s)\n",debugstr_w(file->File));  
1163
1164     list_add_tail( &package->files, &file->entry );
1165  
1166     return ERROR_SUCCESS;
1167 }
1168
1169 static UINT load_all_files(MSIPACKAGE *package)
1170 {
1171     MSIQUERY * view;
1172     UINT rc;
1173     static const WCHAR Query[] =
1174         {'S','E','L','E','C','T',' ','*',' ', 'F','R','O','M',' ',
1175          '`','F','i','l','e','`',' ', 'O','R','D','E','R',' ','B','Y',' ',
1176          '`','S','e','q','u','e','n','c','e','`', 0};
1177
1178     if (!package)
1179         return ERROR_INVALID_HANDLE;
1180
1181     rc = MSI_DatabaseOpenViewW(package->db, Query, &view);
1182     if (rc != ERROR_SUCCESS)
1183         return ERROR_SUCCESS;
1184
1185     rc = MSI_IterateRecords(view, NULL, load_file, package);
1186     msiobj_release(&view->hdr);
1187
1188     return ERROR_SUCCESS;
1189 }
1190
1191
1192 /*
1193  * I am not doing any of the costing functionality yet. 
1194  * Mostly looking at doing the Component and Feature loading
1195  *
1196  * The native MSI does A LOT of modification to tables here. Mostly adding
1197  * a lot of temporary columns to the Feature and Component tables. 
1198  *
1199  *    note: Native msi also tracks the short filename. But I am only going to
1200  *          track the long ones.  Also looking at this directory table
1201  *          it appears that the directory table does not get the parents
1202  *          resolved base on property only based on their entries in the 
1203  *          directory table.
1204  */
1205 static UINT ACTION_CostInitialize(MSIPACKAGE *package)
1206 {
1207     MSIQUERY * view;
1208     UINT rc;
1209     static const WCHAR Query_all[] =
1210         {'S','E','L','E','C','T',' ','*',' ', 'F','R','O','M',' ',
1211          '`','F','e','a','t','u','r','e','`',0};
1212     static const WCHAR szCosting[] =
1213         {'C','o','s','t','i','n','g','C','o','m','p','l','e','t','e',0 };
1214     static const WCHAR szZero[] = { '0', 0 };
1215     WCHAR buffer[3];
1216     DWORD sz = 3;
1217
1218     MSI_GetPropertyW(package, szCosting, buffer, &sz);
1219     if (buffer[0]=='1')
1220         return ERROR_SUCCESS;
1221     
1222     MSI_SetPropertyW(package, szCosting, szZero);
1223     MSI_SetPropertyW(package, cszRootDrive , c_colon);
1224
1225     rc = MSI_DatabaseOpenViewW(package->db,Query_all,&view);
1226     if (rc != ERROR_SUCCESS)
1227         return rc;
1228
1229     rc = MSI_IterateRecords(view, NULL, load_feature, package);
1230     msiobj_release(&view->hdr);
1231
1232     load_all_files(package);
1233
1234     return ERROR_SUCCESS;
1235 }
1236
1237 static UINT execute_script(MSIPACKAGE *package, UINT script )
1238 {
1239     int i;
1240     UINT rc = ERROR_SUCCESS;
1241
1242     TRACE("Executing Script %i\n",script);
1243
1244     for (i = 0; i < package->script->ActionCount[script]; i++)
1245     {
1246         LPWSTR action;
1247         action = package->script->Actions[script][i];
1248         ui_actionstart(package, action);
1249         TRACE("Executing Action (%s)\n",debugstr_w(action));
1250         rc = ACTION_PerformAction(package, action, TRUE);
1251         HeapFree(GetProcessHeap(),0,package->script->Actions[script][i]);
1252         if (rc != ERROR_SUCCESS)
1253             break;
1254     }
1255     HeapFree(GetProcessHeap(),0,package->script->Actions[script]);
1256
1257     package->script->ActionCount[script] = 0;
1258     package->script->Actions[script] = NULL;
1259     return rc;
1260 }
1261
1262 static UINT ACTION_FileCost(MSIPACKAGE *package)
1263 {
1264     return ERROR_SUCCESS;
1265 }
1266
1267
1268 static MSIFOLDER *load_folder( MSIPACKAGE *package, LPCWSTR dir )
1269 {
1270     static const WCHAR Query[] =
1271         {'S','E','L','E','C','T',' ','*',' ','F','R','O','M',' ',
1272          '`','D','i','r','e','c', 't','o','r','y','`',' ',
1273          'W','H','E','R','E',' ', '`', 'D','i','r','e','c','t', 'o','r','y','`',
1274          ' ','=',' ','\'','%','s','\'',
1275          0};
1276     LPWSTR ptargetdir, targetdir, srcdir;
1277     LPCWSTR parent;
1278     LPWSTR shortname = NULL;
1279     MSIRECORD * row = 0;
1280     MSIFOLDER *folder;
1281
1282     TRACE("Looking for dir %s\n",debugstr_w(dir));
1283
1284     folder = get_loaded_folder( package, dir );
1285     if (folder)
1286         return folder;
1287
1288     TRACE("Working to load %s\n",debugstr_w(dir));
1289
1290     folder = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof (MSIFOLDER) );
1291     if (!folder)
1292         return NULL;
1293
1294     folder->Directory = strdupW(dir);
1295
1296     row = MSI_QueryGetRecord(package->db, Query, dir);
1297     if (!row)
1298         return NULL;
1299
1300     ptargetdir = targetdir = load_dynamic_stringW(row,3);
1301
1302     /* split src and target dir */
1303     if (strchrW(targetdir,':'))
1304     {
1305         srcdir=strchrW(targetdir,':');
1306         *srcdir=0;
1307         srcdir ++;
1308     }
1309     else
1310         srcdir=NULL;
1311
1312     /* for now only pick long filename versions */
1313     if (strchrW(targetdir,'|'))
1314     {
1315         shortname = targetdir;
1316         targetdir = strchrW(targetdir,'|'); 
1317         *targetdir = 0;
1318         targetdir ++;
1319     }
1320     /* for the sourcedir pick the short filename */
1321     if (srcdir && strchrW(srcdir,'|'))
1322     {
1323         LPWSTR p = strchrW(srcdir,'|'); 
1324         *p = 0;
1325     }
1326
1327     /* now check for root dirs */
1328     if (targetdir[0] == '.' && targetdir[1] == 0)
1329         targetdir = NULL;
1330         
1331     if (targetdir)
1332     {
1333         TRACE("   TargetDefault = %s\n",debugstr_w(targetdir));
1334         HeapFree(GetProcessHeap(),0, folder->TargetDefault);
1335         folder->TargetDefault = strdupW(targetdir);
1336     }
1337
1338     if (srcdir)
1339         folder->SourceDefault = strdupW(srcdir);
1340     else if (shortname)
1341         folder->SourceDefault = strdupW(shortname);
1342     else if (targetdir)
1343         folder->SourceDefault = strdupW(targetdir);
1344     HeapFree(GetProcessHeap(), 0, ptargetdir);
1345         TRACE("   SourceDefault = %s\n", debugstr_w( folder->SourceDefault ));
1346
1347     parent = MSI_RecordGetString(row,2);
1348     if (parent) 
1349     {
1350         folder->Parent = load_folder( package, parent );
1351         if ( folder->Parent )
1352             TRACE("loaded parent %p %s\n", folder->Parent,
1353                   debugstr_w(folder->Parent->Directory));
1354         else
1355             ERR("failed to load parent folder %s\n", debugstr_w(parent));
1356     }
1357
1358     folder->Property = msi_dup_property( package, dir );
1359
1360     msiobj_release(&row->hdr);
1361
1362     list_add_tail( &package->folders, &folder->entry );
1363
1364     TRACE("%s returning %p\n",debugstr_w(dir),folder);
1365
1366     return folder;
1367 }
1368
1369 /* scan for and update current install states */
1370 static void ACTION_UpdateInstallStates(MSIPACKAGE *package)
1371 {
1372     MSICOMPONENT *comp;
1373     MSIFEATURE *feature;
1374
1375     LIST_FOR_EACH_ENTRY( comp, &package->components, MSICOMPONENT, entry )
1376     {
1377         INSTALLSTATE res;
1378         res = MsiGetComponentPathW( package->ProductCode, 
1379                                     comp->ComponentId, NULL, NULL);
1380         if (res < 0)
1381             res = INSTALLSTATE_ABSENT;
1382         comp->Installed = res;
1383     }
1384
1385     LIST_FOR_EACH_ENTRY( feature, &package->features, MSIFEATURE, entry )
1386     {
1387         ComponentList *cl;
1388         INSTALLSTATE res = -10;
1389
1390         LIST_FOR_EACH_ENTRY( cl, &feature->Components, ComponentList, entry )
1391         {
1392             comp= cl->component;
1393
1394             if (res == -10)
1395                 res = comp->Installed;
1396             else
1397             {
1398                 if (res == comp->Installed)
1399                     continue;
1400
1401                 if (res != comp->Installed)
1402                         res = INSTALLSTATE_INCOMPLETE;
1403             }
1404         }
1405         feature->Installed = res;
1406     }
1407 }
1408
1409 static BOOL process_state_property (MSIPACKAGE* package, LPCWSTR property, 
1410                                     INSTALLSTATE state)
1411 {
1412     static const WCHAR all[]={'A','L','L',0};
1413     LPWSTR override;
1414     MSIFEATURE *feature;
1415
1416     override = msi_dup_property( package, property );
1417     if (!override)
1418         return FALSE;
1419  
1420     LIST_FOR_EACH_ENTRY( feature, &package->features, MSIFEATURE, entry )
1421     {
1422         if (strcmpiW(override,all)==0)
1423         {
1424             feature->ActionRequest= state;
1425             feature->Action = state;
1426         }
1427         else
1428         {
1429             LPWSTR ptr = override;
1430             LPWSTR ptr2 = strchrW(override,',');
1431
1432             while (ptr)
1433             {
1434                 if ((ptr2 && strncmpW(ptr,feature->Feature, ptr2-ptr)==0)
1435                     || (!ptr2 && strcmpW(ptr,feature->Feature)==0))
1436                 {
1437                     feature->ActionRequest= state;
1438                     feature->Action = state;
1439                     break;
1440                 }
1441                 if (ptr2)
1442                 {
1443                     ptr=ptr2+1;
1444                     ptr2 = strchrW(ptr,',');
1445                 }
1446                 else
1447                     break;
1448             }
1449         }
1450     } 
1451     HeapFree(GetProcessHeap(),0,override);
1452
1453     return TRUE;
1454 }
1455
1456 static UINT SetFeatureStates(MSIPACKAGE *package)
1457 {
1458     LPWSTR level;
1459     INT install_level;
1460     static const WCHAR szlevel[] =
1461         {'I','N','S','T','A','L','L','L','E','V','E','L',0};
1462     static const WCHAR szAddLocal[] =
1463         {'A','D','D','L','O','C','A','L',0};
1464     static const WCHAR szRemove[] =
1465         {'R','E','M','O','V','E',0};
1466     BOOL override = FALSE;
1467     MSICOMPONENT* component;
1468     MSIFEATURE *feature;
1469
1470
1471     /* I do not know if this is where it should happen.. but */
1472
1473     TRACE("Checking Install Level\n");
1474
1475     level = msi_dup_property( package, szlevel );
1476     if (level)
1477     {
1478         install_level = atoiW(level);
1479         HeapFree(GetProcessHeap(), 0, level);
1480     }
1481     else
1482         install_level = 1;
1483
1484     /* ok hereis the _real_ rub
1485      * all these activation/deactivation things happen in order and things
1486      * later on the list override things earlier on the list.
1487      * 1) INSTALLLEVEL processing
1488      * 2) ADDLOCAL
1489      * 3) REMOVE
1490      * 4) ADDSOURCE
1491      * 5) ADDDEFAULT
1492      * 6) REINSTALL
1493      * 7) COMPADDLOCAL
1494      * 8) COMPADDSOURCE
1495      * 9) FILEADDLOCAL
1496      * 10) FILEADDSOURCE
1497      * 11) FILEADDDEFAULT
1498      * I have confirmed that if ADDLOCAL is stated then the INSTALLLEVEL is
1499      * ignored for all the features. seems strange, especially since it is not
1500      * documented anywhere, but it is how it works. 
1501      *
1502      * I am still ignoring a lot of these. But that is ok for now, ADDLOCAL and
1503      * REMOVE are the big ones, since we don't handle administrative installs
1504      * yet anyway.
1505      */
1506     override |= process_state_property(package,szAddLocal,INSTALLSTATE_LOCAL);
1507     override |= process_state_property(package,szRemove,INSTALLSTATE_ABSENT);
1508
1509     if (!override)
1510     {
1511         LIST_FOR_EACH_ENTRY( feature, &package->features, MSIFEATURE, entry )
1512         {
1513             BOOL feature_state = ((feature->Level > 0) &&
1514                              (feature->Level <= install_level));
1515
1516             if ((feature_state) && (feature->Action == INSTALLSTATE_UNKNOWN))
1517             {
1518                 if (feature->Attributes & msidbFeatureAttributesFavorSource)
1519                 {
1520                     feature->ActionRequest = INSTALLSTATE_SOURCE;
1521                     feature->Action = INSTALLSTATE_SOURCE;
1522                 }
1523                 else if (feature->Attributes & msidbFeatureAttributesFavorAdvertise)
1524                 {
1525                     feature->ActionRequest = INSTALLSTATE_ADVERTISED;
1526                     feature->Action = INSTALLSTATE_ADVERTISED;
1527                 }
1528                 else
1529                 {
1530                     feature->ActionRequest = INSTALLSTATE_LOCAL;
1531                     feature->Action = INSTALLSTATE_LOCAL;
1532                 }
1533             }
1534         }
1535     }
1536     else
1537     {
1538         /* set the Preselected Property */
1539         static const WCHAR szPreselected[] = {'P','r','e','s','e','l','e','c','t','e','d',0};
1540         static const WCHAR szOne[] = { '1', 0 };
1541
1542         MSI_SetPropertyW(package,szPreselected,szOne);
1543     }
1544
1545     /*
1546      * now we want to enable or disable components base on feature 
1547     */
1548
1549     LIST_FOR_EACH_ENTRY( feature, &package->features, MSIFEATURE, entry )
1550     {
1551         ComponentList *cl;
1552
1553         TRACE("Examining Feature %s (Installed %i, Action %i, Request %i)\n",
1554             debugstr_w(feature->Feature), feature->Installed, feature->Action,
1555             feature->ActionRequest);
1556
1557         LIST_FOR_EACH_ENTRY( cl, &feature->Components, ComponentList, entry )
1558         {
1559             component = cl->component;
1560
1561             if (!component->Enabled)
1562             {
1563                 component->Action = INSTALLSTATE_UNKNOWN;
1564                 component->ActionRequest = INSTALLSTATE_UNKNOWN;
1565             }
1566             else
1567             {
1568                 if (feature->Action == INSTALLSTATE_LOCAL)
1569                 {
1570                     component->Action = INSTALLSTATE_LOCAL;
1571                     component->ActionRequest = INSTALLSTATE_LOCAL;
1572                 }
1573                 else if (feature->ActionRequest == INSTALLSTATE_SOURCE)
1574                 {
1575                     if ((component->Action == INSTALLSTATE_UNKNOWN) ||
1576                         (component->Action == INSTALLSTATE_ABSENT) ||
1577                         (component->Action == INSTALLSTATE_ADVERTISED))
1578                            
1579                     {
1580                         component->Action = INSTALLSTATE_SOURCE;
1581                         component->ActionRequest = INSTALLSTATE_SOURCE;
1582                     }
1583                 }
1584                 else if (feature->ActionRequest == INSTALLSTATE_ADVERTISED)
1585                 {
1586                     if ((component->Action == INSTALLSTATE_UNKNOWN) ||
1587                         (component->Action == INSTALLSTATE_ABSENT))
1588                            
1589                     {
1590                         component->Action = INSTALLSTATE_ADVERTISED;
1591                         component->ActionRequest = INSTALLSTATE_ADVERTISED;
1592                     }
1593                 }
1594                 else if (feature->ActionRequest == INSTALLSTATE_ABSENT)
1595                 {
1596                     if (component->Action == INSTALLSTATE_UNKNOWN)
1597                     {
1598                         component->Action = INSTALLSTATE_ABSENT;
1599                         component->ActionRequest = INSTALLSTATE_ABSENT;
1600                     }
1601                 }
1602             }
1603         }
1604     } 
1605
1606     LIST_FOR_EACH_ENTRY( component, &package->components, MSICOMPONENT, entry )
1607     {
1608         TRACE("Result: Component %s (Installed %i, Action %i, Request %i)\n",
1609             debugstr_w(component->Component), component->Installed, 
1610             component->Action, component->ActionRequest);
1611     }
1612
1613
1614     return ERROR_SUCCESS;
1615 }
1616
1617 static UINT ITERATE_CostFinalizeDirectories(MSIRECORD *row, LPVOID param)
1618 {
1619     MSIPACKAGE *package = (MSIPACKAGE*)param;
1620     LPCWSTR name;
1621     LPWSTR path;
1622
1623     name = MSI_RecordGetString(row,1);
1624
1625     /* This helper function now does ALL the work */
1626     TRACE("Dir %s ...\n",debugstr_w(name));
1627     load_folder(package,name);
1628     path = resolve_folder(package,name,FALSE,TRUE,NULL);
1629     TRACE("resolves to %s\n",debugstr_w(path));
1630     HeapFree( GetProcessHeap(), 0, path);
1631
1632     return ERROR_SUCCESS;
1633 }
1634
1635 static UINT ITERATE_CostFinalizeConditions(MSIRECORD *row, LPVOID param)
1636 {
1637     MSIPACKAGE *package = (MSIPACKAGE*)param;
1638     LPCWSTR name;
1639     MSIFEATURE *feature;
1640
1641     name = MSI_RecordGetString( row, 1 );
1642
1643     feature = get_loaded_feature( package, name );
1644     if (!feature)
1645         ERR("FAILED to find loaded feature %s\n",debugstr_w(name));
1646     else
1647     {
1648         LPCWSTR Condition;
1649         Condition = MSI_RecordGetString(row,3);
1650
1651         if (MSI_EvaluateConditionW(package,Condition) == MSICONDITION_TRUE)
1652         {
1653             int level = MSI_RecordGetInteger(row,2);
1654             TRACE("Reseting feature %s to level %i\n", debugstr_w(name), level);
1655             feature->Level = level;
1656         }
1657     }
1658     return ERROR_SUCCESS;
1659 }
1660
1661
1662 /* 
1663  * A lot is done in this function aside from just the costing.
1664  * The costing needs to be implemented at some point but for now I am going
1665  * to focus on the directory building
1666  *
1667  */
1668 static UINT ACTION_CostFinalize(MSIPACKAGE *package)
1669 {
1670     static const WCHAR ExecSeqQuery[] =
1671         {'S','E','L','E','C','T',' ','*',' ','F','R','O','M',' ',
1672          '`','D','i','r','e','c','t','o','r','y','`',0};
1673     static const WCHAR ConditionQuery[] =
1674         {'S','E','L','E','C','T',' ','*',' ','F','R','O','M',' ',
1675          '`','C','o','n','d','i','t','i','o','n','`',0};
1676     static const WCHAR szCosting[] =
1677         {'C','o','s','t','i','n','g','C','o','m','p','l','e','t','e',0 };
1678     static const WCHAR szlevel[] =
1679         {'I','N','S','T','A','L','L','L','E','V','E','L',0};
1680     static const WCHAR szOne[] = { '1', 0 };
1681     MSICOMPONENT *comp;
1682     MSIFILE *file;
1683     UINT rc;
1684     MSIQUERY * view;
1685     LPWSTR level;
1686     DWORD sz = 3;
1687     WCHAR buffer[3];
1688
1689     MSI_GetPropertyW(package, szCosting, buffer, &sz);
1690     if (buffer[0]=='1')
1691         return ERROR_SUCCESS;
1692
1693     TRACE("Building Directory properties\n");
1694
1695     rc = MSI_DatabaseOpenViewW(package->db, ExecSeqQuery, &view);
1696     if (rc == ERROR_SUCCESS)
1697     {
1698         rc = MSI_IterateRecords(view, NULL, ITERATE_CostFinalizeDirectories,
1699                         package);
1700         msiobj_release(&view->hdr);
1701     }
1702
1703     TRACE("File calculations\n");
1704
1705     LIST_FOR_EACH_ENTRY( file, &package->files, MSIFILE, entry )
1706     {
1707         MSICOMPONENT* comp = file->Component;
1708         LPWSTR p;
1709
1710         if (!comp)
1711             continue;
1712
1713         /* calculate target */
1714         p = resolve_folder(package, comp->Directory, FALSE, FALSE, NULL);
1715
1716         HeapFree(GetProcessHeap(),0,file->TargetPath);
1717
1718         TRACE("file %s is named %s\n",
1719                debugstr_w(file->File),debugstr_w(file->FileName));       
1720
1721         file->TargetPath = build_directory_name(2, p, file->FileName);
1722
1723         HeapFree(GetProcessHeap(),0,p);
1724
1725         TRACE("file %s resolves to %s\n",
1726                debugstr_w(file->File),debugstr_w(file->TargetPath));       
1727
1728         if (GetFileAttributesW(file->TargetPath) == INVALID_FILE_ATTRIBUTES)
1729         {
1730             file->State = 1;
1731             comp->Cost += file->FileSize;
1732             continue;
1733         }
1734
1735         if (file->Version)
1736         {
1737             DWORD handle;
1738             DWORD versize;
1739             UINT sz;
1740             LPVOID version;
1741             static const WCHAR name[] = 
1742                 {'\\',0};
1743             static const WCHAR name_fmt[] = 
1744                 {'%','u','.','%','u','.','%','u','.','%','u',0};
1745             WCHAR filever[0x100];
1746             VS_FIXEDFILEINFO *lpVer;
1747
1748             TRACE("Version comparison.. \n");
1749             versize = GetFileVersionInfoSizeW(file->TargetPath,&handle);
1750             version = HeapAlloc(GetProcessHeap(),0,versize);
1751             GetFileVersionInfoW(file->TargetPath, 0, versize, version);
1752
1753             VerQueryValueW(version, name, (LPVOID*)&lpVer, &sz);
1754
1755             sprintfW(filever,name_fmt,
1756                 HIWORD(lpVer->dwFileVersionMS),
1757                 LOWORD(lpVer->dwFileVersionMS),
1758                 HIWORD(lpVer->dwFileVersionLS),
1759                 LOWORD(lpVer->dwFileVersionLS));
1760
1761             TRACE("new %s old %s\n", debugstr_w(file->Version),
1762                   debugstr_w(filever));
1763             if (strcmpiW(filever,file->Version)<0)
1764             {
1765                 file->State = 2;
1766                 FIXME("cost should be diff in size\n");
1767                 comp->Cost += file->FileSize;
1768             }
1769             else
1770                 file->State = 3;
1771             HeapFree(GetProcessHeap(),0,version);
1772         }
1773         else
1774             file->State = 3;
1775     }
1776
1777     TRACE("Evaluating Condition Table\n");
1778
1779     rc = MSI_DatabaseOpenViewW(package->db, ConditionQuery, &view);
1780     if (rc == ERROR_SUCCESS)
1781     {
1782         rc = MSI_IterateRecords(view, NULL, ITERATE_CostFinalizeConditions,
1783                     package);
1784         msiobj_release(&view->hdr);
1785     }
1786
1787     TRACE("Enabling or Disabling Components\n");
1788     LIST_FOR_EACH_ENTRY( comp, &package->components, MSICOMPONENT, entry )
1789     {
1790         if (comp->Condition)
1791         {
1792             if (MSI_EvaluateConditionW(package,
1793                 comp->Condition) == MSICONDITION_FALSE)
1794             {
1795                 TRACE("Disabling component %s\n", debugstr_w(comp->Component));
1796                 comp->Enabled = FALSE;
1797             }
1798         }
1799     }
1800
1801     MSI_SetPropertyW(package,szCosting,szOne);
1802     /* set default run level if not set */
1803     level = msi_dup_property( package, szlevel );
1804     if (!level)
1805         MSI_SetPropertyW(package,szlevel, szOne);
1806     HeapFree(GetProcessHeap(),0,level);
1807
1808     ACTION_UpdateInstallStates(package);
1809
1810     return SetFeatureStates(package);
1811 }
1812
1813 /* OK this value is "interpreted" and then formatted based on the 
1814    first few characters */
1815 static LPSTR parse_value(MSIPACKAGE *package, LPCWSTR value, DWORD *type, 
1816                          DWORD *size)
1817 {
1818     LPSTR data = NULL;
1819     if (value[0]=='#' && value[1]!='#' && value[1]!='%')
1820     {
1821         if (value[1]=='x')
1822         {
1823             LPWSTR ptr;
1824             CHAR byte[5];
1825             LPWSTR deformated = NULL;
1826             int count;
1827
1828             deformat_string(package, &value[2], &deformated);
1829
1830             /* binary value type */
1831             ptr = deformated;
1832             *type = REG_BINARY;
1833             if (strlenW(ptr)%2)
1834                 *size = (strlenW(ptr)/2)+1;
1835             else
1836                 *size = strlenW(ptr)/2;
1837
1838             data = HeapAlloc(GetProcessHeap(),0,*size);
1839
1840             byte[0] = '0'; 
1841             byte[1] = 'x'; 
1842             byte[4] = 0; 
1843             count = 0;
1844             /* if uneven pad with a zero in front */
1845             if (strlenW(ptr)%2)
1846             {
1847                 byte[2]= '0';
1848                 byte[3]= *ptr;
1849                 ptr++;
1850                 data[count] = (BYTE)strtol(byte,NULL,0);
1851                 count ++;
1852                 TRACE("Uneven byte count\n");
1853             }
1854             while (*ptr)
1855             {
1856                 byte[2]= *ptr;
1857                 ptr++;
1858                 byte[3]= *ptr;
1859                 ptr++;
1860                 data[count] = (BYTE)strtol(byte,NULL,0);
1861                 count ++;
1862             }
1863             HeapFree(GetProcessHeap(),0,deformated);
1864
1865             TRACE("Data %li bytes(%i)\n",*size,count);
1866         }
1867         else
1868         {
1869             LPWSTR deformated;
1870             LPWSTR p;
1871             DWORD d = 0;
1872             deformat_string(package, &value[1], &deformated);
1873
1874             *type=REG_DWORD; 
1875             *size = sizeof(DWORD);
1876             data = HeapAlloc(GetProcessHeap(),0,*size);
1877             p = deformated;
1878             if (*p == '-')
1879                 p++;
1880             while (*p)
1881             {
1882                 if ( (*p < '0') || (*p > '9') )
1883                     break;
1884                 d *= 10;
1885                 d += (*p - '0');
1886                 p++;
1887             }
1888             if (deformated[0] == '-')
1889                 d = -d;
1890             *(LPDWORD)data = d;
1891             TRACE("DWORD %li\n",*(LPDWORD)data);
1892
1893             HeapFree(GetProcessHeap(),0,deformated);
1894         }
1895     }
1896     else
1897     {
1898         static const WCHAR szMulti[] = {'[','~',']',0};
1899         LPCWSTR ptr;
1900         *type=REG_SZ;
1901
1902         if (value[0]=='#')
1903         {
1904             if (value[1]=='%')
1905             {
1906                 ptr = &value[2];
1907                 *type=REG_EXPAND_SZ;
1908             }
1909             else
1910                 ptr = &value[1];
1911          }
1912          else
1913             ptr=value;
1914
1915         if (strstrW(value,szMulti))
1916             *type = REG_MULTI_SZ;
1917
1918         *size = deformat_string(package, ptr,(LPWSTR*)&data);
1919     }
1920     return data;
1921 }
1922
1923 static UINT ITERATE_WriteRegistryValues(MSIRECORD *row, LPVOID param)
1924 {
1925     MSIPACKAGE *package = (MSIPACKAGE*)param;
1926     static const WCHAR szHCR[] = 
1927         {'H','K','E','Y','_','C','L','A','S','S','E','S','_',
1928          'R','O','O','T','\\',0};
1929     static const WCHAR szHCU[] =
1930         {'H','K','E','Y','_','C','U','R','R','E','N','T','_',
1931          'U','S','E','R','\\',0};
1932     static const WCHAR szHLM[] =
1933         {'H','K','E','Y','_','L','O','C','A','L','_',
1934          'M','A','C','H','I','N','E','\\',0};
1935     static const WCHAR szHU[] =
1936         {'H','K','E','Y','_','U','S','E','R','S','\\',0};
1937
1938     LPSTR value_data = NULL;
1939     HKEY  root_key, hkey;
1940     DWORD type,size;
1941     LPWSTR  deformated;
1942     LPCWSTR szRoot, component, name, key, value;
1943     MSICOMPONENT *comp;
1944     MSIRECORD * uirow;
1945     LPWSTR uikey;
1946     INT   root;
1947     BOOL check_first = FALSE;
1948     UINT rc;
1949
1950     ui_progress(package,2,0,0,0);
1951
1952     value = NULL;
1953     key = NULL;
1954     uikey = NULL;
1955     name = NULL;
1956
1957     component = MSI_RecordGetString(row, 6);
1958     comp = get_loaded_component(package,component);
1959     if (!comp)
1960         return ERROR_SUCCESS;
1961
1962     if (!ACTION_VerifyComponentForAction(package, comp, INSTALLSTATE_LOCAL))
1963     {
1964         TRACE("Skipping write due to disabled component %s\n",
1965                         debugstr_w(component));
1966
1967         comp->Action = comp->Installed;
1968
1969         return ERROR_SUCCESS;
1970     }
1971
1972     comp->Action = INSTALLSTATE_LOCAL;
1973
1974     name = MSI_RecordGetString(row, 4);
1975     if( MSI_RecordIsNull(row,5) && name )
1976     {
1977         /* null values can have special meanings */
1978         if (name[0]=='-' && name[1] == 0)
1979                 return ERROR_SUCCESS;
1980         else if ((name[0]=='+' && name[1] == 0) || 
1981                  (name[0] == '*' && name[1] == 0))
1982                 name = NULL;
1983         check_first = TRUE;
1984     }
1985
1986     root = MSI_RecordGetInteger(row,2);
1987     key = MSI_RecordGetString(row, 3);
1988
1989     /* get the root key */
1990     switch (root)
1991     {
1992         case -1: 
1993             {
1994                 static const WCHAR szALLUSER[] = {'A','L','L','U','S','E','R','S',0};
1995                 LPWSTR all_users = msi_dup_property( package, szALLUSER );
1996                 if (all_users && all_users[0] == '1')
1997                 {
1998                     root_key = HKEY_LOCAL_MACHINE;
1999                     szRoot = szHLM;
2000                 }
2001                 else
2002                 {
2003                     root_key = HKEY_CURRENT_USER;
2004                     szRoot = szHCU;
2005                 }
2006                 HeapFree(GetProcessHeap(),0,all_users);
2007             }
2008                  break;
2009         case 0:  root_key = HKEY_CLASSES_ROOT; 
2010                  szRoot = szHCR;
2011                  break;
2012         case 1:  root_key = HKEY_CURRENT_USER;
2013                  szRoot = szHCU;
2014                  break;
2015         case 2:  root_key = HKEY_LOCAL_MACHINE;
2016                  szRoot = szHLM;
2017                  break;
2018         case 3:  root_key = HKEY_USERS; 
2019                  szRoot = szHU;
2020                  break;
2021         default:
2022                  ERR("Unknown root %i\n",root);
2023                  root_key=NULL;
2024                  szRoot = NULL;
2025                  break;
2026     }
2027     if (!root_key)
2028         return ERROR_SUCCESS;
2029
2030     deformat_string(package, key , &deformated);
2031     size = strlenW(deformated) + strlenW(szRoot) + 1;
2032     uikey = HeapAlloc(GetProcessHeap(), 0, size*sizeof(WCHAR));
2033     strcpyW(uikey,szRoot);
2034     strcatW(uikey,deformated);
2035
2036     if (RegCreateKeyW( root_key, deformated, &hkey))
2037     {
2038         ERR("Could not create key %s\n",debugstr_w(deformated));
2039         HeapFree(GetProcessHeap(),0,deformated);
2040         HeapFree(GetProcessHeap(),0,uikey);
2041         return ERROR_SUCCESS;
2042     }
2043     HeapFree(GetProcessHeap(),0,deformated);
2044
2045     value = MSI_RecordGetString(row,5);
2046     if (value)
2047         value_data = parse_value(package, value, &type, &size); 
2048     else
2049     {
2050         static const WCHAR szEmpty[] = {0};
2051         value_data = (LPSTR)strdupW(szEmpty);
2052         size = 0;
2053         type = REG_SZ;
2054     }
2055
2056     deformat_string(package, name, &deformated);
2057
2058     /* get the double nulls to terminate SZ_MULTI */
2059     if (type == REG_MULTI_SZ)
2060         size +=sizeof(WCHAR);
2061
2062     if (!check_first)
2063     {
2064         TRACE("Setting value %s of %s\n",debugstr_w(deformated),
2065                         debugstr_w(uikey));
2066         RegSetValueExW(hkey, deformated, 0, type, (LPBYTE)value_data, size);
2067     }
2068     else
2069     {
2070         DWORD sz = 0;
2071         rc = RegQueryValueExW(hkey, deformated, NULL, NULL, NULL, &sz);
2072         if (rc == ERROR_SUCCESS || rc == ERROR_MORE_DATA)
2073         {
2074             TRACE("value %s of %s checked already exists\n",
2075                             debugstr_w(deformated), debugstr_w(uikey));
2076         }
2077         else
2078         {
2079             TRACE("Checked and setting value %s of %s\n",
2080                             debugstr_w(deformated), debugstr_w(uikey));
2081             if (deformated || size)
2082                 RegSetValueExW(hkey, deformated, 0, type, (LPBYTE) value_data, size);
2083         }
2084     }
2085     RegCloseKey(hkey);
2086
2087     uirow = MSI_CreateRecord(3);
2088     MSI_RecordSetStringW(uirow,2,deformated);
2089     MSI_RecordSetStringW(uirow,1,uikey);
2090
2091     if (type == REG_SZ)
2092         MSI_RecordSetStringW(uirow,3,(LPWSTR)value_data);
2093     else
2094         MSI_RecordSetStringW(uirow,3,value);
2095
2096     ui_actiondata(package,szWriteRegistryValues,uirow);
2097     msiobj_release( &uirow->hdr );
2098
2099     HeapFree(GetProcessHeap(),0,value_data);
2100     HeapFree(GetProcessHeap(),0,deformated);
2101     HeapFree(GetProcessHeap(),0,uikey);
2102
2103     return ERROR_SUCCESS;
2104 }
2105
2106 static UINT ACTION_WriteRegistryValues(MSIPACKAGE *package)
2107 {
2108     UINT rc;
2109     MSIQUERY * view;
2110     static const WCHAR ExecSeqQuery[] =
2111         {'S','E','L','E','C','T',' ','*',' ','F','R','O','M',' ',
2112          '`','R','e','g','i','s','t','r','y','`',0 };
2113
2114     if (!package)
2115         return ERROR_INVALID_HANDLE;
2116
2117     rc = MSI_DatabaseOpenViewW(package->db, ExecSeqQuery, &view);
2118     if (rc != ERROR_SUCCESS)
2119         return ERROR_SUCCESS;
2120
2121     /* increment progress bar each time action data is sent */
2122     ui_progress(package,1,REG_PROGRESS_VALUE,1,0);
2123
2124     rc = MSI_IterateRecords(view, NULL, ITERATE_WriteRegistryValues, package);
2125
2126     msiobj_release(&view->hdr);
2127     return rc;
2128 }
2129
2130 static UINT ACTION_InstallInitialize(MSIPACKAGE *package)
2131 {
2132     package->script->CurrentlyScripting = TRUE;
2133
2134     return ERROR_SUCCESS;
2135 }
2136
2137
2138 static UINT ACTION_InstallValidate(MSIPACKAGE *package)
2139 {
2140     MSICOMPONENT *comp;
2141     DWORD progress = 0;
2142     DWORD total = 0;
2143     static const WCHAR q1[]=
2144         {'S','E','L','E','C','T',' ','*',' ', 'F','R','O','M',' ',
2145          '`','R','e','g','i','s','t','r','y','`',0};
2146     UINT rc;
2147     MSIQUERY * view;
2148     MSIRECORD * row = 0;
2149     MSIFEATURE *feature;
2150     MSIFILE *file;
2151
2152     TRACE(" InstallValidate \n");
2153
2154     rc = MSI_DatabaseOpenViewW(package->db, q1, &view);
2155     if (rc != ERROR_SUCCESS)
2156         return ERROR_SUCCESS;
2157
2158     rc = MSI_ViewExecute(view, 0);
2159     if (rc != ERROR_SUCCESS)
2160     {
2161         MSI_ViewClose(view);
2162         msiobj_release(&view->hdr);
2163         return rc;
2164     }
2165     while (1)
2166     {
2167         rc = MSI_ViewFetch(view,&row);
2168         if (rc != ERROR_SUCCESS)
2169         {
2170             rc = ERROR_SUCCESS;
2171             break;
2172         }
2173         progress +=1;
2174
2175         msiobj_release(&row->hdr);
2176     }
2177     MSI_ViewClose(view);
2178     msiobj_release(&view->hdr);
2179
2180     total = total + progress * REG_PROGRESS_VALUE;
2181     LIST_FOR_EACH_ENTRY( comp, &package->components, MSICOMPONENT, entry )
2182     {
2183         total += COMPONENT_PROGRESS_VALUE;
2184     }
2185     LIST_FOR_EACH_ENTRY( file, &package->files, MSIFILE, entry )
2186     {
2187         total += file->FileSize;
2188     }
2189     ui_progress(package,0,total,0,0);
2190
2191     LIST_FOR_EACH_ENTRY( feature, &package->features, MSIFEATURE, entry )
2192     {
2193         TRACE("Feature: %s; Installed: %i; Action %i; Request %i\n",
2194             debugstr_w(feature->Feature), feature->Installed, feature->Action,
2195             feature->ActionRequest);
2196     }
2197     
2198     return ERROR_SUCCESS;
2199 }
2200
2201 static UINT ITERATE_LaunchConditions(MSIRECORD *row, LPVOID param)
2202 {
2203     MSIPACKAGE* package = (MSIPACKAGE*)param;
2204     LPCWSTR cond = NULL; 
2205     LPCWSTR message = NULL;
2206     static const WCHAR title[]=
2207         {'I','n','s','t','a','l','l',' ','F','a', 'i','l','e','d',0};
2208
2209     cond = MSI_RecordGetString(row,1);
2210
2211     if (MSI_EvaluateConditionW(package,cond) != MSICONDITION_TRUE)
2212     {
2213         LPWSTR deformated;
2214         message = MSI_RecordGetString(row,2);
2215         deformat_string(package,message,&deformated); 
2216         MessageBoxW(NULL,deformated,title,MB_OK);
2217         HeapFree(GetProcessHeap(),0,deformated);
2218         return ERROR_FUNCTION_FAILED;
2219     }
2220
2221     return ERROR_SUCCESS;
2222 }
2223
2224 static UINT ACTION_LaunchConditions(MSIPACKAGE *package)
2225 {
2226     UINT rc;
2227     MSIQUERY * view = NULL;
2228     static const WCHAR ExecSeqQuery[] =
2229         {'S','E','L','E','C','T',' ','*',' ','F','R','O','M',' ',
2230          '`','L','a','u','n','c','h','C','o','n','d','i','t','i','o','n','`',0};
2231
2232     TRACE("Checking launch conditions\n");
2233
2234     rc = MSI_DatabaseOpenViewW(package->db, ExecSeqQuery, &view);
2235     if (rc != ERROR_SUCCESS)
2236         return ERROR_SUCCESS;
2237
2238     rc = MSI_IterateRecords(view, NULL, ITERATE_LaunchConditions, package);
2239     msiobj_release(&view->hdr);
2240
2241     return rc;
2242 }
2243
2244 static LPWSTR resolve_keypath( MSIPACKAGE* package, MSICOMPONENT *cmp )
2245 {
2246
2247     if (!cmp->KeyPath)
2248         return resolve_folder(package,cmp->Directory,FALSE,FALSE,NULL);
2249
2250     if (cmp->Attributes & msidbComponentAttributesRegistryKeyPath)
2251     {
2252         MSIRECORD * row = 0;
2253         UINT root,len;
2254         LPWSTR deformated,buffer,deformated_name;
2255         LPCWSTR key,name;
2256         static const WCHAR ExecSeqQuery[] =
2257             {'S','E','L','E','C','T',' ','*',' ','F','R','O','M',' ',
2258              '`','R','e','g','i','s','t','r','y','`',' ',
2259              'W','H','E','R','E',' ', '`','R','e','g','i','s','t','r','y','`',
2260              ' ','=',' ' ,'\'','%','s','\'',0 };
2261         static const WCHAR fmt[]={'%','0','2','i',':','\\','%','s','\\',0};
2262         static const WCHAR fmt2[]=
2263             {'%','0','2','i',':','\\','%','s','\\','%','s',0};
2264
2265         row = MSI_QueryGetRecord(package->db, ExecSeqQuery,cmp->KeyPath);
2266         if (!row)
2267             return NULL;
2268
2269         root = MSI_RecordGetInteger(row,2);
2270         key = MSI_RecordGetString(row, 3);
2271         name = MSI_RecordGetString(row, 4);
2272         deformat_string(package, key , &deformated);
2273         deformat_string(package, name, &deformated_name);
2274
2275         len = strlenW(deformated) + 6;
2276         if (deformated_name)
2277             len+=strlenW(deformated_name);
2278
2279         buffer = HeapAlloc(GetProcessHeap(),0, len *sizeof(WCHAR));
2280
2281         if (deformated_name)
2282             sprintfW(buffer,fmt2,root,deformated,deformated_name);
2283         else
2284             sprintfW(buffer,fmt,root,deformated);
2285
2286         HeapFree(GetProcessHeap(),0,deformated);
2287         HeapFree(GetProcessHeap(),0,deformated_name);
2288         msiobj_release(&row->hdr);
2289
2290         return buffer;
2291     }
2292     else if (cmp->Attributes & msidbComponentAttributesODBCDataSource)
2293     {
2294         FIXME("UNIMPLEMENTED keypath as ODBC Source\n");
2295         return NULL;
2296     }
2297     else
2298     {
2299         MSIFILE *file = get_loaded_file( package, cmp->KeyPath );
2300
2301         if (file)
2302             return strdupW( file->TargetPath );
2303     }
2304     return NULL;
2305 }
2306
2307 static HKEY openSharedDLLsKey(void)
2308 {
2309     HKEY hkey=0;
2310     static const WCHAR path[] =
2311         {'S','o','f','t','w','a','r','e','\\',
2312          'M','i','c','r','o','s','o','f','t','\\',
2313          'W','i','n','d','o','w','s','\\',
2314          'C','u','r','r','e','n','t','V','e','r','s','i','o','n','\\',
2315          'S','h','a','r','e','d','D','L','L','s',0};
2316
2317     RegCreateKeyW(HKEY_LOCAL_MACHINE,path,&hkey);
2318     return hkey;
2319 }
2320
2321 static UINT ACTION_GetSharedDLLsCount(LPCWSTR dll)
2322 {
2323     HKEY hkey;
2324     DWORD count=0;
2325     DWORD type;
2326     DWORD sz = sizeof(count);
2327     DWORD rc;
2328     
2329     hkey = openSharedDLLsKey();
2330     rc = RegQueryValueExW(hkey, dll, NULL, &type, (LPBYTE)&count, &sz);
2331     if (rc != ERROR_SUCCESS)
2332         count = 0;
2333     RegCloseKey(hkey);
2334     return count;
2335 }
2336
2337 static UINT ACTION_WriteSharedDLLsCount(LPCWSTR path, UINT count)
2338 {
2339     HKEY hkey;
2340
2341     hkey = openSharedDLLsKey();
2342     if (count > 0)
2343         msi_reg_set_val_dword( hkey, path, count );
2344     else
2345         RegDeleteValueW(hkey,path);
2346     RegCloseKey(hkey);
2347     return count;
2348 }
2349
2350 /*
2351  * Return TRUE if the count should be written out and FALSE if not
2352  */
2353 static void ACTION_RefCountComponent( MSIPACKAGE* package, MSICOMPONENT *comp )
2354 {
2355     MSIFEATURE *feature;
2356     INT count = 0;
2357     BOOL write = FALSE;
2358
2359     /* only refcount DLLs */
2360     if (comp->KeyPath == NULL || 
2361         comp->Attributes & msidbComponentAttributesRegistryKeyPath || 
2362         comp->Attributes & msidbComponentAttributesODBCDataSource)
2363         write = FALSE;
2364     else
2365     {
2366         count = ACTION_GetSharedDLLsCount( comp->FullKeypath);
2367         write = (count > 0);
2368
2369         if (comp->Attributes & msidbComponentAttributesSharedDllRefCount)
2370             write = TRUE;
2371     }
2372
2373     /* increment counts */
2374     LIST_FOR_EACH_ENTRY( feature, &package->features, MSIFEATURE, entry )
2375     {
2376         ComponentList *cl;
2377
2378         if (!ACTION_VerifyFeatureForAction( feature, INSTALLSTATE_LOCAL ))
2379             continue;
2380
2381         LIST_FOR_EACH_ENTRY( cl, &feature->Components, ComponentList, entry )
2382         {
2383             if ( cl->component == comp )
2384                 count++;
2385         }
2386     }
2387
2388     /* decrement counts */
2389     LIST_FOR_EACH_ENTRY( feature, &package->features, MSIFEATURE, entry )
2390     {
2391         ComponentList *cl;
2392
2393         if (!ACTION_VerifyFeatureForAction( feature, INSTALLSTATE_ABSENT ))
2394             continue;
2395
2396         LIST_FOR_EACH_ENTRY( cl, &feature->Components, ComponentList, entry )
2397         {
2398             if ( cl->component == comp )
2399                 count--;
2400         }
2401     }
2402
2403     /* ref count all the files in the component */
2404     if (write)
2405     {
2406         MSIFILE *file;
2407
2408         LIST_FOR_EACH_ENTRY( file, &package->files, MSIFILE, entry )
2409         {
2410             if (file->Component == comp)
2411                 ACTION_WriteSharedDLLsCount( file->TargetPath, count );
2412         }
2413     }
2414     
2415     /* add a count for permenent */
2416     if (comp->Attributes & msidbComponentAttributesPermanent)
2417         count ++;
2418     
2419     comp->RefCount = count;
2420
2421     if (write)
2422         ACTION_WriteSharedDLLsCount( comp->FullKeypath, comp->RefCount );
2423 }
2424
2425 /*
2426  * Ok further analysis makes me think that this work is
2427  * actually done in the PublishComponents and PublishFeatures
2428  * step, and not here.  It appears like the keypath and all that is
2429  * resolved in this step, however actually written in the Publish steps.
2430  * But we will leave it here for now because it is unclear
2431  */
2432 static UINT ACTION_ProcessComponents(MSIPACKAGE *package)
2433 {
2434     WCHAR squished_pc[GUID_SIZE];
2435     WCHAR squished_cc[GUID_SIZE];
2436     UINT rc;
2437     MSICOMPONENT *comp;
2438     HKEY hkey=0,hkey2=0;
2439
2440     if (!package)
2441         return ERROR_INVALID_HANDLE;
2442
2443     /* writes the Component and Features values to the registry */
2444
2445     rc = MSIREG_OpenComponents(&hkey);
2446     if (rc != ERROR_SUCCESS)
2447         goto end;
2448       
2449     squash_guid(package->ProductCode,squished_pc);
2450     ui_progress(package,1,COMPONENT_PROGRESS_VALUE,1,0);
2451
2452     LIST_FOR_EACH_ENTRY( comp, &package->components, MSICOMPONENT, entry )
2453     {
2454         ui_progress(package,2,0,0,0);
2455         if (comp->ComponentId)
2456         {
2457             WCHAR *keypath = NULL;
2458             MSIRECORD * uirow;
2459
2460             squash_guid(comp->ComponentId,squished_cc);
2461            
2462             keypath = resolve_keypath( package, comp );
2463             comp->FullKeypath = keypath;
2464
2465             /* do the refcounting */
2466             ACTION_RefCountComponent( package, comp );
2467
2468             TRACE("Component %s (%s), Keypath=%s, RefCount=%i\n", 
2469                             debugstr_w(comp->Component),
2470                             debugstr_w(squished_cc),
2471                             debugstr_w(comp->FullKeypath), 
2472                             comp->RefCount);
2473             /*
2474             * Write the keypath out if the component is to be registered
2475             * and delete the key if the component is to be deregistered
2476             */
2477             if (ACTION_VerifyComponentForAction(package, comp,
2478                                     INSTALLSTATE_LOCAL))
2479             {
2480                 rc = RegCreateKeyW(hkey,squished_cc,&hkey2);
2481                 if (rc != ERROR_SUCCESS)
2482                     continue;
2483
2484                 if (keypath)
2485                 {
2486                     msi_reg_set_val_str( hkey2, squished_pc, keypath );
2487
2488                     if (comp->Attributes & msidbComponentAttributesPermanent)
2489                     {
2490                         static const WCHAR szPermKey[] =
2491                             { '0','0','0','0','0','0','0','0','0','0','0','0',
2492                               '0','0','0','0','0','0','0','0','0','0','0','0',
2493                               '0','0','0','0','0','0','0','0',0};
2494
2495                         msi_reg_set_val_str( hkey2, szPermKey, keypath );
2496                     }
2497                     
2498                     RegCloseKey(hkey2);
2499         
2500                     /* UI stuff */
2501                     uirow = MSI_CreateRecord(3);
2502                     MSI_RecordSetStringW(uirow,1,package->ProductCode);
2503                     MSI_RecordSetStringW(uirow,2,comp->ComponentId);
2504                     MSI_RecordSetStringW(uirow,3,keypath);
2505                     ui_actiondata(package,szProcessComponents,uirow);
2506                     msiobj_release( &uirow->hdr );
2507                }
2508             }
2509             else if (ACTION_VerifyComponentForAction(package, comp,
2510                                     INSTALLSTATE_ABSENT))
2511             {
2512                 DWORD res;
2513
2514                 rc = RegOpenKeyW(hkey,squished_cc,&hkey2);
2515                 if (rc != ERROR_SUCCESS)
2516                     continue;
2517
2518                 RegDeleteValueW(hkey2,squished_pc);
2519
2520                 /* if the key is empty delete it */
2521                 res = RegEnumKeyExW(hkey2,0,NULL,0,0,NULL,0,NULL);
2522                 RegCloseKey(hkey2);
2523                 if (res == ERROR_NO_MORE_ITEMS)
2524                     RegDeleteKeyW(hkey,squished_cc);
2525         
2526                 /* UI stuff */
2527                 uirow = MSI_CreateRecord(2);
2528                 MSI_RecordSetStringW(uirow,1,package->ProductCode);
2529                 MSI_RecordSetStringW(uirow,2,comp->ComponentId);
2530                 ui_actiondata(package,szProcessComponents,uirow);
2531                 msiobj_release( &uirow->hdr );
2532             }
2533         }
2534     } 
2535 end:
2536     RegCloseKey(hkey);
2537     return rc;
2538 }
2539
2540 typedef struct {
2541     CLSID       clsid;
2542     LPWSTR      source;
2543
2544     LPWSTR      path;
2545     ITypeLib    *ptLib;
2546 } typelib_struct;
2547
2548 static BOOL CALLBACK Typelib_EnumResNameProc( HMODULE hModule, LPCWSTR lpszType, 
2549                                        LPWSTR lpszName, LONG_PTR lParam)
2550 {
2551     TLIBATTR *attr;
2552     typelib_struct *tl_struct = (typelib_struct*) lParam;
2553     static const WCHAR fmt[] = {'%','s','\\','%','i',0};
2554     int sz; 
2555     HRESULT res;
2556
2557     if (!IS_INTRESOURCE(lpszName))
2558     {
2559         ERR("Not Int Resource Name %s\n",debugstr_w(lpszName));
2560         return TRUE;
2561     }
2562
2563     sz = strlenW(tl_struct->source)+4;
2564     sz *= sizeof(WCHAR);
2565
2566     if ((INT)lpszName == 1)
2567         tl_struct->path = strdupW(tl_struct->source);
2568     else
2569     {
2570         tl_struct->path = HeapAlloc(GetProcessHeap(),0,sz);
2571         sprintfW(tl_struct->path,fmt,tl_struct->source, lpszName);
2572     }
2573
2574     TRACE("trying %s\n", debugstr_w(tl_struct->path));
2575     res = LoadTypeLib(tl_struct->path,&tl_struct->ptLib);
2576     if (!SUCCEEDED(res))
2577     {
2578         HeapFree(GetProcessHeap(),0,tl_struct->path);
2579         tl_struct->path = NULL;
2580
2581         return TRUE;
2582     }
2583
2584     ITypeLib_GetLibAttr(tl_struct->ptLib, &attr);
2585     if (IsEqualGUID(&(tl_struct->clsid),&(attr->guid)))
2586     {
2587         ITypeLib_ReleaseTLibAttr(tl_struct->ptLib, attr);
2588         return FALSE;
2589     }
2590
2591     HeapFree(GetProcessHeap(),0,tl_struct->path);
2592     tl_struct->path = NULL;
2593
2594     ITypeLib_ReleaseTLibAttr(tl_struct->ptLib, attr);
2595     ITypeLib_Release(tl_struct->ptLib);
2596
2597     return TRUE;
2598 }
2599
2600 static UINT ITERATE_RegisterTypeLibraries(MSIRECORD *row, LPVOID param)
2601 {
2602     MSIPACKAGE* package = (MSIPACKAGE*)param;
2603     LPCWSTR component;
2604     MSICOMPONENT *comp;
2605     MSIFILE *file;
2606     typelib_struct tl_struct;
2607     HMODULE module;
2608     static const WCHAR szTYPELIB[] = {'T','Y','P','E','L','I','B',0};
2609
2610     component = MSI_RecordGetString(row,3);
2611     comp = get_loaded_component(package,component);
2612     if (!comp)
2613         return ERROR_SUCCESS;
2614
2615     if (!ACTION_VerifyComponentForAction(package, comp, INSTALLSTATE_LOCAL))
2616     {
2617         TRACE("Skipping typelib reg due to disabled component\n");
2618
2619         comp->Action = comp->Installed;
2620
2621         return ERROR_SUCCESS;
2622     }
2623
2624     comp->Action = INSTALLSTATE_LOCAL;
2625
2626     file = get_loaded_file( package, comp->KeyPath ); 
2627     if (!file)
2628         return ERROR_SUCCESS;
2629
2630     module = LoadLibraryExW( file->TargetPath, NULL, LOAD_LIBRARY_AS_DATAFILE );
2631     if (module != NULL)
2632     {
2633         LPWSTR guid;
2634         guid = load_dynamic_stringW(row,1);
2635         CLSIDFromString(guid, &tl_struct.clsid);
2636         HeapFree(GetProcessHeap(),0,guid);
2637         tl_struct.source = strdupW( file->TargetPath );
2638         tl_struct.path = NULL;
2639
2640         EnumResourceNamesW(module, szTYPELIB, Typelib_EnumResNameProc,
2641                         (LONG_PTR)&tl_struct);
2642
2643         if (tl_struct.path != NULL)
2644         {
2645             LPWSTR help = NULL;
2646             LPCWSTR helpid;
2647             HRESULT res;
2648
2649             helpid = MSI_RecordGetString(row,6);
2650
2651             if (helpid)
2652                 help = resolve_folder(package,helpid,FALSE,FALSE,NULL);
2653             res = RegisterTypeLib(tl_struct.ptLib,tl_struct.path,help);
2654             HeapFree(GetProcessHeap(),0,help);
2655
2656             if (!SUCCEEDED(res))
2657                 ERR("Failed to register type library %s\n",
2658                         debugstr_w(tl_struct.path));
2659             else
2660             {
2661                 ui_actiondata(package,szRegisterTypeLibraries,row);
2662
2663                 TRACE("Registered %s\n", debugstr_w(tl_struct.path));
2664             }
2665
2666             ITypeLib_Release(tl_struct.ptLib);
2667             HeapFree(GetProcessHeap(),0,tl_struct.path);
2668         }
2669         else
2670             ERR("Failed to load type library %s\n",
2671                     debugstr_w(tl_struct.source));
2672
2673         FreeLibrary(module);
2674         HeapFree(GetProcessHeap(),0,tl_struct.source);
2675     }
2676     else
2677         ERR("Could not load file! %s\n", debugstr_w(file->TargetPath));
2678
2679     return ERROR_SUCCESS;
2680 }
2681
2682 static UINT ACTION_RegisterTypeLibraries(MSIPACKAGE *package)
2683 {
2684     /* 
2685      * OK this is a bit confusing.. I am given a _Component key and I believe
2686      * that the file that is being registered as a type library is the "key file
2687      * of that component" which I interpret to mean "The file in the KeyPath of
2688      * that component".
2689      */
2690     UINT rc;
2691     MSIQUERY * view;
2692     static const WCHAR Query[] =
2693         {'S','E','L','E','C','T',' ','*',' ','F','R','O','M',' ',
2694          '`','T','y','p','e','L','i','b','`',0};
2695
2696     if (!package)
2697         return ERROR_INVALID_HANDLE;
2698
2699     rc = MSI_DatabaseOpenViewW(package->db, Query, &view);
2700     if (rc != ERROR_SUCCESS)
2701         return ERROR_SUCCESS;
2702
2703     rc = MSI_IterateRecords(view, NULL, ITERATE_RegisterTypeLibraries, package);
2704     msiobj_release(&view->hdr);
2705     return rc;
2706 }
2707
2708 static UINT ITERATE_CreateShortcuts(MSIRECORD *row, LPVOID param)
2709 {
2710     MSIPACKAGE *package = (MSIPACKAGE*)param;
2711     LPWSTR target_file, target_folder;
2712     LPCWSTR buffer;
2713     WCHAR filename[0x100];
2714     DWORD sz;
2715     MSICOMPONENT *comp;
2716     static const WCHAR szlnk[]={'.','l','n','k',0};
2717     IShellLinkW *sl;
2718     IPersistFile *pf;
2719     HRESULT res;
2720
2721     buffer = MSI_RecordGetString(row,4);
2722     comp = get_loaded_component(package,buffer);
2723     if (!comp)
2724         return ERROR_SUCCESS;
2725
2726     if (!ACTION_VerifyComponentForAction(package, comp, INSTALLSTATE_LOCAL))
2727     {
2728         TRACE("Skipping shortcut creation due to disabled component\n");
2729
2730         comp->Action = comp->Installed;
2731
2732         return ERROR_SUCCESS;
2733     }
2734
2735     comp->Action = INSTALLSTATE_LOCAL;
2736
2737     ui_actiondata(package,szCreateShortcuts,row);
2738
2739     res = CoCreateInstance( &CLSID_ShellLink, NULL, CLSCTX_INPROC_SERVER,
2740                     &IID_IShellLinkW, (LPVOID *) &sl );
2741
2742     if (FAILED(res))
2743     {
2744         ERR("Is IID_IShellLink\n");
2745         return ERROR_SUCCESS;
2746     }
2747
2748     res = IShellLinkW_QueryInterface( sl, &IID_IPersistFile,(LPVOID*) &pf );
2749     if( FAILED( res ) )
2750     {
2751         ERR("Is IID_IPersistFile\n");
2752         return ERROR_SUCCESS;
2753     }
2754
2755     buffer = MSI_RecordGetString(row,2);
2756     target_folder = resolve_folder(package, buffer,FALSE,FALSE,NULL);
2757
2758     /* may be needed because of a bug somehwere else */
2759     create_full_pathW(target_folder);
2760
2761     sz = 0x100;
2762     MSI_RecordGetStringW(row,3,filename,&sz);
2763     reduce_to_longfilename(filename);
2764     if (!strchrW(filename,'.') || strcmpiW(strchrW(filename,'.'),szlnk))
2765         strcatW(filename,szlnk);
2766     target_file = build_directory_name(2, target_folder, filename);
2767     HeapFree(GetProcessHeap(),0,target_folder);
2768
2769     buffer = MSI_RecordGetString(row,5);
2770     if (strchrW(buffer,'['))
2771     {
2772         LPWSTR deformated;
2773         deformat_string(package,buffer,&deformated);
2774         IShellLinkW_SetPath(sl,deformated);
2775         HeapFree(GetProcessHeap(),0,deformated);
2776     }
2777     else
2778     {
2779         LPWSTR keypath;
2780         FIXME("poorly handled shortcut format, advertised shortcut\n");
2781         keypath = strdupW( comp->FullKeypath );
2782         IShellLinkW_SetPath(sl,keypath);
2783         HeapFree(GetProcessHeap(),0,keypath);
2784     }
2785
2786     if (!MSI_RecordIsNull(row,6))
2787     {
2788         LPWSTR deformated;
2789         buffer = MSI_RecordGetString(row,6);
2790         deformat_string(package,buffer,&deformated);
2791         IShellLinkW_SetArguments(sl,deformated);
2792         HeapFree(GetProcessHeap(),0,deformated);
2793     }
2794
2795     if (!MSI_RecordIsNull(row,7))
2796     {
2797         buffer = MSI_RecordGetString(row,7);
2798         IShellLinkW_SetDescription(sl,buffer);
2799     }
2800
2801     if (!MSI_RecordIsNull(row,8))
2802         IShellLinkW_SetHotkey(sl,MSI_RecordGetInteger(row,8));
2803
2804     if (!MSI_RecordIsNull(row,9))
2805     {
2806         WCHAR *Path = NULL;
2807         INT index; 
2808
2809         buffer = MSI_RecordGetString(row,9);
2810
2811         build_icon_path(package,buffer,&Path);
2812         index = MSI_RecordGetInteger(row,10);
2813
2814         IShellLinkW_SetIconLocation(sl,Path,index);
2815         HeapFree(GetProcessHeap(),0,Path);
2816     }
2817
2818     if (!MSI_RecordIsNull(row,11))
2819         IShellLinkW_SetShowCmd(sl,MSI_RecordGetInteger(row,11));
2820
2821     if (!MSI_RecordIsNull(row,12))
2822     {
2823         LPWSTR Path;
2824         buffer = MSI_RecordGetString(row,12);
2825         Path = resolve_folder(package, buffer, FALSE, FALSE, NULL);
2826         IShellLinkW_SetWorkingDirectory(sl,Path);
2827         HeapFree(GetProcessHeap(), 0, Path);
2828     }
2829
2830     TRACE("Writing shortcut to %s\n",debugstr_w(target_file));
2831     IPersistFile_Save(pf,target_file,FALSE);
2832
2833     HeapFree(GetProcessHeap(),0,target_file);    
2834
2835     IPersistFile_Release( pf );
2836     IShellLinkW_Release( sl );
2837
2838     return ERROR_SUCCESS;
2839 }
2840
2841 static UINT ACTION_CreateShortcuts(MSIPACKAGE *package)
2842 {
2843     UINT rc;
2844     HRESULT res;
2845     MSIQUERY * view;
2846     static const WCHAR Query[] =
2847         {'S','E','L','E','C','T',' ','*',' ','F','R','O','M',' ',
2848          '`','S','h','o','r','t','c','u','t','`',0};
2849
2850     if (!package)
2851         return ERROR_INVALID_HANDLE;
2852
2853     rc = MSI_DatabaseOpenViewW(package->db, Query, &view);
2854     if (rc != ERROR_SUCCESS)
2855         return ERROR_SUCCESS;
2856
2857     res = CoInitialize( NULL );
2858     if (FAILED (res))
2859     {
2860         ERR("CoInitialize failed\n");
2861         return ERROR_FUNCTION_FAILED;
2862     }
2863
2864     rc = MSI_IterateRecords(view, NULL, ITERATE_CreateShortcuts, package);
2865     msiobj_release(&view->hdr);
2866
2867     CoUninitialize();
2868
2869     return rc;
2870 }
2871
2872 static UINT ITERATE_PublishProduct(MSIRECORD *row, LPVOID param)
2873 {
2874     MSIPACKAGE* package = (MSIPACKAGE*)param;
2875     HANDLE the_file;
2876     LPWSTR FilePath=NULL;
2877     LPCWSTR FileName=NULL;
2878     CHAR buffer[1024];
2879     DWORD sz;
2880     UINT rc;
2881
2882     FileName = MSI_RecordGetString(row,1);
2883     if (!FileName)
2884     {
2885         ERR("Unable to get FileName\n");
2886         return ERROR_SUCCESS;
2887     }
2888
2889     build_icon_path(package,FileName,&FilePath);
2890
2891     TRACE("Creating icon file at %s\n",debugstr_w(FilePath));
2892
2893     the_file = CreateFileW(FilePath, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS,
2894                         FILE_ATTRIBUTE_NORMAL, NULL);
2895
2896     if (the_file == INVALID_HANDLE_VALUE)
2897     {
2898         ERR("Unable to create file %s\n",debugstr_w(FilePath));
2899         HeapFree(GetProcessHeap(),0,FilePath);
2900         return ERROR_SUCCESS;
2901     }
2902
2903     do 
2904     {
2905         DWORD write;
2906         sz = 1024;
2907         rc = MSI_RecordReadStream(row,2,buffer,&sz);
2908         if (rc != ERROR_SUCCESS)
2909         {
2910             ERR("Failed to get stream\n");
2911             CloseHandle(the_file);  
2912             DeleteFileW(FilePath);
2913             break;
2914         }
2915         WriteFile(the_file,buffer,sz,&write,NULL);
2916     } while (sz == 1024);
2917
2918     HeapFree(GetProcessHeap(),0,FilePath);
2919
2920     CloseHandle(the_file);
2921     return ERROR_SUCCESS;
2922 }
2923
2924 /*
2925  * 99% of the work done here is only done for 
2926  * advertised installs. However this is where the
2927  * Icon table is processed and written out
2928  * so that is what I am going to do here.
2929  */
2930 static UINT ACTION_PublishProduct(MSIPACKAGE *package)
2931 {
2932     UINT rc;
2933     MSIQUERY * view;
2934     static const WCHAR Query[]=
2935         {'S','E','L','E','C','T',' ','*',' ','F','R','O','M',' ',
2936          '`','I','c','o','n','`',0};
2937     /* for registry stuff */
2938     HKEY hkey=0;
2939     HKEY hukey=0;
2940     static const WCHAR szProductLanguage[] =
2941         {'P','r','o','d','u','c','t','L','a','n','g','u','a','g','e',0};
2942     static const WCHAR szARPProductIcon[] =
2943         {'A','R','P','P','R','O','D','U','C','T','I','C','O','N',0};
2944     static const WCHAR szProductVersion[] =
2945         {'P','r','o','d','u','c','t','V','e','r','s','i','o','n',0};
2946     DWORD langid;
2947     LPWSTR buffer;
2948     DWORD size;
2949     MSIHANDLE hDb, hSumInfo;
2950
2951     if (!package)
2952         return ERROR_INVALID_HANDLE;
2953
2954     /* write out icon files */
2955
2956     rc = MSI_DatabaseOpenViewW(package->db, Query, &view);
2957     if (rc == ERROR_SUCCESS)
2958     {
2959         MSI_IterateRecords(view, NULL, ITERATE_PublishProduct, package);
2960         msiobj_release(&view->hdr);
2961     }
2962
2963     /* ok there is a lot more done here but i need to figure out what */
2964
2965     rc = MSIREG_OpenProductsKey(package->ProductCode,&hkey,TRUE);
2966     if (rc != ERROR_SUCCESS)
2967         goto end;
2968
2969     rc = MSIREG_OpenUserProductsKey(package->ProductCode,&hukey,TRUE);
2970     if (rc != ERROR_SUCCESS)
2971         goto end;
2972
2973
2974     buffer = msi_dup_property( package, INSTALLPROPERTY_PRODUCTNAMEW );
2975     msi_reg_set_val_str( hukey, INSTALLPROPERTY_PRODUCTNAMEW, buffer );
2976     HeapFree(GetProcessHeap(),0,buffer);
2977
2978     buffer = msi_dup_property( package, szProductLanguage );
2979     langid = atoiW(buffer);
2980     msi_reg_set_val_dword( hkey, INSTALLPROPERTY_LANGUAGEW, langid );
2981     HeapFree(GetProcessHeap(),0,buffer);
2982
2983     buffer = msi_dup_property( package, szARPProductIcon );
2984     if (buffer)
2985     {
2986         LPWSTR path;
2987         build_icon_path(package,buffer,&path);
2988         msi_reg_set_val_str( hukey, INSTALLPROPERTY_PRODUCTICONW, path );
2989     }
2990     HeapFree(GetProcessHeap(),0,buffer);
2991
2992     buffer = msi_dup_property( package, szProductVersion );
2993     if (buffer)
2994     {
2995         DWORD verdword = build_version_dword(buffer);
2996         msi_reg_set_val_dword( hkey, INSTALLPROPERTY_VERSIONW, verdword );
2997     }
2998     HeapFree(GetProcessHeap(),0,buffer);
2999     
3000     FIXME("Need to write more keys to the user registry\n");
3001   
3002     hDb= alloc_msihandle( &package->db->hdr );
3003     rc = MsiGetSummaryInformationW(hDb, NULL, 0, &hSumInfo); 
3004     MsiCloseHandle(hDb);
3005     if (rc == ERROR_SUCCESS)
3006     {
3007         WCHAR guidbuffer[0x200];
3008         size = 0x200;
3009         rc = MsiSummaryInfoGetPropertyW(hSumInfo, 9, NULL, NULL, NULL,
3010                                         guidbuffer, &size);
3011         if (rc == ERROR_SUCCESS)
3012         {
3013             WCHAR squashed[GUID_SIZE];
3014             /* for now we only care about the first guid */
3015             LPWSTR ptr = strchrW(guidbuffer,';');
3016             if (ptr) *ptr = 0;
3017             squash_guid(guidbuffer,squashed);
3018             msi_reg_set_val_str( hukey, INSTALLPROPERTY_PACKAGECODEW, squashed );
3019         }
3020         else
3021         {
3022             ERR("Unable to query Revision_Number... \n");
3023             rc = ERROR_SUCCESS;
3024         }
3025         MsiCloseHandle(hSumInfo);
3026     }
3027     else
3028     {
3029         ERR("Unable to open Summary Information\n");
3030         rc = ERROR_SUCCESS;
3031     }
3032
3033 end:
3034
3035     RegCloseKey(hkey);
3036     RegCloseKey(hukey);
3037
3038     return rc;
3039 }
3040
3041 static UINT ITERATE_WriteIniValues(MSIRECORD *row, LPVOID param)
3042 {
3043     MSIPACKAGE *package = (MSIPACKAGE*)param;
3044     LPCWSTR component,section,key,value,identifier,filename,dirproperty;
3045     LPWSTR deformated_section, deformated_key, deformated_value;
3046     LPWSTR folder, fullname = NULL;
3047     MSIRECORD * uirow;
3048     INT action;
3049     MSICOMPONENT *comp;
3050     static const WCHAR szWindowsFolder[] =
3051           {'W','i','n','d','o','w','s','F','o','l','d','e','r',0};
3052
3053     component = MSI_RecordGetString(row, 8);
3054     comp = get_loaded_component(package,component);
3055
3056     if (!ACTION_VerifyComponentForAction(package, comp, INSTALLSTATE_LOCAL))
3057     {
3058         TRACE("Skipping ini file due to disabled component %s\n",
3059                         debugstr_w(component));
3060
3061         comp->Action = comp->Installed;
3062
3063         return ERROR_SUCCESS;
3064     }
3065
3066     comp->Action = INSTALLSTATE_LOCAL;
3067
3068     identifier = MSI_RecordGetString(row,1); 
3069     filename = MSI_RecordGetString(row,2);
3070     dirproperty = MSI_RecordGetString(row,3);
3071     section = MSI_RecordGetString(row,4);
3072     key = MSI_RecordGetString(row,5);
3073     value = MSI_RecordGetString(row,6);
3074     action = MSI_RecordGetInteger(row,7);
3075
3076     deformat_string(package,section,&deformated_section);
3077     deformat_string(package,key,&deformated_key);
3078     deformat_string(package,value,&deformated_value);
3079
3080     if (dirproperty)
3081     {
3082         folder = resolve_folder(package, dirproperty, FALSE, FALSE, NULL);
3083         if (!folder)
3084             folder = msi_dup_property( package, dirproperty );
3085     }
3086     else
3087         folder = msi_dup_property( package, szWindowsFolder );
3088
3089     if (!folder)
3090     {
3091         ERR("Unable to resolve folder! (%s)\n",debugstr_w(dirproperty));
3092         goto cleanup;
3093     }
3094
3095     fullname = build_directory_name(2, folder, filename);
3096
3097     if (action == 0)
3098     {
3099         TRACE("Adding value %s to section %s in %s\n",
3100                 debugstr_w(deformated_key), debugstr_w(deformated_section),
3101                 debugstr_w(fullname));
3102         WritePrivateProfileStringW(deformated_section, deformated_key,
3103                                    deformated_value, fullname);
3104     }
3105     else if (action == 1)
3106     {
3107         WCHAR returned[10];
3108         GetPrivateProfileStringW(deformated_section, deformated_key, NULL,
3109                                  returned, 10, fullname);
3110         if (returned[0] == 0)
3111         {
3112             TRACE("Adding value %s to section %s in %s\n",
3113                     debugstr_w(deformated_key), debugstr_w(deformated_section),
3114                     debugstr_w(fullname));
3115
3116             WritePrivateProfileStringW(deformated_section, deformated_key,
3117                                        deformated_value, fullname);
3118         }
3119     }
3120     else if (action == 3)
3121         FIXME("Append to existing section not yet implemented\n");
3122
3123     uirow = MSI_CreateRecord(4);
3124     MSI_RecordSetStringW(uirow,1,identifier);
3125     MSI_RecordSetStringW(uirow,2,deformated_section);
3126     MSI_RecordSetStringW(uirow,3,deformated_key);
3127     MSI_RecordSetStringW(uirow,4,deformated_value);
3128     ui_actiondata(package,szWriteIniValues,uirow);
3129     msiobj_release( &uirow->hdr );
3130 cleanup:
3131     HeapFree(GetProcessHeap(),0,fullname);
3132     HeapFree(GetProcessHeap(),0,folder);
3133     HeapFree(GetProcessHeap(),0,deformated_key);
3134     HeapFree(GetProcessHeap(),0,deformated_value);
3135     HeapFree(GetProcessHeap(),0,deformated_section);
3136     return ERROR_SUCCESS;
3137 }
3138
3139 static UINT ACTION_WriteIniValues(MSIPACKAGE *package)
3140 {
3141     UINT rc;
3142     MSIQUERY * view;
3143     static const WCHAR ExecSeqQuery[] = 
3144         {'S','E','L','E','C','T',' ','*',' ','F','R','O','M',' ',
3145          '`','I','n','i','F','i','l','e','`',0};
3146
3147     rc = MSI_DatabaseOpenViewW(package->db, ExecSeqQuery, &view);
3148     if (rc != ERROR_SUCCESS)
3149     {
3150         TRACE("no IniFile table\n");
3151         return ERROR_SUCCESS;
3152     }
3153
3154     rc = MSI_IterateRecords(view, NULL, ITERATE_WriteIniValues, package);
3155     msiobj_release(&view->hdr);
3156     return rc;
3157 }
3158
3159 static UINT ITERATE_SelfRegModules(MSIRECORD *row, LPVOID param)
3160 {
3161     MSIPACKAGE *package = (MSIPACKAGE*)param;
3162     LPCWSTR filename;
3163     LPWSTR FullName;
3164     MSIFILE *file;
3165     DWORD len;
3166     static const WCHAR ExeStr[] =
3167         {'r','e','g','s','v','r','3','2','.','e','x','e',' ','\"',0};
3168     static const WCHAR close[] =  {'\"',0};
3169     STARTUPINFOW si;
3170     PROCESS_INFORMATION info;
3171     BOOL brc;
3172
3173     memset(&si,0,sizeof(STARTUPINFOW));
3174
3175     filename = MSI_RecordGetString(row,1);
3176     file = get_loaded_file( package, filename );
3177
3178     if (!file)
3179     {
3180         ERR("Unable to find file id %s\n",debugstr_w(filename));
3181         return ERROR_SUCCESS;
3182     }
3183
3184     len = strlenW(ExeStr) + strlenW( file->TargetPath ) + 2;
3185
3186     FullName = HeapAlloc(GetProcessHeap(),0,len*sizeof(WCHAR));
3187     strcpyW(FullName,ExeStr);
3188     strcatW( FullName, file->TargetPath );
3189     strcatW(FullName,close);
3190
3191     TRACE("Registering %s\n",debugstr_w(FullName));
3192     brc = CreateProcessW(NULL, FullName, NULL, NULL, FALSE, 0, NULL, c_colon,
3193                     &si, &info);
3194
3195     if (brc)
3196         msi_dialog_check_messages(info.hProcess);
3197
3198     HeapFree(GetProcessHeap(),0,FullName);
3199     return ERROR_SUCCESS;
3200 }
3201
3202 static UINT ACTION_SelfRegModules(MSIPACKAGE *package)
3203 {
3204     UINT rc;
3205     MSIQUERY * view;
3206     static const WCHAR ExecSeqQuery[] = 
3207         {'S','E','L','E','C','T',' ','*',' ','F','R','O','M',' ',
3208          '`','S','e','l','f','R','e','g','`',0};
3209
3210     rc = MSI_DatabaseOpenViewW(package->db, ExecSeqQuery, &view);
3211     if (rc != ERROR_SUCCESS)
3212     {
3213         TRACE("no SelfReg table\n");
3214         return ERROR_SUCCESS;
3215     }
3216
3217     MSI_IterateRecords(view, NULL, ITERATE_SelfRegModules, package);
3218     msiobj_release(&view->hdr);
3219
3220     return ERROR_SUCCESS;
3221 }
3222
3223 static UINT ACTION_PublishFeatures(MSIPACKAGE *package)
3224 {
3225     MSIFEATURE *feature;
3226     UINT rc;
3227     HKEY hkey=0;
3228     HKEY hukey=0;
3229     
3230     if (!package)
3231         return ERROR_INVALID_HANDLE;
3232
3233     rc = MSIREG_OpenFeaturesKey(package->ProductCode,&hkey,TRUE);
3234     if (rc != ERROR_SUCCESS)
3235         goto end;
3236
3237     rc = MSIREG_OpenUserFeaturesKey(package->ProductCode,&hukey,TRUE);
3238     if (rc != ERROR_SUCCESS)
3239         goto end;
3240
3241     /* here the guids are base 85 encoded */
3242     LIST_FOR_EACH_ENTRY( feature, &package->features, MSIFEATURE, entry )
3243     {
3244         ComponentList *cl;
3245         LPWSTR data = NULL;
3246         GUID clsid;
3247         INT size;
3248         BOOL absent = FALSE;
3249
3250         if (!ACTION_VerifyFeatureForAction( feature, INSTALLSTATE_LOCAL ) &&
3251             !ACTION_VerifyFeatureForAction( feature, INSTALLSTATE_SOURCE ) &&
3252             !ACTION_VerifyFeatureForAction( feature, INSTALLSTATE_ADVERTISED ))
3253             absent = TRUE;
3254
3255         size = 1;
3256         LIST_FOR_EACH_ENTRY( cl, &feature->Components, ComponentList, entry )
3257         {
3258             size += 21;
3259         }
3260         if (feature->Feature_Parent)
3261             size += strlenW( feature->Feature_Parent )+2;
3262
3263         data = HeapAlloc(GetProcessHeap(), 0, size * sizeof(WCHAR));
3264
3265         data[0] = 0;
3266         LIST_FOR_EACH_ENTRY( cl, &feature->Components, ComponentList, entry )
3267         {
3268             MSICOMPONENT* component = cl->component;
3269             WCHAR buf[21];
3270
3271             memset(buf,0,sizeof(buf));
3272             if (component->ComponentId)
3273             {
3274                 TRACE("From %s\n",debugstr_w(component->ComponentId));
3275                 CLSIDFromString(component->ComponentId, &clsid);
3276                 encode_base85_guid(&clsid,buf);
3277                 TRACE("to %s\n",debugstr_w(buf));
3278                 strcatW(data,buf);
3279             }
3280         }
3281         if (feature->Feature_Parent)
3282         {
3283             static const WCHAR sep[] = {'\2',0};
3284             strcatW(data,sep);
3285             strcatW(data,feature->Feature_Parent);
3286         }
3287
3288         msi_reg_set_val_str( hkey, feature->Feature, data );
3289         HeapFree(GetProcessHeap(),0,data);
3290
3291         size = 0;
3292         if (feature->Feature_Parent)
3293             size = strlenW(feature->Feature_Parent)*sizeof(WCHAR);
3294         if (!absent)
3295         {
3296             RegSetValueExW(hukey,feature->Feature,0,REG_SZ,
3297                        (LPBYTE)feature->Feature_Parent,size);
3298         }
3299         else
3300         {
3301             size += 2*sizeof(WCHAR);
3302             data = HeapAlloc(GetProcessHeap(),0,size);
3303             data[0] = 0x6;
3304             data[1] = 0;
3305             if (feature->Feature_Parent)
3306                 strcpyW( &data[1], feature->Feature_Parent );
3307             RegSetValueExW(hukey,feature->Feature,0,REG_SZ,
3308                        (LPBYTE)data,size);
3309             HeapFree(GetProcessHeap(),0,data);
3310         }
3311     }
3312
3313 end:
3314     RegCloseKey(hkey);
3315     RegCloseKey(hukey);
3316     return rc;
3317 }
3318
3319 static UINT ACTION_RegisterProduct(MSIPACKAGE *package)
3320 {
3321     HKEY hkey=0;
3322     LPWSTR buffer = NULL;
3323     UINT rc,i;
3324     DWORD size;
3325     static const WCHAR szWindowsInstaller[] = 
3326     {'W','i','n','d','o','w','s','I','n','s','t','a','l','l','e','r',0};
3327     static const WCHAR szPropKeys[][80] = 
3328     {
3329 {'A','R','P','A','U','T','H','O','R','I','Z','E','D','C','D','F','P','R','E','F','I','X',0},
3330 {'A','R','P','C','O','N','T','A','C','T',0},
3331 {'A','R','P','C','O','M','M','E','N','T','S',0},
3332 {'P','r','o','d','u','c','t','N','a','m','e',0},
3333 {'P','r','o','d','u','c','t','V','e','r','s','i','o','n',0},
3334 {'A','R','P','H','E','L','P','L','I','N','K',0},
3335 {'A','R','P','H','E','L','P','T','E','L','E','P','H','O','N','E',0},
3336 {'A','R','P','I','N','S','T','A','L','L','L','O','C','A','T','I','O','N',0},
3337 {'S','o','u','r','c','e','D','i','r',0},
3338 {'M','a','n','u','f','a','c','t','u','r','e','r',0},
3339 {'A','R','P','R','E','A','D','M','E',0},
3340 {'A','R','P','S','I','Z','E',0},
3341 {'A','R','P','U','R','L','I','N','F','O','A','B','O','U','T',0},
3342 {'A','R','P','U','R','L','U','P','D','A','T','E','I','N','F','O',0},
3343 {0},
3344     };
3345
3346     static const WCHAR szRegKeys[][80] = 
3347     {
3348 {'A','u','t','h','o','r','i','z','e','d','C','D','F','P','r','e','f','i','x',0},
3349 {'C','o','n','t','a','c','t',0},
3350 {'C','o','m','m','e','n','t','s',0},
3351 {'D','i','s','p','l','a','y','N','a','m','e',0},
3352 {'D','i','s','p','l','a','y','V','e','r','s','i','o','n',0},
3353 {'H','e','l','p','L','i','n','k',0},
3354 {'H','e','l','p','T','e','l','e','p','h','o','n','e',0},
3355 {'I','n','s','t','a','l','l','L','o','c','a','t','i','o','n',0},
3356 {'I','n','s','t','a','l','l','S','o','u','r','c','e',0},
3357 {'P','u','b','l','i','s','h','e','r',0},
3358 {'R','e','a','d','m','e',0},
3359 {'S','i','z','e',0},
3360 {'U','R','L','I','n','f','o','A','b','o','u','t',0},
3361 {'U','R','L','U','p','d','a','t','e','I','n','f','o',0},
3362 {0},
3363     };
3364
3365     static const WCHAR installerPathFmt[] = {
3366     '%','s','\\',
3367     'I','n','s','t','a','l','l','e','r','\\',0};
3368     static const WCHAR fmt[] = {
3369     '%','s','\\',
3370     'I','n','s','t','a','l','l','e','r','\\',
3371     '%','x','.','m','s','i',0};
3372     static const WCHAR szUpgradeCode[] = 
3373         {'U','p','g','r','a','d','e','C','o','d','e',0};
3374     static const WCHAR modpath_fmt[] = 
3375         {'M','s','i','E','x','e','c','.','e','x','e',' ','/','I','[','P','r','o','d','u','c','t','C','o','d','e',']',0};
3376     static const WCHAR szModifyPath[] = 
3377         {'M','o','d','i','f','y','P','a','t','h',0};
3378     static const WCHAR szUninstallString[] = 
3379         {'U','n','i','n','s','t','a','l','l','S','t','r','i','n','g',0};
3380     static const WCHAR szEstimatedSize[] = 
3381         {'E','s','t','i','m','a','t','e','d','S','i','z','e',0};
3382     static const WCHAR szProductLanguage[] =
3383         {'P','r','o','d','u','c','t','L','a','n','g','u','a','g','e',0};
3384     static const WCHAR szProductVersion[] =
3385         {'P','r','o','d','u','c','t','V','e','r','s','i','o','n',0};
3386
3387     SYSTEMTIME systime;
3388     static const WCHAR date_fmt[] = {'%','i','%','i','%','i',0};
3389     LPWSTR upgrade_code;
3390     WCHAR windir[MAX_PATH], path[MAX_PATH], packagefile[MAX_PATH];
3391     INT num,start;
3392
3393     if (!package)
3394         return ERROR_INVALID_HANDLE;
3395
3396     rc = MSIREG_OpenUninstallKey(package->ProductCode,&hkey,TRUE);
3397     if (rc != ERROR_SUCCESS)
3398         goto end;
3399
3400     /* dump all the info i can grab */
3401     FIXME("Flesh out more information \n");
3402
3403     for( i=0; szPropKeys[i][0]; i++ )
3404     {
3405         buffer = msi_dup_property( package, szPropKeys[i] );
3406         msi_reg_set_val_str( hkey, szRegKeys[i], buffer );
3407         HeapFree(GetProcessHeap(),0,buffer);
3408     }
3409
3410     msi_reg_set_val_dword( hkey, szWindowsInstaller, 1 );
3411     
3412     /* copy the package locally */
3413     num = GetTickCount() & 0xffff;
3414     if (!num) 
3415         num = 1;
3416     start = num;
3417     GetWindowsDirectoryW(windir, sizeof(windir) / sizeof(windir[0]));
3418     snprintfW(packagefile,sizeof(packagefile)/sizeof(packagefile[0]),fmt,
3419      windir,num);
3420     do 
3421     {
3422         HANDLE handle = CreateFileW(packagefile,GENERIC_WRITE, 0, NULL,
3423                                   CREATE_NEW, FILE_ATTRIBUTE_NORMAL, 0 );
3424         if (handle != INVALID_HANDLE_VALUE)
3425         {
3426             CloseHandle(handle);
3427             break;
3428         }
3429         if (GetLastError() != ERROR_FILE_EXISTS &&
3430             GetLastError() != ERROR_SHARING_VIOLATION)
3431             break;
3432         if (!(++num & 0xffff)) num = 1;
3433         sprintfW(packagefile,fmt,num);
3434     } while (num != start);
3435
3436     snprintfW(path,sizeof(path)/sizeof(path[0]),installerPathFmt,windir);
3437     create_full_pathW(path);
3438     TRACE("Copying to local package %s\n",debugstr_w(packagefile));
3439     if (!CopyFileW(package->msiFilePath,packagefile,FALSE))
3440         ERR("Unable to copy package (%s -> %s) (error %ld)\n",
3441             debugstr_w(package->msiFilePath), debugstr_w(packagefile),
3442             GetLastError());
3443     msi_reg_set_val_str( hkey, INSTALLPROPERTY_LOCALPACKAGEW, packagefile );
3444
3445     /* do ModifyPath and UninstallString */
3446     size = deformat_string(package,modpath_fmt,&buffer);
3447     RegSetValueExW(hkey,szModifyPath,0,REG_EXPAND_SZ,(LPBYTE)buffer,size);
3448     RegSetValueExW(hkey,szUninstallString,0,REG_EXPAND_SZ,(LPBYTE)buffer,size);
3449     HeapFree(GetProcessHeap(),0,buffer);
3450
3451     FIXME("Write real Estimated Size when we have it\n");
3452     msi_reg_set_val_dword( hkey, szEstimatedSize, 0 );
3453    
3454     GetLocalTime(&systime);
3455     size = 9*sizeof(WCHAR);
3456     buffer= HeapAlloc(GetProcessHeap(),0,size);
3457     sprintfW(buffer,date_fmt,systime.wYear,systime.wMonth,systime.wDay);
3458     msi_reg_set_val_str( hkey, INSTALLPROPERTY_INSTALLDATEW, buffer );
3459     HeapFree(GetProcessHeap(),0,buffer);
3460    
3461     buffer = msi_dup_property( package, szProductLanguage );
3462     msi_reg_set_val_dword( hkey, INSTALLPROPERTY_LANGUAGEW, atoiW(buffer) );
3463     HeapFree(GetProcessHeap(),1,buffer);
3464
3465     buffer = msi_dup_property( package, szProductVersion );
3466     if (buffer)
3467     {
3468         DWORD verdword = build_version_dword(buffer);
3469
3470         msi_reg_set_val_dword( hkey, INSTALLPROPERTY_VERSIONW, verdword );
3471         msi_reg_set_val_dword( hkey, INSTALLPROPERTY_VERSIONMAJORW, verdword>>24 );
3472         msi_reg_set_val_dword( hkey, INSTALLPROPERTY_VERSIONMINORW, (verdword>>16)&0x00FF );
3473     }
3474     HeapFree(GetProcessHeap(),0,buffer);
3475     
3476     /* Handle Upgrade Codes */
3477     upgrade_code = msi_dup_property( package, szUpgradeCode );
3478     if (upgrade_code)
3479     {
3480         HKEY hkey2;
3481         WCHAR squashed[33];
3482         MSIREG_OpenUpgradeCodesKey(upgrade_code, &hkey2, TRUE);
3483         squash_guid(package->ProductCode,squashed);
3484         msi_reg_set_val_str( hkey2, squashed, NULL );
3485         RegCloseKey(hkey2);
3486         MSIREG_OpenUserUpgradeCodesKey(upgrade_code, &hkey2, TRUE);
3487         squash_guid(package->ProductCode,squashed);
3488         msi_reg_set_val_str( hkey2, squashed, NULL );
3489         RegCloseKey(hkey2);
3490
3491         HeapFree(GetProcessHeap(),0,upgrade_code);
3492     }
3493     
3494 end:
3495     RegCloseKey(hkey);
3496
3497     return ERROR_SUCCESS;
3498 }
3499
3500 static UINT ACTION_InstallExecute(MSIPACKAGE *package)
3501 {
3502     UINT rc;
3503
3504     if (!package)
3505         return ERROR_INVALID_HANDLE;
3506
3507     rc = execute_script(package,INSTALL_SCRIPT);
3508
3509     return rc;
3510 }
3511
3512 static UINT ACTION_InstallFinalize(MSIPACKAGE *package)
3513 {
3514     UINT rc;
3515
3516     if (!package)
3517         return ERROR_INVALID_HANDLE;
3518
3519     /* turn off scheduleing */
3520     package->script->CurrentlyScripting= FALSE;
3521
3522     /* first do the same as an InstallExecute */
3523     rc = ACTION_InstallExecute(package);
3524     if (rc != ERROR_SUCCESS)
3525         return rc;
3526
3527     /* then handle Commit Actions */
3528     rc = execute_script(package,COMMIT_SCRIPT);
3529
3530     return rc;
3531 }
3532
3533 static UINT ACTION_ForceReboot(MSIPACKAGE *package)
3534 {
3535     static const WCHAR RunOnce[] = {
3536     'S','o','f','t','w','a','r','e','\\',
3537     'M','i','c','r','o','s','o','f','t','\\',
3538     'W','i','n','d','o','w','s','\\',
3539     'C','u','r','r','e','n','t','V','e','r','s','i','o','n','\\',
3540     'R','u','n','O','n','c','e',0};
3541     static const WCHAR InstallRunOnce[] = {
3542     'S','o','f','t','w','a','r','e','\\',
3543     'M','i','c','r','o','s','o','f','t','\\',
3544     'W','i','n','d','o','w','s','\\',
3545     'C','u','r','r','e','n','t','V','e','r','s','i','o','n','\\',
3546     'I','n','s','t','a','l','l','e','r','\\',
3547     'R','u','n','O','n','c','e','E','n','t','r','i','e','s',0};
3548
3549     static const WCHAR msiexec_fmt[] = {
3550     '%','s',
3551     '\\','M','s','i','E','x','e','c','.','e','x','e',' ','/','@',' ',
3552     '\"','%','s','\"',0};
3553     static const WCHAR install_fmt[] = {
3554     '/','I',' ','\"','%','s','\"',' ',
3555     'A','F','T','E','R','R','E','B','O','O','T','=','1',' ',
3556     'R','U','N','O','N','C','E','E','N','T','R','Y','=','\"','%','s','\"',0};
3557     WCHAR buffer[256], sysdir[MAX_PATH];
3558     HKEY hkey;
3559     WCHAR squished_pc[100];
3560
3561     if (!package)
3562         return ERROR_INVALID_HANDLE;
3563
3564     squash_guid(package->ProductCode,squished_pc);
3565
3566     GetSystemDirectoryW(sysdir, sizeof(sysdir)/sizeof(sysdir[0]));
3567     RegCreateKeyW(HKEY_LOCAL_MACHINE,RunOnce,&hkey);
3568     snprintfW(buffer,sizeof(buffer)/sizeof(buffer[0]),msiexec_fmt,sysdir,
3569      squished_pc);
3570
3571     msi_reg_set_val_str( hkey, squished_pc, buffer );
3572     RegCloseKey(hkey);
3573
3574     TRACE("Reboot command %s\n",debugstr_w(buffer));
3575
3576     RegCreateKeyW(HKEY_LOCAL_MACHINE,InstallRunOnce,&hkey);
3577     sprintfW(buffer,install_fmt,package->ProductCode,squished_pc);
3578
3579     msi_reg_set_val_str( hkey, squished_pc, buffer );
3580     RegCloseKey(hkey);
3581
3582     return ERROR_INSTALL_SUSPEND;
3583 }
3584
3585 UINT ACTION_ResolveSource(MSIPACKAGE* package)
3586 {
3587     DWORD attrib;
3588     UINT rc;
3589     /*
3590      * we are currently doing what should be done here in the top level Install
3591      * however for Adminastrative and uninstalls this step will be needed
3592      */
3593     if (!package->PackagePath)
3594         return ERROR_SUCCESS;
3595
3596     attrib = GetFileAttributesW(package->PackagePath);
3597     if (attrib == INVALID_FILE_ATTRIBUTES)
3598     {
3599         LPWSTR prompt;
3600         LPWSTR msg;
3601         DWORD size = 0;
3602
3603         rc = MsiSourceListGetInfoW(package->ProductCode, NULL, 
3604                 MSIINSTALLCONTEXT_USERMANAGED, MSICODE_PRODUCT,
3605                 INSTALLPROPERTY_DISKPROMPTW,NULL,&size);
3606         if (rc == ERROR_MORE_DATA)
3607         {
3608             prompt = HeapAlloc(GetProcessHeap(),0,size * sizeof(WCHAR));
3609             MsiSourceListGetInfoW(package->ProductCode, NULL, 
3610                     MSIINSTALLCONTEXT_USERMANAGED, MSICODE_PRODUCT,
3611                     INSTALLPROPERTY_DISKPROMPTW,prompt,&size);
3612         }
3613         else
3614             prompt = strdupW(package->PackagePath);
3615
3616         msg = generate_error_string(package,1302,1,prompt);
3617         while(attrib == INVALID_FILE_ATTRIBUTES)
3618         {
3619             rc = MessageBoxW(NULL,msg,NULL,MB_OKCANCEL);
3620             if (rc == IDCANCEL)
3621             {
3622                 rc = ERROR_INSTALL_USEREXIT;
3623                 break;
3624             }
3625             attrib = GetFileAttributesW(package->PackagePath);
3626         }
3627         HeapFree(GetProcessHeap(),0,prompt);
3628         rc = ERROR_SUCCESS;
3629     }
3630     else
3631         return ERROR_SUCCESS;
3632
3633     return rc;
3634 }
3635
3636 static UINT ACTION_RegisterUser(MSIPACKAGE *package)
3637 {
3638     HKEY hkey=0;
3639     LPWSTR buffer;
3640     LPWSTR productid;
3641     UINT rc,i;
3642
3643     static const WCHAR szPropKeys[][80] = 
3644     {
3645         {'P','r','o','d','u','c','t','I','D',0},
3646         {'U','S','E','R','N','A','M','E',0},
3647         {'C','O','M','P','A','N','Y','N','A','M','E',0},
3648         {0},
3649     };
3650
3651     static const WCHAR szRegKeys[][80] = 
3652     {
3653         {'P','r','o','d','u','c','t','I','D',0},
3654         {'R','e','g','O','w','n','e','r',0},
3655         {'R','e','g','C','o','m','p','a','n','y',0},
3656         {0},
3657     };
3658
3659     if (!package)
3660         return ERROR_INVALID_HANDLE;
3661
3662     productid = msi_dup_property( package, INSTALLPROPERTY_PRODUCTIDW );
3663     if (!productid)
3664         return ERROR_SUCCESS;
3665
3666     rc = MSIREG_OpenUninstallKey(package->ProductCode,&hkey,TRUE);
3667     if (rc != ERROR_SUCCESS)
3668         goto end;
3669
3670     for( i = 0; szPropKeys[i][0]; i++ )
3671     {
3672         buffer = msi_dup_property( package, szPropKeys[i] );
3673         msi_reg_set_val_str( hkey, szRegKeys[i], buffer );
3674         HeapFree( GetProcessHeap(), 0, buffer );
3675     }
3676
3677 end:
3678     HeapFree(GetProcessHeap(),0,productid);
3679     RegCloseKey(hkey);
3680
3681     return ERROR_SUCCESS;
3682 }
3683
3684
3685 static UINT ACTION_ExecuteAction(MSIPACKAGE *package)
3686 {
3687     static const WCHAR szUILevel[] = {'U','I','L','e','v','e','l',0};
3688     static const WCHAR szTwo[] = {'2',0};
3689     UINT rc;
3690     LPWSTR level;
3691     level = msi_dup_property( package, szUILevel );
3692
3693     MSI_SetPropertyW(package,szUILevel,szTwo);
3694     package->script->InWhatSequence |= SEQUENCE_EXEC;
3695     rc = ACTION_ProcessExecSequence(package,FALSE);
3696     MSI_SetPropertyW(package,szUILevel,level);
3697     HeapFree(GetProcessHeap(),0,level);
3698     return rc;
3699 }
3700
3701
3702 /*
3703  * Code based off of code located here
3704  * http://www.codeproject.com/gdi/fontnamefromfile.asp
3705  *
3706  * Using string index 4 (full font name) instead of 1 (family name)
3707  */
3708 static LPWSTR load_ttfname_from(LPCWSTR filename)
3709 {
3710     HANDLE handle;
3711     LPWSTR ret = NULL;
3712     int i;
3713
3714     typedef struct _tagTT_OFFSET_TABLE{
3715         USHORT uMajorVersion;
3716         USHORT uMinorVersion;
3717         USHORT uNumOfTables;
3718         USHORT uSearchRange;
3719         USHORT uEntrySelector;
3720         USHORT uRangeShift;
3721     }TT_OFFSET_TABLE;
3722
3723     typedef struct _tagTT_TABLE_DIRECTORY{
3724         char szTag[4]; /* table name */
3725         ULONG uCheckSum; /* Check sum */
3726         ULONG uOffset; /* Offset from beginning of file */
3727         ULONG uLength; /* length of the table in bytes */
3728     }TT_TABLE_DIRECTORY;
3729
3730     typedef struct _tagTT_NAME_TABLE_HEADER{
3731     USHORT uFSelector; /* format selector. Always 0 */
3732     USHORT uNRCount; /* Name Records count */
3733     USHORT uStorageOffset; /* Offset for strings storage, 
3734                             * from start of the table */
3735     }TT_NAME_TABLE_HEADER;
3736    
3737     typedef struct _tagTT_NAME_RECORD{
3738         USHORT uPlatformID;
3739         USHORT uEncodingID;
3740         USHORT uLanguageID;
3741         USHORT uNameID;
3742         USHORT uStringLength;
3743         USHORT uStringOffset; /* from start of storage area */
3744     }TT_NAME_RECORD;
3745
3746 #define SWAPWORD(x) MAKEWORD(HIBYTE(x), LOBYTE(x))
3747 #define SWAPLONG(x) MAKELONG(SWAPWORD(HIWORD(x)), SWAPWORD(LOWORD(x)))
3748
3749     handle = CreateFileW(filename ,GENERIC_READ, 0, NULL, OPEN_EXISTING,
3750                     FILE_ATTRIBUTE_NORMAL, 0 );
3751     if (handle != INVALID_HANDLE_VALUE)
3752     {
3753         TT_TABLE_DIRECTORY tblDir;
3754         BOOL bFound = FALSE;
3755         TT_OFFSET_TABLE ttOffsetTable;
3756
3757         ReadFile(handle,&ttOffsetTable, sizeof(TT_OFFSET_TABLE),NULL,NULL);
3758         ttOffsetTable.uNumOfTables = SWAPWORD(ttOffsetTable.uNumOfTables);
3759         ttOffsetTable.uMajorVersion = SWAPWORD(ttOffsetTable.uMajorVersion);
3760         ttOffsetTable.uMinorVersion = SWAPWORD(ttOffsetTable.uMinorVersion);
3761         
3762         if (ttOffsetTable.uMajorVersion != 1 || 
3763                         ttOffsetTable.uMinorVersion != 0)
3764             return NULL;
3765
3766         for (i=0; i< ttOffsetTable.uNumOfTables; i++)
3767         {
3768             ReadFile(handle,&tblDir, sizeof(TT_TABLE_DIRECTORY),NULL,NULL);
3769             if (strncmp(tblDir.szTag,"name",4)==0)
3770             {
3771                 bFound = TRUE;
3772                 tblDir.uLength = SWAPLONG(tblDir.uLength);
3773                 tblDir.uOffset = SWAPLONG(tblDir.uOffset);
3774                 break;
3775             }
3776         }
3777
3778         if (bFound)
3779         {
3780             TT_NAME_TABLE_HEADER ttNTHeader;
3781             TT_NAME_RECORD ttRecord;
3782
3783             SetFilePointer(handle, tblDir.uOffset, NULL, FILE_BEGIN);
3784             ReadFile(handle,&ttNTHeader, sizeof(TT_NAME_TABLE_HEADER),
3785                             NULL,NULL);
3786
3787             ttNTHeader.uNRCount = SWAPWORD(ttNTHeader.uNRCount);
3788             ttNTHeader.uStorageOffset = SWAPWORD(ttNTHeader.uStorageOffset);
3789             bFound = FALSE;
3790             for(i=0; i<ttNTHeader.uNRCount; i++)
3791             {
3792                 ReadFile(handle,&ttRecord, sizeof(TT_NAME_RECORD),NULL,NULL);
3793                 ttRecord.uNameID = SWAPWORD(ttRecord.uNameID);
3794                 /* 4 is the Full Font Name */
3795                 if(ttRecord.uNameID == 4)
3796                 {
3797                     int nPos;
3798                     LPSTR buf;
3799                     static LPCSTR tt = " (TrueType)";
3800
3801                     ttRecord.uStringLength = SWAPWORD(ttRecord.uStringLength);
3802                     ttRecord.uStringOffset = SWAPWORD(ttRecord.uStringOffset);
3803                     nPos = SetFilePointer(handle, 0, NULL, FILE_CURRENT);
3804                     SetFilePointer(handle, tblDir.uOffset + 
3805                                     ttRecord.uStringOffset + 
3806                                     ttNTHeader.uStorageOffset,
3807                                     NULL, FILE_BEGIN);
3808                     buf = HeapAlloc(GetProcessHeap(), 0, 
3809                                     ttRecord.uStringLength + 1 + strlen(tt));
3810                     memset(buf, 0, ttRecord.uStringLength + 1 + strlen(tt));
3811                     ReadFile(handle, buf, ttRecord.uStringLength, NULL, NULL);
3812                     if (strlen(buf) > 0)
3813                     {
3814                         strcat(buf,tt);
3815                         ret = strdupAtoW(buf);
3816                         HeapFree(GetProcessHeap(),0,buf);
3817                         break;
3818                     }
3819
3820                     HeapFree(GetProcessHeap(),0,buf);
3821                     SetFilePointer(handle,nPos, NULL, FILE_BEGIN);
3822                 }
3823             }
3824         }
3825         CloseHandle(handle);
3826     }
3827     else
3828         ERR("Unable to open font file %s\n", debugstr_w(filename));
3829
3830     TRACE("Returning fontname %s\n",debugstr_w(ret));
3831     return ret;
3832 }
3833
3834 static UINT ITERATE_RegisterFonts(MSIRECORD *row, LPVOID param)
3835 {
3836     MSIPACKAGE *package = (MSIPACKAGE*)param;
3837     LPWSTR name;
3838     LPCWSTR filename;
3839     MSIFILE *file;
3840     static const WCHAR regfont1[] =
3841         {'S','o','f','t','w','a','r','e','\\',
3842          'M','i','c','r','o','s','o','f','t','\\',
3843          'W','i','n','d','o','w','s',' ','N','T','\\',
3844          'C','u','r','r','e','n','t','V','e','r','s','i','o','n','\\',
3845          'F','o','n','t','s',0};
3846     static const WCHAR regfont2[] =
3847         {'S','o','f','t','w','a','r','e','\\',
3848          'M','i','c','r','o','s','o','f','t','\\',
3849          'W','i','n','d','o','w','s','\\',
3850          'C','u','r','r','e','n','t','V','e','r','s','i','o','n','\\',
3851          'F','o','n','t','s',0};
3852     HKEY hkey1;
3853     HKEY hkey2;
3854
3855     filename = MSI_RecordGetString( row, 1 );
3856     file = get_loaded_file( package, filename );
3857     if (!file)
3858     {
3859         ERR("Unable to load file\n");
3860         return ERROR_SUCCESS;
3861     }
3862
3863     /* check to make sure that component is installed */
3864     if (!ACTION_VerifyComponentForAction(package, 
3865                 file->Component, INSTALLSTATE_LOCAL))
3866     {
3867         TRACE("Skipping: Component not scheduled for install\n");
3868         return ERROR_SUCCESS;
3869     }
3870
3871     RegCreateKeyW(HKEY_LOCAL_MACHINE,regfont1,&hkey1);
3872     RegCreateKeyW(HKEY_LOCAL_MACHINE,regfont2,&hkey2);
3873
3874     if (MSI_RecordIsNull(row,2))
3875         name = load_ttfname_from( file->TargetPath );
3876     else
3877         name = load_dynamic_stringW(row,2);
3878
3879     if (name)
3880     {
3881         msi_reg_set_val_str( hkey1, name, file->FileName );
3882         msi_reg_set_val_str( hkey2, name, file->FileName );
3883     }
3884
3885     HeapFree(GetProcessHeap(),0,name);
3886     RegCloseKey(hkey1);
3887     RegCloseKey(hkey2);
3888     return ERROR_SUCCESS;
3889 }
3890
3891 static UINT ACTION_RegisterFonts(MSIPACKAGE *package)
3892 {
3893     UINT rc;
3894     MSIQUERY * view;
3895     static const WCHAR ExecSeqQuery[] =
3896         {'S','E','L','E','C','T',' ','*',' ','F','R','O','M',' ',
3897          '`','F','o','n','t','`',0};
3898
3899     rc = MSI_DatabaseOpenViewW(package->db, ExecSeqQuery, &view);
3900     if (rc != ERROR_SUCCESS)
3901     {
3902         TRACE("MSI_DatabaseOpenViewW failed: %d\n", rc);
3903         return ERROR_SUCCESS;
3904     }
3905
3906     MSI_IterateRecords(view, NULL, ITERATE_RegisterFonts, package);
3907     msiobj_release(&view->hdr);
3908
3909     return ERROR_SUCCESS;
3910 }
3911
3912 static UINT ITERATE_PublishComponent(MSIRECORD *rec, LPVOID param)
3913 {
3914     MSIPACKAGE *package = (MSIPACKAGE*)param;
3915     LPCWSTR compgroupid=NULL;
3916     LPCWSTR feature=NULL;
3917     LPCWSTR text = NULL;
3918     LPCWSTR qualifier = NULL;
3919     LPCWSTR component = NULL;
3920     LPWSTR advertise = NULL;
3921     LPWSTR output = NULL;
3922     HKEY hkey;
3923     UINT rc = ERROR_SUCCESS;
3924     MSICOMPONENT *comp;
3925     DWORD sz = 0;
3926
3927     component = MSI_RecordGetString(rec,3);
3928     comp = get_loaded_component(package,component);
3929
3930     if (!ACTION_VerifyComponentForAction(package, comp, INSTALLSTATE_LOCAL) && 
3931        !ACTION_VerifyComponentForAction(package, comp, INSTALLSTATE_SOURCE) &&
3932        !ACTION_VerifyComponentForAction(package, comp, INSTALLSTATE_ADVERTISED))
3933     {
3934         TRACE("Skipping: Component %s not scheduled for install\n",
3935                         debugstr_w(component));
3936
3937         return ERROR_SUCCESS;
3938     }
3939
3940     compgroupid = MSI_RecordGetString(rec,1);
3941
3942     rc = MSIREG_OpenUserComponentsKey(compgroupid, &hkey, TRUE);
3943     if (rc != ERROR_SUCCESS)
3944         goto end;
3945     
3946     text = MSI_RecordGetString(rec,4);
3947     qualifier = MSI_RecordGetString(rec,2);
3948     feature = MSI_RecordGetString(rec,5);
3949   
3950     advertise = create_component_advertise_string(package, comp, feature);
3951
3952     sz = strlenW(advertise);
3953
3954     if (text)
3955         sz += lstrlenW(text);
3956
3957     sz+=3;
3958     sz *= sizeof(WCHAR);
3959            
3960     output = HeapAlloc(GetProcessHeap(),0,sz);
3961     memset(output,0,sz);
3962     strcpyW(output,advertise);
3963
3964     if (text)
3965         strcatW(output,text);
3966
3967     msi_reg_set_val_multi_str( hkey, qualifier, output );
3968     
3969 end:
3970     RegCloseKey(hkey);
3971     HeapFree(GetProcessHeap(),0,output);
3972     
3973     return rc;
3974 }
3975
3976 /*
3977  * At present I am ignorning the advertised components part of this and only
3978  * focusing on the qualified component sets
3979  */
3980 static UINT ACTION_PublishComponents(MSIPACKAGE *package)
3981 {
3982     UINT rc;
3983     MSIQUERY * view;
3984     static const WCHAR ExecSeqQuery[] =
3985         {'S','E','L','E','C','T',' ','*',' ','F','R','O','M',' ',
3986          '`','P','u','b','l','i','s','h',
3987          'C','o','m','p','o','n','e','n','t','`',0};
3988     
3989     rc = MSI_DatabaseOpenViewW(package->db, ExecSeqQuery, &view);
3990     if (rc != ERROR_SUCCESS)
3991         return ERROR_SUCCESS;
3992
3993     rc = MSI_IterateRecords(view, NULL, ITERATE_PublishComponent, package);
3994     msiobj_release(&view->hdr);
3995
3996     return rc;
3997 }