msi: Add tests for the WHERE SQL clause.
[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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, 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 %s\n", debugstr_w(szPackage) );
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( szPackage == NULL )
518         return ERROR_INVALID_PARAMETER;
519
520     if( dwOptions )
521         FIXME("dwOptions %08lx not supported\n", dwOptions);
522
523     ret = MSI_OpenPackageW( szPackage, &package );
524     if( ret == ERROR_SUCCESS )
525     {
526         *phPackage = alloc_msihandle( &package->hdr );
527         msiobj_release( &package->hdr );
528     }
529
530     return ret;
531 }
532
533 UINT WINAPI MsiOpenPackageW(LPCWSTR szPackage, MSIHANDLE *phPackage)
534 {
535     return MsiOpenPackageExW( szPackage, 0, phPackage );
536 }
537
538 UINT WINAPI MsiOpenPackageExA(LPCSTR szPackage, DWORD dwOptions, MSIHANDLE *phPackage)
539 {
540     LPWSTR szwPack = NULL;
541     UINT ret;
542
543     if( szPackage )
544     {
545         szwPack = strdupAtoW( szPackage );
546         if( !szwPack )
547             return ERROR_OUTOFMEMORY;
548     }
549
550     ret = MsiOpenPackageExW( szwPack, dwOptions, phPackage );
551
552     msi_free( szwPack );
553
554     return ret;
555 }
556
557 UINT WINAPI MsiOpenPackageA(LPCSTR szPackage, MSIHANDLE *phPackage)
558 {
559     return MsiOpenPackageExA( szPackage, 0, phPackage );
560 }
561
562 MSIHANDLE WINAPI MsiGetActiveDatabase(MSIHANDLE hInstall)
563 {
564     MSIPACKAGE *package;
565     MSIHANDLE handle = 0;
566
567     TRACE("(%ld)\n",hInstall);
568
569     package = msihandle2msiinfo( hInstall, MSIHANDLETYPE_PACKAGE);
570     if( package)
571     {
572         handle = alloc_msihandle( &package->db->hdr );
573         msiobj_release( &package->hdr );
574     }
575
576     return handle;
577 }
578
579 INT MSI_ProcessMessage( MSIPACKAGE *package, INSTALLMESSAGE eMessageType,
580                                MSIRECORD *record)
581 {
582     static const WCHAR szActionData[] =
583         {'A','c','t','i','o','n','D','a','t','a',0};
584     static const WCHAR szSetProgress[] =
585         {'S','e','t','P','r','o','g','r','e','s','s',0};
586     static const WCHAR szActionText[] =
587         {'A','c','t','i','o','n','T','e','x','t',0};
588     DWORD log_type = 0;
589     LPWSTR message;
590     DWORD sz;
591     DWORD total_size = 0;
592     INT i;
593     INT rc;
594     char *msg;
595     int len;
596
597     TRACE("%x\n", eMessageType);
598     rc = 0;
599
600     if ((eMessageType & 0xff000000) == INSTALLMESSAGE_ERROR)
601         log_type |= INSTALLLOGMODE_ERROR;
602     if ((eMessageType & 0xff000000) == INSTALLMESSAGE_WARNING)
603         log_type |= INSTALLLOGMODE_WARNING;
604     if ((eMessageType & 0xff000000) == INSTALLMESSAGE_USER)
605         log_type |= INSTALLLOGMODE_USER;
606     if ((eMessageType & 0xff000000) == INSTALLMESSAGE_INFO)
607         log_type |= INSTALLLOGMODE_INFO;
608     if ((eMessageType & 0xff000000) == INSTALLMESSAGE_COMMONDATA)
609         log_type |= INSTALLLOGMODE_COMMONDATA;
610     if ((eMessageType & 0xff000000) == INSTALLMESSAGE_ACTIONSTART)
611         log_type |= INSTALLLOGMODE_ACTIONSTART;
612     if ((eMessageType & 0xff000000) == INSTALLMESSAGE_ACTIONDATA)
613         log_type |= INSTALLLOGMODE_ACTIONDATA;
614     /* just a guess */
615     if ((eMessageType & 0xff000000) == INSTALLMESSAGE_PROGRESS)
616         log_type |= 0x800;
617
618     if ((eMessageType & 0xff000000) == INSTALLMESSAGE_ACTIONSTART)
619     {
620         static const WCHAR template_s[]=
621             {'A','c','t','i','o','n',' ','%','s',':',' ','%','s','.',' ',0};
622         static const WCHAR format[] = 
623             {'H','H','\'',':','\'','m','m','\'',':','\'','s','s',0};
624         WCHAR timet[0x100];
625         LPCWSTR action_text, action;
626         LPWSTR deformatted = NULL;
627
628         GetTimeFormatW(LOCALE_USER_DEFAULT, 0, NULL, format, timet, 0x100);
629
630         action = MSI_RecordGetString(record, 1);
631         action_text = MSI_RecordGetString(record, 2);
632         deformat_string(package, action_text, &deformatted);
633
634         len = strlenW(timet) + strlenW(action) + strlenW(template_s);
635         if (deformatted)
636             len += strlenW(deformatted);
637         message = msi_alloc(len*sizeof(WCHAR));
638         sprintfW(message, template_s, timet, action);
639         if (deformatted)
640             strcatW(message, deformatted);
641         msi_free(deformatted);
642     }
643     else
644     {
645         INT msg_field=1;
646         message = msi_alloc(1*sizeof (WCHAR));
647         message[0]=0;
648         msg_field = MSI_RecordGetFieldCount(record);
649         for (i = 1; i <= msg_field; i++)
650         {
651             LPWSTR tmp;
652             WCHAR number[3];
653             static const WCHAR format[] = { '%','i',':',' ',0};
654             static const WCHAR space[] = { ' ',0};
655             sz = 0;
656             MSI_RecordGetStringW(record,i,NULL,&sz);
657             sz+=4;
658             total_size+=sz*sizeof(WCHAR);
659             tmp = msi_alloc(sz*sizeof(WCHAR));
660             message = msi_realloc(message,total_size*sizeof (WCHAR));
661
662             MSI_RecordGetStringW(record,i,tmp,&sz);
663
664             if (msg_field > 1)
665             {
666                 sprintfW(number,format,i);
667                 strcatW(message,number);
668             }
669             strcatW(message,tmp);
670             if (msg_field > 1)
671                 strcatW(message,space);
672
673             msi_free(tmp);
674         }
675     }
676
677     TRACE("(%p %lx %lx %s)\n",gUIHandlerA, gUIFilter, log_type,
678                              debugstr_w(message));
679
680     /* convert it to ASCII */
681     len = WideCharToMultiByte( CP_ACP, 0, message, -1,
682                                NULL, 0, NULL, NULL );
683     msg = msi_alloc( len );
684     WideCharToMultiByte( CP_ACP, 0, message, -1,
685                          msg, len, NULL, NULL );
686
687     if (gUIHandlerA && (gUIFilter & log_type))
688     {
689         rc = gUIHandlerA(gUIContext,eMessageType,msg);
690     }
691
692     if ((!rc) && (gszLogFile[0]) && !((eMessageType & 0xff000000) ==
693                                       INSTALLMESSAGE_PROGRESS))
694     {
695         DWORD write;
696         HANDLE log_file = CreateFileW(gszLogFile,GENERIC_WRITE, 0, NULL,
697                                   OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
698
699         if (log_file != INVALID_HANDLE_VALUE)
700         {
701             SetFilePointer(log_file,0, NULL, FILE_END);
702             WriteFile(log_file,msg,strlen(msg),&write,NULL);
703             WriteFile(log_file,"\n",1,&write,NULL);
704             CloseHandle(log_file);
705         }
706     }
707     msi_free( msg );
708
709     msi_free( message);
710
711     switch (eMessageType & 0xff000000)
712     {
713     case INSTALLMESSAGE_ACTIONDATA:
714         /* FIXME: format record here instead of in ui_actiondata to get the
715          * correct action data for external scripts */
716         ControlEvent_FireSubscribedEvent(package, szActionData, record);
717         break;
718     case INSTALLMESSAGE_ACTIONSTART:
719     {
720         MSIRECORD *uirow;
721         LPWSTR deformated;
722         LPCWSTR action_text = MSI_RecordGetString(record, 2);
723
724         deformat_string(package, action_text, &deformated);
725         uirow = MSI_CreateRecord(1);
726         MSI_RecordSetStringW(uirow, 1, deformated);
727         TRACE("INSTALLMESSAGE_ACTIONSTART: %s\n", debugstr_w(deformated));
728         msi_free(deformated);
729
730         ControlEvent_FireSubscribedEvent(package, szActionText, uirow);
731
732         msiobj_release(&uirow->hdr);
733         break;
734     }
735     case INSTALLMESSAGE_PROGRESS:
736         ControlEvent_FireSubscribedEvent(package, szSetProgress, record);
737         break;
738     }
739
740     return ERROR_SUCCESS;
741 }
742
743 INT WINAPI MsiProcessMessage( MSIHANDLE hInstall, INSTALLMESSAGE eMessageType,
744                               MSIHANDLE hRecord)
745 {
746     UINT ret = ERROR_INVALID_HANDLE;
747     MSIPACKAGE *package = NULL;
748     MSIRECORD *record = NULL;
749
750     package = msihandle2msiinfo( hInstall, MSIHANDLETYPE_PACKAGE );
751     if( !package )
752         return ERROR_INVALID_HANDLE;
753
754     record = msihandle2msiinfo( hRecord, MSIHANDLETYPE_RECORD );
755     if( !record )
756         goto out;
757
758     ret = MSI_ProcessMessage( package, eMessageType, record );
759
760 out:
761     msiobj_release( &package->hdr );
762     if( record )
763         msiobj_release( &record->hdr );
764
765     return ret;
766 }
767
768 /* property code */
769 UINT WINAPI MsiSetPropertyA( MSIHANDLE hInstall, LPCSTR szName, LPCSTR szValue )
770 {
771     LPWSTR szwName = NULL, szwValue = NULL;
772     UINT r = ERROR_OUTOFMEMORY;
773
774     szwName = strdupAtoW( szName );
775     if( szName && !szwName )
776         goto end;
777
778     szwValue = strdupAtoW( szValue );
779     if( szValue && !szwValue )
780         goto end;
781
782     r = MsiSetPropertyW( hInstall, szwName, szwValue);
783
784 end:
785     msi_free( szwName );
786     msi_free( szwValue );
787
788     return r;
789 }
790
791 UINT MSI_SetPropertyW( MSIPACKAGE *package, LPCWSTR szName, LPCWSTR szValue)
792 {
793     MSIQUERY *view;
794     MSIRECORD *row;
795     UINT rc;
796     DWORD sz = 0;
797     static const WCHAR Insert[]=
798      {'I','N','S','E','R','T',' ','i','n','t','o',' ','`','_','P','r','o','p'
799 ,'e','r','t','y','`',' ','(','`','_','P','r','o','p','e','r','t','y','`'
800 ,',','`','V','a','l','u','e','`',')',' ','V','A','L','U','E','S'
801 ,' ','(','?',',','?',')',0};
802     static const WCHAR Update[]=
803      {'U','P','D','A','T','E',' ','_','P','r','o','p','e'
804 ,'r','t','y',' ','s','e','t',' ','`','V','a','l','u','e','`',' ','='
805 ,' ','?',' ','w','h','e','r','e',' ','`','_','P','r','o','p'
806 ,'e','r','t','y','`',' ','=',' ','\'','%','s','\'',0};
807     WCHAR Query[1024];
808
809     TRACE("%p %s %s\n", package, debugstr_w(szName), debugstr_w(szValue));
810
811     if (!szName)
812         return ERROR_INVALID_PARAMETER;
813
814     /* this one is weird... */
815     if (!szName[0])
816         return szValue ? ERROR_FUNCTION_FAILED : ERROR_SUCCESS;
817
818     rc = MSI_GetPropertyW(package,szName,0,&sz);
819     if (rc==ERROR_MORE_DATA || rc == ERROR_SUCCESS)
820     {
821         sprintfW(Query,Update,szName);
822
823         row = MSI_CreateRecord(1);
824         MSI_RecordSetStringW(row,1,szValue);
825
826     }
827     else
828     {
829         strcpyW(Query,Insert);
830
831         row = MSI_CreateRecord(2);
832         MSI_RecordSetStringW(row,1,szName);
833         MSI_RecordSetStringW(row,2,szValue);
834     }
835
836     rc = MSI_DatabaseOpenViewW(package->db,Query,&view);
837     if (rc == ERROR_SUCCESS)
838     {
839         rc = MSI_ViewExecute(view,row);
840
841         MSI_ViewClose(view);
842         msiobj_release(&view->hdr);
843     }
844     msiobj_release(&row->hdr);
845
846     return rc;
847 }
848
849 UINT WINAPI MsiSetPropertyW( MSIHANDLE hInstall, LPCWSTR szName, LPCWSTR szValue)
850 {
851     MSIPACKAGE *package;
852     UINT ret;
853
854     package = msihandle2msiinfo( hInstall, MSIHANDLETYPE_PACKAGE);
855     if( !package )
856         return ERROR_INVALID_HANDLE;
857     ret = MSI_SetPropertyW( package, szName, szValue);
858     msiobj_release( &package->hdr );
859     return ret;
860 }
861
862 static MSIRECORD *MSI_GetPropertyRow( MSIPACKAGE *package, LPCWSTR name )
863 {
864     static const WCHAR query[]=
865     {'S','E','L','E','C','T',' ','`','V','a','l','u','e','`',' ',
866      'F','R','O','M',' ' ,'`','_','P','r','o','p','e','r','t','y','`',
867      ' ','W','H','E','R','E',' ' ,'`','_','P','r','o','p','e','r','t','y','`',
868      '=','\'','%','s','\'',0};
869
870     if (!name || !name[0])
871         return NULL;
872
873     return MSI_QueryGetRecord( package->db, query, name );
874 }
875  
876 /* internal function, not compatible with MsiGetPropertyW */
877 UINT MSI_GetPropertyW( MSIPACKAGE *package, LPCWSTR szName, 
878                        LPWSTR szValueBuf, DWORD* pchValueBuf )
879 {
880     MSIRECORD *row;
881     UINT rc = ERROR_FUNCTION_FAILED;
882
883     row = MSI_GetPropertyRow( package, szName );
884
885     if (*pchValueBuf > 0)
886         szValueBuf[0] = 0;
887
888     if (row)
889     {
890         rc = MSI_RecordGetStringW(row,1,szValueBuf,pchValueBuf);
891         msiobj_release(&row->hdr);
892     }
893
894     if (rc == ERROR_SUCCESS)
895         TRACE("returning %s for property %s\n", debugstr_w(szValueBuf),
896             debugstr_w(szName));
897     else if (rc == ERROR_MORE_DATA)
898         TRACE("need %li sized buffer for %s\n", *pchValueBuf,
899             debugstr_w(szName));
900     else
901     {
902         *pchValueBuf = 0;
903         TRACE("property %s not found\n", debugstr_w(szName));
904     }
905
906     return rc;
907 }
908
909 static UINT MSI_GetProperty( MSIHANDLE handle, LPCWSTR name, 
910                              awstring *szValueBuf, DWORD* pchValueBuf )
911 {
912     static const WCHAR empty[] = {0};
913     MSIPACKAGE *package;
914     MSIRECORD *row = NULL;
915     UINT r;
916     LPCWSTR val = NULL;
917
918     TRACE("%lu %s %p %p\n", handle, debugstr_w(name),
919           szValueBuf->str.w, pchValueBuf );
920
921     if (!name)
922         return ERROR_INVALID_PARAMETER;
923
924     package = msihandle2msiinfo( handle, MSIHANDLETYPE_PACKAGE );
925     if (!package)
926         return ERROR_INVALID_HANDLE;
927
928     row = MSI_GetPropertyRow( package, name );
929     if (row)
930         val = MSI_RecordGetString( row, 1 );
931
932     if (!val)
933         val = empty;
934
935     r = msi_strcpy_to_awstring( val, szValueBuf, pchValueBuf );
936
937     if (row)
938         msiobj_release( &row->hdr );
939     msiobj_release( &package->hdr );
940
941     return r;
942 }
943
944 UINT WINAPI MsiGetPropertyA( MSIHANDLE hInstall, LPCSTR szName,
945                              LPSTR szValueBuf, DWORD* pchValueBuf )
946 {
947     awstring val;
948     LPWSTR name;
949     UINT r;
950
951     val.unicode = FALSE;
952     val.str.a = szValueBuf;
953
954     name = strdupAtoW( szName );
955     if (szName && !name)
956         return ERROR_OUTOFMEMORY;
957
958     r = MSI_GetProperty( hInstall, name, &val, pchValueBuf );
959     msi_free( name );
960     return r;
961 }
962   
963 UINT WINAPI MsiGetPropertyW( MSIHANDLE hInstall, LPCWSTR szName,
964                              LPWSTR szValueBuf, DWORD* pchValueBuf )
965 {
966     awstring val;
967
968     val.unicode = TRUE;
969     val.str.w = szValueBuf;
970
971     return MSI_GetProperty( hInstall, szName, &val, pchValueBuf );
972 }