Fixed gcc 4.0 warnings.
[wine] / dlls / msi / helpers.c
1 /*
2  * Implementation of the Microsoft Installer (msi.dll)
3  *
4  * Copyright 2005 Aric Stewart for CodeWeavers
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  */
20
21 /*
22  * Here are helper functions formally in action.c that are used by a variaty of
23  * actions and functions.
24  */
25
26 #include <stdarg.h>
27
28 #include "windef.h"
29 #include "winbase.h"
30 #include "winerror.h"
31 #include "wine/debug.h"
32 #include "msipriv.h"
33 #include "winuser.h"
34 #include "wine/unicode.h"
35 #include "action.h"
36
37 WINE_DEFAULT_DEBUG_CHANNEL(msi);
38
39 static const WCHAR cszTargetDir[] = {'T','A','R','G','E','T','D','I','R',0};
40 static const WCHAR cszDatabase[]={'D','A','T','A','B','A','S','E',0};
41
42 const WCHAR cszSourceDir[] = {'S','o','u','r','c','e','D','i','r',0};
43 const WCHAR cszRootDrive[] = {'R','O','O','T','D','R','I','V','E',0};
44 const WCHAR cszbs[]={'\\',0};
45
46 DWORD build_version_dword(LPCWSTR version_string)
47 {
48     SHORT major,minor;
49     WORD build;
50     DWORD rc = 0x00000000;
51     LPCWSTR ptr1;
52
53     ptr1 = version_string;
54
55     if (!ptr1)
56         return rc;
57     else
58         major = atoiW(ptr1);
59
60
61     if(ptr1)
62         ptr1 = strchrW(ptr1,'.');
63     if (ptr1)
64     {
65         ptr1++;
66         minor = atoiW(ptr1);
67     }
68     else
69         minor = 0;
70
71     if (ptr1)
72         ptr1 = strchrW(ptr1,'.');
73
74     if (ptr1)
75     {
76         ptr1++;
77         build = atoiW(ptr1);
78     }
79     else
80         build = 0;
81
82     rc = MAKELONG(build,MAKEWORD(minor,major));
83     TRACE("%s -> 0x%lx\n",debugstr_w(version_string),rc);
84     return rc;
85 }
86
87 UINT build_icon_path(MSIPACKAGE *package, LPCWSTR icon_name, 
88                             LPWSTR *FilePath)
89 {
90     LPWSTR SystemFolder;
91     LPWSTR dest;
92
93     static const WCHAR szInstaller[] = 
94         {'M','i','c','r','o','s','o','f','t','\\',
95          'I','n','s','t','a','l','l','e','r','\\',0};
96     static const WCHAR szFolder[] =
97         {'A','p','p','D','a','t','a','F','o','l','d','e','r',0};
98
99     SystemFolder = load_dynamic_property(package,szFolder,NULL);
100
101     dest = build_directory_name(3, SystemFolder, szInstaller, package->ProductCode);
102
103     create_full_pathW(dest);
104
105     *FilePath = build_directory_name(2, dest, icon_name);
106
107     HeapFree(GetProcessHeap(),0,SystemFolder);
108     HeapFree(GetProcessHeap(),0,dest);
109     return ERROR_SUCCESS;
110 }
111
112 WCHAR *load_dynamic_stringW(MSIRECORD *row, INT index)
113 {
114     UINT rc;
115     DWORD sz;
116     LPWSTR ret;
117    
118     sz = 0; 
119     if (MSI_RecordIsNull(row,index))
120         return NULL;
121
122     rc = MSI_RecordGetStringW(row,index,NULL,&sz);
123
124     /* having an empty string is different than NULL */
125     if (sz == 0)
126     {
127         ret = HeapAlloc(GetProcessHeap(),0,sizeof(WCHAR));
128         ret[0] = 0;
129         return ret;
130     }
131
132     sz ++;
133     ret = HeapAlloc(GetProcessHeap(),0,sz * sizeof (WCHAR));
134     rc = MSI_RecordGetStringW(row,index,ret,&sz);
135     if (rc!=ERROR_SUCCESS)
136     {
137         ERR("Unable to load dynamic string\n");
138         HeapFree(GetProcessHeap(), 0, ret);
139         ret = NULL;
140     }
141     return ret;
142 }
143
144 LPWSTR load_dynamic_property(MSIPACKAGE *package, LPCWSTR prop, UINT* rc)
145 {
146     DWORD sz = 0;
147     LPWSTR str;
148     UINT r;
149
150     r = MSI_GetPropertyW(package, prop, NULL, &sz);
151     if (r != ERROR_SUCCESS && r != ERROR_MORE_DATA)
152     {
153         if (rc)
154             *rc = r;
155         return NULL;
156     }
157     sz++;
158     str = HeapAlloc(GetProcessHeap(),0,sz*sizeof(WCHAR));
159     r = MSI_GetPropertyW(package, prop, str, &sz);
160     if (r != ERROR_SUCCESS)
161     {
162         HeapFree(GetProcessHeap(),0,str);
163         str = NULL;
164     }
165     if (rc)
166         *rc = r;
167     return str;
168 }
169
170 MSICOMPONENT* get_loaded_component( MSIPACKAGE* package, LPCWSTR Component )
171 {
172     MSICOMPONENT *comp;
173
174     LIST_FOR_EACH_ENTRY( comp, &package->components, MSICOMPONENT, entry )
175     {
176         if (lstrcmpW(Component,comp->Component)==0)
177             return comp;
178     }
179     return NULL;
180 }
181
182 MSIFEATURE* get_loaded_feature(MSIPACKAGE* package, LPCWSTR Feature )
183 {
184     MSIFEATURE *feature;
185
186     LIST_FOR_EACH_ENTRY( feature, &package->features, MSIFEATURE, entry )
187     {
188         if (lstrcmpW( Feature, feature->Feature )==0)
189             return feature;
190     }
191     return NULL;
192 }
193
194 MSIFILE* get_loaded_file( MSIPACKAGE* package, LPCWSTR key )
195 {
196     MSIFILE *file;
197
198     LIST_FOR_EACH_ENTRY( file, &package->files, MSIFILE, entry )
199     {
200         if (lstrcmpW( key, file->File )==0)
201             return file;
202     }
203     return NULL;
204 }
205
206 int track_tempfile( MSIPACKAGE *package, LPCWSTR name, LPCWSTR path )
207 {
208     MSITEMPFILE *temp;
209
210     if (!package)
211         return -1;
212
213     LIST_FOR_EACH_ENTRY( temp, &package->tempfiles, MSITEMPFILE, entry )
214     {
215         if (lstrcmpW( name, temp->File )==0)
216         {
217             TRACE("tempfile %s already exists with path %s\n",
218                 debugstr_w(temp->File), debugstr_w(temp->Path));
219             return -1;
220         }
221     }
222
223     temp = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof (MSITEMPFILE) );
224     if (!temp)
225         return -1;
226
227     list_add_head( &package->tempfiles, &temp->entry );
228
229     temp->File = strdupW( name );
230     temp->Path = strdupW( path );
231
232     TRACE("adding tempfile %s with path %s\n",
233            debugstr_w(temp->File), debugstr_w(temp->Path));
234
235     return 0;
236 }
237
238 MSIFOLDER *get_loaded_folder( MSIPACKAGE *package, LPCWSTR dir )
239 {
240     MSIFOLDER *folder;
241     
242     LIST_FOR_EACH_ENTRY( folder, &package->folders, MSIFOLDER, entry )
243     {
244         if (lstrcmpW( dir, folder->Directory )==0)
245             return folder;
246     }
247     return NULL;
248 }
249
250 LPWSTR resolve_folder(MSIPACKAGE *package, LPCWSTR name, BOOL source, 
251                       BOOL set_prop, MSIFOLDER **folder)
252 {
253     MSIFOLDER *f;
254     LPWSTR p, path = NULL;
255
256     TRACE("Working to resolve %s\n",debugstr_w(name));
257
258     if (!name)
259         return NULL;
260
261     /* special resolving for Target and Source root dir */
262     if (strcmpW(name,cszTargetDir)==0 || strcmpW(name,cszSourceDir)==0)
263     {
264         if (!source)
265         {
266             LPWSTR check_path;
267             check_path = load_dynamic_property(package,cszTargetDir,NULL);
268             if (!check_path)
269             {
270                 check_path = load_dynamic_property(package,cszRootDrive,NULL);
271                 if (set_prop)
272                     MSI_SetPropertyW(package,cszTargetDir,check_path);
273             }
274
275             /* correct misbuilt target dir */
276             path = build_directory_name(2, check_path, NULL);
277             if (strcmpiW(path,check_path)!=0)
278                 MSI_SetPropertyW(package,cszTargetDir,path);
279             HeapFree(GetProcessHeap(),0,check_path);
280         }
281         else
282         {
283             path = load_dynamic_property(package,cszSourceDir,NULL);
284             if (!path)
285             {
286                 path = load_dynamic_property(package,cszDatabase,NULL);
287                 if (path)
288                 {
289                     p = strrchrW(path,'\\');
290                     if (p)
291                         *(p+1) = 0;
292                 }
293             }
294         }
295         if (folder)
296             *folder = get_loaded_folder( package, name );
297         return path;
298     }
299
300     f = get_loaded_folder( package, name );
301     if (!f)
302         return NULL;
303
304     if (folder)
305         *folder = f;
306
307     if (!source && f->ResolvedTarget)
308     {
309         path = strdupW( f->ResolvedTarget );
310         TRACE("   already resolved to %s\n",debugstr_w(path));
311         return path;
312     }
313     else if (source && f->ResolvedSource)
314     {
315         path = strdupW( f->ResolvedSource );
316         TRACE("   (source)already resolved to %s\n",debugstr_w(path));
317         return path;
318     }
319     else if (!source && f->Property)
320     {
321         path = build_directory_name( 2, f->Property, NULL );
322                     
323         TRACE("   internally set to %s\n",debugstr_w(path));
324         if (set_prop)
325             MSI_SetPropertyW( package, name, path );
326         return path;
327     }
328
329     if (f->Parent)
330     {
331         LPWSTR parent = f->Parent->Directory;
332
333         TRACE(" ! Parent is %s\n", debugstr_w(parent));
334
335         p = resolve_folder(package, parent, source, set_prop, NULL);
336         if (!source)
337         {
338             TRACE("   TargetDefault = %s\n", debugstr_w(f->TargetDefault));
339
340             path = build_directory_name( 3, p, f->TargetDefault, NULL );
341             f->ResolvedTarget = strdupW( path );
342             TRACE("   resolved into %s\n",debugstr_w(path));
343             if (set_prop)
344                 MSI_SetPropertyW(package,name,path);
345         }
346         else 
347         {
348             if (f->SourceDefault && f->SourceDefault[0]!='.')
349                 path = build_directory_name( 3, p, f->SourceDefault, NULL );
350             else
351                 path = strdupW(p);
352             TRACE("   (source)resolved into %s\n",debugstr_w(path));
353             f->ResolvedSource = strdupW( path );
354         }
355         HeapFree(GetProcessHeap(),0,p);
356     }
357     return path;
358 }
359
360 /* wrapper to resist a need for a full rewrite right now */
361 DWORD deformat_string(MSIPACKAGE *package, LPCWSTR ptr, WCHAR** data )
362 {
363     if (ptr)
364     {
365         MSIRECORD *rec = MSI_CreateRecord(1);
366         DWORD size = 0;
367
368         MSI_RecordSetStringW(rec,0,ptr);
369         MSI_FormatRecordW(package,rec,NULL,&size);
370         if (size >= 0)
371         {
372             size++;
373             *data = HeapAlloc(GetProcessHeap(),0,size*sizeof(WCHAR));
374             if (size > 1)
375                 MSI_FormatRecordW(package,rec,*data,&size);
376             else
377                 *data[0] = 0;
378             msiobj_release( &rec->hdr );
379             return sizeof(WCHAR)*size;
380         }
381         msiobj_release( &rec->hdr );
382     }
383
384     *data = NULL;
385     return 0;
386 }
387
388 UINT schedule_action(MSIPACKAGE *package, UINT script, LPCWSTR action)
389 {
390     UINT count;
391     LPWSTR *newbuf = NULL;
392     if (script >= TOTAL_SCRIPTS)
393     {
394         FIXME("Unknown script requested %i\n",script);
395         return ERROR_FUNCTION_FAILED;
396     }
397     TRACE("Scheduling Action %s in script %i\n",debugstr_w(action), script);
398     
399     count = package->script->ActionCount[script];
400     package->script->ActionCount[script]++;
401     if (count != 0)
402         newbuf = HeapReAlloc(GetProcessHeap(),0,
403                         package->script->Actions[script],
404                         package->script->ActionCount[script]* sizeof(LPWSTR));
405     else
406         newbuf = HeapAlloc(GetProcessHeap(),0, sizeof(LPWSTR));
407
408     newbuf[count] = strdupW(action);
409     package->script->Actions[script] = newbuf;
410
411    return ERROR_SUCCESS;
412 }
413
414 static void remove_tracked_tempfiles(MSIPACKAGE* package)
415 {
416     struct list *item, *cursor;
417
418     LIST_FOR_EACH_SAFE( item, cursor, &package->tempfiles )
419     {
420         MSITEMPFILE *temp = LIST_ENTRY( item, MSITEMPFILE, entry );
421
422         list_remove( &temp->entry );
423         TRACE("deleting temp file %s\n", debugstr_w( temp->Path ));
424         DeleteFileW( temp->Path );
425         HeapFree( GetProcessHeap(), 0, temp->File );
426         HeapFree( GetProcessHeap(), 0, temp->Path );
427         HeapFree( GetProcessHeap(), 0, temp );
428     }
429 }
430
431 static void free_feature( MSIFEATURE *feature )
432 {
433     struct list *item, *cursor;
434
435     LIST_FOR_EACH_SAFE( item, cursor, &feature->Components )
436     {
437         ComponentList *cl = LIST_ENTRY( item, ComponentList, entry );
438         list_remove( &cl->entry );
439         HeapFree( GetProcessHeap(), 0, cl );
440     }
441     HeapFree( GetProcessHeap(), 0, feature->Description );
442     HeapFree( GetProcessHeap(), 0, feature->Title );
443     HeapFree( GetProcessHeap(), 0, feature );
444 }
445
446 void free_extension( MSIEXTENSION *ext )
447 {
448     struct list *item, *cursor;
449
450     LIST_FOR_EACH_SAFE( item, cursor, &ext->verbs )
451     {
452         MSIVERB *verb = LIST_ENTRY( item, MSIVERB, entry );
453
454         list_remove( &verb->entry );
455         HeapFree( GetProcessHeap(), 0, verb->Verb );
456         HeapFree( GetProcessHeap(), 0, verb->Command );
457         HeapFree( GetProcessHeap(), 0, verb->Argument );
458         HeapFree( GetProcessHeap(), 0, verb );
459     }
460
461     HeapFree( GetProcessHeap(), 0, ext->Extension );
462     HeapFree( GetProcessHeap(), 0, ext->ProgIDText );
463     HeapFree( GetProcessHeap(), 0, ext );
464 }
465
466 /* Called when the package is being closed */
467 void ACTION_free_package_structures( MSIPACKAGE* package)
468 {
469     INT i;
470     struct list *item, *cursor;
471     
472     TRACE("Freeing package action data\n");
473
474     remove_tracked_tempfiles(package);
475
476     LIST_FOR_EACH_SAFE( item, cursor, &package->features )
477     {
478         MSIFEATURE *feature = LIST_ENTRY( item, MSIFEATURE, entry );
479         list_remove( &feature->entry );
480         free_feature( feature );
481     }
482
483     LIST_FOR_EACH_SAFE( item, cursor, &package->folders )
484     {
485         MSIFOLDER *folder = LIST_ENTRY( item, MSIFOLDER, entry );
486
487         list_remove( &folder->entry );
488         HeapFree( GetProcessHeap(), 0, folder->Directory );
489         HeapFree( GetProcessHeap(), 0, folder->TargetDefault );
490         HeapFree( GetProcessHeap(), 0, folder->SourceDefault );
491         HeapFree( GetProcessHeap(), 0, folder->ResolvedTarget );
492         HeapFree( GetProcessHeap(), 0, folder->ResolvedSource );
493         HeapFree( GetProcessHeap(), 0, folder->Property );
494     }
495
496     LIST_FOR_EACH_SAFE( item, cursor, &package->components )
497     {
498         MSICOMPONENT *comp = LIST_ENTRY( item, MSICOMPONENT, entry );
499         
500         list_remove( &comp->entry );
501         HeapFree( GetProcessHeap(), 0, comp->Condition );
502         HeapFree( GetProcessHeap(), 0, comp->FullKeypath );
503         HeapFree( GetProcessHeap(), 0, comp );
504     }
505
506     LIST_FOR_EACH_SAFE( item, cursor, &package->files )
507     {
508         MSIFILE *file = LIST_ENTRY( item, MSIFILE, entry );
509
510         list_remove( &file->entry );
511         HeapFree( GetProcessHeap(), 0, file->File );
512         HeapFree( GetProcessHeap(), 0, file->FileName );
513         HeapFree( GetProcessHeap(), 0, file->ShortName );
514         HeapFree( GetProcessHeap(), 0, file->Version );
515         HeapFree( GetProcessHeap(), 0, file->Language );
516         HeapFree( GetProcessHeap(), 0, file->SourcePath );
517         HeapFree( GetProcessHeap(), 0, file->TargetPath );
518         HeapFree( GetProcessHeap(), 0, file );
519     }
520
521     /* clean up extension, progid, class and verb structures */
522     LIST_FOR_EACH_SAFE( item, cursor, &package->classes )
523     {
524         MSICLASS *cls = LIST_ENTRY( item, MSICLASS, entry );
525
526         list_remove( &cls->entry );
527         HeapFree( GetProcessHeap(), 0, cls->Description );
528         HeapFree( GetProcessHeap(), 0, cls->FileTypeMask );
529         HeapFree( GetProcessHeap(), 0, cls->IconPath );
530         HeapFree( GetProcessHeap(), 0, cls->DefInprocHandler );
531         HeapFree( GetProcessHeap(), 0, cls->DefInprocHandler32 );
532         HeapFree( GetProcessHeap(), 0, cls->Argument );
533         HeapFree( GetProcessHeap(), 0, cls->ProgIDText );
534         HeapFree( GetProcessHeap(), 0, cls );
535     }
536
537     LIST_FOR_EACH_SAFE( item, cursor, &package->extensions )
538     {
539         MSIEXTENSION *ext = LIST_ENTRY( item, MSIEXTENSION, entry );
540
541         list_remove( &ext->entry );
542         free_extension( ext );
543     }
544
545     LIST_FOR_EACH_SAFE( item, cursor, &package->progids )
546     {
547         MSIPROGID *progid = LIST_ENTRY( item, MSIPROGID, entry );
548
549         list_remove( &progid->entry );
550         HeapFree( GetProcessHeap(), 0, progid->ProgID );
551         HeapFree( GetProcessHeap(), 0, progid->Description );
552         HeapFree( GetProcessHeap(), 0, progid->IconPath );
553         HeapFree( GetProcessHeap(), 0, progid );
554     }
555
556     LIST_FOR_EACH_SAFE( item, cursor, &package->mimes )
557     {
558         MSIMIME *mt = LIST_ENTRY( item, MSIMIME, entry );
559
560         list_remove( &mt->entry );
561         HeapFree( GetProcessHeap(), 0, mt->ContentType );
562         HeapFree( GetProcessHeap(), 0, mt );
563     }
564
565     LIST_FOR_EACH_SAFE( item, cursor, &package->appids )
566     {
567         MSIAPPID *appid = LIST_ENTRY( item, MSIAPPID, entry );
568
569         list_remove( &appid->entry );
570         HeapFree( GetProcessHeap(), 0, appid->RemoteServerName );
571         HeapFree( GetProcessHeap(), 0, appid->LocalServer );
572         HeapFree( GetProcessHeap(), 0, appid->ServiceParameters );
573         HeapFree( GetProcessHeap(), 0, appid->DllSurrogate );
574         HeapFree( GetProcessHeap(), 0, appid );
575     }
576
577     if (package->script)
578     {
579         for (i = 0; i < TOTAL_SCRIPTS; i++)
580         {
581             int j;
582             for (j = 0; j < package->script->ActionCount[i]; j++)
583                 HeapFree(GetProcessHeap(),0,package->script->Actions[i][j]);
584         
585             HeapFree(GetProcessHeap(),0,package->script->Actions[i]);
586         }
587
588         for (i = 0; i < package->script->UniqueActionsCount; i++)
589             HeapFree(GetProcessHeap(),0,package->script->UniqueActions[i]);
590
591         HeapFree(GetProcessHeap(),0,package->script->UniqueActions);
592         HeapFree(GetProcessHeap(),0,package->script);
593     }
594
595     HeapFree(GetProcessHeap(),0,package->PackagePath);
596     HeapFree(GetProcessHeap(),0,package->msiFilePath);
597     HeapFree(GetProcessHeap(),0,package->ProductCode);
598
599     /* cleanup control event subscriptions */
600     ControlEvent_CleanupSubscriptions(package);
601 }
602
603 /*
604  *  build_directory_name()
605  *
606  *  This function is to save messing round with directory names
607  *  It handles adding backslashes between path segments, 
608  *   and can add \ at the end of the directory name if told to.
609  *
610  *  It takes a variable number of arguments.
611  *  It always allocates a new string for the result, so make sure
612  *   to free the return value when finished with it.
613  *
614  *  The first arg is the number of path segments that follow.
615  *  The arguments following count are a list of path segments.
616  *  A path segment may be NULL.
617  *
618  *  Path segments will be added with a \ separating them.
619  *  A \ will not be added after the last segment, however if the
620  *    last segment is NULL, then the last character will be a \
621  * 
622  */
623 LPWSTR build_directory_name(DWORD count, ...)
624 {
625     DWORD sz = 1, i;
626     LPWSTR dir;
627     va_list va;
628
629     va_start(va,count);
630     for(i=0; i<count; i++)
631     {
632         LPCWSTR str = va_arg(va,LPCWSTR);
633         if (str)
634             sz += strlenW(str) + 1;
635     }
636     va_end(va);
637
638     dir = HeapAlloc(GetProcessHeap(), 0, sz*sizeof(WCHAR));
639     dir[0]=0;
640
641     va_start(va,count);
642     for(i=0; i<count; i++)
643     {
644         LPCWSTR str = va_arg(va,LPCWSTR);
645         if (!str)
646             continue;
647         strcatW(dir, str);
648         if( ((i+1)!=count) && dir[strlenW(dir)-1]!='\\')
649             strcatW(dir, cszbs);
650     }
651     return dir;
652 }
653
654 /***********************************************************************
655  *            create_full_pathW
656  *
657  * Recursively create all directories in the path.
658  *
659  * shamelessly stolen from setupapi/queue.c
660  */
661 BOOL create_full_pathW(const WCHAR *path)
662 {
663     BOOL ret = TRUE;
664     int len;
665     WCHAR *new_path;
666
667     new_path = HeapAlloc(GetProcessHeap(), 0, (strlenW(path) + 1) *
668                                               sizeof(WCHAR));
669
670     strcpyW(new_path, path);
671
672     while((len = strlenW(new_path)) && new_path[len - 1] == '\\')
673     new_path[len - 1] = 0;
674
675     while(!CreateDirectoryW(new_path, NULL))
676     {
677         WCHAR *slash;
678         DWORD last_error = GetLastError();
679         if(last_error == ERROR_ALREADY_EXISTS)
680             break;
681
682         if(last_error != ERROR_PATH_NOT_FOUND)
683         {
684             ret = FALSE;
685             break;
686         }
687
688         if(!(slash = strrchrW(new_path, '\\')))
689         {
690             ret = FALSE;
691             break;
692         }
693
694         len = slash - new_path;
695         new_path[len] = 0;
696         if(!create_full_pathW(new_path))
697         {
698             ret = FALSE;
699             break;
700         }
701         new_path[len] = '\\';
702     }
703
704     HeapFree(GetProcessHeap(), 0, new_path);
705     return ret;
706 }
707
708 void ui_progress(MSIPACKAGE *package, int a, int b, int c, int d )
709 {
710     MSIRECORD * row;
711
712     row = MSI_CreateRecord(4);
713     MSI_RecordSetInteger(row,1,a);
714     MSI_RecordSetInteger(row,2,b);
715     MSI_RecordSetInteger(row,3,c);
716     MSI_RecordSetInteger(row,4,d);
717     MSI_ProcessMessage(package, INSTALLMESSAGE_PROGRESS, row);
718     msiobj_release(&row->hdr);
719
720     msi_dialog_check_messages(NULL);
721 }
722
723 void ui_actiondata(MSIPACKAGE *package, LPCWSTR action, MSIRECORD * record)
724 {
725     static const WCHAR Query_t[] = 
726         {'S','E','L','E','C','T',' ','*',' ','F','R','O','M',' ',
727          '`','A','c','t','i','o', 'n','T','e','x','t','`',' ',
728          'W','H','E','R','E',' ', '`','A','c','t','i','o','n','`',' ','=', 
729          ' ','\'','%','s','\'',0};
730     WCHAR message[1024];
731     MSIRECORD * row = 0;
732     DWORD size;
733     static const WCHAR szActionData[] = 
734         {'A','c','t','i','o','n','D','a','t','a',0};
735
736     if (!package->LastAction || strcmpW(package->LastAction,action))
737     {
738         row = MSI_QueryGetRecord(package->db, Query_t, action);
739         if (!row)
740             return;
741
742         if (MSI_RecordIsNull(row,3))
743         {
744             msiobj_release(&row->hdr);
745             return;
746         }
747
748         /* update the cached actionformat */
749         HeapFree(GetProcessHeap(),0,package->ActionFormat);
750         package->ActionFormat = load_dynamic_stringW(row,3);
751
752         HeapFree(GetProcessHeap(),0,package->LastAction);
753         package->LastAction = strdupW(action);
754
755         msiobj_release(&row->hdr);
756     }
757
758     MSI_RecordSetStringW(record,0,package->ActionFormat);
759     size = 1024;
760     MSI_FormatRecordW(package,record,message,&size);
761
762     row = MSI_CreateRecord(1);
763     MSI_RecordSetStringW(row,1,message);
764  
765     MSI_ProcessMessage(package, INSTALLMESSAGE_ACTIONDATA, row);
766
767     ControlEvent_FireSubscribedEvent(package,szActionData, row);
768
769     msiobj_release(&row->hdr);
770 }
771
772 BOOL ACTION_VerifyComponentForAction(MSIPACKAGE* package, MSICOMPONENT* comp,
773                                             INSTALLSTATE check )
774 {
775     if (comp->Installed == check)
776         return FALSE;
777
778     if (comp->ActionRequest == check)
779         return TRUE;
780     else
781         return FALSE;
782 }
783
784 BOOL ACTION_VerifyFeatureForAction( MSIFEATURE* feature, INSTALLSTATE check )
785 {
786     if (feature->Installed == check)
787         return FALSE;
788
789     if (feature->ActionRequest == check)
790         return TRUE;
791     else
792         return FALSE;
793 }
794
795 void reduce_to_longfilename(WCHAR* filename)
796 {
797     LPWSTR p = strchrW(filename,'|');
798     if (p)
799         memmove(filename, p+1, (strlenW(p+1)+1)*sizeof(WCHAR));
800 }
801
802 void reduce_to_shortfilename(WCHAR* filename)
803 {
804     LPWSTR p = strchrW(filename,'|');
805     if (p)
806         *p = 0;
807 }
808
809 LPWSTR create_component_advertise_string(MSIPACKAGE* package, 
810                 MSICOMPONENT* component, LPCWSTR feature)
811 {
812     GUID clsid;
813     WCHAR productid_85[21];
814     WCHAR component_85[21];
815     /*
816      * I have a fair bit of confusion as to when a < is used and when a > is
817      * used. I do not think i have it right...
818      *
819      * Ok it appears that the > is used if there is a guid for the compoenent
820      * and the < is used if not.
821      */
822     static WCHAR fmt1[] = {'%','s','%','s','<',0,0};
823     static WCHAR fmt2[] = {'%','s','%','s','>','%','s',0,0};
824     LPWSTR output = NULL;
825     DWORD sz = 0;
826
827     memset(productid_85,0,sizeof(productid_85));
828     memset(component_85,0,sizeof(component_85));
829
830     CLSIDFromString(package->ProductCode, &clsid);
831     
832     encode_base85_guid(&clsid,productid_85);
833
834     CLSIDFromString(component->ComponentId, &clsid);
835     encode_base85_guid(&clsid,component_85);
836
837     TRACE("Doing something with this... %s %s %s\n", 
838             debugstr_w(productid_85), debugstr_w(feature),
839             debugstr_w(component_85));
840  
841     sz = lstrlenW(productid_85) + lstrlenW(feature);
842     if (component)
843         sz += lstrlenW(component_85);
844
845     sz+=3;
846     sz *= sizeof(WCHAR);
847            
848     output = HeapAlloc(GetProcessHeap(),0,sz);
849     memset(output,0,sz);
850
851     if (component)
852         sprintfW(output,fmt2,productid_85,feature,component_85);
853     else
854         sprintfW(output,fmt1,productid_85,feature);
855     
856     return output;
857 }
858
859 /* update compoennt state based on a feature change */
860 void ACTION_UpdateComponentStates(MSIPACKAGE *package, LPCWSTR szFeature)
861 {
862     INSTALLSTATE newstate;
863     MSIFEATURE *feature;
864     ComponentList *cl;
865
866     feature = get_loaded_feature(package,szFeature);
867     if (!feature)
868         return;
869
870     newstate = feature->ActionRequest;
871
872     LIST_FOR_EACH_ENTRY( cl, &feature->Components, ComponentList, entry )
873     {
874         MSICOMPONENT* component = cl->component;
875     
876         TRACE("MODIFYING(%i): Component %s (Installed %i, Action %i, Request %i)\n",
877             newstate, debugstr_w(component->Component), component->Installed, 
878             component->Action, component->ActionRequest);
879         
880         if (!component->Enabled)
881             continue;
882  
883         if (newstate == INSTALLSTATE_LOCAL)
884         {
885             component->ActionRequest = INSTALLSTATE_LOCAL;
886             component->Action = INSTALLSTATE_LOCAL;
887         }
888         else 
889         {
890             ComponentList *clist;
891             MSIFEATURE *f;
892
893             component->ActionRequest = newstate;
894             component->Action = newstate;
895
896             /*if any other feature wants is local we need to set it local*/
897             LIST_FOR_EACH_ENTRY( f, &package->features, MSIFEATURE, entry )
898             {
899                 if ( component->ActionRequest != INSTALLSTATE_LOCAL )
900                     break;
901
902                 LIST_FOR_EACH_ENTRY( clist, &f->Components, ComponentList, entry )
903                 {
904                     if ( clist->component == component )
905                     {
906                         if (f->ActionRequest == INSTALLSTATE_LOCAL)
907                         {
908                             TRACE("Saved by %s\n", debugstr_w(f->Feature));
909                             component->ActionRequest = INSTALLSTATE_LOCAL;
910                             component->Action = INSTALLSTATE_LOCAL;
911                         }
912                         break;
913                     }
914                 }
915             }
916         }
917         TRACE("Result (%i): Component %s (Installed %i, Action %i, Request %i)\n",
918             newstate, debugstr_w(component->Component), component->Installed, 
919             component->Action, component->ActionRequest);
920     } 
921 }
922
923 UINT register_unique_action(MSIPACKAGE *package, LPCWSTR action)
924 {
925     UINT count;
926     LPWSTR *newbuf = NULL;
927
928     if (!package || !package->script)
929         return FALSE;
930
931     TRACE("Registering Action %s as having fun\n",debugstr_w(action));
932     
933     count = package->script->UniqueActionsCount;
934     package->script->UniqueActionsCount++;
935     if (count != 0)
936         newbuf = HeapReAlloc(GetProcessHeap(),0,
937                         package->script->UniqueActions,
938                         package->script->UniqueActionsCount* sizeof(LPWSTR));
939     else
940         newbuf = HeapAlloc(GetProcessHeap(),0, sizeof(LPWSTR));
941
942     newbuf[count] = strdupW(action);
943     package->script->UniqueActions = newbuf;
944
945     return ERROR_SUCCESS;
946 }
947
948 BOOL check_unique_action(MSIPACKAGE *package, LPCWSTR action)
949 {
950     INT i;
951
952     if (!package || !package->script)
953         return FALSE;
954
955     for (i = 0; i < package->script->UniqueActionsCount; i++)
956         if (!strcmpW(package->script->UniqueActions[i],action))
957             return TRUE;
958
959     return FALSE;
960 }
961
962 WCHAR* generate_error_string(MSIPACKAGE *package, UINT error, DWORD count, ... )
963 {
964     static const WCHAR query[] = {'S','E','L','E','C','T',' ','`','M','e','s','s','a','g','e','`',' ','F','R','O','M',' ','`','E','r','r','o','r','`',' ','W','H','E','R','E',' ','`','E','r','r','o','r','`',' ','=',' ','%','i',0};
965
966     MSIRECORD *rec;
967     MSIRECORD *row;
968     DWORD size = 0;
969     DWORD i;
970     va_list va;
971     LPCWSTR str;
972     LPWSTR data;
973
974     row = MSI_QueryGetRecord(package->db, query, error);
975     if (!row)
976         return 0;
977
978     rec = MSI_CreateRecord(count+2);
979
980     str = MSI_RecordGetString(row,1);
981     MSI_RecordSetStringW(rec,0,str);
982     msiobj_release( &row->hdr );
983     MSI_RecordSetInteger(rec,1,error);
984
985     va_start(va,count);
986     for (i = 0; i < count; i++)
987     {
988         str = va_arg(va,LPCWSTR);
989         MSI_RecordSetStringW(rec,(i+2),str);
990     }
991     va_end(va);
992
993     MSI_FormatRecordW(package,rec,NULL,&size);
994     if (size >= 0)
995     {
996         size++;
997         data = HeapAlloc(GetProcessHeap(),0,size*sizeof(WCHAR));
998         if (size > 1)
999             MSI_FormatRecordW(package,rec,data,&size);
1000         else
1001             data[0] = 0;
1002         msiobj_release( &rec->hdr );
1003         return data;
1004     }
1005
1006     msiobj_release( &rec->hdr );
1007     data = NULL;
1008     return data;
1009 }