xref: /trunk/main/solenv/bin/modules/installer/windows/registry.pm (revision 38aa938a10cf4eb5c7d27b61a2e833b439f8159f)
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    for ( my $m = 0; $m <= $#{$languagesarrayref}; $m++ )
354    {
355        my $onelanguage = ${$languagesarrayref}[$m];
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
363        for ( my $i = 0; $i <= $#{$registryref}; $i++ )
364        {
365            my $oneregistry = ${$registryref}[$i];
366
367            # Controlling the language!
368            # Only language independent folderitems or folderitems with the correct language
369            # will be included into the table
370
371            if (! (!(( $oneregistry->{'ismultilingual'} )) || ( $oneregistry->{'specificlanguage'} eq $onelanguage )) )  { next; }
372
373            my %registry = ();
374
375            $registry{'Registry'} = get_registry_identifier($oneregistry);
376            $registry{'Root'} = get_registry_root($oneregistry);
377            $registry{'Key'} = get_registry_key($oneregistry, $allvariableshashref);
378            $registry{'Name'} = get_registry_name($oneregistry, $allvariableshashref);
379            $registry{'Value'} = get_registry_value($oneregistry, $allvariableshashref);
380            $registry{'Val64'} = get_registry_val64($oneregistry, $allvariableshashref);
381            $registry{'Component_'} = get_registry_component($oneregistry, $allvariableshashref);
382
383            # Collecting all components
384            if (!(installer::existence::exists_in_array($registry{'Component_'}, $allregistrycomponentsref)))
385            {
386                push(@{$allregistrycomponentsref}, $registry{'Component_'});
387            }
388
389            # Collecting all components with DONT_DELETE style
390            my $style = "";
391            if ( $oneregistry->{'Styles'} ) { $style = $oneregistry->{'Styles'}; }
392            if ( $style =~ /\bDONT_DELETE\b/ ) { $installer::globals::dontdeletecomponents{$registry{'Component_'}} = 1; }
393
394            # Saving upgradekey to write this into setup.ini for minor upgrades
395            if ( $style =~ /\bUPGRADEKEY\b/ ) { $installer::globals::minorupgradekey = $registry{'Key'}; }
396
397            # Collecting all registry components with ALWAYS_REQUIRED style
398            if ( ! ( $style =~ /\bALWAYS_REQUIRED\b/ ))
399            {
400                # Setting a component condition for unforced registry components!
401                # Only write into registry, if WRITE_REGISTRY is set.
402                if ( $oneregistry->{'ComponentCondition'} ) { $oneregistry->{'ComponentCondition'} = "(" . $oneregistry->{'ComponentCondition'} . ") AND (WRITE_REGISTRY=1)"; }
403                else { $oneregistry->{'ComponentCondition'} = "WRITE_REGISTRY=1"; }
404            }
405
406            # Collecting all component conditions
407            if ( $oneregistry->{'ComponentCondition'} )
408            {
409                if ( ! exists($installer::globals::componentcondition{$registry{'Component_'}}))
410                {
411                    $installer::globals::componentcondition{$registry{'Component_'}} = $oneregistry->{'ComponentCondition'};
412                }
413            }
414
415            my $oneline = $registry{'Registry'} . "\t" . $registry{'Root'} . "\t" . $registry{'Key'} . "\t"
416                        . $registry{'Name'} . "\t" . $registry{'Value'} . "\t" . $registry{'Component_'} . "\n";
417
418            my $oneline64 = $registry{'Registry'} . "\t" . $registry{'Root'} . "\t" . $registry{'Key'} . "\t"
419                        . $registry{'Name'} . "\t" . $registry{'Val64'} . "\t" . $registry{'Component_'} . "\n";
420
421            if ( ! ( $style =~ /\bX64_ONLY\b/ )) { push(@registrytable, $oneline); }    # standard registry table for 32 Bit
422            if (( $style =~ /\bX64\b/ ) || ( $style =~ /\bX64_ONLY\b/ )) { push(@reg64table , $oneline64); }
423        }
424
425        # If there are added user registry keys for files collected in
426        # @installer::globals::userregistrycollector (file.pm), then
427        # this registry keys have to be added now. This is necessary for
428        # files in PREDEFINED_OSSHELLNEWDIR, because their component
429        # needs as KeyPath a RegistryItem in HKCU.
430
431        if ( $installer::globals::addeduserregitrykeys ) { add_userregs_to_registry_table(\@registrytable, $allvariableshashref); }
432
433        # Saving the file
434
435        my $registrytablename = $basedir . $installer::globals::separator . "Registry.idt" . "." . $onelanguage;
436        installer::files::save_file($registrytablename ,\@registrytable);
437        $installer::logger::Lang->printf("Created idt file: %s\n", $registrytablename);
438
439        $registrytablename = $basedir . $installer::globals::separator . "Reg64.idt" . "." . $onelanguage;
440        installer::files::save_file($registrytablename ,\@reg64table );
441        $installer::logger::Lang->printf("Created idt file: %s\n", $registrytablename);
442    }
443}
444
4451;
446