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