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