Replace a few more direct lpVtbl accesses by the proper macros.
[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         }
280         else
281         {
282             path = load_dynamic_property(package,cszSourceDir,NULL);
283             if (!path)
284             {
285                 path = load_dynamic_property(package,cszDatabase,NULL);
286                 if (path)
287                 {
288                     p = strrchrW(path,'\\');
289                     if (p)
290                         *(p+1) = 0;
291                 }
292             }
293         }
294         if (folder)
295             *folder = get_loaded_folder( package, name );
296         return path;
297     }
298
299     f = get_loaded_folder( package, name );
300     if (!f)
301         return NULL;
302
303     if (folder)
304         *folder = f;
305
306     if (!source && f->ResolvedTarget)
307     {
308         path = strdupW( f->ResolvedTarget );
309         TRACE("   already resolved to %s\n",debugstr_w(path));
310         return path;
311     }
312     else if (source && f->ResolvedSource)
313     {
314         path = strdupW( f->ResolvedSource );
315         TRACE("   (source)already resolved to %s\n",debugstr_w(path));
316         return path;
317     }
318     else if (!source && f->Property)
319     {
320         path = build_directory_name( 2, f->Property, NULL );
321                     
322         TRACE("   internally set to %s\n",debugstr_w(path));
323         if (set_prop)
324             MSI_SetPropertyW( package, name, path );
325         return path;
326     }
327
328     if (f->Parent)
329     {
330         LPWSTR parent = f->Parent->Directory;
331
332         TRACE(" ! Parent is %s\n", debugstr_w(parent));
333
334         p = resolve_folder(package, parent, source, set_prop, NULL);
335         if (!source)
336         {
337             TRACE("   TargetDefault = %s\n", debugstr_w(f->TargetDefault));
338
339             path = build_directory_name( 3, p, f->TargetDefault, NULL );
340             f->ResolvedTarget = strdupW( path );
341             TRACE("   resolved into %s\n",debugstr_w(path));
342             if (set_prop)
343                 MSI_SetPropertyW(package,name,path);
344         }
345         else 
346         {
347             if (f->SourceDefault && f->SourceDefault[0]!='.')
348                 path = build_directory_name( 3, p, f->SourceDefault, NULL );
349             else
350                 path = strdupW(p);
351             TRACE("   (source)resolved into %s\n",debugstr_w(path));
352             f->ResolvedSource = strdupW( path );
353         }
354         HeapFree(GetProcessHeap(),0,p);
355     }
356     return path;
357 }
358
359 /* wrapper to resist a need for a full rewrite right now */
360 DWORD deformat_string(MSIPACKAGE *package, LPCWSTR ptr, WCHAR** data )
361 {
362     if (ptr)
363     {
364         MSIRECORD *rec = MSI_CreateRecord(1);
365         DWORD size = 0;
366
367         MSI_RecordSetStringW(rec,0,ptr);
368         MSI_FormatRecordW(package,rec,NULL,&size);
369         if (size >= 0)
370         {
371             size++;
372             *data = HeapAlloc(GetProcessHeap(),0,size*sizeof(WCHAR));
373             if (size > 1)
374                 MSI_FormatRecordW(package,rec,*data,&size);
375             else
376                 *data[0] = 0;
377             msiobj_release( &rec->hdr );
378             return sizeof(WCHAR)*size;
379         }
380         msiobj_release( &rec->hdr );
381     }
382
383     *data = NULL;
384     return 0;
385 }
386
387 UINT schedule_action(MSIPACKAGE *package, UINT script, LPCWSTR action)
388 {
389     UINT count;
390     LPWSTR *newbuf = NULL;
391     if (script >= TOTAL_SCRIPTS)
392     {
393         FIXME("Unknown script requested %i\n",script);
394         return ERROR_FUNCTION_FAILED;
395     }
396     TRACE("Scheduling Action %s in script %i\n",debugstr_w(action), script);
397     
398     count = package->script->ActionCount[script];
399     package->script->ActionCount[script]++;
400     if (count != 0)
401         newbuf = HeapReAlloc(GetProcessHeap(),0,
402                         package->script->Actions[script],
403                         package->script->ActionCount[script]* sizeof(LPWSTR));
404     else
405         newbuf = HeapAlloc(GetProcessHeap(),0, sizeof(LPWSTR));
406
407     newbuf[count] = strdupW(action);
408     package->script->Actions[script] = newbuf;
409
410    return ERROR_SUCCESS;
411 }
412
413 static void remove_tracked_tempfiles(MSIPACKAGE* package)
414 {
415     struct list *item, *cursor;
416
417     LIST_FOR_EACH_SAFE( item, cursor, &package->tempfiles )
418     {
419         MSITEMPFILE *temp = LIST_ENTRY( item, MSITEMPFILE, entry );
420
421         list_remove( &temp->entry );
422         TRACE("deleting temp file %s\n", debugstr_w( temp->Path ));
423         DeleteFileW( temp->Path );
424         HeapFree( GetProcessHeap(), 0, temp->File );
425         HeapFree( GetProcessHeap(), 0, temp->Path );
426         HeapFree( GetProcessHeap(), 0, temp );
427     }
428 }
429
430 static void free_feature( MSIFEATURE *feature )
431 {
432     struct list *item, *cursor;
433
434     LIST_FOR_EACH_SAFE( item, cursor, &feature->Components )
435     {
436         ComponentList *cl = LIST_ENTRY( item, ComponentList, entry );
437         list_remove( &cl->entry );
438         HeapFree( GetProcessHeap(), 0, cl );
439     }
440     HeapFree( GetProcessHeap(), 0, feature );
441 }
442
443 void free_extension( MSIEXTENSION *ext )
444 {
445     struct list *item, *cursor;
446
447     LIST_FOR_EACH_SAFE( item, cursor, &ext->verbs )
448     {
449         MSIVERB *verb = LIST_ENTRY( item, MSIVERB, entry );
450
451         list_remove( &verb->entry );
452         HeapFree( GetProcessHeap(), 0, verb->Verb );
453         HeapFree( GetProcessHeap(), 0, verb->Command );
454         HeapFree( GetProcessHeap(), 0, verb->Argument );
455         HeapFree( GetProcessHeap(), 0, verb );
456     }
457
458     HeapFree( GetProcessHeap(), 0, ext->ProgIDText );
459     HeapFree( GetProcessHeap(), 0, ext );
460 }
461
462
463 /* Called when the package is being closed */
464 void ACTION_free_package_structures( MSIPACKAGE* package)
465 {
466     INT i;
467     struct list *item, *cursor;
468     
469     TRACE("Freeing package action data\n");
470
471     remove_tracked_tempfiles(package);
472
473     LIST_FOR_EACH_SAFE( item, cursor, &package->features )
474     {
475         MSIFEATURE *feature = LIST_ENTRY( item, MSIFEATURE, entry );
476         list_remove( &feature->entry );
477         free_feature( feature );
478     }
479
480     LIST_FOR_EACH_SAFE( item, cursor, &package->folders )
481     {
482         MSIFOLDER *folder = LIST_ENTRY( item, MSIFOLDER, entry );
483
484         list_remove( &folder->entry );
485         HeapFree( GetProcessHeap(), 0, folder->Directory );
486         HeapFree( GetProcessHeap(), 0, folder->TargetDefault );
487         HeapFree( GetProcessHeap(), 0, folder->SourceDefault );
488         HeapFree( GetProcessHeap(), 0, folder->ResolvedTarget );
489         HeapFree( GetProcessHeap(), 0, folder->ResolvedSource );
490         HeapFree( GetProcessHeap(), 0, folder->Property );
491     }
492
493     LIST_FOR_EACH_SAFE( item, cursor, &package->components )
494     {
495         MSICOMPONENT *comp = LIST_ENTRY( item, MSICOMPONENT, entry );
496         
497         list_remove( &comp->entry );
498         HeapFree( GetProcessHeap(), 0, comp->Condition );
499         HeapFree( GetProcessHeap(), 0, comp->FullKeypath );
500         HeapFree( GetProcessHeap(), 0, comp );
501     }
502
503     LIST_FOR_EACH_SAFE( item, cursor, &package->files )
504     {
505         MSIFILE *file = LIST_ENTRY( item, MSIFILE, entry );
506
507         list_remove( &file->entry );
508         HeapFree( GetProcessHeap(), 0, file->File );
509         HeapFree( GetProcessHeap(), 0, file->FileName );
510         HeapFree( GetProcessHeap(), 0, file->ShortName );
511         HeapFree( GetProcessHeap(), 0, file->Version );
512         HeapFree( GetProcessHeap(), 0, file->Language );
513         HeapFree( GetProcessHeap(), 0, file->SourcePath );
514         HeapFree( GetProcessHeap(), 0, file->TargetPath );
515         HeapFree( GetProcessHeap(), 0, file );
516     }
517
518     /* clean up extension, progid, class and verb structures */
519     LIST_FOR_EACH_SAFE( item, cursor, &package->classes )
520     {
521         MSICLASS *cls = LIST_ENTRY( item, MSICLASS, entry );
522
523         list_remove( &cls->entry );
524         HeapFree( GetProcessHeap(), 0, cls->Description );
525         HeapFree( GetProcessHeap(), 0, cls->FileTypeMask );
526         HeapFree( GetProcessHeap(), 0, cls->IconPath );
527         HeapFree( GetProcessHeap(), 0, cls->DefInprocHandler );
528         HeapFree( GetProcessHeap(), 0, cls->DefInprocHandler32 );
529         HeapFree( GetProcessHeap(), 0, cls->Argument );
530         HeapFree( GetProcessHeap(), 0, cls->ProgIDText );
531         HeapFree( GetProcessHeap(), 0, cls );
532     }
533
534     LIST_FOR_EACH_SAFE( item, cursor, &package->extensions )
535     {
536         MSIEXTENSION *ext = LIST_ENTRY( item, MSIEXTENSION, entry );
537
538         list_remove( &ext->entry );
539         free_extension( ext );
540     }
541
542     LIST_FOR_EACH_SAFE( item, cursor, &package->progids )
543     {
544         MSIPROGID *progid = LIST_ENTRY( item, MSIPROGID, entry );
545
546         list_remove( &progid->entry );
547         HeapFree( GetProcessHeap(), 0, progid->ProgID );
548         HeapFree( GetProcessHeap(), 0, progid->Description );
549         HeapFree( GetProcessHeap(), 0, progid->IconPath );
550         HeapFree( GetProcessHeap(), 0, progid );
551     }
552
553     LIST_FOR_EACH_SAFE( item, cursor, &package->mimes )
554     {
555         MSIMIME *mt = LIST_ENTRY( item, MSIMIME, entry );
556
557         list_remove( &mt->entry );
558         HeapFree( GetProcessHeap(), 0, mt->ContentType );
559         HeapFree( GetProcessHeap(), 0, mt );
560     }
561
562     LIST_FOR_EACH_SAFE( item, cursor, &package->appids )
563     {
564         MSIAPPID *appid = LIST_ENTRY( item, MSIAPPID, entry );
565
566         list_remove( &appid->entry );
567         HeapFree( GetProcessHeap(), 0, appid->RemoteServerName );
568         HeapFree( GetProcessHeap(), 0, appid->LocalServer );
569         HeapFree( GetProcessHeap(), 0, appid->ServiceParameters );
570         HeapFree( GetProcessHeap(), 0, appid->DllSurrogate );
571         HeapFree( GetProcessHeap(), 0, appid );
572     }
573
574     if (package->script)
575     {
576         for (i = 0; i < TOTAL_SCRIPTS; i++)
577         {
578             int j;
579             for (j = 0; j < package->script->ActionCount[i]; j++)
580                 HeapFree(GetProcessHeap(),0,package->script->Actions[i][j]);
581         
582             HeapFree(GetProcessHeap(),0,package->script->Actions[i]);
583         }
584
585         for (i = 0; i < package->script->UniqueActionsCount; i++)
586             HeapFree(GetProcessHeap(),0,package->script->UniqueActions[i]);
587
588         HeapFree(GetProcessHeap(),0,package->script->UniqueActions);
589         HeapFree(GetProcessHeap(),0,package->script);
590     }
591
592     HeapFree(GetProcessHeap(),0,package->PackagePath);
593     HeapFree(GetProcessHeap(),0,package->msiFilePath);
594     HeapFree(GetProcessHeap(),0,package->ProductCode);
595
596     /* cleanup control event subscriptions */
597     ControlEvent_CleanupSubscriptions(package);
598 }
599
600 /*
601  *  build_directory_name()
602  *
603  *  This function is to save messing round with directory names
604  *  It handles adding backslashes between path segments, 
605  *   and can add \ at the end of the directory name if told to.
606  *
607  *  It takes a variable number of arguments.
608  *  It always allocates a new string for the result, so make sure
609  *   to free the return value when finished with it.
610  *
611  *  The first arg is the number of path segments that follow.
612  *  The arguments following count are a list of path segments.
613  *  A path segment may be NULL.
614  *
615  *  Path segments will be added with a \ separating them.
616  *  A \ will not be added after the last segment, however if the
617  *    last segment is NULL, then the last character will be a \
618  * 
619  */
620 LPWSTR build_directory_name(DWORD count, ...)
621 {
622     DWORD sz = 1, i;
623     LPWSTR dir;
624     va_list va;
625
626     va_start(va,count);
627     for(i=0; i<count; i++)
628     {
629         LPCWSTR str = va_arg(va,LPCWSTR);
630         if (str)
631             sz += strlenW(str) + 1;
632     }
633     va_end(va);
634
635     dir = HeapAlloc(GetProcessHeap(), 0, sz*sizeof(WCHAR));
636     dir[0]=0;
637
638     va_start(va,count);
639     for(i=0; i<count; i++)
640     {
641         LPCWSTR str = va_arg(va,LPCWSTR);
642         if (!str)
643             continue;
644         strcatW(dir, str);
645         if( ((i+1)!=count) && dir[strlenW(dir)-1]!='\\')
646             strcatW(dir, cszbs);
647     }
648     return dir;
649 }
650
651 /***********************************************************************
652  *            create_full_pathW
653  *
654  * Recursively create all directories in the path.
655  *
656  * shamelessly stolen from setupapi/queue.c
657  */
658 BOOL create_full_pathW(const WCHAR *path)
659 {
660     BOOL ret = TRUE;
661     int len;
662     WCHAR *new_path;
663
664     new_path = HeapAlloc(GetProcessHeap(), 0, (strlenW(path) + 1) *
665                                               sizeof(WCHAR));
666
667     strcpyW(new_path, path);
668
669     while((len = strlenW(new_path)) && new_path[len - 1] == '\\')
670     new_path[len - 1] = 0;
671
672     while(!CreateDirectoryW(new_path, NULL))
673     {
674         WCHAR *slash;
675         DWORD last_error = GetLastError();
676         if(last_error == ERROR_ALREADY_EXISTS)
677             break;
678
679         if(last_error != ERROR_PATH_NOT_FOUND)
680         {
681             ret = FALSE;
682             break;
683         }
684
685         if(!(slash = strrchrW(new_path, '\\')))
686         {
687             ret = FALSE;
688             break;
689         }
690
691         len = slash - new_path;
692         new_path[len] = 0;
693         if(!create_full_pathW(new_path))
694         {
695             ret = FALSE;
696             break;
697         }
698         new_path[len] = '\\';
699     }
700
701     HeapFree(GetProcessHeap(), 0, new_path);
702     return ret;
703 }
704
705 void ui_progress(MSIPACKAGE *package, int a, int b, int c, int d )
706 {
707     MSIRECORD * row;
708
709     row = MSI_CreateRecord(4);
710     MSI_RecordSetInteger(row,1,a);
711     MSI_RecordSetInteger(row,2,b);
712     MSI_RecordSetInteger(row,3,c);
713     MSI_RecordSetInteger(row,4,d);
714     MSI_ProcessMessage(package, INSTALLMESSAGE_PROGRESS, row);
715     msiobj_release(&row->hdr);
716
717     msi_dialog_check_messages(NULL);
718 }
719
720 void ui_actiondata(MSIPACKAGE *package, LPCWSTR action, MSIRECORD * record)
721 {
722     static const WCHAR Query_t[] = 
723         {'S','E','L','E','C','T',' ','*',' ','F','R','O','M',' ',
724          '`','A','c','t','i','o', 'n','T','e','x','t','`',' ',
725          'W','H','E','R','E',' ', '`','A','c','t','i','o','n','`',' ','=', 
726          ' ','\'','%','s','\'',0};
727     WCHAR message[1024];
728     MSIRECORD * row = 0;
729     DWORD size;
730     static const WCHAR szActionData[] = 
731         {'A','c','t','i','o','n','D','a','t','a',0};
732
733     if (!package->LastAction || strcmpW(package->LastAction,action))
734     {
735         row = MSI_QueryGetRecord(package->db, Query_t, action);
736         if (!row)
737             return;
738
739         if (MSI_RecordIsNull(row,3))
740         {
741             msiobj_release(&row->hdr);
742             return;
743         }
744
745         /* update the cached actionformat */
746         HeapFree(GetProcessHeap(),0,package->ActionFormat);
747         package->ActionFormat = load_dynamic_stringW(row,3);
748
749         HeapFree(GetProcessHeap(),0,package->LastAction);
750         package->LastAction = strdupW(action);
751
752         msiobj_release(&row->hdr);
753     }
754
755     MSI_RecordSetStringW(record,0,package->ActionFormat);
756     size = 1024;
757     MSI_FormatRecordW(package,record,message,&size);
758
759     row = MSI_CreateRecord(1);
760     MSI_RecordSetStringW(row,1,message);
761  
762     MSI_ProcessMessage(package, INSTALLMESSAGE_ACTIONDATA, row);
763
764     ControlEvent_FireSubscribedEvent(package,szActionData, row);
765
766     msiobj_release(&row->hdr);
767 }
768
769 BOOL ACTION_VerifyComponentForAction(MSIPACKAGE* package, MSICOMPONENT* comp,
770                                             INSTALLSTATE check )
771 {
772     if (comp->Installed == check)
773         return FALSE;
774
775     if (comp->ActionRequest == check)
776         return TRUE;
777     else
778         return FALSE;
779 }
780
781 BOOL ACTION_VerifyFeatureForAction( MSIFEATURE* feature, INSTALLSTATE check )
782 {
783     if (feature->Installed == check)
784         return FALSE;
785
786     if (feature->ActionRequest == check)
787         return TRUE;
788     else
789         return FALSE;
790 }
791
792 void reduce_to_longfilename(WCHAR* filename)
793 {
794     LPWSTR p = strchrW(filename,'|');
795     if (p)
796         memmove(filename, p+1, (strlenW(p+1)+1)*sizeof(WCHAR));
797 }
798
799 void reduce_to_shortfilename(WCHAR* filename)
800 {
801     LPWSTR p = strchrW(filename,'|');
802     if (p)
803         *p = 0;
804 }
805
806 LPWSTR create_component_advertise_string(MSIPACKAGE* package, 
807                 MSICOMPONENT* component, LPCWSTR feature)
808 {
809     GUID clsid;
810     WCHAR productid_85[21];
811     WCHAR component_85[21];
812     /*
813      * I have a fair bit of confusion as to when a < is used and when a > is
814      * used. I do not think i have it right...
815      *
816      * Ok it appears that the > is used if there is a guid for the compoenent
817      * and the < is used if not.
818      */
819     static WCHAR fmt1[] = {'%','s','%','s','<',0,0};
820     static WCHAR fmt2[] = {'%','s','%','s','>','%','s',0,0};
821     LPWSTR output = NULL;
822     DWORD sz = 0;
823
824     memset(productid_85,0,sizeof(productid_85));
825     memset(component_85,0,sizeof(component_85));
826
827     CLSIDFromString(package->ProductCode, &clsid);
828     
829     encode_base85_guid(&clsid,productid_85);
830
831     CLSIDFromString(component->ComponentId, &clsid);
832     encode_base85_guid(&clsid,component_85);
833
834     TRACE("Doing something with this... %s %s %s\n", 
835             debugstr_w(productid_85), debugstr_w(feature),
836             debugstr_w(component_85));
837  
838     sz = lstrlenW(productid_85) + lstrlenW(feature);
839     if (component)
840         sz += lstrlenW(component_85);
841
842     sz+=3;
843     sz *= sizeof(WCHAR);
844            
845     output = HeapAlloc(GetProcessHeap(),0,sz);
846     memset(output,0,sz);
847
848     if (component)
849         sprintfW(output,fmt2,productid_85,feature,component_85);
850     else
851         sprintfW(output,fmt1,productid_85,feature);
852     
853     return output;
854 }
855
856 /* update compoennt state based on a feature change */
857 void ACTION_UpdateComponentStates(MSIPACKAGE *package, LPCWSTR szFeature)
858 {
859     INSTALLSTATE newstate;
860     MSIFEATURE *feature;
861     ComponentList *cl;
862
863     feature = get_loaded_feature(package,szFeature);
864     if (!feature)
865         return;
866
867     newstate = feature->ActionRequest;
868
869     LIST_FOR_EACH_ENTRY( cl, &feature->Components, ComponentList, entry )
870     {
871         MSICOMPONENT* component = cl->component;
872     
873         TRACE("MODIFYING(%i): Component %s (Installed %i, Action %i, Request %i)\n",
874             newstate, debugstr_w(component->Component), component->Installed, 
875             component->Action, component->ActionRequest);
876         
877         if (!component->Enabled)
878             continue;
879  
880         if (newstate == INSTALLSTATE_LOCAL)
881         {
882             component->ActionRequest = INSTALLSTATE_LOCAL;
883             component->Action = INSTALLSTATE_LOCAL;
884         }
885         else 
886         {
887             ComponentList *clist;
888             MSIFEATURE *f;
889
890             component->ActionRequest = newstate;
891             component->Action = newstate;
892
893             /*if any other feature wants is local we need to set it local*/
894             LIST_FOR_EACH_ENTRY( f, &package->features, MSIFEATURE, entry )
895             {
896                 if ( component->ActionRequest != INSTALLSTATE_LOCAL )
897                     break;
898
899                 LIST_FOR_EACH_ENTRY( clist, &f->Components, ComponentList, entry )
900                 {
901                     if ( clist->component == component )
902                     {
903                         if (f->ActionRequest == INSTALLSTATE_LOCAL)
904                         {
905                             TRACE("Saved by %s\n", debugstr_w(f->Feature));
906                             component->ActionRequest = INSTALLSTATE_LOCAL;
907                             component->Action = INSTALLSTATE_LOCAL;
908                         }
909                         break;
910                     }
911                 }
912             }
913         }
914         TRACE("Result (%i): Component %s (Installed %i, Action %i, Request %i)\n",
915             newstate, debugstr_w(component->Component), component->Installed, 
916             component->Action, component->ActionRequest);
917     } 
918 }
919
920 UINT register_unique_action(MSIPACKAGE *package, LPCWSTR action)
921 {
922     UINT count;
923     LPWSTR *newbuf = NULL;
924
925     if (!package || !package->script)
926         return FALSE;
927
928     TRACE("Registering Action %s as having fun\n",debugstr_w(action));
929     
930     count = package->script->UniqueActionsCount;
931     package->script->UniqueActionsCount++;
932     if (count != 0)
933         newbuf = HeapReAlloc(GetProcessHeap(),0,
934                         package->script->UniqueActions,
935                         package->script->UniqueActionsCount* sizeof(LPWSTR));
936     else
937         newbuf = HeapAlloc(GetProcessHeap(),0, sizeof(LPWSTR));
938
939     newbuf[count] = strdupW(action);
940     package->script->UniqueActions = newbuf;
941
942     return ERROR_SUCCESS;
943 }
944
945 BOOL check_unique_action(MSIPACKAGE *package, LPCWSTR action)
946 {
947     INT i;
948
949     if (!package || !package->script)
950         return FALSE;
951
952     for (i = 0; i < package->script->UniqueActionsCount; i++)
953         if (!strcmpW(package->script->UniqueActions[i],action))
954             return TRUE;
955
956     return FALSE;
957 }
958
959 WCHAR* generate_error_string(MSIPACKAGE *package, UINT error, DWORD count, ... )
960 {
961     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};
962
963     MSIRECORD *rec;
964     MSIRECORD *row;
965     DWORD size = 0;
966     DWORD i;
967     va_list va;
968     LPCWSTR str;
969     LPWSTR data;
970
971     row = MSI_QueryGetRecord(package->db, query, error);
972     if (!row)
973         return 0;
974
975     rec = MSI_CreateRecord(count+2);
976
977     str = MSI_RecordGetString(row,1);
978     MSI_RecordSetStringW(rec,0,str);
979     msiobj_release( &row->hdr );
980     MSI_RecordSetInteger(rec,1,error);
981
982     va_start(va,count);
983     for (i = 0; i < count; i++)
984     {
985         str = va_arg(va,LPCWSTR);
986         MSI_RecordSetStringW(rec,(i+2),str);
987     }
988     va_end(va);
989
990     MSI_FormatRecordW(package,rec,NULL,&size);
991     if (size >= 0)
992     {
993         size++;
994         data = HeapAlloc(GetProcessHeap(),0,size*sizeof(WCHAR));
995         if (size > 1)
996             MSI_FormatRecordW(package,rec,data,&size);
997         else
998             data[0] = 0;
999         msiobj_release( &rec->hdr );
1000         return data;
1001     }
1002
1003     msiobj_release( &rec->hdr );
1004     data = NULL;
1005     return data;
1006 }