File Coverage

lib/Kephra/Config.pm
Criterion Covered Total %
statement 12 105 11.4
branch 0 40 0.0
condition 0 18 0.0
subroutine 4 21 19.0
pod 0 14 0.0
total 16 198 8.0


line stmt bran cond sub pod time code
1             package Kephra::Config;
2             our $VERSION = '0.35';
3            
4 1     1   872 use strict;
  1         2  
  1         30  
5 1     1   5 use warnings;
  1         2  
  1         22  
6            
7 1     1   5 use Cwd();
  1         1  
  1         12  
8 1     1   36 use File::Spec ();
  1         2  
  1         1592  
9             #
10             # Files and Dirs
11             #
12             my $dir;
13 0 0   0     sub _dir { if (defined $_[0]) {$dir = $_[0]} else {$dir} }
  0            
  0            
14            
15             # Generate a path to a configuration file
16 0 0   0 0   sub filepath { File::Spec->catfile( $dir, @_ ) if $_[0] }
17             sub existing_filepath {
18 0     0 0   my $path = filepath( @_ );
19 0 0         unless ( -f $path ) {
20 0           warn("The config file '$path' does not exist");
21             }
22 0           return $path;
23             }
24            
25             sub path_from_node {
26 0     0 0   my $node = shift;
27 0 0 0       return unless defined $node and ref $node eq 'HASH';
28 0 0         if (exists $node->{file}){
29 0 0         if (exists $node->{directory}){
30 0           return filepath($node->{directory}, $node->{file});
31             } else {
32 0           return filepath($node->{file});
33             }
34 0           } else { warn "Wrong node to build config path from." }
35             }
36            
37 0     0 0   sub dirpath { File::Spec->catdir( $dir, @_ ) }
38             sub existing_dirpath {
39 0     0 0   my $path = dirpath( @_ );
40 0 0         unless ( -d $path ) {
41 0           warn("The config directory '$path' does not exist");
42             }
43 0           return $path;
44             }
45            
46 0     0 0   sub standartize_path_slashes { File::Spec->canonpath( shift ) }
47             sub path_matches {
48 0     0 0   my $given = shift;
49 0 0 0       return unless defined $given and $given and @_;
      0        
50 0           for my $path (@_) {
51 0 0 0       return 1 if defined $path
52             and index (standartize_path_slashes($path), $given) > -1;
53             }
54 0           return 0;
55             }
56            
57 0     0 0   sub open_file { open_file_absolute( filepath(@_) ) }
58             sub open_file_absolute {
59 0     0 0   Kephra::Document::add( $_[0] );
60 0           Kephra::Document::Data::set_attribute('config_file',1);
61 0           Kephra::App::TabBar::refresh_current_label();
62             }
63             #
64             # Wx GUI Stuff
65             #
66             # Create a Wx::Colour from a config string
67             # Either hex "0066FF" or decimal "0,128,255" is allowed.
68             sub color {
69 0     0 0   my $string = shift;
70 0 0         return Kephra::App::warn("Color string is not defined") unless defined $string;
71            
72             # Handle hex format
73 0           $string = lc $string;
74 0 0         if ( $string =~ /^([0-9a-f]{2})([0-9a-f]{2})([0-9a-f]{2})$/i ) {
75 0           return Wx::Colour->new( hex $1, hex $2, hex $3 );
76             }
77            
78             # Handle comma-seperated
79 0 0         if ( $string =~ /^(\d+),(\d+),(\d+)$/ ) {
80 0           return Wx::Colour->new( $1 + 0, $2 + 0, $3 + 0 );
81             }
82            
83             # Unknown
84 0           die "Unknown color string '$string'";
85             }
86            
87             # Create an icon bitmap Wx::Bitmap for a named icon
88             sub icon_bitmap {
89             # Find the path from the name
90 0     0 0   my $name = shift;
91 0 0         unless ( defined $name ) {
92 0           warn "Did not provide an icon name to icon_bitmap";
93 0           return;
94             }
95 0 0         $name .= '.xpm' unless $name =~ /\.xpm$/ ;
96            
97 0           my $path = filepath( Kephra::API::settings()->{app}{iconset_path}, $name );
98 0 0         return Wx::Bitmap->new(16,16) unless -e $path;
99            
100 0           my $bitmap = Wx::Bitmap->new( $path, &Wx::wxBITMAP_TYPE_ANY );
101 0 0         unless ( $bitmap ) {
102 0           warn "Failed to create bit map from $path";
103 0           return;
104             }
105 0           return $bitmap;
106             }
107            
108            
109             sub set_xp_style {
110 0     0 0   my $xp_def_file = "$^X.manifest";
111 0 0         if ( $^O eq 'MSWin32' ) {
112 0 0 0       if ( ( Kephra::API::settings()->{app}{xp_style} eq '1' )
113             and ( !-r $xp_def_file ) ) {
114 0           Kephra::Config::Default::drop_xp_style_file($xp_def_file);
115             }
116 0 0 0       if ( ( Kephra::API::settings()->{app}{xp_style} eq '0' )
117             and ( -r $xp_def_file ) ) {
118 0           unlink $xp_def_file;
119             }
120             }
121             }
122            
123             #
124             # misc helper stuff
125             #
126             sub build_fileendings2syntaxstyle_map {
127 0     0 0   foreach ( keys %{ Kephra::API::settings()->{file}{endings} } ) {
  0            
128 0           my $language_id = $_;
129             my @fileendings
130 0           = split( /\s+/, Kephra::API::settings()->{file}{endings}{$language_id} );
131 0           foreach ( @fileendings ) {
132 0           $Kephra::temp{file}{end2langmap}{$_} = $language_id;
133             }
134             }
135             }
136            
137             sub build_fileendings_filterstring {
138 0     0 0   my $l18n = Kephra::Config::Localisation::strings()->{dialog};
139 0           my $files = $l18n->{file}{files};
140 0           my $all = $l18n->{general}{all} . " $files ";
141 0 0         $all .= $^O =~ /win/i
142             ? "(*.*)|*.*"
143             : "(*)|*";
144 0           my $tfile = $Kephra::temp{file};
145 0           $tfile->{filterstring}{all} = $all;
146 0           my $conf = Kephra::API::settings()->{file};
147 0           foreach ( keys %{$conf->{group}} ) {
  0            
148 0           my ( $filter_id, $file_filter ) = ( $_, '' );
149 0           my $filter_name = ucfirst($filter_id);
150 0           my @language_ids = split( /\s+/, $conf->{group}{$filter_id} );
151 0           foreach ( @language_ids ) {
152 0           my @fileendings = split( /\s+/, $conf->{endings}{$_} );
153 0           foreach (@fileendings) { $file_filter .= "*.$_;"; }
  0            
154             }
155 0           chop($file_filter);
156 0           $tfile->{filterstring}{all} .= "|$filter_name $files ($file_filter)|$file_filter";
157             }
158 0           $tfile->{filterstring}{config} = "Config $files (*.conf;*.yaml)|*.conf;*.yaml|$all";
159 0           $tfile->{filterstring}{scite} = "Scite $files (*.ses)|*.ses|$all";
160             }
161            
162             sub _map2hash {
163 0     0     my ( $style, $types_str ) = @_;
164 0           my $stylemap = {}; # holds the style map
165 0           my @types = split( /\s+/, $types_str );
166 0           foreach (@types) { $$stylemap{$_} = $style; }
  0            
167 0           return ($stylemap);
168             }
169            
170             sub _lc_utf {
171 0     0     my $uc = shift;
172 0           my $lc = "";
173 0           for ( 0 .. length($uc) - 1 ) {
174 0           $lc .= lcfirst( substr( $uc, $_, 1 ) );
175             }
176 0           $lc;
177             }
178             #pce:dialog::msg_box(undef,$mode,''); #Wx::wxUNICODE()
179            
180             1;
181            
182             =head1 NAME
183            
184             Kephra::Config - low level config stuff and basics
185            
186             =head1 DESCRIPTION
187            
188            
189             =cut