1 package SL::LxOfficeConf;
7 my $environment_initialized;
10 my ($class, $may_fail);
12 my $failed = !eval { require Config::Std; };
29 my ($class, $file_name, $may_fail) = @_;
31 return unless $class->safe_require($may_fail);
33 read_config('config/lx_office.conf.default' => \%::lx_office_conf);
34 _decode_recursively(\%::lx_office_conf);
36 $file_name ||= 'config/lx_office.conf';
39 read_config($file_name => \ my %local_conf);
40 _decode_recursively(\%local_conf);
41 _flat_merge(\%::lx_office_conf, \%local_conf);
45 _determine_application_paths();
50 sub _decode_recursively {
53 while (my ($key, $value) = each %{ $obj }) {
54 if (ref($value) eq 'HASH') {
55 _decode_recursively($value);
57 $obj->{$key} = decode('UTF-8', $value);
65 while (my ($key, $value) = each %{ $src }) {
66 if (!exists $dst->{$key}) {
67 $dst->{$key} = $value;
70 map { $dst->{$key}->{$_} = $value->{$_} } keys %{ $value };
75 sub _init_environment {
76 return if $environment_initialized;
78 my %key_map = ( lib => { name => 'PERL5LIB', append_path => 1 },
79 path => { name => 'PATH', append_path => 1 },
81 my $cfg = $::lx_office_conf{environment} || {};
83 while (my ($key, $value) = each %{ $cfg }) {
86 my $info = $key_map{$key} || {};
87 $key = $info->{name} || $key;
89 if ($info->{append_path}) {
90 $value = ':' . $value unless $value =~ m/^:/ || !$ENV{$key};
91 $value = $ENV{$key} . $value if $ENV{$key};
97 $environment_initialized = 1;
100 sub _determine_application_paths {
101 my @paths = grep { $_ } split m/:/, $ENV{PATH};
103 foreach my $key (keys %{ $::lx_office_conf{applications} }) {
104 my ($program) = split m/\s+/, $::lx_office_conf{applications}->{$key};
105 next if $program =~ m|/|;
107 foreach my $path (@paths) {
108 next unless -f "${path}/${program}";
109 $::lx_office_conf{applications}->{$key} = "${path}/" . $::lx_office_conf{applications}->{$key};