| 1 | #! perl | ||
| 2 | |||
| 3 | use v5.26; | ||
| 4 | use Object::Pad; | ||
| 5 | |||
| 6 | class ChordPro::Wx::Config; | ||
| 7 | |||
| 8 | our %state; | ||
| 9 | our %preferences; | ||
| 10 | |||
| 11 | use Ref::Util qw( is_hashref is_arrayref ); | ||
| 12 | use List::Util qw( uniq any first ); | ||
| 13 | |||
| 14 | use Exporter 'import'; | ||
| 15 | our @EXPORT = qw( %state %preferences ); | ||
| 16 | |||
| 17 | my $cb; | ||
| 18 | |||
| 19 | use Wx qw(:everything); | ||
| 20 | use Wx::Locale gettext => '_T'; | ||
| 21 | use ChordPro::Files; | ||
| 22 | use ChordPro::Paths; | ||
| 23 | use ChordPro::Utils qw( plural json_load is_true ); | ||
| 24 | |||
| 25 | use constant FONTSIZE => 12; | ||
| 26 | use constant SETTINGS_VERSION => 3; | ||
| 27 | |||
| 28 | use Encode qw( decode_utf8 ); | ||
| 29 | |||
| 30 | # Legacy font numbers. | ||
| 31 | my @fonts = | ||
| 32 | ( # Monospace | ||
| 33 | Wx::Font->new( FONTSIZE, wxFONTFAMILY_TELETYPE, | ||
| 34 | wxFONTSTYLE_NORMAL, wxFONTWEIGHT_NORMAL ), | ||
| 35 | # Serif | ||
| 36 | Wx::Font->new( FONTSIZE, wxFONTFAMILY_ROMAN, | ||
| 37 | wxFONTSTYLE_NORMAL, wxFONTWEIGHT_NORMAL ), | ||
| 38 | |||
| 39 | # Sans serif | ||
| 40 | Wx::Font->new( FONTSIZE, wxFONTFAMILY_SWISS, | ||
| 41 | wxFONTSTYLE_NORMAL, wxFONTWEIGHT_NORMAL ), | ||
| 42 | # Modern | ||
| 43 | Wx::Font->new( FONTSIZE, wxFONTFAMILY_MODERN, | ||
| 44 | wxFONTSTYLE_NORMAL, wxFONTWEIGHT_NORMAL ), | ||
| 45 | ); | ||
| 46 | |||
| 47 | my %prefs = | ||
| 48 | ( | ||
| 49 | # (Old) config version. | ||
| 50 | settings_version => SETTINGS_VERSION - 1, | ||
| 51 | |||
| 52 | # Skip default (system, user, song) configs. | ||
| 53 | skipstdcfg => 1, | ||
| 54 | # Skip legacy (un-classified) configs. | ||
| 55 | skipoldcfg => 0, | ||
| 56 | |||
| 57 | # Presets. | ||
| 58 | # Title as defined by or derived from the JSON file. | ||
| 59 | # When multiple presets are possible, a list of titles separated by TABs | ||
| 60 | # (which are imported as a []). | ||
| 61 | # Note it is always a [] even when there's only one. | ||
| 62 | # Every preset_xxx is a list of entries from $state{preset}{xxx}. | ||
| 63 | preset_instruments => [], # single | ||
| 64 | preset_styles => [], # single | ||
| 65 | preset_stylemods => [], | ||
| 66 | preset_notations => [], # single | ||
| 67 | preset_tasks => [], # not used | ||
| 68 | |||
| 69 | # This one is slightly different, since it does not have configs associated. | ||
| 70 | preset_xcodes => [], # single | ||
| 71 | |||
| 72 | # Custom config file. | ||
| 73 | enable_configfile => 0, | ||
| 74 | configfile => "", | ||
| 75 | |||
| 76 | # Custom library. | ||
| 77 | enable_customlib => 0, # defined($ENV{CHORDPRO_LIB}), | ||
| 78 | customlib => "", # $ENV{CHORDPRO_LIB}, | ||
| 79 | |||
| 80 | # New song template. | ||
| 81 | enable_tmplfile => 0, | ||
| 82 | tmplfile => "", | ||
| 83 | |||
| 84 | # Editor. | ||
| 85 | editfont => 0, # inital, later "Monospace 10" etc. | ||
| 86 | editsize => FONTSIZE, | ||
| 87 | editortheme => "auto", | ||
| 88 | |||
| 89 | # Mostly for STC. TextCtrl fallback uses fg and bg only. | ||
| 90 | editcolour_light_fg => "#000000", | ||
| 91 | editcolour_light_bg => "#ffffff", | ||
| 92 | editcolour_light_s1 => "#b1b1b1", | ||
| 93 | editcolour_light_s2 => "#b1b1b1", | ||
| 94 | editcolour_light_s3 => "#b1b1b1", | ||
| 95 | editcolour_light_s4 => "#ff3c31", | ||
| 96 | editcolour_light_s5 => "#0068d0", | ||
| 97 | editcolour_light_s6 => "#ef6c2a", | ||
| 98 | editcolour_light_annfg => "#ff0000", | ||
| 99 | editcolour_light_annbg => "#ffffa0", | ||
| 100 | editcolour_light_numfg => "#303030", | ||
| 101 | editcolour_light_numbg => "#e8e8e8", | ||
| 102 | editcolour_dark_fg => "#ffffff", | ||
| 103 | editcolour_dark_bg => "#000000", | ||
| 104 | editcolour_dark_s1 => "#b1b1b1", | ||
| 105 | editcolour_dark_s2 => "#b1b1b1", | ||
| 106 | editcolour_dark_s3 => "#b1b1b1", | ||
| 107 | editcolour_dark_s4 => "#ff3c31", | ||
| 108 | editcolour_dark_s5 => "#0068d0", | ||
| 109 | editcolour_dark_s6 => "#ef6c2a", | ||
| 110 | editcolour_dark_annfg => "#ff0000", | ||
| 111 | editcolour_dark_annbg => "#ffffa0", | ||
| 112 | editcolour_dark_numfg => "#e8e8e8", | ||
| 113 | editcolour_dark_numbg => "#303030", | ||
| 114 | |||
| 115 | editorwrap => 1, | ||
| 116 | editorwrapindent => 2, | ||
| 117 | |||
| 118 | # Messages. | ||
| 119 | msgsfont => 0, # inital, later "Monospace 10" etc. | ||
| 120 | |||
| 121 | # Transcode. | ||
| 122 | enable_xcode => 0, | ||
| 123 | |||
| 124 | # PDF Viewer. | ||
| 125 | enable_pdfviewer => undef, | ||
| 126 | pdfviewer => "", | ||
| 127 | |||
| 128 | # HTML Viewer. | ||
| 129 | enable_htmlviewer => undef, | ||
| 130 | |||
| 131 | # Insert spec chars. | ||
| 132 | enable_insert_symbols => 0, | ||
| 133 | |||
| 134 | # Preferences w/o UI. | ||
| 135 | chordproext => ".chordpro", # for Nick | ||
| 136 | dumpstate => 0, | ||
| 137 | expert => 0, | ||
| 138 | advanced => 0, | ||
| 139 | ); | ||
| 140 | |||
| 141 | use constant MAXRECENTS => 10; | ||
| 142 | my $config_root = "/"; | ||
| 143 | |||
| 144 | # Establish a connection with the persistent data store. | ||
| 145 | |||
| 146 | #method Setup :common ($options) { | ||
| 147 | sub Setup( $class, $options ) { | ||
| 148 | |||
| 149 | if ( $options->{config} ) { | ||
| 150 | Wx::ConfigBase::Set | ||
| 151 | ( $cb = Wx::FileConfig->new | ||
| 152 | ( "WxChordPro", | ||
| 153 | "ChordPro_ORG", | ||
| 154 | $options->{config}, | ||
| 155 | '', | ||
| 156 | wxCONFIG_USE_LOCAL_FILE, | ||
| 157 | )); | ||
| 158 | } | ||
| 159 | elsif ( $^O =~ /^mswin/i ) { | ||
| 160 | $cb = Wx::ConfigBase::Get; | ||
| 161 | $config_root = "/wxchordpro"; | ||
| 162 | $cb->SetPath($config_root); | ||
| 163 | } | ||
| 164 | else { | ||
| 165 | my $file; | ||
| 166 | if ( $ENV{XDG_CONFIG_HOME} && fs_test( d => $ENV{XDG_CONFIG_HOME} ) ) { | ||
| 167 | $file = | ||
| 168 | $ENV{XDG_CONFIG_HOME} . "/wxchordpro/wxchordpro"; | ||
| 169 | } | ||
| 170 | elsif ( -d "$ENV{HOME}/.config" ) { | ||
| 171 | $file = "$ENV{HOME}/.config/wxchordpro/wxchordpro"; | ||
| 172 | mkdir("$ENV{HOME}/.config/wxchordpro"); | ||
| 173 | } | ||
| 174 | else { | ||
| 175 | $file = "$ENV{HOME}/.wxchordpro"; | ||
| 176 | } | ||
| 177 | unless ( fs_test( f => $file ) ) { | ||
| 178 | my $fd = fs_open( $file, '>' ); | ||
| 179 | } | ||
| 180 | Wx::ConfigBase::Set | ||
| 181 | ( $cb = Wx::FileConfig->new | ||
| 182 | ( "WxChordPro", | ||
| 183 | "ChordPro_ORG", | ||
| 184 | $file, | ||
| 185 | '', | ||
| 186 | wxCONFIG_USE_LOCAL_FILE, | ||
| 187 | )); | ||
| 188 | } | ||
| 189 | |||
| 190 | unless ( $cb->Exists("preferences") ) { # new | ||
| 191 | $cb->Write("/preferences/settings_version", SETTINGS_VERSION ); | ||
| 192 | } | ||
| 193 | } | ||
| 194 | |||
| 195 | method Ok :common { | ||
| 196 | $preferences{settings_version} == SETTINGS_VERSION; | ||
| 197 | } | ||
| 198 | method SetOk :common { | ||
| 199 | $preferences{settings_version} = SETTINGS_VERSION; | ||
| 200 | } | ||
| 201 | |||
| 202 | # Load all data from the persistent data store into %state. | ||
| 203 | # Adds information collected from the environment (e.g. config files). | ||
| 204 | # Try to compensate for incompatibilities (legacy). | ||
| 205 | |||
| 206 | method Load :common { | ||
| 207 | use Hash::Util qw( lock_keys unlock_keys ); | ||
| 208 | unlock_keys(%preferences); | ||
| 209 | %preferences = ( %prefs ); | ||
| 210 | while ( my ( $k, $v ) = each %prefs ) { | ||
| 211 | next unless $k =~ /^(editcolour)_(\w+)_(\w+)/; | ||
| 212 | $preferences{$1}{$2}{$3} = $v; | ||
| 213 | } | ||
| 214 | while ( my ( $k, $v ) = each %preferences ) { | ||
| 215 | delete $preferences{$k} if $k =~ /^(editcolour)_/; | ||
| 216 | } | ||
| 217 | %state = ( preferences => \%preferences, | ||
| 218 | recents => [], | ||
| 219 | ); | ||
| 220 | |||
| 221 | $cb->SetPath($config_root); | ||
| 222 | my ( $ggoon, $group, $gindex ) = $cb->GetFirstGroup; | ||
| 223 | my %pp = $ggoon ? %prefs : (); | ||
| 224 | while ( $ggoon ) { | ||
| 225 | my $cp = $cb->GetPath; | ||
| 226 | $cb->SetPath($group); | ||
| 227 | |||
| 228 | $state{$group} = [] if $group eq "recents"; | ||
| 229 | |||
| 230 | my ( $goon, $entry, $index ) = $cb->GetFirstEntry; | ||
| 231 | while ( $goon ) { | ||
| 232 | my $value = $cb->Read($entry); | ||
| 233 | # printf STDERR ( "$group.$entry:\t%s\n", $value ); | ||
| 234 | if ( $group eq "preferences" ) { | ||
| 235 | my $o; | ||
| 236 | if ( exists $pp{$entry} ) { | ||
| 237 | $o = delete $pp{$entry}; | ||
| 238 | } | ||
| 239 | else { | ||
| 240 | warn("Preferences: unknown key: $entry"); | ||
| 241 | $cb->DeleteEntry($entry); | ||
| 242 | next; | ||
| 243 | } | ||
| 244 | |||
| 245 | # These are always returned as lists of hashes. | ||
| 246 | if ( $entry =~ m/^preset_(instruments|styles|stylemods|notations|tasks|xcodes)/ ) { | ||
| 247 | $preferences{$entry} = | ||
| 248 | [ map { +{ title => lc($_) } } split( /\t+/, $value ) ]; | ||
| 249 | } | ||
| 250 | |||
| 251 | elsif ( $entry eq "editcolours" ) { | ||
| 252 | my @c = split( /,\s*/, $value ); | ||
| 253 | if ( @c <= 1 ) { | ||
| 254 | ...; | ||
| 255 | } | ||
| 256 | else { | ||
| 257 | $preferences{editcolour}{light}{fg} = $c[0]; | ||
| 258 | $preferences{editcolour}{light}{bg} = $c[1]; | ||
| 259 | $preferences{editcolour}{light}{s1} = $c[2]; | ||
| 260 | $preferences{editcolour}{light}{s2} = $c[3]; | ||
| 261 | $preferences{editcolour}{light}{s3} = $c[4]; | ||
| 262 | $preferences{editcolour}{light}{s4} = $c[5]; | ||
| 263 | $preferences{editcolour}{light}{s5} = $c[6]; | ||
| 264 | $preferences{editcolour}{light}{annbg} = $c[7]; | ||
| 265 | $preferences{editcolour}{light}{annfg} = "#ff0000"; | ||
| 266 | $preferences{editcolour}{light}{numbg} = | ||
| 267 | $preferences{editcolour}{dark}{numfg} = "#e8e8e8"; | ||
| 268 | $preferences{editcolour}{light}{numfg} = | ||
| 269 | $preferences{editcolour}{dark}{numbg} = "#303030"; | ||
| 270 | } | ||
| 271 | } | ||
| 272 | elsif ( $entry =~ /^(editcolour)_(\w+)_(\w+)$/ ) { | ||
| 273 | $cb->DeleteEntry($entry), next if $2 eq "auto"; | ||
| 274 | $preferences{$1}{$2}{$3} = $value; | ||
| 275 | } | ||
| 276 | else { | ||
| 277 | $preferences{$entry} = $value; | ||
| 278 | } | ||
| 279 | } | ||
| 280 | elsif ( $group eq "recents" ) { | ||
| 281 | push( @{$state{$group}}, $value ) | ||
| 282 | if fs_test( 's', $value ); | ||
| 283 | } | ||
| 284 | else { | ||
| 285 | $state{$group}->{$entry} = $value; | ||
| 286 | } | ||
| 287 | } | ||
| 288 | continue { | ||
| 289 | ( $goon, $entry, $index ) = $cb->GetNextEntry($index); | ||
| 290 | } | ||
| 291 | $cb->SetPath($cp); | ||
| 292 | ( $ggoon, $group, $gindex ) = $cb->GetNextGroup($gindex); | ||
| 293 | } | ||
| 294 | |||
| 295 | # Catch mistakes and abuse. | ||
| 296 | lock_keys(%preferences); | ||
| 297 | |||
| 298 | # Legacy font number -> font desc. | ||
| 299 | for ( qw( editfont msgsfont ) ) { | ||
| 300 | next unless $preferences{$_} =~ /^\d+$/; | ||
| 301 | $preferences{$_} = $fonts[$preferences{$_}]->GetNativeFontInfoDesc; | ||
| 302 | } | ||
| 303 | delete $ENV{CHORDPRO_LIB}; | ||
| 304 | |||
| 305 | if ( $preferences{settings_version} < SETTINGS_VERSION ) { | ||
| 306 | for ( qw( windows sash ) ) { | ||
| 307 | delete $state{$_}; | ||
| 308 | $cb->DeleteGroup($_); | ||
| 309 | } | ||
| 310 | } | ||
| 311 | |||
| 312 | $preferences{enable_pdfviewer} //= 0; | ||
| 313 | $preferences{enable_htmlviewer} //= 0; | ||
| 314 | $cb->Flush; | ||
| 315 | |||
| 316 | # Collect from the environment. | ||
| 317 | CP->setup_resdirs; | ||
| 318 | setup_styles(); | ||
| 319 | |||
| 320 | # For convenience. | ||
| 321 | setup_filters(); | ||
| 322 | |||
| 323 | if ( $preferences{dumpstate} ) { | ||
| 324 | use DDP; p %state; | ||
| 325 | } | ||
| 326 | } | ||
| 327 | |||
| 328 | # Store the preferences and other persistent state. | ||
| 329 | |||
| 330 | method Store :common { | ||
| 331 | |||
| 332 | my $cp = $config_root; | ||
| 333 | $preferences{settings_version} = SETTINGS_VERSION; | ||
| 334 | $cb->DeleteAll; | ||
| 335 | $cb->SetPath($cp); | ||
| 336 | |||
| 337 | # We sort all the keys, so we can compare configs (for testing). | ||
| 338 | for my $group ( sort keys %state ) { | ||
| 339 | my $v = $state{$group}; | ||
| 340 | |||
| 341 | next unless $group =~ m{ ^(?: | ||
| 342 | preferences | messages | recents | | ||
| 343 | sash | songbookexport | windows | ||
| 344 | )$ }x; | ||
| 345 | $cb->SetPath($cp); | ||
| 346 | |||
| 347 | # Re-write the recents. Array. | ||
| 348 | if ( $group eq "recents" && is_arrayref($v) ) { | ||
| 349 | # $cb->DeleteGroup($group); | ||
| 350 | $cb->SetPath($group); | ||
| 351 | for ( my $i = 0; $i < @$v; $i++ ) { | ||
| 352 | last if $i >= MAXRECENTS; | ||
| 353 | $cb->Write( "$i", $v->[$i] ); | ||
| 354 | } | ||
| 355 | next; | ||
| 356 | } | ||
| 357 | |||
| 358 | # Everything else are hash refs. | ||
| 359 | next unless is_hashref($v); | ||
| 360 | |||
| 361 | $cb->SetPath($group); | ||
| 362 | for my $k ( sort keys %$v ) { | ||
| 363 | my $v = $v->{$k}; | ||
| 364 | if ( $group eq "preferences" ) { | ||
| 365 | if ( $k eq "editcolour" && is_hashref($v) ) { | ||
| 366 | for my $k ( sort keys %$v ) { | ||
| 367 | my $v = $v->{$k}; | ||
| 368 | my $p = "editcolour_$k"; | ||
| 369 | for my $k ( sort keys %$v ) { | ||
| 370 | $cb->Write($p."_$k", $v->{$k} ); | ||
| 371 | } | ||
| 372 | } | ||
| 373 | next; | ||
| 374 | } | ||
| 375 | if ( $k =~ /^preset_(instruments|styles|stylemods|notations|xcodes)$/ ) { | ||
| 376 | die("ASSERT $k is array") unless is_arrayref($v); | ||
| 377 | $v = join( "\t", | ||
| 378 | sort( uniq( map { lc($_->{title}) } @$v ) ) ) | ||
| 379 | if @$v; | ||
| 380 | } | ||
| 381 | next if $k eq "editcolours"; | ||
| 382 | $v = join( ",", @$v ) if is_arrayref($v); | ||
| 383 | } | ||
| 384 | if ( defined $v ) { | ||
| 385 | $cb->Write( $k, $v ); | ||
| 386 | } | ||
| 387 | else { | ||
| 388 | warn("Preferences: No value for $group.$k (removed)\n"); | ||
| 389 | } | ||
| 390 | } | ||
| 391 | } | ||
| 392 | $cb->Flush; | ||
| 393 | } | ||
| 394 | |||
| 395 | ################ Private Subroutines ################ | ||
| 396 | |||
| 397 | # Fetch available config presets (styles, stylemods, ...). | ||
| 398 | sub setup_styles( $refresh = 0 ) { | ||
| 399 | return if $state{presets}->{styles} && !$refresh; | ||
| 400 | |||
| 401 | my %styles; # new style | ||
| 402 | my %stylemods; # new style | ||
| 403 | my %instruments; # new style | ||
| 404 | my %tasks; # new style | ||
| 405 | |||
| 406 | my $findopts = { filter => qr/^.*\.json$/i, recurse => 0 }; | ||
| 407 | |||
| 408 | local $ENV{CHORDPRO_LIB}; | ||
| 409 | CP->setup_resdirs; | ||
| 410 | |||
| 411 | # Collect standard style files (presets). | ||
| 412 | my @cfglibs = @{ CP->findresdirs("") }; | ||
| 413 | # use DDP; p @cfglibs, as => "cfglibs (for configs, raw)"; | ||
| 414 | |||
| 415 | # At this point, we can have one or two libs. The last one is | ||
| 416 | # the ChordPro standard library. | ||
| 417 | # If there are two, the first one is the user config lib. | ||
| 418 | # To this/these we prepend the custom lib. | ||
| 419 | my @c; | ||
| 420 | if ( $preferences{enable_customlib} && $preferences{customlib} ) { | ||
| 421 | push( @c, { src => "custom", | ||
| 422 | lib => fn_catfile( $preferences{customlib}, "config" ) } ); | ||
| 423 | } | ||
| 424 | if ( @cfglibs > 1) { | ||
| 425 | # Split off user config lib. | ||
| 426 | my $u = shift(@cfglibs); | ||
| 427 | # Push back only when !skipping. | ||
| 428 | push( @c, { src => "user", | ||
| 429 | lib => fn_catfile( $u, "config" ) } ) | ||
| 430 | if !$preferences{skipstdcfg}; | ||
| 431 | } | ||
| 432 | # Add ChordPro lib. | ||
| 433 | push( @c, { src => "std", | ||
| 434 | lib => fn_catfile( $cfglibs[-1], "config" ) } ); | ||
| 435 | $state{cfglibs} = \@c; | ||
| 436 | # use DDP; p @c, as => "cfglibs (for configs)"; | ||
| 437 | # warn("Config libs for configs\n"); | ||
| 438 | # warn( sprintf(" %-6s %s\n", $_->{src}, $_->{lib} ) ) | ||
| 439 | # for @c; | ||
| 440 | |||
| 441 | for ( @c ) { | ||
| 442 | my $cfglib = $_; | ||
| 443 | my $src = $cfglib->{src}; | ||
| 444 | $cfglib = $cfglib->{lib}; | ||
| 445 | next unless $cfglib && fs_test( d => $cfglib ); | ||
| 446 | next unless my $entries = fs_find( $cfglib, $findopts ); | ||
| 447 | |||
| 448 | foreach ( @$entries ) { | ||
| 449 | my $file = fn_catfile( $cfglib, $_->{name} ); | ||
| 450 | # warn("try $file\n"); | ||
| 451 | next unless fs_test( s => $file ); | ||
| 452 | |||
| 453 | my $data = fs_blob( $file ); | ||
| 454 | $data = json_load( decode_utf8($data), $file ); | ||
| 455 | next if is_true($data->{config}->{omit}); | ||
| 456 | |||
| 457 | my $types; | ||
| 458 | my %meta = ( src => $src, file => $file ); | ||
| 459 | if ( $data->{config}->{type} ) { | ||
| 460 | $types = $data->{config}->{type}; | ||
| 461 | $types = [ $types ] unless is_arrayref($types); | ||
| 462 | |||
| 463 | # If there is a type, it must be one of the supported types. | ||
| 464 | next unless any { /^(?:style|stylemod|instrument|task)$/ } @$types; | ||
| 465 | |||
| 466 | $meta{title} = $data->{config}->{title}; | ||
| 467 | $meta{desc} = $data->{config}->{description}; | ||
| 468 | $meta{exclude_id} = $data->{config}->{exclude_id}; | ||
| 469 | $meta{preview} = $data->{config}->{preview}; | ||
| 470 | $meta{default} = $data->{config}->{default}; | ||
| 471 | } | ||
| 472 | else { | ||
| 473 | next if $preferences{skipoldcfg}; | ||
| 474 | $types = [ "unknown" ]; | ||
| 475 | my $base = fn_basename( $_->{name}, ".json" ); | ||
| 476 | $meta{title} = $meta{desc} = _neat($base); | ||
| 477 | } | ||
| 478 | |||
| 479 | for my $type ( @$types ) { | ||
| 480 | $meta{type} = $type; | ||
| 481 | |||
| 482 | if ( $type eq "style" ) { | ||
| 483 | $styles{lc $meta{title}} = { %$_, %meta }; | ||
| 484 | } | ||
| 485 | elsif ( $type eq "stylemod" ) { | ||
| 486 | $stylemods{lc $meta{title}} = { %$_, %meta }; | ||
| 487 | } | ||
| 488 | elsif ( $type eq "instrument" ) { | ||
| 489 | $instruments{lc $meta{title}} = { %$_, %meta }; | ||
| 490 | } | ||
| 491 | elsif ( $type eq "task" ) { | ||
| 492 | $tasks{lc $meta{title}} = { %$_, %meta }; | ||
| 493 | } | ||
| 494 | elsif ( $type eq "unknown" ) { | ||
| 495 | $stylemods{lc $meta{title}} = { %$_, %meta }; | ||
| 496 | } | ||
| 497 | } | ||
| 498 | } | ||
| 499 | } | ||
| 500 | |||
| 501 | # Add the tasks and notations. | ||
| 502 | _add_tasks( \%tasks ); | ||
| 503 | _add_notations( \my %notes ); | ||
| 504 | |||
| 505 | $state{presets} = | ||
| 506 | { styles => \%styles, | ||
| 507 | tasks => \%tasks, | ||
| 508 | stylemods => \%stylemods, | ||
| 509 | instruments => \%instruments, | ||
| 510 | notations => \%notes, | ||
| 511 | }; | ||
| 512 | |||
| 513 | # for ( qw( instruments styles stylemods tasks notes ) ) { | ||
| 514 | # warn( ucfirst($_), ": ", | ||
| 515 | # plural( 0+keys(%{$state{presets}{$_}}), " entry", " entries" ), "\n" ); | ||
| 516 | # } | ||
| 517 | |||
| 518 | # Associate preset names with actual config files. | ||
| 519 | assoc_presets($_) for qw( instruments styles stylemods ); | ||
| 520 | assoc_notations($_) for qw( notations xcodes ); | ||
| 521 | |||
| 522 | # use DDP; warn np $state{presets}, as => "presets"; | ||
| 523 | |||
| 524 | } | ||
| 525 | |||
| 526 | sub assoc_presets( $preset ) { | ||
| 527 | my $args = $preferences{"preset_$preset"}; | ||
| 528 | my $list = $state{presets}{$preset}; | ||
| 529 | my $found; | ||
| 530 | my $multi = $preset eq "stylemods"; | ||
| 531 | |||
| 532 | 0 and warn("assoc: $preset\n"); | ||
| 533 | $preferences{"preset_$preset"} = []; | ||
| 534 | for ( values %$list ) { | ||
| 535 | my $looking_for = lc($_->{title}); | ||
| 536 | 0 and warn("assoc: looking for $preset ($looking_for)\n"); | ||
| 537 | for my $v ( @$args ) { | ||
| 538 | my $have = $v->{title}; | ||
| 539 | 0 and warn("assoc: try $have <> $looking_for\n"); | ||
| 540 | if ( $_->{exclude_id} ) { | ||
| 541 | for my $p ( @{$preferences{"preset_$preset"}} ) { | ||
| 542 | $have = "", last | ||
| 543 | if $p->{exclude_id} // "" eq $_->{exclude_id}; | ||
| 544 | } | ||
| 545 | } | ||
| 546 | push( @{$preferences{"preset_$preset"}}, $_ ) | ||
| 547 | if $have eq $looking_for; | ||
| 548 | last unless $multi; | ||
| 549 | } | ||
| 550 | last if $found && !$multi; | ||
| 551 | } | ||
| 552 | |||
| 553 | # Look up in the stylemods if not yet found. | ||
| 554 | unless ( $found || $preset =~ /^(stylemods|tasks|notations)$/ ) { | ||
| 555 | for ( values %{$state{presets}{stylemods}} ) { | ||
| 556 | my $looking_for = lc($_->{title}); | ||
| 557 | 0 and warn("assoc: looking for $looking_for in stylemods\n"); | ||
| 558 | for my $v ( @$args ) { | ||
| 559 | my $have = $v->{title}; | ||
| 560 | 0 and warn("assoc: try $have <> $looking_for\n"); | ||
| 561 | push( @{$preferences{"preset_$preset"}}, $_ ) | ||
| 562 | if $have eq $looking_for; | ||
| 563 | last unless $multi; | ||
| 564 | } | ||
| 565 | last if $found && !$multi; | ||
| 566 | } | ||
| 567 | } | ||
| 568 | |||
| 569 | return; | ||
| 570 | # use DDP; p $preferences{"preset_$preset"}; | ||
| 571 | warn("Presets for $preset\n"); | ||
| 572 | warn( sprintf(" %-6s %s\n", $_->{src}, $_->{title} ) ) | ||
| 573 | for @{$preferences{"preset_$preset"}}; | ||
| 574 | } | ||
| 575 | |||
| 576 | sub assoc_notations( $preset ) { | ||
| 577 | my $args = $preferences{"preset_$preset"}; | ||
| 578 | my $list = $state{presets}{notations}; | ||
| 579 | my $found; | ||
| 580 | |||
| 581 | 0 and warn("assoc: $preset\n"); | ||
| 582 | |||
| 583 | $preferences{"preset_$preset"} = [ $state{default_notation} ]; | ||
| 584 | |||
| 585 | for ( values %$list ) { | ||
| 586 | my $looking_for = lc($_->{title}); | ||
| 587 | 0 and warn("assoc: looking for $preset ($looking_for)\n"); | ||
| 588 | for my $v ( @$args ) { | ||
| 589 | my $have = $v->{title}; | ||
| 590 | 0 and warn("assoc: try $have <> $looking_for\n"); | ||
| 591 | if ( $have eq $looking_for ) { | ||
| 592 | $preferences{"preset_$preset"} = [ $_ ]; | ||
| 593 | $found++; | ||
| 594 | last; | ||
| 595 | } | ||
| 596 | } | ||
| 597 | last if $found; | ||
| 598 | } | ||
| 599 | |||
| 600 | return; | ||
| 601 | # use DDP; p $list, as => "list"; | ||
| 602 | # use DDP; p $preferences{"preset_$preset"}, as => "select"; | ||
| 603 | warn("Presets for $preset\n"); | ||
| 604 | warn( sprintf(" %-6s %s\n", $_->{src}, $_->{title} ) ) | ||
| 605 | for @{$preferences{"preset_$preset"}}; | ||
| 606 | } | ||
| 607 | |||
| 608 | # Fetch available tasks. Called by setup_presets. | ||
| 609 | sub _add_tasks( $tasks ) { | ||
| 610 | my @cfglibs; | ||
| 611 | for ( my $i = 0; $i < @{ $state{cfglibs} }; $i++ ) { | ||
| 612 | my $lib = $state{cfglibs}->[$i]; | ||
| 613 | push( @cfglibs, | ||
| 614 | { src => $lib->{src}, | ||
| 615 | lib => fn_catfile( fn_dirname($lib->{lib}), "tasks" ) } ); | ||
| 616 | pop( @cfglibs ) unless fs_test( d => $cfglibs[-1]->{lib} ); | ||
| 617 | } | ||
| 618 | #use DDP; p @cfglibs, as => "cfglibs (for tasks)"; | ||
| 619 | #warn("Config libs for tasks\n"); | ||
| 620 | #warn( sprintf(" %-6s %s\n", $_->{src}, $_->{lib} ) ) | ||
| 621 | # for @cfglibs; | ||
| 622 | |||
| 623 | my $findopts = { filter => qr/^.*\.(?:json|prp)$/i, recurse => 0 }; | ||
| 624 | |||
| 625 | for ( @cfglibs ) { | ||
| 626 | my $cfglib = $_; | ||
| 627 | my $src = $cfglib->{src}; | ||
| 628 | $cfglib = $cfglib->{lib}; | ||
| 629 | next unless my $entries = fs_find( $cfglib, $findopts ); | ||
| 630 | |||
| 631 | foreach ( @$entries ) { | ||
| 632 | my $file = fn_catfile( $cfglib, $_->{name} ); | ||
| 633 | # warn("try $file\n"); | ||
| 634 | next unless fs_test( s => $file ); | ||
| 635 | |||
| 636 | my $blob = fs_blob( $file ); | ||
| 637 | my $data = json_load( decode_utf8($blob), $file ); | ||
| 638 | next if is_true($data->{config}->{omit}); | ||
| 639 | |||
| 640 | my $types; | ||
| 641 | my %meta = ( src => $src, file => $file ); | ||
| 642 | if ( $data->{config}->{type} ) { | ||
| 643 | $types = $data->{config}->{type}; | ||
| 644 | $types = [ $types ] unless is_arrayref($types); | ||
| 645 | $meta{title} = $data->{config}->{title}; | ||
| 646 | $meta{desc} = $data->{config}->{description}; | ||
| 647 | for my $type ( @$types ) { | ||
| 648 | next unless $type eq "task"; | ||
| 649 | $tasks->{lc $meta{title}} = { %$_, %meta }; | ||
| 650 | } | ||
| 651 | } | ||
| 652 | else { | ||
| 653 | my $base = fn_basename( $_->{name}, ".json", ".prp" ); | ||
| 654 | |||
| 655 | # Tentative title (description). | ||
| 656 | my $desc = _neat($base); | ||
| 657 | |||
| 658 | # See if there's a title in the first line. | ||
| 659 | # E.g. // ChordPro Task: Chords In-line | ||
| 660 | if ( $blob =~ m;(?://|\#)\s*(?:chordpro\s*)?task:\s*(.*);i ) { | ||
| 661 | $desc = $1; | ||
| 662 | } | ||
| 663 | $meta{title} = $meta{desc} = $desc; | ||
| 664 | $tasks->{lc $desc} = { %$_, %meta }; | ||
| 665 | } | ||
| 666 | } | ||
| 667 | } | ||
| 668 | } | ||
| 669 | |||
| 670 | # List of available notation systems. | ||
| 671 | sub _add_notations( $notes ) { | ||
| 672 | |||
| 673 | my @cfglibs; | ||
| 674 | for ( my $i = 0; $i < @{ $state{cfglibs} }; $i++ ) { | ||
| 675 | my $lib = $state{cfglibs}->[$i]; | ||
| 676 | push( @cfglibs, | ||
| 677 | { src => $lib->{src}, | ||
| 678 | lib => fn_catfile( $lib->{lib}, "notes" ) } ); | ||
| 679 | pop( @cfglibs ) unless fs_test( d => $cfglibs[-1]->{lib} ); | ||
| 680 | } | ||
| 681 | # use DDP; p @cfglibs, as => "cfglibs (for notes)"; | ||
| 682 | # warn("Config libs for notes\n"); | ||
| 683 | # warn( sprintf(" %-6s %s\n", $_->{src}, $_->{lib} ) ) | ||
| 684 | # for @cfglibs; | ||
| 685 | |||
| 686 | my $findopts = { filter => qr/^.*\.json$/i, recurse => 0 }; | ||
| 687 | delete $state{default_notation}; | ||
| 688 | |||
| 689 | for ( @cfglibs ) { | ||
| 690 | my $cfglib = $_; | ||
| 691 | my $src = $cfglib->{src}; | ||
| 692 | $cfglib = $cfglib->{lib}; | ||
| 693 | next unless my $entries = fs_find( $cfglib, $findopts ); | ||
| 694 | |||
| 695 | foreach ( @$entries ) { | ||
| 696 | my $file = fn_catfile( $cfglib, $_->{name} ); | ||
| 697 | # warn("try $file\n"); | ||
| 698 | next unless fs_test( s => $file ); | ||
| 699 | |||
| 700 | my $blob = fs_blob( $file ); | ||
| 701 | my $data = json_load( decode_utf8($blob), $file ); | ||
| 702 | next if is_true($data->{config}->{omit}); | ||
| 703 | my $types = $data->{config}->{type}; | ||
| 704 | next unless $types; | ||
| 705 | |||
| 706 | my %meta = ( src => $src, file => $file ); | ||
| 707 | $types = [ $types ] unless is_arrayref($types); | ||
| 708 | $meta{title} = $data->{config}->{title}; | ||
| 709 | $meta{desc} = $data->{config}->{description}; | ||
| 710 | $meta{default} = $data->{config}->{default}; | ||
| 711 | $meta{system} = $data->{notes}->{system}; | ||
| 712 | for my $type ( @$types ) { | ||
| 713 | next unless $type eq "notes"; | ||
| 714 | $notes->{lc $meta{title}} = { %$_, %meta }; | ||
| 715 | $state{default_notation} = { %$_, %meta } | ||
| 716 | if $meta{default}; | ||
| 717 | } | ||
| 718 | } | ||
| 719 | } | ||
| 720 | # use DDP; p $notes, as => "notes"; | ||
| 721 | $state{default_notation} or | ||
| 722 | die("ASSERT FAIL: No default notation found\n"); | ||
| 723 | } | ||
| 724 | |||
| 725 | sub _neat { | ||
| 726 | my ($t ) = @_; | ||
| 727 | $t = ucfirst(lc($t)); | ||
| 728 | $t =~ s/_/ /g; | ||
| 729 | $t =~ s/ (.)/" ".uc($1)/eg; | ||
| 730 | $t; | ||
| 731 | } | ||
| 732 | |||
| 733 | sub setup_filters() { | ||
| 734 | my $lst = "*." . | ||
| 735 | join( ",*.", | ||
| 736 | uniq( substr($preferences{chordproext},1), | ||
| 737 | qw( cho crd chopro chord chordpro pro ) ) ); | ||
| 738 | $state{ffilters} = "ChordPro files ($lst)|" . $lst =~ s/,/;/gr . | ||
| 739 | (is_macos ? ";*.txt" : "|All files|*.*"); | ||
| 740 | } | ||
| 741 | |||
| 742 | 1; |