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