| 1 | # -*- perl -*- | ||
| 2 | |||
| 3 | # | ||
| 4 | # Author: Slaven Rezic | ||
| 5 | # | ||
| 6 | # Copyright (C) 2002,2012 Slaven Rezic. All rights reserved. | ||
| 7 | # This program is free software; you can redistribute it and/or | ||
| 8 | # modify it under the same terms as Perl itself. | ||
| 9 | # | ||
| 10 | # Mail: slaven@rezic.de | ||
| 11 | # WWW: http://www.rezic.de/eserte/ | ||
| 12 | # | ||
| 13 | |||
| 14 | package Tk::Pod::Cache; | ||
| 15 | use strict; | ||
| 16 | use vars qw($VERSION $MAX_CACHE %CACHE); | ||
| 17 | |||
| 18 | BEGIN { # Make a DEBUG constant very first thing... | ||
| 19 | if(defined &DEBUG) { | ||
| 20 | } elsif(($ENV{'TKPODDEBUG'} || '') =~ m/^(\d+)/) { # untaint | ||
| 21 | my $debug = $1; | ||
| 22 | *DEBUG = sub () { $debug }; | ||
| 23 | } else { | ||
| 24 | *DEBUG = sub () {0}; | ||
| 25 | } | ||
| 26 | } | ||
| 27 | |||
| 28 | $VERSION = '5.02'; | ||
| 29 | |||
| 30 | $MAX_CACHE = 20; # documents # XXX not yet used, LRU etc... | ||
| 31 | |||
| 32 | sub add_to_cache { | ||
| 33 | my($w, $pod) = @_; | ||
| 34 | $pod = $w->cget(-path) if !defined $pod; | ||
| 35 | return if !defined $pod; | ||
| 36 | return if $CACHE{$pod}; # XXX check for recentness | ||
| 37 | DEBUG and warn "Add contents for $pod to cache.\n"; | ||
| 38 | $CACHE{$pod} = $w->dump_contents; | ||
| 39 | } | ||
| 40 | |||
| 41 | sub get_from_cache { | ||
| 42 | my($w, $pod) = @_; | ||
| 43 | $pod = $w->cget(-path) if !defined $pod; | ||
| 44 | return 0 if !$CACHE{$pod}; | ||
| 45 | # XXX check for recentness | ||
| 46 | $w->delete("1.0", "end"); | ||
| 47 | DEBUG and warn "Restore contents for $pod from cache.\n"; | ||
| 48 | $w->restore_contents($CACHE{$pod}); | ||
| 49 | 1; | ||
| 50 | } | ||
| 51 | |||
| 52 | sub delete_from_cache { | ||
| 53 | my($w, $pod) = @_; | ||
| 54 | $pod = $w->cget(-path) if !defined $pod; | ||
| 55 | return if !defined $pod; | ||
| 56 | DEBUG and warn "Delete contents for $pod from cache.\n"; | ||
| 57 | delete $CACHE{$pod}; | ||
| 58 | } | ||
| 59 | |||
| 60 | sub clear_cache { | ||
| 61 | %CACHE = (); | ||
| 62 | } | ||
| 63 | |||
| 64 | sub dump_contents { | ||
| 65 | my $w = shift; | ||
| 66 | my @dump = $w->dump('-all', "1.0", "end"); | ||
| 67 | if (@dump == 0) { | ||
| 68 | warn "Workaround strange bug under RedHat 8.0 --- try dump again..."; | ||
| 69 | @dump = $w->dump('-all', "1.0", "end"); | ||
| 70 | if (@dump == 0) { | ||
| 71 | warn "Giving up, cache disabled for current page"; | ||
| 72 | return undef; | ||
| 73 | } | ||
| 74 | } | ||
| 75 | my %tags_def; | ||
| 76 | foreach my $tag ($w->tagNames) { | ||
| 77 | # XXX check for used/existing tags missing | ||
| 78 | my @tag_def; | ||
| 79 | foreach my $item ($w->tagConfigure($tag)) { | ||
| 80 | my $value = $item->[4]; | ||
| 81 | my $option = $item->[0]; | ||
| 82 | push @tag_def, $option, $value; | ||
| 83 | } | ||
| 84 | $tags_def{$tag} = \@tag_def; | ||
| 85 | } | ||
| 86 | return {Dump => \@dump, | ||
| 87 | Tags => \%tags_def, | ||
| 88 | Sections => $w->{'sections'}, | ||
| 89 | PodTitle => $w->{'pod_title'}, | ||
| 90 | }; | ||
| 91 | } | ||
| 92 | |||
| 93 | sub restore_contents { | ||
| 94 | my($w, $def) = @_; | ||
| 95 | |||
| 96 | my $dumpref = $def->{Dump}; | ||
| 97 | my $tagref = $def->{Tags}; | ||
| 98 | $w->{'sections'} = $def->{Sections}; | ||
| 99 | $w->{'pod_title'} = $def->{PodTitle}; | ||
| 100 | |||
| 101 | $w->toplevel->title( "Tkpod: " . $w->{'pod_title'} . " (restoring)"); | ||
| 102 | $w->idletasks; | ||
| 103 | # XXX Is it bad form to manipulate the top level? | ||
| 104 | |||
| 105 | my $process_no; | ||
| 106 | $w->{ProcessNo}++; | ||
| 107 | $process_no = $w->{ProcessNo}; | ||
| 108 | |||
| 109 | if ($tagref) { | ||
| 110 | while(my($tag,$def) = each %$tagref) { | ||
| 111 | #XXX tagDelete? | ||
| 112 | $w->tagConfigure($tag, @$def); | ||
| 113 | } | ||
| 114 | } | ||
| 115 | |||
| 116 | my @taglist; | ||
| 117 | |||
| 118 | my $last_update = Tk::timeofday(); | ||
| 119 | for(my $i=0; $i<$#$dumpref; $i+=3) { | ||
| 120 | my($key, $val, $index) = @{$dumpref}[$i..$i+2]; | ||
| 121 | if ($key eq 'text') { | ||
| 122 | $w->insert($index, $val, [@taglist]); | ||
| 123 | } elsif ($key eq 'tagon') { | ||
| 124 | unshift @taglist, $val; | ||
| 125 | } elsif ($key eq 'tagoff') { | ||
| 126 | my $j; | ||
| 127 | for (0 .. $#taglist) { | ||
| 128 | if ($taglist[$_] eq $val) { | ||
| 129 | $j = $_; | ||
| 130 | last; | ||
| 131 | } | ||
| 132 | } | ||
| 133 | if (defined $j) { | ||
| 134 | splice @taglist, $j, 1; | ||
| 135 | } | ||
| 136 | $w->tag('remove', $val, 'insert'); | ||
| 137 | } elsif ($key eq 'mark') { | ||
| 138 | $w->markSet($val, $index); # XXX ->see() to current or insert? | ||
| 139 | } elsif ($key eq 'windows') { | ||
| 140 | die "not yet supported"; | ||
| 141 | } elsif ($key eq 'image') { | ||
| 142 | die "not yet supported"; | ||
| 143 | } elsif ($key eq 'imgdef') { | ||
| 144 | die "not yet supported"; | ||
| 145 | } | ||
| 146 | |||
| 147 | if (Tk::timeofday() > $last_update+0.5) { # XXX make configurable | ||
| 148 | $w->update; | ||
| 149 | $last_update = Tk::timeofday(); | ||
| 150 | do { warn "ABORT!"; return } if $w->{ProcessNo} != $process_no; | ||
| 151 | } | ||
| 152 | } | ||
| 153 | |||
| 154 | $w->parent->add_section_menu if $w->parent->can('add_section_menu'); | ||
| 155 | $w->Callback('-poddone', $w->cget(-file)); | ||
| 156 | |||
| 157 | $w->toplevel->title( "Tkpod: " . $w->{'pod_title'}); | ||
| 158 | } | ||
| 159 | |||
| 160 | 1; | ||
| 161 | |||
| 162 | __END__ |