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