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