1#**************************************************************
2#
3#  Licensed to the Apache Software Foundation (ASF) under one
4#  or more contributor license agreements.  See the NOTICE file
5#  distributed with this work for additional information
6#  regarding copyright ownership.  The ASF licenses this file
7#  to you under the Apache License, Version 2.0 (the
8#  "License"); you may not use this file except in compliance
9#  with the License.  You may obtain a copy of the License at
10#
11#    http://www.apache.org/licenses/LICENSE-2.0
12#
13#  Unless required by applicable law or agreed to in writing,
14#  software distributed under the License is distributed on an
15#  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16#  KIND, either express or implied.  See the License for the
17#  specific language governing permissions and limitations
18#  under the License.
19#
20#**************************************************************
21
22
23
24package installer::windows::component;
25
26use installer::converter;
27use installer::existence;
28use installer::exiter;
29use installer::files;
30use installer::globals;
31use installer::windows::idtglobal;
32use installer::windows::language;
33
34use strict;
35
36##############################################################
37# Returning a globally unique ID (GUID) for a component
38# If the component is new, a unique guid has to be created.
39# If the component already exists, the guid has to be
40# taken from a list component <-> guid
41# Sample for a guid: {B68FD953-3CEF-4489-8269-8726848056E8}
42##############################################################
43
44sub get_component_guid ($)
45{
46	my ($componentname) = @_;
47
48	# At this time only a template
49	my $returnvalue = "\{COMPONENTGUID\}";
50
51	# Returning a ComponentID, that is assigned in scp project
52	if ( exists($installer::globals::componentid{$componentname}) )
53	{
54        $installer::logger::Lang->printf("reusing guid %s for component %s\n",
55            $installer::globals::componentid{$componentname},
56            $componentname);
57		$returnvalue = "\{" . $installer::globals::componentid{$componentname} . "\}";
58	}
59
60	return $returnvalue;
61}
62
63##############################################################
64# Returning the directory for a file component.
65##############################################################
66
67sub get_file_component_directory ($$$)
68{
69    my ($componentname, $filesref, $dirref) = @_;
70
71    my ($component,  $uniquedir);
72
73    foreach my $onefile (@$filesref)
74    {
75        if ($onefile->{'componentname'} eq $componentname)
76        {
77            return get_file_component_directory_for_file($onefile, $dirref);
78        }
79    }
80
81    # This component can be ignored, if it exists in a version with
82    # extension "_pff" (this was renamed in file::get_sequence_for_file() )
83    my $ignore_this_component = 0;
84    my $origcomponentname = $componentname;
85    my $componentname_pff = $componentname . "_pff";
86
87    foreach my $onefile (@$filesref)
88    {
89        if ($onefile->{'componentname'} eq $componentname_pff)
90        {
91            return "IGNORE_COMP";
92        }
93    }
94
95    installer::exiter::exit_program(
96        "ERROR: Did not find component \"$origcomponentname\" in file collection",
97        "get_file_component_directory");
98}
99
100
101
102
103sub get_file_component_directory_for_file ($$)
104{
105    my ($onefile, $dirref) = @_;
106
107    my $localstyles = $onefile->{'Styles'};
108    $localstyles = "" unless defined $localstyles;
109
110    if ( $localstyles =~ /\bFONT\b/ )	# special handling for font files
111    {
112	return $installer::globals::fontsfolder;
113    }
114
115    my $destdir = "";
116
117    if ( $onefile->{'Dir'} ) { $destdir = $onefile->{'Dir'}; }
118
119    if ( $destdir =~ /\bPREDEFINED_OSSHELLNEWDIR\b/ )	# special handling for shellnew files
120    {
121	return $installer::globals::templatefolder;
122    }
123
124    my $destination = $onefile->{'destination'};
125
126    installer::pathanalyzer::get_path_from_fullqualifiedname(\$destination);
127
128    $destination =~ s/\Q$installer::globals::separator\E\s*$//;
129
130    # This path has to be defined in the directory collection at "HostName"
131
132    my $uniquedir = undef;
133    if ($destination eq "")		# files in the installation root
134    {
135	$uniquedir = "INSTALLLOCATION";
136    }
137    else
138    {
139	my $found = 0;
140        foreach my $directory (@$dirref)
141	{
142	    if ($directory->{'HostName'} eq $destination)
143	    {
144		$found = 1;
145                $uniquedir = $directory->{'uniquename'};
146		last;
147	    }
148	}
149
150	if ( ! $found)
151	{
152	    installer::exiter::exit_program(
153                "ERROR: Did not find destination $destination in directory collection",
154                "get_file_component_directory");
155	}
156
157	if ( $uniquedir eq $installer::globals::officeinstalldirectory )
158	{
159	    $uniquedir = "INSTALLLOCATION";
160	}
161    }
162
163    $onefile->{'uniquedirname'} = $uniquedir;		# saving it in the file collection
164
165    return $uniquedir
166}
167
168##############################################################
169# Returning the directory for a registry component.
170# This cannot be a useful value
171##############################################################
172
173sub get_registry_component_directory
174{
175	my $componentdir = "INSTALLLOCATION";
176
177	return $componentdir;
178}
179
180##############################################################
181# Returning the attributes for a file component.
182# Always 8 in this first try?
183##############################################################
184
185sub get_file_component_attributes
186{
187	my ($componentname, $filesref, $allvariables) = @_;
188
189	my $attributes;
190
191	$attributes = 2;
192
193	# special handling for font files
194
195	my $onefile;
196	my $found = 0;
197
198	for ( my $i = 0; $i <= $#{$filesref}; $i++ )
199	{
200		$onefile = 	${$filesref}[$i];
201		my $component = $onefile->{'componentname'};
202
203		if ( $component eq $componentname )
204		{
205			$found = 1;
206			last;
207		}
208	}
209
210	if (!($found))
211	{
212		installer::exiter::exit_program("ERROR: Did not find component in file collection", "get_file_component_attributes");
213	}
214
215	my $localstyles = "";
216
217	if ( $onefile->{'Styles'} ) { $localstyles = $onefile->{'Styles'}; }
218
219	if ( $localstyles =~ /\bFONT\b/ )
220	{
221		$attributes = 8;	# font files will be deinstalled if the ref count is 0
222	}
223
224	if ( $localstyles =~ /\bASSEMBLY\b/ )
225	{
226		$attributes = 0;	# Assembly files cannot run from source
227	}
228
229	if ((defined $onefile->{'Dir'} && $onefile->{'Dir'} =~ /\bPREDEFINED_OSSHELLNEWDIR\b/)
230        || $onefile->{'needs_user_registry_key'})
231	{
232		$attributes = 4;	# Files in shellnew dir and in non advertised startmenu entries must have user registry key as KeyPath
233	}
234
235	# Adding 256, if this is a 64 bit installation set.
236	if (( $allvariables->{'64BITPRODUCT'} ) && ( $allvariables->{'64BITPRODUCT'} == 1 )) { $attributes = $attributes + 256; }
237
238	return $attributes
239}
240
241##############################################################
242# Returning the attributes for a registry component.
243# Always 4, indicating, the keypath is a defined in
244# table registry
245##############################################################
246
247sub get_registry_component_attributes
248{
249	my ($componentname, $allvariables) = @_;
250
251	my $attributes;
252
253	$attributes = 4;
254
255	# Adding 256, if this is a 64 bit installation set.
256	if (( $allvariables->{'64BITPRODUCT'} ) && ( $allvariables->{'64BITPRODUCT'} == 1 )) { $attributes = $attributes + 256; }
257
258	if ( exists($installer::globals::dontdeletecomponents{$componentname}) ) { $attributes = $attributes + 16; }
259
260	return $attributes
261}
262
263##############################################################
264# Returning the conditions for a component.
265# This is important for language dependent components
266# in multilingual installation sets.
267##############################################################
268
269sub get_file_component_condition
270{
271	my ($componentname, $filesref) = @_;
272
273	my $condition = "";
274
275	if (exists($installer::globals::componentcondition{$componentname}))
276	{
277		$condition = $installer::globals::componentcondition{$componentname};
278	}
279
280	# there can be also tree conditions for multilayer products
281	if (exists($installer::globals::treeconditions{$componentname}))
282	{
283		if ( $condition eq "" )
284		{
285			$condition = $installer::globals::treeconditions{$componentname};
286		}
287		else
288		{
289			$condition = "($condition) And ($installer::globals::treeconditions{$componentname})";
290		}
291	}
292
293	return $condition
294}
295
296##############################################################
297# Returning the conditions for a registry component.
298##############################################################
299
300sub get_component_condition
301{
302	my ($componentname) = @_;
303
304	my $condition;
305
306	$condition = "";	# Always ?
307
308	if (exists($installer::globals::componentcondition{$componentname}))
309	{
310		$condition = $installer::globals::componentcondition{$componentname};
311	}
312
313	return $condition
314}
315
316####################################################################
317# Returning the keypath for a component.
318# This will be the name of the first file/registry, found in the
319# collection $itemsref
320# Attention: This has to be the unique (file)name, not the
321# real filename!
322####################################################################
323
324sub get_component_keypath ($$)
325{
326	my ($componentname, $itemsref) = @_;
327
328	foreach my $oneitem (@$itemsref)
329	{
330		my $component = $oneitem->{'componentname'};
331
332		if ( ! defined $component)
333        {
334            installer::scriptitems::print_script_item($oneitem);
335            installer::logger::PrintError("item in get_component_keypath has no 'componentname'\n");
336            return "";
337        }
338		if ( $component eq $componentname )
339		{
340            my $keypath = $oneitem->{'uniquename'};	# "uniquename", not "Name"
341
342            # Special handling for components in
343            # PREDEFINED_OSSHELLNEWDIR. These components need as
344            # KeyPath a RegistryItem in HKCU
345            if ($oneitem->{'userregkeypath'})
346            {
347                $keypath = $oneitem->{'userregkeypath'};
348            }
349
350            # saving it in the file and registry collection
351            $oneitem->{'keypath'} = $keypath;
352
353            return $keypath
354		}
355	}
356
357    installer::exiter::exit_program(
358        "ERROR: Did not find component in file/registry collection, function get_component_keypath",
359        "get_component_keypath");
360}
361
362
363
364
365sub remove_ooversion_from_component_name($)
366{
367    my ($component_name) = @_;
368
369    $component_name =~ s/_openoffice\d+//;
370
371    return $component_name;
372}
373
374
375
376
377sub prepare_component_table_creation ($$$)
378{
379    my ($file_components, $registry_components, $variables) = @_;
380
381    if ($installer::globals::is_release)
382    {
383        my %source_component_data = ();
384
385        # Collect the components that are used in the source release.
386        my $component_table = $installer::globals::source_msi->GetTable("Component");
387        foreach my $row (@{$component_table->GetAllRows()})
388        {
389            $source_component_data{$row->GetValue("Component")} = $row;
390        }
391
392        # Find source components that do not exist in the target components, ie have been removed.
393
394        # Process file components.
395        my @missing_source_component_names = ();
396        my %file_component_hash = map {$_ => 1} @$file_components;
397        foreach my $source_component_name (keys %source_component_data)
398        {
399            # In this loop we only process components for files and ignore those for registry entries.
400            next if $source_component_name =~ /^registry_/;
401
402            if ( ! defined $file_component_hash{$source_component_name})
403            {
404                push @missing_source_component_names, [$source_component_name, $source_component_name];
405                $installer::logger::Info->printf("missing file component %s\n", $source_component_name);
406            }
407        }
408
409        # Process registry components.
410        my %registry_component_hash = map {$_ => 1} @$registry_components;
411        my %registry_component_hash_normalized = map {remove_ooversion_from_component_name($_) => $_} @$registry_components;
412        my %target_registry_component_translation = ();
413        foreach my $source_component_name (keys %source_component_data)
414        {
415            # In this loop we only process components for registry entries and ignore those for files.
416            next if $source_component_name !~ /^registry_/;
417
418            if (defined $registry_component_hash{$source_component_name})
419            {
420                # Found the non-normalized name.
421            }
422            elsif (defined $registry_component_hash_normalized{
423                remove_ooversion_from_component_name($source_component_name)})
424            {
425                # Found the normalized name.
426                my $target_component_name = $registry_component_hash_normalized{
427                    remove_ooversion_from_component_name($source_component_name)};
428                $target_registry_component_translation{$target_component_name} = $source_component_name;
429                $installer::logger::Info->printf("found normalized component name %s\n", $source_component_name);
430                $installer::logger::Info->printf("    %s -> %s\n", $target_component_name, $source_component_name);
431            }
432            else
433            {
434                # Source component was not found.
435                push @missing_source_component_names, $source_component_name;
436                $installer::logger::Info->printf("missing component %s\n", $source_component_name);
437            }
438        }
439
440        if (scalar @missing_source_component_names > 0)
441        {
442            $installer::logger::Info->printf("Error: there are %d missing components\n",
443                scalar @missing_source_component_names);
444            return {};
445        }
446        else
447        {
448            return \%target_registry_component_translation;
449        }
450    }
451
452    return {};
453}
454
455
456
457
458sub get_component_data ($$$$)
459{
460	my ($file_component_names,
461        $registry_component_names,
462        $files,
463        $registry_entries) = @_;
464
465    # When we are building a release then prepare building a patch by looking up some data
466    # from the previous release.
467    my %source_data = ();
468    if ($installer::globals::is_release)
469    {
470        my $source_component_table = $installer::globals::source_msi->GetTable("Component");
471        my $component_column_index = $source_component_table->GetColumnIndex("Component");
472        my $component_id_column_index = $source_component_table->GetColumnIndex("ComponentId");
473        my $key_path_column_index = $source_component_table->GetColumnIndex("KeyPath");
474        foreach my $source_row (@{$source_component_table->GetAllRows()})
475        {
476            my $component_name = $source_row->GetValue($component_column_index);
477            my $component_id = $source_row->GetValue($component_id_column_index);
478            my $key_path = $source_row->GetValue($key_path_column_index);
479
480            $source_data{$component_name} = {
481                'component_id' => $component_id,
482                'key_path' => $key_path
483            };
484        }
485    }
486
487    # Set up data for the target release.
488    # Use data from the source version where possible.
489    # Create missind data where necessary.
490
491    # Set up the target data with flags that remember whether a
492    # component contains files or registry entries.
493    my %target_data = ();
494    foreach my $name (@$file_component_names)
495    {
496        $target_data{$name} = {'is_file' => 1};
497    }
498    foreach my $name (@$registry_component_names)
499    {
500        $target_data{$name} = {'is_file' => 0};
501    }
502
503    # Add values for the ComponentId column.
504    $installer::logger::Lang->printf("preparing Component->ComponentId values\n");
505    foreach my $name (@$file_component_names,@$registry_component_names)
506    {
507        # Determine the component id.
508        my $guid = $installer::globals::is_release
509            ? $source_data{$name}->{'component_id'}
510            : undef;
511        if (defined $guid)
512        {
513            $installer::logger::Lang->printf("    reusing guid %s\n", $guid);
514        }
515        else
516        {
517            $guid = "{" . installer::windows::msiglobal::create_guid() . "}";
518            $installer::logger::Lang->printf("    creating new guid %s\n", $guid);
519        }
520        $target_data{$name}->{'component_id'} = $guid;
521    }
522
523    # Add values for the KeyPath column.
524    $installer::logger::Lang->printf("preparing Component->KeyPath values\n");
525    foreach my $component_name (@$file_component_names,@$registry_component_names)
526    {
527        # Determine the key path.
528        my $key_path = $installer::globals::is_release
529            ? $source_data{$component_name}->{'key_path'}
530            : undef;
531        if (defined $key_path)
532        {
533            $installer::logger::Lang->printf("    reusing key path %s for component %s\n",
534                $key_path,
535                $component_name);
536        }
537        else
538        {
539            if ($target_data{$component_name}->{'is_file'})
540            {
541                $key_path = get_component_keypath($component_name, $files);
542            }
543            else
544            {
545                $key_path = get_component_keypath($component_name, $registry_entries);
546            }
547            $installer::logger::Lang->printf("    created key path %s for component %s\n",
548                $key_path,
549                $component_name);
550        }
551        $target_data{$component_name}->{'key_path'} = $key_path;
552    }
553
554    return \%target_data;
555}
556
557
558
559
560sub	create_component_table_data ($$$$$$)
561{
562	my ($filesref, $registryref, $dirref, $allfilecomponentsref, $allregistrycomponents, $allvariables) = @_;
563
564    my $target_data = get_component_data($allfilecomponentsref, $allregistrycomponents, $filesref, $registryref);
565
566    my @table_data = ();
567
568	# File components
569	foreach my $name (@$allfilecomponentsref)
570	{
571		my %onecomponent = ();
572
573		$onecomponent{'name'} = $name;
574		$onecomponent{'guid'} = $target_data->{$name}->{'component_id'};
575		$onecomponent{'directory'} = get_file_component_directory($name, $filesref, $dirref);
576		if ( $onecomponent{'directory'} eq "IGNORE_COMP" ) { next; }
577		$onecomponent{'attributes'} = get_file_component_attributes($name, $filesref, $allvariables);
578		$onecomponent{'condition'} = get_file_component_condition($name, $filesref);
579		$onecomponent{'keypath'} = $target_data->{$name}->{'key_path'};
580
581        push @table_data, \%onecomponent;
582	}
583
584	# Registry components
585	foreach my $name (@$allregistrycomponents)
586	{
587		my %onecomponent = ();
588
589        $onecomponent{'name'} = $name;
590		$onecomponent{'guid'} = $target_data->{$name}->{'component_id'};
591		$onecomponent{'directory'} = get_registry_component_directory();
592		$onecomponent{'attributes'} = get_registry_component_attributes($name, $allvariables);
593		$onecomponent{'condition'} = get_component_condition($name);
594		$onecomponent{'keypath'} = $target_data->{$name}->{'key_path'};
595
596		push(@table_data, \%onecomponent);
597	}
598
599    return \@table_data;
600}
601
602
603
604
605###################################################################
606# Creating the file Componen.idt dynamically
607# Content:
608# Component ComponentId Directory_ Attributes Condition KeyPath
609###################################################################
610
611
612sub	create_component_table ($$)
613{
614	my ($table_data, $basedir) = @_;
615
616	my @componenttable = ();
617
618	my ($oneline, $infoline);
619
620	installer::windows::idtglobal::write_idt_header(\@componenttable, "component");
621
622	foreach my $item (@$table_data)
623	{
624		$oneline = sprintf("%s\t%s\t%s\t%s\t%s\t%s\n",
625            $item->{'name'},
626            $item->{'guid'},
627            $item->{'directory'},
628            $item->{'attributes'},
629            $item->{'condition'},
630            $item->{'keypath'});
631		push(@componenttable, $oneline);
632	}
633
634	# Saving the file
635
636	my $componenttablename = $basedir . $installer::globals::separator . "Componen.idt";
637	installer::files::save_file($componenttablename ,\@componenttable);
638	$infoline = "Created idt file: $componenttablename\n";
639	$installer::logger::Lang->print($infoline);
640}
641
642
643
644
645####################################################################################
646# Returning a component for a scp module gid.
647# Pairs are saved in the files collector.
648####################################################################################
649
650sub get_component_name_from_modulegid
651{
652	my ($modulegid, $filesref) = @_;
653
654	my $componentname = "";
655
656	for ( my $i = 0; $i <= $#{$filesref}; $i++ )
657	{
658		my $onefile = ${$filesref}[$i];
659
660		if ( $onefile->{'modules'} )
661		{
662			my $filemodules = $onefile->{'modules'};
663			my $filemodulesarrayref = installer::converter::convert_stringlist_into_array_without_newline(\$filemodules, ",");
664
665			if (installer::existence::exists_in_array($modulegid, $filemodulesarrayref))
666			{
667				$componentname = $onefile->{'componentname'};
668				last;
669			}
670		}
671	}
672
673	return $componentname;
674}
675
676####################################################################################
677# Updating the file Environm.idt dynamically
678# Content:
679# Environment Name Value Component_
680####################################################################################
681
682sub set_component_in_environment_table
683{
684	my ($basedir, $filesref) = @_;
685
686	my $infoline = "";
687
688	my $environmentfilename = $basedir . $installer::globals::separator . "Environm.idt";
689
690	if ( -f $environmentfilename )	# only do something, if file exists
691	{
692		my $environmentfile = installer::files::read_file($environmentfilename);
693
694		for ( my $i = 3; $i <= $#{$environmentfile}; $i++ )	# starting in line 4 of Environm.idt
695		{
696			if ( ${$environmentfile}[$i] =~ /^\s*(.*?)\t(.*?)\t(.*?)\t(.*?)\s*$/ )
697			{
698				my $modulegid = $4; # in Environment table a scp module gid can be used as component replacement
699
700				my $componentname = get_component_name_from_modulegid($modulegid, $filesref);
701
702				if ( $componentname )	# only do something if a component could be found
703				{
704					$infoline = "Updated Environment table:\n";
705					$installer::logger::Lang->print($infoline);
706					$infoline = "Old line: ${$environmentfile}[$i]\n";
707					$installer::logger::Lang->print($infoline);
708
709					${$environmentfile}[$i] =~ s/$modulegid/$componentname/;
710
711					$infoline = "New line: ${$environmentfile}[$i]\n";
712					$installer::logger::Lang->print($infoline);
713
714				}
715			}
716		}
717
718		# Saving the file
719
720		installer::files::save_file($environmentfilename ,$environmentfile);
721		$infoline = "Updated idt file: $environmentfilename\n";
722		$installer::logger::Lang->print($infoline);
723
724	}
725}
726
727
728
729
730sub apply_component_translation ($@)
731{
732    my ($translation_map, @component_names) = @_;
733
734    my @translated_names = ();
735    foreach my $component_name (@component_names)
736    {
737        my $translated_name = $translation_map->{$component_name};
738        if (defined $translated_name)
739        {
740            push @translated_names, $translated_name;
741        }
742        else
743        {
744            push @translated_names, $component_name;
745        }
746    }
747
748    return @translated_names;
749}
750
751
7521;
753