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::registry;
25
26use installer::files;
27use installer::globals;
28use installer::worker;
29use installer::windows::msiglobal;
30use installer::windows::idtglobal;
31
32#####################################################
33# Generating the component name from a registryitem
34#####################################################
35
36sub get_registry_component_name
37{
38	my ($registryref, $allvariables) = @_;
39
40	# In this function exists the rule to create components from registryitems
41	# Rule:
42	# The componentname can be directly taken from the ModuleID.
43	# All registryitems belonging to one module can get the same component.
44
45	my $componentname = "";
46	my $isrootmodule = 0;
47
48	if ( $registryref->{'ModuleID'} ) { $componentname = $registryref->{'ModuleID'}; }
49
50	$componentname =~ s/\\/\_/g;
51	$componentname =~ s/\//\_/g;
52	$componentname =~ s/\-/\_/g;
53	$componentname =~ s/\_\s*$//g;
54
55	$componentname = lc($componentname);	# componentnames always lowercase
56
57	if ( $componentname eq "gid_module_root" ) { $isrootmodule = 1; }
58
59	# Attention: Maximum length for the componentname is 72
60
61	# identifying this component as registryitem component
62	$componentname = "registry_" . $componentname;
63
64	$componentname =~ s/gid_module_/g_m_/g;
65	$componentname =~ s/_optional_/_o_/g;
66	$componentname =~ s/_javafilter_/_jf_/g;
67
68	# This componentname must be more specific
69	my $addon = "_";
70	if ( $allvariables->{'PRODUCTNAME'} ) { $addon = $addon . $allvariables->{'PRODUCTNAME'}; }
71	if ( $allvariables->{'PRODUCTVERSION'} ) { $addon = $addon . $allvariables->{'PRODUCTVERSION'}; }
72	$addon = lc($addon);
73	$addon =~ s/ //g;
74	$addon =~ s/-//g;
75	$addon =~ s/\.//g;
76
77	my $styles = "";
78	if ( $registryref->{'Styles'} ) { $styles = $registryref->{'Styles'}; }
79
80	$componentname = $componentname . $addon;
81
82	if (( $styles =~ /\bLANGUAGEPACK\b/ ) && ( $installer::globals::languagepack )) { $componentname = $componentname . "_lang"; }
83	if ( $styles =~ /\bALWAYS_REQUIRED\b/ ) { $componentname = $componentname . "_forced"; }
84
85	# Attention: Maximum length for the componentname is 72
86	# %installer::globals::allregistrycomponents_in_this_database_ : resetted for each database
87	# %installer::globals::allregistrycomponents_ : not resetted for each database
88	# Component strings must be unique for the complete product, because they are used for
89	# the creation of the globally unique identifier.
90
91	my $fullname = $componentname;  # This can be longer than 72
92
93	if (( exists($installer::globals::allregistrycomponents_{$fullname}) ) && ( ! exists($installer::globals::allregistrycomponents_in_this_database_{$fullname}) ))
94	{
95		# This is not allowed: One component cannot be installed with different packages.
96		installer::exiter::exit_program("ERROR: Windows registry component \"$fullname\" is already included into another package. This is not allowed.", "get_registry_component_name");
97	}
98
99	if ( exists($installer::globals::allregistrycomponents_{$fullname}) )
100	{
101		$componentname = $installer::globals::allregistrycomponents_{$fullname};
102	}
103	else
104	{
105		if ( length($componentname) > 70 )
106		{
107			$componentname = generate_new_short_registrycomponentname($componentname); # This has to be unique for the complete product, not only one package
108		}
109
110		$installer::globals::allregistrycomponents_{$fullname} = $componentname;
111		$installer::globals::allregistrycomponents_in_this_database_{$fullname} = 1;
112	}
113
114	if ( $isrootmodule ) { $installer::globals::registryrootcomponent = $componentname; }
115
116	return $componentname;
117}
118
119#########################################################
120# Create a shorter version of a long component name,
121# because maximum length in msi database is 72.
122# Attention: In multi msi installation sets, the short
123# names have to be unique over all packages, because
124# this string is used to create the globally unique id
125# -> no resetting of
126# %installer::globals::allshortregistrycomponents
127# after a package was created.
128#########################################################
129
130sub generate_new_short_registrycomponentname
131{
132	my ($componentname) = @_;
133
134	my $startversion = substr($componentname, 0, 60); # taking only the first 60 characters
135	my $subid = installer::windows::msiglobal::calculate_id($componentname, 9); # taking only the first 9 digits
136	my $shortcomponentname = $startversion . "_" . $subid;
137
138	if ( exists($installer::globals::allshortregistrycomponents{$shortcomponentname}) ) { installer::exiter::exit_program("Failed to create unique component name: \"$shortcomponentname\"", "generate_new_short_registrycomponentname"); }
139
140	$installer::globals::allshortregistrycomponents{$shortcomponentname} = 1;
141
142	return $shortcomponentname;
143}
144
145##############################################################
146# Returning identifier for registry table.
147##############################################################
148
149sub get_registry_identifier
150{
151	my ($registry) = @_;
152
153	my $identifier = "";
154
155	if ( $registry->{'gid'} ) { $identifier = $registry->{'gid'}; }
156
157	$identifier = lc($identifier);	# always lower case
158
159	# Attention: Maximum length is 72
160
161	$identifier =~ s/gid_regitem_/g_r_/;
162	$identifier =~ s/_soffice_/_s_/;
163	$identifier =~ s/_clsid_/_c_/;
164	$identifier =~ s/_currentversion_/_cv_/;
165	$identifier =~ s/_microsoft_/_ms_/;
166	$identifier =~ s/_manufacturer_/_mf_/;
167	$identifier =~ s/_productname_/_pn_/;
168	$identifier =~ s/_productversion_/_pv_/;
169	$identifier =~ s/_staroffice_/_so_/;
170	$identifier =~ s/_software_/_sw_/;
171	$identifier =~ s/_capabilities_/_cap_/;
172	$identifier =~ s/_classpath_/_cp_/;
173	$identifier =~ s/_extension_/_ex_/;
174	$identifier =~ s/_fileassociations_/_fa_/;
175	$identifier =~ s/_propertysheethandlers_/_psh_/;
176	$identifier =~ s/__/_/g;
177
178	# Saving this in the registry collector
179
180	$registry->{'uniquename'} = $identifier;
181
182	return $identifier;
183}
184
185##################################################################
186# Returning root value for registry table.
187##################################################################
188
189sub get_registry_root
190{
191	my ($registry) = @_;
192
193	my $rootvalue = 0;	# Default: Parent is KKEY_CLASSES_ROOT
194	my $scproot = "";
195
196	if ( $registry->{'ParentID'} ) { $scproot = $registry->{'ParentID'}; }
197
198	if ( $scproot eq "PREDEFINED_HKEY_LOCAL_MACHINE" ) { $rootvalue = -1; }
199
200	if ( $scproot eq "PREDEFINED_HKEY_CLASSES_ROOT" ) { $rootvalue = 0; }
201
202	if ( $scproot eq "PREDEFINED_HKEY_CURRENT_USER_ONLY" ) { $rootvalue = 1; }
203
204	if ( $scproot eq "PREDEFINED_HKEY_LOCAL_MACHINE_ONLY" ) { $rootvalue = 2; }
205
206	return $rootvalue;
207}
208
209##############################################################
210# Returning key for registry table.
211##############################################################
212
213sub get_registry_key
214{
215	my ($registry, $allvariableshashref) = @_;
216
217	my $key = "";
218
219	if ( $registry->{'Subkey'} ) { $key = $registry->{'Subkey'}; }
220
221	if ( $key =~ /\%/ ) { $key = installer::worker::replace_variables_in_string($key, $allvariableshashref); }
222
223	return $key;
224}
225
226##############################################################
227# Returning name for registry table.
228##############################################################
229
230sub get_registry_name
231{
232	my ($registry, $allvariableshashref) = @_;
233
234	my $name = "";
235
236	if ( $registry->{'Name'} ) { $name = $registry->{'Name'}; }
237
238	if ( $name =~ /\%/ ) { $name = installer::worker::replace_variables_in_string($name, $allvariableshashref); }
239
240	return $name;
241}
242
243##############################################################
244# Returning value for registry table.
245##############################################################
246
247sub get_registry_value
248{
249	my ($registry, $allvariableshashref) = @_;
250
251	my $value = "";
252
253	if ( $registry->{'Value'} ) { $value = $registry->{'Value'}; }
254
255	$value =~ s/\\\"/\"/g;	# no more masquerading of '"'
256	$value =~ s/\\\\\s*$/\\/g;	# making "\\" at end of value to "\"
257	$value =~ s/\<progpath\>/\[INSTALLLOCATION\]/;
258	$value =~ s/\[INSTALLLOCATION\]\\/\[INSTALLLOCATION\]/;	# removing "\" after "[INSTALLLOCATION]"
259
260	if ( $value =~ /\%/ ) { $value = installer::worker::replace_variables_in_string($value, $allvariableshashref); }
261
262	return $value;
263}
264
265##############################################################
266# Returning 64 bit value for registry table.
267##############################################################
268
269sub get_registry_val64
270{
271	my ($registry, $allvariableshashref) = @_;
272
273	my $value = "";
274
275	if ( $registry->{'Val64'} ) { $value = $registry->{'Val64'}; }
276
277	$value =~ s/\\\"/\"/g;	# no more masquerading of '"'
278	$value =~ s/\\\\\s*$/\\/g;	# making "\\" at end of value to "\"
279	$value =~ s/\<progpath\>/\[INSTALLLOCATION\]/;
280	$value =~ s/\[INSTALLLOCATION\]\\/\[INSTALLLOCATION\]/;	# removing "\" after "[INSTALLLOCATION]"
281
282	if ( $value =~ /\%/ ) { $value = installer::worker::replace_variables_in_string($value, $allvariableshashref); }
283
284	return $value;
285}
286
287##############################################################
288# Returning component for registry table.
289##############################################################
290
291sub get_registry_component
292{
293	my ($registry, $allvariables) = @_;
294
295	# All registry items belonging to one module can
296	# be included into one component
297
298	my $componentname = get_registry_component_name($registry, $allvariables);
299
300	# saving componentname in the registryitem collector
301
302	$registry->{'componentname'} = $componentname;
303
304	return $componentname;
305}
306
307######################################################
308# Adding the content of
309# @installer::globals::userregistrycollector
310# to the registry table. The content was collected
311# in create_files_table() in file.pm.
312######################################################
313
314sub add_userregs_to_registry_table
315{
316	my ( $registrytable, $allvariables ) = @_;
317
318	for ( my $i = 0; $i <= $#installer::globals::userregistrycollector; $i++ )
319	{
320		my $onefile = $installer::globals::userregistrycollector[$i];
321
322		my $styles = "";
323		if ( $onefile->{'Styles'} ) { $styles = $onefile->{'Styles'}; }
324
325		my %registry = ();
326
327		$registry{'Registry'} = $onefile->{'userregkeypath'};
328		$registry{'Root'} = "1";  # always HKCU
329		$registry{'Key'} = "Software\\$allvariables->{'MANUFACTURER'}\\$allvariables->{'PRODUCTNAME'} $allvariables->{'PRODUCTVERSION'}\\";
330		if ( $onefile->{'needs_user_registry_key'} ) { $registry{'Key'} = $registry{'Key'} . "StartMenu"; }
331		else { $registry{'Key'} = $registry{'Key'} . "ShellNew"; }
332		$registry{'Name'} = $onefile->{'Name'};
333		$registry{'Value'} = "1";
334		$registry{'Component_'} = $onefile->{'componentname'};
335
336		my $oneline = $registry{'Registry'} . "\t" . $registry{'Root'} . "\t" . $registry{'Key'} . "\t"
337					. $registry{'Name'} . "\t" . $registry{'Value'} . "\t" . $registry{'Component_'} . "\n";
338
339		push(@{$registrytable}, $oneline);
340	}
341}
342
343######################################################
344# Creating the file Registry.idt dynamically
345# Content:
346# Registry Root Key Name Value Component_
347######################################################
348
349sub create_registry_table
350{
351	my ($registryref, $allregistrycomponentsref, $basedir, $languagesarrayref, $allvariableshashref) = @_;
352
353    my %table_data = ();
354	foreach my $onelanguage (@$languagesarrayref)
355	{
356
357		my @registrytable = ();
358		my @reg64table = ();
359
360		installer::windows::idtglobal::write_idt_header(\@registrytable, "registry");
361		installer::windows::idtglobal::write_idt_header(\@reg64table, "reg64");
362        my $table_items = [];
363		foreach my $oneregistry (@$registryref)
364		{
365			# Controlling the language!
366			# Only language independent folderitems or folderitems with the correct language
367			# will be included into the table
368
369			next if $oneregistry->{'ismultilingual'}
370                && $oneregistry->{'specificlanguage'} ne $onelanguage;
371
372			my %registry = ();
373
374			$registry{'Registry'} = get_registry_identifier($oneregistry);
375			$registry{'Root'} = get_registry_root($oneregistry);
376			$registry{'Key'} = get_registry_key($oneregistry, $allvariableshashref);
377			$registry{'Name'} = get_registry_name($oneregistry, $allvariableshashref);
378			$registry{'Value'} = get_registry_value($oneregistry, $allvariableshashref);
379			$registry{'Val64'} = get_registry_val64($oneregistry, $allvariableshashref);
380            my $component_name = get_registry_component_name($oneregistry, $allvariableshashref);
381            $oneregistry->{'componentname'} = $component_name;
382			$registry{'Component_'} = $component_name;
383
384			# Collecting all components
385			if (!(installer::existence::exists_in_array($registry{'Component_'}, $allregistrycomponentsref)))
386			{
387				push(@{$allregistrycomponentsref}, $registry{'Component_'});
388			}
389
390			# Collecting all components with DONT_DELETE style
391			my $style = $oneregistry->{'Styles'} // "";
392            $registry{'styles'} = $style;
393
394			if ( $style =~ /\bDONT_DELETE\b/ )
395            {
396                $installer::globals::dontdeletecomponents{$component_name} = 1;
397            }
398
399			# Saving upgradekey to write this into setup.ini for minor upgrades
400			if ( $style =~ /\bUPGRADEKEY\b/ )
401            {
402                $installer::globals::minorupgradekey = $registry{'Key'};
403            }
404
405			# Collecting all registry components with ALWAYS_REQUIRED style
406			if ( ! ( $style =~ /\bALWAYS_REQUIRED\b/ ))
407			{
408				# Setting a component condition for unforced registry components!
409				# Only write into registry, if WRITE_REGISTRY is set.
410				if ( $oneregistry->{'ComponentCondition'} ) { $oneregistry->{'ComponentCondition'} = "(" . $oneregistry->{'ComponentCondition'} . ") AND (WRITE_REGISTRY=1)"; }
411				else { $oneregistry->{'ComponentCondition'} = "WRITE_REGISTRY=1"; }
412			}
413
414			# Collecting all component conditions
415			if ( $oneregistry->{'ComponentCondition'} )
416			{
417				if ( ! exists($installer::globals::componentcondition{$registry{'Component_'}}))
418				{
419					$installer::globals::componentcondition{$registry{'Component_'}} = $oneregistry->{'ComponentCondition'};
420				}
421			}
422
423			my $oneline = $registry{'Registry'} . "\t" . $registry{'Root'} . "\t" . $registry{'Key'} . "\t"
424						. $registry{'Name'} . "\t" . $registry{'Value'} . "\t" . $registry{'Component_'} . "\n";
425
426			my $oneline64 = $registry{'Registry'} . "\t" . $registry{'Root'} . "\t" . $registry{'Key'} . "\t"
427						. $registry{'Name'} . "\t" . $registry{'Val64'} . "\t" . $registry{'Component_'} . "\n";
428
429			if ( ! ( $style =~ /\bX64_ONLY\b/ )) { push(@registrytable, $oneline); }	# standard registry table for 32 Bit
430			if (( $style =~ /\bX64\b/ ) || ( $style =~ /\bX64_ONLY\b/ )) { push(@reg64table , $oneline64); }
431		}
432
433		# If there are added user registry keys for files collected in
434		# @installer::globals::userregistrycollector (file.pm), then
435		# this registry keys have to be added now. This is necessary for
436		# files in PREDEFINED_OSSHELLNEWDIR, because their component
437		# needs as KeyPath a RegistryItem in HKCU.
438
439		if ( $installer::globals::addeduserregitrykeys ) { add_userregs_to_registry_table(\@registrytable, $allvariableshashref); }
440
441		# Saving the file
442
443		my $registrytablename = $basedir . $installer::globals::separator . "Registry.idt" . "." . $onelanguage;
444		installer::files::save_file($registrytablename ,\@registrytable);
445        $installer::logger::Lang->printf("Created idt file: %s\n", $registrytablename);
446
447		$registrytablename = $basedir . $installer::globals::separator . "Reg64.idt" . "." . $onelanguage;
448		installer::files::save_file($registrytablename ,\@reg64table );
449        $installer::logger::Lang->printf("Created idt file: %s\n", $registrytablename);
450	}
451}
452
4531;
454