File Coverage

lib/Kephra/File/History.pm
Criterion Covered Total %
statement 6 64 9.3
branch 0 16 0.0
condition 0 5 0.0
subroutine 2 13 15.3
pod 0 9 0.0
total 8 107 7.4


line stmt bran cond sub pod time code
1             package Kephra::File::History;
2             our $VERSION = '0.06';
3              
4 1     1   982 use strict;
  1         2  
  1         33  
5 1     1   6 use warnings;
  1         2  
  1         1017  
6              
7             my @session = ();
8             my $menu_id = '&file_history';
9             my $refresh_needed;
10             my $loaded;
11             # internal Module API
12 0     0     sub _config { Kephra::API::settings()->{file}{session}{history} }
13             # external Appwide API
14             sub init {
15 0 0   0 0   return if scalar @session;
16 0           my $config = Kephra::File::Session::_config();
17 0 0         return unless defined $config;
18              
19 0           my $subdir = $config->{directory};
20 0           my $file = Kephra::Config::filepath( $subdir, _config()->{file} );
21 0           my $config_tree = Kephra::Config::File::load($file);
22 0 0         if (ref $config_tree->{document} eq 'ARRAY'){
23 0           @session = @{$config_tree->{document}};
  0            
24             }
25             Kephra::EventTable::add_call ( 'document.close', __PACKAGE__, sub {
26 0     0     Kephra::File::History::add( Kephra::Document::Data::current_nr() );
27 0           }, __PACKAGE__ );
28              
29 0           $loaded = 1;
30             }
31 0     0 0   sub had_init {$loaded}
32              
33             sub save {
34 0     0 0   my $subdir = Kephra::File::Session::_config()->{directory};
35 0           my $file = Kephra::Config::filepath( $subdir, _config()->{file} );
36 0           my $config_tree;
37 0           @{$config_tree->{document}} = @session;
  0            
38 0           Kephra::Config::File::store( $file, $config_tree);
39             }
40              
41             sub delete_gone {
42 0     0 0   my $length = @session;
43 0           my $file = Kephra::Document::Data::get_file_path();
44 0           @session = grep { $_->{file_path} ne $file } @session;
  0            
45 0 0         $refresh_needed = 1 if $length != @session;
46             }
47              
48             sub get {
49 0     0 0   delete_gone();
50 0           \@session;
51             }
52              
53             sub update {
54 0     0 0   delete_gone();
55 0 0         if ($refresh_needed){
56 0           $refresh_needed = 0;
57 0           return 1;
58             }
59             }
60              
61             sub add {
62 0     0 0   my $doc_nr = Kephra::Document::Data::validate_doc_nr(shift);
63 0 0         return if $doc_nr < 0;
64 0           my $attr = Kephra::Document::Data::_hash($doc_nr);
65 0 0         return unless $attr->{'file_name'};
66 0           my %saved_attr;
67 0           $saved_attr{$_} = $attr->{$_} for @{ Kephra::File::Session::_saved_properties() };
  0            
68 0           unshift @session, \%saved_attr;
69 0   0       my $length = _config->{length} || 0;
70 0           pop @session while @session > $length;
71 0           $refresh_needed = 1;
72             }
73              
74             sub open {
75 0     0 0   my $hist_nr = shift;
76 0 0 0       return if $hist_nr < 0 or $hist_nr > $#session;
77 0           my $doc_nr = Kephra::Document::Data::get_current_nr();
78 0           my $new_nr = Kephra::Document::restore( splice @session, $hist_nr , 1 );
79 0           Kephra::Document::Data::set_current_nr( $doc_nr );
80 0           Kephra::Document::Change::to_number( $new_nr );
81 0           $refresh_needed = 1;
82 0           Kephra::EventTable::trigger('document.list');
83             }
84              
85             sub open_all {
86 0     0 0   my $new_nr;
87 0           my $doc_nr = Kephra::Document::Data::get_current_nr();
88 0           $new_nr = Kephra::Document::restore( $_ ) for @session;
89 0           Kephra::Document::Data::set_current_nr( $doc_nr );
90 0           Kephra::Document::Change::to_number( $new_nr );
91 0           @session = ();
92 0           $refresh_needed = 1;
93 0           Kephra::EventTable::trigger('document.list');
94             }
95              
96             1;