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