X-Git-Url: https://git.camperquake.de/gitweb.cgi?p=videosite.git;a=blobdiff_plain;f=libvideosite.pm;h=01fa7d9f2e15429311bdf7f0df02d7044a942b9f;hp=f7f3d9da2d4e47416ed08ad5546513b80033b422;hb=01fc17a9fd2a11cd5826a2581fe662e96ac5912c;hpb=f9c997ac49bfdcd65ee89127f4d806e813355d8c diff --git a/libvideosite.pm b/libvideosite.pm index f7f3d9d..01fa7d9 100644 --- a/libvideosite.pm +++ b/libvideosite.pm @@ -17,6 +17,8 @@ use LWP::UserAgent; use Data::Dumper; use File::Basename; use Cwd qw(realpath); +use JSON -support_by_pp; +use File::Temp qw(tempfile); use strict; @ISA = qw(Exporter); @@ -30,6 +32,8 @@ my @grabbers; my @getters; my $getter; my %builtin_config = (); +my $builtin_config_path; +my $builtin_config_default; our $error; # @@ -881,8 +885,6 @@ sub _cmd_debug { _io("Debug for this window enabled"); $debugwindows{$event->{window}} = 1; } - - _io("keys in debugwindows: %s", join(", ", keys(%debugwindows))); } # @@ -903,8 +905,6 @@ sub _cmd_nodebug { delete($debugwindows{$event->{window}}); _io("Debug for this window disabled"); } - - _io("keys in debugwindows: %s", join(", ", keys(%debugwindows))); } @@ -925,6 +925,29 @@ sub _grabbers { # ============================================== # sub _builtin_config_init { + + if (defined($builtin_config_path)) { + my $filename = File::Spec->catfile($builtin_config_path, 'videosite.json'); + + _debug("Trying to load configuration from %s", $filename); + + if (-r $filename) { + eval { + local $/; + open(CONF, '<', $filename); + %builtin_config = %{JSON->new->utf8->decode()}; + close(CONF); + } or do { + _io("Error loading configuration: %s", $@); + } + }; + } elsif (defined($builtin_config_default)) { + _debug("Initializing builtin config from external default"); + foreach (keys(%{$builtin_config_default})) { + _debug("Setting %s=%s", $_, $builtin_config_default->{$_}); + $builtin_config{$_} = $builtin_config_default->{$_}; + } + } } sub _builtin_config_get { @@ -940,6 +963,23 @@ sub _builtin_config_has { } sub _builtin_config_save { + + if (defined($builtin_config_path)) { + my $filename = File::Spec->catfile($builtin_config_path, 'videosite.json'); + + _debug("Attempting to save config to %s", $filename); + + eval { + my ($tempfile, $tempfn) = tempfile("videosite.json.XXXXXX", dir => $builtin_config_path); + print $tempfile JSON->new->pretty->utf8->encode(\%builtin_config); + close($tempfile); + rename($tempfn, $filename); + } or do { + return 0; + } + } + + return 1; } sub _builtin_config_del { @@ -1038,6 +1078,14 @@ sub register_api { $debug = $a->{_debug}->(); } + if (exists($a->{_config_path})) { + $builtin_config_path = $a->{_config_path}->(); + } + + if (exists($a->{_config_default})) { + $builtin_config_default = $a->{_config_default}->(); + } + @outputstack = ({ewpf => $remote_api->{'io'}, window => ""}); return 1;