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