comctl32: A couple fixes for tab icon offsets.
[wine] / dlls / msi / package.c
1 /*
2  * Implementation of the Microsoft Installer (msi.dll)
3  *
4  * Copyright 2004 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 #define NONAMELESSUNION
22 #define NONAMELESSSTRUCT
23
24 #include <stdarg.h>
25 #include <stdio.h>
26 #include "windef.h"
27 #include "winbase.h"
28 #include "winreg.h"
29 #include "winnls.h"
30 #include "shlwapi.h"
31 #include "wingdi.h"
32 #include "wine/debug.h"
33 #include "msi.h"
34 #include "msiquery.h"
35 #include "objidl.h"
36 #include "wincrypt.h"
37 #include "winuser.h"
38 #include "shlobj.h"
39 #include "wine/unicode.h"
40 #include "objbase.h"
41
42 #include "msipriv.h"
43 #include "action.h"
44
45 WINE_DEFAULT_DEBUG_CHANNEL(msi);
46
47 static void MSI_FreePackage( MSIOBJECTHDR *arg)
48 {
49     MSIPACKAGE *package= (MSIPACKAGE*) arg;
50
51     if( package->dialog )
52         msi_dialog_destroy( package->dialog );
53     ACTION_free_package_structures(package);
54
55     msiobj_release( &package->db->hdr );
56 }
57
58 static UINT clone_properties(MSIDATABASE *db)
59 {
60     MSIQUERY * view = NULL;
61     UINT rc;
62     static const WCHAR CreateSql[] = {
63        'C','R','E','A','T','E',' ','T','A','B','L','E',' ','`','_','P','r','o',
64        'p','e','r','t','y','`',' ','(',' ','`','_','P','r','o','p','e','r','t',
65        'y','`',' ','C','H','A','R','(','5','6',')',' ','N','O','T',' ','N','U',
66        'L','L',',',' ','`','V','a','l','u','e','`',' ','C','H','A','R','(','9',
67        '8',')',' ','N','O','T',' ','N','U','L','L',' ','P','R','I','M','A','R',
68        'Y',' ','K','E','Y',' ','`','_','P','r','o','p','e','r','t','y','`',')',0};
69     static const WCHAR Query[] = {
70        'S','E','L','E','C','T',' ','*',' ',
71        'F','R','O','M',' ','`','P','r','o','p','e','r','t','y','`',0};
72     static const WCHAR Insert[] = {
73        'I','N','S','E','R','T',' ','i','n','t','o',' ',
74        '`','_','P','r','o','p','e','r','t','y','`',' ',
75        '(','`','_','P','r','o','p','e','r','t','y','`',',',
76        '`','V','a','l','u','e','`',')',' ',
77        'V','A','L','U','E','S',' ','(','?',',','?',')',0};
78
79     /* create the temporary properties table */
80     rc = MSI_DatabaseOpenViewW(db, CreateSql, &view);
81     if (rc != ERROR_SUCCESS)
82         return rc;
83     rc = MSI_ViewExecute(view,0);   
84     MSI_ViewClose(view);
85     msiobj_release(&view->hdr); 
86     if (rc != ERROR_SUCCESS)
87         return rc;
88
89     /* clone the existing properties */
90     rc = MSI_DatabaseOpenViewW(db, Query, &view);
91     if (rc != ERROR_SUCCESS)
92         return rc;
93
94     rc = MSI_ViewExecute(view, 0);
95     if (rc != ERROR_SUCCESS)
96     {
97         MSI_ViewClose(view);
98         msiobj_release(&view->hdr); 
99         return rc;
100     }
101     while (1)
102     {
103         MSIRECORD * row;
104         MSIQUERY * view2;
105
106         rc = MSI_ViewFetch(view,&row);
107         if (rc != ERROR_SUCCESS)
108             break;
109
110         rc = MSI_DatabaseOpenViewW(db,Insert,&view2);  
111         if (rc!= ERROR_SUCCESS)
112             continue;
113         rc = MSI_ViewExecute(view2,row);
114         MSI_ViewClose(view2);
115         msiobj_release(&view2->hdr);
116  
117         if (rc == ERROR_SUCCESS) 
118             msiobj_release(&row->hdr); 
119     }
120     MSI_ViewClose(view);
121     msiobj_release(&view->hdr);
122     
123     return rc;
124 }
125
126 /*
127  * set_installed_prop
128  *
129  * Sets the "Installed" property to indicate that
130  *  the product is installed for the current user.
131  */
132 static UINT set_installed_prop( MSIPACKAGE *package )
133 {
134     static const WCHAR szInstalled[] = {
135         'I','n','s','t','a','l','l','e','d',0 };
136     WCHAR val[2] = { '1', 0 };
137     HKEY hkey = 0;
138     UINT r;
139
140     r = MSIREG_OpenUninstallKey( package->ProductCode, &hkey, FALSE );
141     if (r == ERROR_SUCCESS)
142     {
143         RegCloseKey( hkey );
144         MSI_SetPropertyW( package, szInstalled, val );
145     }
146
147     return r;
148 }
149
150 /*
151  * There are a whole slew of these we need to set
152  *
153  *
154 http://msdn.microsoft.com/library/default.asp?url=/library/en-us/msi/setup/properties.asp
155  */
156 static VOID set_installer_properties(MSIPACKAGE *package)
157 {
158     WCHAR pth[MAX_PATH];
159     WCHAR *ptr;
160     OSVERSIONINFOA OSVersion;
161     MEMORYSTATUSEX msex;
162     DWORD verval;
163     WCHAR verstr[10], bufstr[20];
164     HDC dc;
165
166     static const WCHAR cszbs[]={'\\',0};
167     static const WCHAR CFF[] = 
168 {'C','o','m','m','o','n','F','i','l','e','s','F','o','l','d','e','r',0};
169     static const WCHAR PFF[] = 
170 {'P','r','o','g','r','a','m','F','i','l','e','s','F','o','l','d','e','r',0};
171     static const WCHAR CADF[] = 
172 {'C','o','m','m','o','n','A','p','p','D','a','t','a','F','o','l','d','e','r',0};
173     static const WCHAR FaF[] = 
174 {'F','a','v','o','r','i','t','e','s','F','o','l','d','e','r',0};
175     static const WCHAR FoF[] = 
176 {'F','o','n','t','s','F','o','l','d','e','r',0};
177     static const WCHAR SendTF[] = 
178 {'S','e','n','d','T','o','F','o','l','d','e','r',0};
179     static const WCHAR SMF[] = 
180 {'S','t','a','r','t','M','e','n','u','F','o','l','d','e','r',0};
181     static const WCHAR StF[] = 
182 {'S','t','a','r','t','u','p','F','o','l','d','e','r',0};
183     static const WCHAR TemplF[] = 
184 {'T','e','m','p','l','a','t','e','F','o','l','d','e','r',0};
185     static const WCHAR DF[] = 
186 {'D','e','s','k','t','o','p','F','o','l','d','e','r',0};
187     static const WCHAR PMF[] = 
188 {'P','r','o','g','r','a','m','M','e','n','u','F','o','l','d','e','r',0};
189     static const WCHAR ATF[] = 
190 {'A','d','m','i','n','T','o','o','l','s','F','o','l','d','e','r',0};
191     static const WCHAR ADF[] = 
192 {'A','p','p','D','a','t','a','F','o','l','d','e','r',0};
193     static const WCHAR SF[] = 
194 {'S','y','s','t','e','m','F','o','l','d','e','r',0};
195     static const WCHAR SF16[] = 
196 {'S','y','s','t','e','m','1','6','F','o','l','d','e','r',0};
197     static const WCHAR LADF[] = 
198 {'L','o','c','a','l','A','p','p','D','a','t','a','F','o','l','d','e','r',0};
199     static const WCHAR MPF[] = 
200 {'M','y','P','i','c','t','u','r','e','s','F','o','l','d','e','r',0};
201     static const WCHAR PF[] = 
202 {'P','e','r','s','o','n','a','l','F','o','l','d','e','r',0};
203     static const WCHAR WF[] = 
204 {'W','i','n','d','o','w','s','F','o','l','d','e','r',0};
205     static const WCHAR WV[] = 
206 {'W','i','n','d','o','w','s','V','o','l','u','m','e',0};
207     static const WCHAR TF[]=
208 {'T','e','m','p','F','o','l','d','e','r',0};
209     static const WCHAR szAdminUser[] =
210 {'A','d','m','i','n','U','s','e','r',0};
211     static const WCHAR szPriv[] =
212 {'P','r','i','v','i','l','e','g','e','d',0};
213     static const WCHAR szOne[] =
214 {'1',0};
215     static const WCHAR v9x[] = { 'V','e','r','s','i','o','n','9','X',0 };
216     static const WCHAR vNT[] = { 'V','e','r','s','i','o','n','N','T',0 };
217     static const WCHAR szFormat[] = {'%','l','i',0};
218     static const WCHAR szWinBuild[] =
219 {'W','i','n','d','o','w','s','B','u','i','l','d', 0 };
220     static const WCHAR szSPL[] = 
221 {'S','e','r','v','i','c','e','P','a','c','k','L','e','v','e','l',0 };
222     static const WCHAR szSix[] = {'6',0 };
223
224     static const WCHAR szVersionMsi[] = { 'V','e','r','s','i','o','n','M','s','i',0 };
225     static const WCHAR szPhysicalMemory[] = { 'P','h','y','s','i','c','a','l','M','e','m','o','r','y',0 };
226     static const WCHAR szFormat2[] = {'%','l','i','.','%','l','i',0};
227 /* Screen properties */
228     static const WCHAR szScreenX[] = {'S','c','r','e','e','n','X',0};
229     static const WCHAR szScreenY[] = {'S','c','r','e','e','n','Y',0};
230     static const WCHAR szColorBits[] = {'C','o','l','o','r','B','i','t','s',0};
231     static const WCHAR szScreenFormat[] = {'%','d',0};
232     static const WCHAR szIntel[] = { 'I','n','t','e','l',0 };
233     SYSTEM_INFO sys_info;
234
235     /*
236      * Other things that probably should be set:
237      *
238      * SystemLanguageID ComputerName UserLanguageID LogonUser VirtualMemory
239      * Intel ShellAdvSupport DefaultUIFont VersionDatabase PackagecodeChanging
240      * ProductState CaptionHeight BorderTop BorderSide TextHeight
241      * RedirectedDllSupport Time Date Privileged
242      */
243
244     SHGetFolderPathW(NULL,CSIDL_PROGRAM_FILES_COMMON,NULL,0,pth);
245     strcatW(pth,cszbs);
246     MSI_SetPropertyW(package, CFF, pth);
247
248     SHGetFolderPathW(NULL,CSIDL_PROGRAM_FILES,NULL,0,pth);
249     strcatW(pth,cszbs);
250     MSI_SetPropertyW(package, PFF, pth);
251
252     SHGetFolderPathW(NULL,CSIDL_COMMON_APPDATA,NULL,0,pth);
253     strcatW(pth,cszbs);
254     MSI_SetPropertyW(package, CADF, pth);
255
256     SHGetFolderPathW(NULL,CSIDL_FAVORITES,NULL,0,pth);
257     strcatW(pth,cszbs);
258     MSI_SetPropertyW(package, FaF, pth);
259
260     SHGetFolderPathW(NULL,CSIDL_FONTS,NULL,0,pth);
261     strcatW(pth,cszbs);
262     MSI_SetPropertyW(package, FoF, pth);
263
264     SHGetFolderPathW(NULL,CSIDL_SENDTO,NULL,0,pth);
265     strcatW(pth,cszbs);
266     MSI_SetPropertyW(package, SendTF, pth);
267
268     SHGetFolderPathW(NULL,CSIDL_STARTMENU,NULL,0,pth);
269     strcatW(pth,cszbs);
270     MSI_SetPropertyW(package, SMF, pth);
271
272     SHGetFolderPathW(NULL,CSIDL_STARTUP,NULL,0,pth);
273     strcatW(pth,cszbs);
274     MSI_SetPropertyW(package, StF, pth);
275
276     SHGetFolderPathW(NULL,CSIDL_TEMPLATES,NULL,0,pth);
277     strcatW(pth,cszbs);
278     MSI_SetPropertyW(package, TemplF, pth);
279
280     SHGetFolderPathW(NULL,CSIDL_DESKTOP,NULL,0,pth);
281     strcatW(pth,cszbs);
282     MSI_SetPropertyW(package, DF, pth);
283
284     SHGetFolderPathW(NULL,CSIDL_PROGRAMS,NULL,0,pth);
285     strcatW(pth,cszbs);
286     MSI_SetPropertyW(package, PMF, pth);
287
288     SHGetFolderPathW(NULL,CSIDL_ADMINTOOLS,NULL,0,pth);
289     strcatW(pth,cszbs);
290     MSI_SetPropertyW(package, ATF, pth);
291
292     SHGetFolderPathW(NULL,CSIDL_APPDATA,NULL,0,pth);
293     strcatW(pth,cszbs);
294     MSI_SetPropertyW(package, ADF, pth);
295
296     SHGetFolderPathW(NULL,CSIDL_SYSTEM,NULL,0,pth);
297     strcatW(pth,cszbs);
298     MSI_SetPropertyW(package, SF, pth);
299     MSI_SetPropertyW(package, SF16, pth);
300
301     SHGetFolderPathW(NULL,CSIDL_LOCAL_APPDATA,NULL,0,pth);
302     strcatW(pth,cszbs);
303     MSI_SetPropertyW(package, LADF, pth);
304
305     SHGetFolderPathW(NULL,CSIDL_MYPICTURES,NULL,0,pth);
306     strcatW(pth,cszbs);
307     MSI_SetPropertyW(package, MPF, pth);
308
309     SHGetFolderPathW(NULL,CSIDL_PERSONAL,NULL,0,pth);
310     strcatW(pth,cszbs);
311     MSI_SetPropertyW(package, PF, pth);
312
313     SHGetFolderPathW(NULL,CSIDL_WINDOWS,NULL,0,pth);
314     strcatW(pth,cszbs);
315     MSI_SetPropertyW(package, WF, pth);
316     
317     /* Physical Memory is specified in MB. Using total amount. */
318     msex.dwLength = sizeof(msex);
319     GlobalMemoryStatusEx( &msex );
320     sprintfW( bufstr, szScreenFormat, (int)(msex.ullTotalPhys/1024/1024));
321     MSI_SetPropertyW(package, szPhysicalMemory, bufstr);
322
323     SHGetFolderPathW(NULL,CSIDL_WINDOWS,NULL,0,pth);
324     ptr = strchrW(pth,'\\');
325     if (ptr)
326         *(ptr+1) = 0;
327     MSI_SetPropertyW(package, WV, pth);
328     
329     GetTempPathW(MAX_PATH,pth);
330     MSI_SetPropertyW(package, TF, pth);
331
332
333     /* in a wine environment the user is always admin and privileged */
334     MSI_SetPropertyW(package,szAdminUser,szOne);
335     MSI_SetPropertyW(package,szPriv,szOne);
336
337     /* set the os things */
338     OSVersion.dwOSVersionInfoSize = sizeof(OSVERSIONINFOA);
339     GetVersionExA(&OSVersion);
340     verval = OSVersion.dwMinorVersion+OSVersion.dwMajorVersion*100;
341     sprintfW(verstr,szFormat,verval);
342     switch (OSVersion.dwPlatformId)
343     {
344         case VER_PLATFORM_WIN32_WINDOWS:    
345             MSI_SetPropertyW(package,v9x,verstr);
346             break;
347         case VER_PLATFORM_WIN32_NT:
348             MSI_SetPropertyW(package,vNT,verstr);
349             break;
350     }
351     sprintfW(verstr,szFormat,OSVersion.dwBuildNumber);
352     MSI_SetPropertyW(package,szWinBuild,verstr);
353     /* just fudge this */
354     MSI_SetPropertyW(package,szSPL,szSix);
355
356     sprintfW( bufstr, szFormat2, MSI_MAJORVERSION, MSI_MINORVERSION);
357     MSI_SetPropertyW( package, szVersionMsi, bufstr );
358
359     GetSystemInfo( &sys_info );
360     if (sys_info.u.s.wProcessorArchitecture == PROCESSOR_ARCHITECTURE_INTEL)
361     {
362         sprintfW( bufstr, szScreenFormat, sys_info.wProcessorLevel );
363         MSI_SetPropertyW( package, szIntel, bufstr );
364     }
365
366     /* Screen properties. */
367     dc = GetDC(0);
368     sprintfW( bufstr, szScreenFormat, GetDeviceCaps( dc, HORZRES ) );
369     MSI_SetPropertyW( package, szScreenX, bufstr );
370     sprintfW( bufstr, szScreenFormat, GetDeviceCaps( dc, VERTRES ));
371     MSI_SetPropertyW( package, szScreenY, bufstr );
372     sprintfW( bufstr, szScreenFormat, GetDeviceCaps( dc, BITSPIXEL ));
373     MSI_SetPropertyW( package, szColorBits, bufstr );
374     ReleaseDC(0, dc);
375 }
376
377 MSIPACKAGE *MSI_CreatePackage( MSIDATABASE *db )
378 {
379     static const WCHAR szLevel[] = { 'U','I','L','e','v','e','l',0 };
380     static const WCHAR szpi[] = {'%','i',0};
381     static const WCHAR szProductCode[] = {
382         'P','r','o','d','u','c','t','C','o','d','e',0};
383     MSIPACKAGE *package = NULL;
384     WCHAR uilevel[10];
385
386     TRACE("%p\n", db);
387
388     package = alloc_msiobject( MSIHANDLETYPE_PACKAGE, sizeof (MSIPACKAGE),
389                                MSI_FreePackage );
390     if( package )
391     {
392         msiobj_addref( &db->hdr );
393
394         package->db = db;
395         list_init( &package->components );
396         list_init( &package->features );
397         list_init( &package->files );
398         list_init( &package->tempfiles );
399         list_init( &package->folders );
400         package->ActionFormat = NULL;
401         package->LastAction = NULL;
402         package->dialog = NULL;
403         package->next_dialog = NULL;
404         list_init( &package->subscriptions );
405         list_init( &package->appids );
406         list_init( &package->classes );
407         list_init( &package->mimes );
408         list_init( &package->extensions );
409         list_init( &package->progids );
410         list_init( &package->RunningActions );
411
412         /* OK, here is where we do a slew of things to the database to 
413          * prep for all that is to come as a package */
414
415         clone_properties(db);
416         set_installer_properties(package);
417         sprintfW(uilevel,szpi,gUILevel);
418         MSI_SetPropertyW(package, szLevel, uilevel);
419
420         package->ProductCode = msi_dup_property( package, szProductCode );
421         set_installed_prop( package );
422     }
423
424     return package;
425 }
426
427 /*
428  * copy_package_to_temp   [internal]
429  *
430  * copy the msi file to a temp file to prevent locking a CD
431  * with a multi disc install 
432  *
433  * FIXME: I think this is wrong, and instead of copying the package,
434  *        we should read all the tables to memory, then open the
435  *        database to read binary streams on demand.
436  */ 
437 static LPCWSTR copy_package_to_temp( LPCWSTR szPackage, LPWSTR filename )
438 {
439     WCHAR path[MAX_PATH];
440     static const WCHAR szMSI[] = {'M','S','I',0};
441
442     GetTempPathW( MAX_PATH, path );
443     GetTempFileNameW( path, szMSI, 0, filename );
444
445     if( !CopyFileW( szPackage, filename, FALSE ) )
446     {
447         ERR("failed to copy package to temp path %s\n", debugstr_w(filename) );
448         return szPackage;
449     }
450
451     TRACE("Opening relocated package %s\n", debugstr_w( filename ));
452     return filename;
453 }
454
455 UINT MSI_OpenPackageW(LPCWSTR szPackage, MSIPACKAGE **pPackage)
456 {
457     MSIDATABASE *db = NULL;
458     MSIPACKAGE *package;
459     MSIHANDLE handle;
460     UINT r;
461
462     TRACE("%s %p\n", debugstr_w(szPackage), pPackage);
463
464     if( szPackage[0] == '#' )
465     {
466         handle = atoiW(&szPackage[1]);
467         db = msihandle2msiinfo( handle, MSIHANDLETYPE_DATABASE );
468         if( !db )
469             return ERROR_INVALID_HANDLE;
470     }
471     else
472     {
473         WCHAR temppath[MAX_PATH];
474         LPCWSTR file = copy_package_to_temp( szPackage, temppath );
475
476         r = MSI_OpenDatabaseW( file, MSIDBOPEN_READONLY, &db );
477
478         if (file != szPackage)
479             DeleteFileW( file );
480
481         if( r != ERROR_SUCCESS )
482             return r;
483     }
484
485     package = MSI_CreatePackage( db );
486     msiobj_release( &db->hdr );
487     if( !package )
488         return ERROR_FUNCTION_FAILED;
489
490     /* 
491      * FIXME:  I don't think this is right.  Maybe we should be storing the
492      * name of the database in the MSIDATABASE structure and fetching this
493      * info from there, or maybe this is only relevant to cached databases.
494      */
495     if( szPackage[0] != '#' )
496     {
497         static const WCHAR OriginalDatabase[] =
498           {'O','r','i','g','i','n','a','l','D','a','t','a','b','a','s','e',0};
499         static const WCHAR Database[] = {'D','A','T','A','B','A','S','E',0};
500
501         MSI_SetPropertyW( package, OriginalDatabase, szPackage );
502         MSI_SetPropertyW( package, Database, szPackage );
503     }
504
505     *pPackage = package;
506
507     return ERROR_SUCCESS;
508 }
509
510 UINT WINAPI MsiOpenPackageExW(LPCWSTR szPackage, DWORD dwOptions, MSIHANDLE *phPackage)
511 {
512     MSIPACKAGE *package = NULL;
513     UINT ret;
514
515     TRACE("%s %08lx %p\n", debugstr_w(szPackage), dwOptions, phPackage );
516
517     if( dwOptions )
518         FIXME("dwOptions %08lx not supported\n", dwOptions);
519
520     ret = MSI_OpenPackageW( szPackage, &package );
521     if( ret == ERROR_SUCCESS )
522     {
523         *phPackage = alloc_msihandle( &package->hdr );
524         msiobj_release( &package->hdr );
525     }
526
527     return ret;
528 }
529
530 UINT WINAPI MsiOpenPackageW(LPCWSTR szPackage, MSIHANDLE *phPackage)
531 {
532     return MsiOpenPackageExW( szPackage, 0, phPackage );
533 }
534
535 UINT WINAPI MsiOpenPackageExA(LPCSTR szPackage, DWORD dwOptions, MSIHANDLE *phPackage)
536 {
537     LPWSTR szwPack = NULL;
538     UINT ret;
539
540     if( szPackage )
541     {
542         szwPack = strdupAtoW( szPackage );
543         if( !szwPack )
544             return ERROR_OUTOFMEMORY;
545     }
546
547     ret = MsiOpenPackageExW( szwPack, dwOptions, phPackage );
548
549     msi_free( szwPack );
550
551     return ret;
552 }
553
554 UINT WINAPI MsiOpenPackageA(LPCSTR szPackage, MSIHANDLE *phPackage)
555 {
556     return MsiOpenPackageExA( szPackage, 0, phPackage );
557 }
558
559 MSIHANDLE WINAPI MsiGetActiveDatabase(MSIHANDLE hInstall)
560 {
561     MSIPACKAGE *package;
562     MSIHANDLE handle = 0;
563
564     TRACE("(%ld)\n",hInstall);
565
566     package = msihandle2msiinfo( hInstall, MSIHANDLETYPE_PACKAGE);
567     if( package)
568     {
569         handle = alloc_msihandle( &package->db->hdr );
570         msiobj_release( &package->hdr );
571     }
572
573     return handle;
574 }
575
576 INT MSI_ProcessMessage( MSIPACKAGE *package, INSTALLMESSAGE eMessageType,
577                                MSIRECORD *record)
578 {
579     static const WCHAR szActionData[] =
580         {'A','c','t','i','o','n','D','a','t','a',0};
581     static const WCHAR szSetProgress[] =
582         {'S','e','t','P','r','o','g','r','e','s','s',0};
583     static const WCHAR szActionText[] =
584         {'A','c','t','i','o','n','T','e','x','t',0};
585     DWORD log_type = 0;
586     LPWSTR message;
587     DWORD sz;
588     DWORD total_size = 0;
589     INT i;
590     INT rc;
591     char *msg;
592     int len;
593
594     TRACE("%x\n", eMessageType);
595     rc = 0;
596
597     if ((eMessageType & 0xff000000) == INSTALLMESSAGE_ERROR)
598         log_type |= INSTALLLOGMODE_ERROR;
599     if ((eMessageType & 0xff000000) == INSTALLMESSAGE_WARNING)
600         log_type |= INSTALLLOGMODE_WARNING;
601     if ((eMessageType & 0xff000000) == INSTALLMESSAGE_USER)
602         log_type |= INSTALLLOGMODE_USER;
603     if ((eMessageType & 0xff000000) == INSTALLMESSAGE_INFO)
604         log_type |= INSTALLLOGMODE_INFO;
605     if ((eMessageType & 0xff000000) == INSTALLMESSAGE_COMMONDATA)
606         log_type |= INSTALLLOGMODE_COMMONDATA;
607     if ((eMessageType & 0xff000000) == INSTALLMESSAGE_ACTIONSTART)
608         log_type |= INSTALLLOGMODE_ACTIONSTART;
609     if ((eMessageType & 0xff000000) == INSTALLMESSAGE_ACTIONDATA)
610         log_type |= INSTALLLOGMODE_ACTIONDATA;
611     /* just a guess */
612     if ((eMessageType & 0xff000000) == INSTALLMESSAGE_PROGRESS)
613         log_type |= 0x800;
614
615     if ((eMessageType & 0xff000000) == INSTALLMESSAGE_ACTIONSTART)
616     {
617         static const WCHAR template_s[]=
618             {'A','c','t','i','o','n',' ','%','s',':',' ','%','s','.',' ',0};
619         static const WCHAR format[] = 
620             {'H','H','\'',':','\'','m','m','\'',':','\'','s','s',0};
621         WCHAR timet[0x100];
622         LPCWSTR action_text, action;
623         LPWSTR deformatted = NULL;
624
625         GetTimeFormatW(LOCALE_USER_DEFAULT, 0, NULL, format, timet, 0x100);
626
627         action = MSI_RecordGetString(record, 1);
628         action_text = MSI_RecordGetString(record, 2);
629         deformat_string(package, action_text, &deformatted);
630
631         len = strlenW(timet) + strlenW(action) + strlenW(template_s);
632         if (deformatted)
633             len += strlenW(deformatted);
634         message = msi_alloc(len*sizeof(WCHAR));
635         sprintfW(message, template_s, timet, action);
636         if (deformatted)
637             strcatW(message, deformatted);
638         msi_free(deformatted);
639     }
640     else
641     {
642         INT msg_field=1;
643         message = msi_alloc(1*sizeof (WCHAR));
644         message[0]=0;
645         msg_field = MSI_RecordGetFieldCount(record);
646         for (i = 1; i <= msg_field; i++)
647         {
648             LPWSTR tmp;
649             WCHAR number[3];
650             static const WCHAR format[] = { '%','i',':',' ',0};
651             static const WCHAR space[] = { ' ',0};
652             sz = 0;
653             MSI_RecordGetStringW(record,i,NULL,&sz);
654             sz+=4;
655             total_size+=sz*sizeof(WCHAR);
656             tmp = msi_alloc(sz*sizeof(WCHAR));
657             message = msi_realloc(message,total_size*sizeof (WCHAR));
658
659             MSI_RecordGetStringW(record,i,tmp,&sz);
660
661             if (msg_field > 1)
662             {
663                 sprintfW(number,format,i);
664                 strcatW(message,number);
665             }
666             strcatW(message,tmp);
667             if (msg_field > 1)
668                 strcatW(message,space);
669
670             msi_free(tmp);
671         }
672     }
673
674     TRACE("(%p %lx %lx %s)\n",gUIHandlerA, gUIFilter, log_type,
675                              debugstr_w(message));
676
677     /* convert it to ASCII */
678     len = WideCharToMultiByte( CP_ACP, 0, message, -1,
679                                NULL, 0, NULL, NULL );
680     msg = msi_alloc( len );
681     WideCharToMultiByte( CP_ACP, 0, message, -1,
682                          msg, len, NULL, NULL );
683
684     if (gUIHandlerA && (gUIFilter & log_type))
685     {
686         rc = gUIHandlerA(gUIContext,eMessageType,msg);
687     }
688
689     if ((!rc) && (gszLogFile[0]) && !((eMessageType & 0xff000000) ==
690                                       INSTALLMESSAGE_PROGRESS))
691     {
692         DWORD write;
693         HANDLE log_file = CreateFileW(gszLogFile,GENERIC_WRITE, 0, NULL,
694                                   OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
695
696         if (log_file != INVALID_HANDLE_VALUE)
697         {
698             SetFilePointer(log_file,0, NULL, FILE_END);
699             WriteFile(log_file,msg,strlen(msg),&write,NULL);
700             WriteFile(log_file,"\n",1,&write,NULL);
701             CloseHandle(log_file);
702         }
703     }
704     msi_free( msg );
705
706     msi_free( message);
707
708     switch (eMessageType & 0xff000000)
709     {
710     case INSTALLMESSAGE_ACTIONDATA:
711         /* FIXME: format record here instead of in ui_actiondata to get the
712          * correct action data for external scripts */
713         ControlEvent_FireSubscribedEvent(package, szActionData, record);
714         break;
715     case INSTALLMESSAGE_ACTIONSTART:
716     {
717         MSIRECORD *uirow;
718         LPWSTR deformated;
719         LPCWSTR action_text = MSI_RecordGetString(record, 2);
720
721         deformat_string(package, action_text, &deformated);
722         uirow = MSI_CreateRecord(1);
723         MSI_RecordSetStringW(uirow, 1, deformated);
724         TRACE("INSTALLMESSAGE_ACTIONSTART: %s\n", debugstr_w(deformated));
725         msi_free(deformated);
726
727         ControlEvent_FireSubscribedEvent(package, szActionText, uirow);
728
729         msiobj_release(&uirow->hdr);
730         break;
731     }
732     case INSTALLMESSAGE_PROGRESS:
733         ControlEvent_FireSubscribedEvent(package, szSetProgress, record);
734         break;
735     }
736
737     return ERROR_SUCCESS;
738 }
739
740 INT WINAPI MsiProcessMessage( MSIHANDLE hInstall, INSTALLMESSAGE eMessageType,
741                               MSIHANDLE hRecord)
742 {
743     UINT ret = ERROR_INVALID_HANDLE;
744     MSIPACKAGE *package = NULL;
745     MSIRECORD *record = NULL;
746
747     package = msihandle2msiinfo( hInstall, MSIHANDLETYPE_PACKAGE );
748     if( !package )
749         return ERROR_INVALID_HANDLE;
750
751     record = msihandle2msiinfo( hRecord, MSIHANDLETYPE_RECORD );
752     if( !record )
753         goto out;
754
755     ret = MSI_ProcessMessage( package, eMessageType, record );
756
757 out:
758     msiobj_release( &package->hdr );
759     if( record )
760         msiobj_release( &record->hdr );
761
762     return ret;
763 }
764
765 /* property code */
766 UINT WINAPI MsiSetPropertyA( MSIHANDLE hInstall, LPCSTR szName, LPCSTR szValue )
767 {
768     LPWSTR szwName = NULL, szwValue = NULL;
769     UINT r = ERROR_OUTOFMEMORY;
770
771     szwName = strdupAtoW( szName );
772     if( szName && !szwName )
773         goto end;
774
775     szwValue = strdupAtoW( szValue );
776     if( szValue && !szwValue )
777         goto end;
778
779     r = MsiSetPropertyW( hInstall, szwName, szwValue);
780
781 end:
782     msi_free( szwName );
783     msi_free( szwValue );
784
785     return r;
786 }
787
788 UINT MSI_SetPropertyW( MSIPACKAGE *package, LPCWSTR szName, LPCWSTR szValue)
789 {
790     MSIQUERY *view;
791     MSIRECORD *row;
792     UINT rc;
793     DWORD sz = 0;
794     static const WCHAR Insert[]=
795      {'I','N','S','E','R','T',' ','i','n','t','o',' ','`','_','P','r','o','p'
796 ,'e','r','t','y','`',' ','(','`','_','P','r','o','p','e','r','t','y','`'
797 ,',','`','V','a','l','u','e','`',')',' ','V','A','L','U','E','S'
798 ,' ','(','?',',','?',')',0};
799     static const WCHAR Update[]=
800      {'U','P','D','A','T','E',' ','_','P','r','o','p','e'
801 ,'r','t','y',' ','s','e','t',' ','`','V','a','l','u','e','`',' ','='
802 ,' ','?',' ','w','h','e','r','e',' ','`','_','P','r','o','p'
803 ,'e','r','t','y','`',' ','=',' ','\'','%','s','\'',0};
804     WCHAR Query[1024];
805
806     TRACE("%p %s %s\n", package, debugstr_w(szName), debugstr_w(szValue));
807
808     if (!szName)
809         return ERROR_INVALID_PARAMETER;
810
811     /* this one is weird... */
812     if (!szName[0])
813         return szValue ? ERROR_FUNCTION_FAILED : ERROR_SUCCESS;
814
815     rc = MSI_GetPropertyW(package,szName,0,&sz);
816     if (rc==ERROR_MORE_DATA || rc == ERROR_SUCCESS)
817     {
818         sprintfW(Query,Update,szName);
819
820         row = MSI_CreateRecord(1);
821         MSI_RecordSetStringW(row,1,szValue);
822
823     }
824     else
825     {
826         strcpyW(Query,Insert);
827
828         row = MSI_CreateRecord(2);
829         MSI_RecordSetStringW(row,1,szName);
830         MSI_RecordSetStringW(row,2,szValue);
831     }
832
833     rc = MSI_DatabaseOpenViewW(package->db,Query,&view);
834     if (rc == ERROR_SUCCESS)
835     {
836         rc = MSI_ViewExecute(view,row);
837
838         MSI_ViewClose(view);
839         msiobj_release(&view->hdr);
840     }
841     msiobj_release(&row->hdr);
842
843     return rc;
844 }
845
846 UINT WINAPI MsiSetPropertyW( MSIHANDLE hInstall, LPCWSTR szName, LPCWSTR szValue)
847 {
848     MSIPACKAGE *package;
849     UINT ret;
850
851     package = msihandle2msiinfo( hInstall, MSIHANDLETYPE_PACKAGE);
852     if( !package )
853         return ERROR_INVALID_HANDLE;
854     ret = MSI_SetPropertyW( package, szName, szValue);
855     msiobj_release( &package->hdr );
856     return ret;
857 }
858
859 static MSIRECORD *MSI_GetPropertyRow( MSIPACKAGE *package, LPCWSTR name )
860 {
861     static const WCHAR query[]=
862     {'S','E','L','E','C','T',' ','`','V','a','l','u','e','`',' ',
863      'F','R','O','M',' ' ,'`','_','P','r','o','p','e','r','t','y','`',
864      ' ','W','H','E','R','E',' ' ,'`','_','P','r','o','p','e','r','t','y','`',
865      '=','\'','%','s','\'',0};
866
867     if (!name || !name[0])
868         return NULL;
869
870     return MSI_QueryGetRecord( package->db, query, name );
871 }
872  
873 /* internal function, not compatible with MsiGetPropertyW */
874 UINT MSI_GetPropertyW( MSIPACKAGE *package, LPCWSTR szName, 
875                        LPWSTR szValueBuf, DWORD* pchValueBuf )
876 {
877     MSIRECORD *row;
878     UINT rc = ERROR_FUNCTION_FAILED;
879
880     row = MSI_GetPropertyRow( package, szName );
881
882     if (*pchValueBuf > 0)
883         szValueBuf[0] = 0;
884
885     if (row)
886     {
887         rc = MSI_RecordGetStringW(row,1,szValueBuf,pchValueBuf);
888         msiobj_release(&row->hdr);
889     }
890
891     if (rc == ERROR_SUCCESS)
892         TRACE("returning %s for property %s\n", debugstr_w(szValueBuf),
893             debugstr_w(szName));
894     else if (rc == ERROR_MORE_DATA)
895         TRACE("need %li sized buffer for %s\n", *pchValueBuf,
896             debugstr_w(szName));
897     else
898     {
899         *pchValueBuf = 0;
900         TRACE("property %s not found\n", debugstr_w(szName));
901     }
902
903     return rc;
904 }
905
906 static UINT MSI_GetProperty( MSIHANDLE handle, LPCWSTR name, 
907                              awstring *szValueBuf, DWORD* pchValueBuf )
908 {
909     static const WCHAR empty[] = {0};
910     MSIPACKAGE *package;
911     MSIRECORD *row = NULL;
912     UINT r;
913     LPCWSTR val = NULL;
914
915     TRACE("%lu %s %p %p\n", handle, debugstr_w(name),
916           szValueBuf->str.w, pchValueBuf );
917
918     if (!name)
919         return ERROR_INVALID_PARAMETER;
920
921     package = msihandle2msiinfo( handle, MSIHANDLETYPE_PACKAGE );
922     if (!package)
923         return ERROR_INVALID_HANDLE;
924
925     row = MSI_GetPropertyRow( package, name );
926     if (row)
927         val = MSI_RecordGetString( row, 1 );
928
929     if (!val)
930         val = empty;
931
932     r = msi_strcpy_to_awstring( val, szValueBuf, pchValueBuf );
933
934     if (row)
935         msiobj_release( &row->hdr );
936     msiobj_release( &package->hdr );
937
938     return r;
939 }
940
941 UINT WINAPI MsiGetPropertyA( MSIHANDLE hInstall, LPCSTR szName,
942                              LPSTR szValueBuf, DWORD* pchValueBuf )
943 {
944     awstring val;
945     LPWSTR name;
946     UINT r;
947
948     val.unicode = FALSE;
949     val.str.a = szValueBuf;
950
951     name = strdupAtoW( szName );
952     if (szName && !name)
953         return ERROR_OUTOFMEMORY;
954
955     r = MSI_GetProperty( hInstall, name, &val, pchValueBuf );
956     msi_free( name );
957     return r;
958 }
959   
960 UINT WINAPI MsiGetPropertyW( MSIHANDLE hInstall, LPCWSTR szName,
961                              LPWSTR szValueBuf, DWORD* pchValueBuf )
962 {
963     awstring val;
964
965     val.unicode = TRUE;
966     val.str.w = szValueBuf;
967
968     return MSI_GetProperty( hInstall, szName, &val, pchValueBuf );
969 }