File Coverage

blib/lib/Wrangler/Config.pm
Criterion Covered Total %
statement 24 84 28.5
branch 0 56 0.0
condition 0 9 0.0
subroutine 8 11 72.7
pod 0 3 0.0
total 32 163 19.6


line stmt bran cond sub pod time code
1             package Wrangler::Config;
2              
3 1     1   6 use strict;
  1         2  
  1         41  
4 1     1   5 use warnings;
  1         2  
  1         31  
5              
6 1     1   6 use Cwd;
  1         2  
  1         187  
7 1     1   1393 use JSON::XS ();
  1         23650  
  1         39  
8 1     1   3023 use File::HomeDir;
  1         7389  
  1         88  
9 1     1   650145 use Data::Dumper ();
  1         10162  
  1         36  
10 1     1   19 use Digest::MD5 ();
  1         2  
  1         15  
11 1     1   1239 use Path::Tiny;
  1         17081  
  1         1711  
12              
13             our %default_settings = (
14             'ui.main.width' => 1000,
15             'ui.main.height' => 800,
16             'ui.main.maximized' => 0,
17             'ui.main.centered' => 1,
18             'ui.language' => 'en',
19             'ui.foreground_colour' => [30, 30, 30], # font-colour, def, if-undefined = use-system/wx-default
20             'ui.layout.menubar' => 1,
21             'ui.layout.navbar' => 1,
22             'ui.layout.sidebar' => 1,
23             'ui.layout.statusbar' => 1,
24             'ui.sidebar.width' => 180,
25             'ui.filebrowser.include_updir' => 1, # def:1
26             'ui.filebrowser.include_hidden' => 0, # def:0
27             'ui.filebrowser.zebra_striping' => 1, # def:1
28             'ui.filebrowser.highlight_media' => 1, # def 1
29             'ui.filebrowser.highlight_colour.audio' => [220, 220, 240], # def
30             'ui.filebrowser.highlight_colour.image' => [220, 240, 220], # def
31             'ui.filebrowser.highlight_colour.video' => [240, 220, 220], # def
32             'ui.filebrowser.font_size' => 9, # def 9
33             'ui.filebrowser.offer.delete' => 1,
34             'ui.filebrowser.confirm.delete' => 1,
35             'ui.filebrowser.columns' => [
36             { label => 'Name', value_from => 'Filesystem::Filename', text_align => 'left', width => 120 },
37             { label => 'Type-Description', value_from => 'MIME::Description', text_align => 'right', width => 100 },
38             { label => 'Modified', value_from => 'Filesystem::Modified', text_align => 'left', width => 140 },
39             { label => 'Size', value_from => 'Filesystem::Size', text_align => 'right', width => 70 },
40             ],
41             'metadata.extractionTimeout' => 1500, # in ms
42             'ui.formeditor.selected' => "Photo 'tagging'",
43             'ui.formeditor' => {
44             "Photo 'tagging'" => [
45             "Filesystem::Filename",
46             "Extended Attributes::orientation",
47             "Extended Attributes::title",
48             "Extended Attributes::description",
49             "Extended Attributes::tags",
50             ]
51             },
52             'openwith' => {
53             'image/*' => '/usr/bin/eog',
54             'audio/*' => '/usr/bin/avplay',
55             'video/*' => '/usr/bin/avplay',
56             'text/*' => '/usr/bin/gedit',
57             },
58             'valueshortcuts' => {
59             '1-49' => {
60             name => "ALT+1",
61             key => "Extended Attributes::orientation",
62             value => "Rotate 270 CW",
63             },
64             '1-50' => {
65             name => "ALT+2",
66             key => "Extended Attributes::orientation",
67             value => "Rotate 90 CW",
68             },
69             },
70             'plugins' => {
71             'ColourLabels' => 1,
72             },
73             );
74             our %settings;
75             our $digest;
76             our %env;
77             if($^O =~ /Win/i){
78             $env{HostOS} = 'Windows';
79             $env{PathSeparator} = '\\';
80             $env{CRLF} = "\n";
81             $env{HelperFfmpeg} = Cwd::getcwd()."/helpers/ffmpeg-shared/ffmpeg.exe";
82             $env{HelperMPlayer} = Cwd::getcwd()."/helpers/mplayer/mplayer.exe";
83             $env{UserConfigDir} = $ENV{APPDATA} . $env{PathSeparator} . 'wrangler';
84             }elsif($^O =~ /nix$|ux$/i){
85             $env{HostOS} = 'Linux';
86             $env{PathSeparator} = '/';
87             $env{CRLF} = "\n";
88             if(-e '/usr/bin/firefox'){
89             $env{BrowserStartCommand} = 'firefox';
90             }elsif(-e '/usr/bin/chromium-browser'){
91             $env{BrowserStartCommand} = 'chromium-browser';
92             }elsif(-e '/usr/bin/mozilla'){
93             $env{BrowserStartCommand} = 'mozilla';
94             }elsif(-e '/usr/bin/konqueror'){
95             $env{BrowserStartCommand} = 'konqueror';
96             }
97             $env{HelperFfmpeg} = "avconv";
98             $env{HelperMPlayer} = "mplayer";
99             if( $ENV{XDG_CONFIG_HOME} ){
100             ($env{UserConfigDir}) = $ENV{XDG_CONFIG_HOME} =~ /:/ ? split(':', $ENV{XDG_CONFIG_HOME}, 2) : ($ENV{XDG_CONFIG_HOME}); # may hold multiple paths
101             $env{UserConfigDir} .= $env{PathSeparator} . 'wrangler';
102             # Wrangler::debug("Wrangler::Config::read: XDG_CONFIG_HOME:$ENV{XDG_CONFIG_HOME})");
103             }else{
104             $env{UserConfigDir} = File::HomeDir->my_home() . $env{PathSeparator} . ".config" . $env{PathSeparator} . 'wrangler';
105             }
106             }elsif($^O =~ /Mac/i){
107             $env{HostOS} = 'Mac';
108             $env{PathSeparator} = ':';
109             $env{CRLF} = "\r";
110             if(-e '/usr/bin/firefox'){
111             $env{BrowserStartCommand} = 'firefox';
112             }else{
113             $env{BrowserStartCommand} = 'safari';
114             }
115             $env{HelperFfmpeg} = "ffmpeg";
116             $env{HelperMPlayer} = "mplayer";
117             $env{UserConfigDir} = undef;
118             }
119             %env = (
120             %env,
121             'config.file.location' => 'home-dir',
122             );
123              
124             sub read {
125 0     0 0   my $path;
126 0 0         if(-e Cwd::cwd . $env{PathSeparator} . '.wrangler.json' ){
127 0           Wrangler::debug('Wrangler::Config::read: Planning to read settings file from working-dir: .wrangler.yaml');
128 0           $path = Cwd::cwd . $env{PathSeparator} . '.wrangler.json';
129 0           $env{'config.file.location'} = 'working-dir';
130             }
131              
132 0 0         unless($path){
133 0           $path = $env{UserConfigDir};
134 0 0         $path .= $env{PathSeparator} . '.wrangler.json' if $path;
135 0 0         if($path){
136 0           $env{'config.file.location'} = 'home-dir';
137 0           Wrangler::debug('Wrangler::Config::read: Planning to read settings file from home-dir: '.$path);
138             }
139             }
140              
141 0 0         unless($path){
142             # OS specific system config-file locations
143 0 0         if( $env{HostOS} eq 'Linux' ){
    0          
    0          
144 0           $path = File::HomeDir->my_home() . $env{PathSeparator} . ".config" . $env{PathSeparator} . 'wrangler';
145 0           $path = '/etc/wrangler/.wrangler.json';
146             }elsif( $env{HostOS} eq 'Windows'){
147 0           $path = undef;
148             }elsif( $env{HostOS} eq 'Mac'){
149             # todo
150             }
151              
152 0 0         if($path){
153 0           $env{'config.file.location'} = 'system-dir';
154 0           Wrangler::debug('Wrangler::Config::read: Planning to read settings file from system-dir: '.$path);
155             }
156             }
157              
158 0 0 0       if($path && -f $path){
159 0           Wrangler::debug('Wrangler::Config::read: reading settings file:'. $path);
160 0 0         my $json = path($path)->slurp_utf8 or Wrangler::debug("Wrangler::Config::read: error reading config file: $!");
161 0           my $ref = eval { JSON::XS::decode_json( $json ) };
  0            
162 0 0         Wrangler::debug("Wrangler::Config::read: error decoding config file: $@") if $@;
163 0           %settings = %$ref;
164             }else{
165 0           %settings = %default_settings;
166              
167 0           Wrangler::debug('Wrangler::Config::read: no config-file found; using default settings; on change, write config to '. $env{'config.file.location'} .': .wrangler.json');
168             }
169              
170 0           $digest = Digest::MD5::md5( Data::Dumper::Dumper(\%settings) );
171              
172             ## is the config file valid? check if show-stopping values are set
173 0 0 0       $settings{'ui.main.width'} = $default_settings{'ui.main.width'} unless $settings{'ui.main.width'} && $settings{'ui.main.width'} > 30;
174 0 0 0       $settings{'ui.main.height'} = $default_settings{'ui.main.height'} unless $settings{'ui.main.height'} && $settings{'ui.main.height'} > 30;
175 0 0         $settings{'ui.filebrowser.columns'} = $default_settings{'ui.filebrowser.columns'} unless $settings{'ui.filebrowser.columns'};
176             }
177              
178             sub config {
179 0 0   0 0   if($_[1]){
180 0 0         unless( defined($settings{ $_[1] }) ){
181 0           Wrangler::debug("Wrangler::Config::config: key:$_[1] not defined!");
182 0 0         return unless $_[2];
183             }
184 0 0         return $settings{ $_[1] } unless $_[2];
185 0           Wrangler::debug("Wrangler::Config::config: $_[1] => $_[2]");
186 0           $settings{ $_[1] } = $_[2];
187             }
188 0           return \%settings;
189             }
190              
191             sub write {
192 0 0   0 0   unless($digest){
193 0           Wrangler::debug("Wrangler::Config::write: stopped (over-) writing an empty config file. Check your config file for syntax errors");
194 0           return;
195             }
196              
197 0 0         if( Digest::MD5::md5( Data::Dumper::Dumper(\%settings) ) eq $digest ){
198 0           Wrangler::debug("Wrangler::Config::write: settings not changed, write skipped");
199             }else{
200 0           my $path;
201 0 0         if( $env{'config.file.location'} eq 'working-dir' ){
202 0           $path = Cwd::cwd . $env{PathSeparator} . '.wrangler.json';
203             }else{ # the default
204 0           $path = File::HomeDir->my_home() . $env{PathSeparator} . ".config";
205 0 0         unless(-d $path){
206 0 0         mkdir($path) or Wrangler::debug("Wrangler::Config::write: error creating directory $path");
207             }
208 0           $path .= $env{PathSeparator} . 'wrangler';
209 0 0         unless(-d $path){
210 0 0         mkdir($path) or Wrangler::debug("Wrangler::Config::write: error creating directory $path");
211             }
212 0           $path .= $env{PathSeparator} . '.wrangler.json';
213             }
214              
215 0           Wrangler::debug("Wrangler::Config::write: settings from ".$env{'config.file.location'}." changed, write to: ".$path);
216              
217 0           my $json = eval { JSON::XS->new->utf8->pretty->encode( \%settings ) };
  0            
218 0 0         Wrangler::debug("Wrangler::Config::write: error encoding config file: $@") if $@;
219              
220 0 0         path($path)->spew_utf8($json) or Wrangler::debug("Wrangler::Config::write: error writing config file: $path: $!")
221             }
222             }
223              
224             1;