From 1fa098b83fe7b32e6d13f165c2a9bea585150fe4 Mon Sep 17 00:00:00 2001 From: Martin Mares Date: Wed, 26 Dec 2012 19:22:42 +0100 Subject: [PATCH] Gallery2: Moving to object-oriented interface (not finished yet) --- gal2/Makefile | 3 +- gal2/UCW/Gallery.pm | 128 +++++++++++++++++------------ gal2/UCW/Gallery/Web.pm | 140 ++++++++++++++++---------------- gal2/UCW/Gallery/Web/NrtBlue.pm | 84 +++++++++++++++++++ gal2/gal-show-cf | 8 +- gal2/nrt-blue/theme.conf | 40 --------- gal2/nrt-blue/theme.pm | 40 --------- 7 files changed, 234 insertions(+), 209 deletions(-) create mode 100644 gal2/UCW/Gallery/Web/NrtBlue.pm delete mode 100644 gal2/nrt-blue/theme.conf delete mode 100644 gal2/nrt-blue/theme.pm diff --git a/gal2/Makefile b/gal2/Makefile index 88b61de..e19fbcf 100644 --- a/gal2/Makefile +++ b/gal2/Makefile @@ -1,6 +1,5 @@ $(eval $(dir-setup)) -$(call lib-copy, UCW/Gallery.pm UCW/Gallery/Web.pm) +$(call lib-copy, UCW/Gallery.pm UCW/Gallery/Web.pm UCW/Gallery/Web/NrtBlue.pm) $(call copy, $(addprefix nrt-blue/,back.png bot.png left.png next.png prev.png right.png top.png style.css)) -$(call lib-copy, nrt-blue/theme.pm) diff --git a/gal2/UCW/Gallery.pm b/gal2/UCW/Gallery.pm index c8bdc68..8b4c3fa 100644 --- a/gal2/UCW/Gallery.pm +++ b/gal2/UCW/Gallery.pm @@ -8,51 +8,42 @@ use warnings; use Storable; -BEGIN { - # Standard Perl module stuff - use Exporter(); - our ($VERSION, @ISA, @EXPORT, @EXPORT_OK, %EXPORT_TAGS); - $VERSION = 1.00; - @ISA = qw(Exporter); - @EXPORT = qw(&SetOptions); - %EXPORT_TAGS = (); - @EXPORT_OK = qw(%CF); +### Class methods ### + +sub new($) { + my ($class) = @_; + my $self = { }; + $self->{cfg} = { + # Directories + OrigDir => '.', # Original images + PhotoDir => 'photo', # Scaled-down photos for web + CacheDir => 'cache', # Cache with meta-data and thumbnails + + # URL prefixes + PhotoUrlPrefix => 'photo/', + ThumbUrlPrefix => 'thumb/', + ThemeUrlPrefix => 'gal/', + + # Processing machinery settings + ScanDefaultTransform => 's', + PhotoMaxWidth => 1024, + PhotoMaxHeight => 1024, + ThumbFormats => {}, # Set up by themes + + # Titles and navigation + Title => 'An Unnamed Gallery', + SubTitle => "", + ParentURL => '../', + BackURL => "", + FwdURL => "", + }; + return bless $self, $class; } -our %CF; - -BEGIN { %CF = ( - # Directories - OrigDir => '.', # Original images - PhotoDir => 'photo', # Scaled-down photos for web - CacheDir => 'cache', # Cache with meta-data and thumbnails - ThemeDir => 'gal', # Themes - - # URL prefixes - PhotoUrlPrefix => 'photo/', - ThumbUrlPrefix => 'thumb/', - ThemeUrlPrefix => 'gal/', - - # Processing machinery settings - ScanDefaultTransform => 's', - PhotoMaxWidth => 1024, - PhotoMaxHeight => 1024, - ThumbFormats => {}, # Set up by themes - - # HTML output settings - Title => 'An Unnamed Gallery', - HeadExtras => "", - TopExtras => "", - BotExtras => "", - ParentURL => '../', - BackURL => "", - FwdURL => "", - ImageSubpages => 1, -); } - -sub LoadConfig() { +sub load_config($) { my $cfg = "./gallery.cf"; - unless (defined do $cfg) { + my $self = do $cfg; + unless (defined $self) { if ($@) { die "Error parsing $cfg: $@"; } elsif ($!) { @@ -61,25 +52,55 @@ sub LoadConfig() { die "Cannot load $cfg, check that it returns true\n"; } } + return $self; } -sub SetOptions(@) { - while (my $o = shift @_) { - my $v = shift @_; - $CF{$o} = $v; - if ($o eq "Theme") { - require $CF{'ThemeDir'} . "/$v/theme.pm"; - UCW::Gallery::Theme::Init($CF{'ThemeUrlPrefix'} . $v); - } +### Object methods ### + +sub get($$) { + my ($self, $key) = @_; + if (exists $self->{cfg}->{$key}) { + my $val = $self->{cfg}->{$key}; + defined $val or warn "Gallery: Config item $key is not set\n"; + return $val; + } else { + warn "Gallery: Config item $key does not exist\n"; + return; + } +} + +sub def($@) { + my $self = shift; + while (my $key = shift @_) { + my $val = shift @_; + !exists $self->{cfg}->{$key} or warn "Gallery: Re-definining config item $key\n"; + $self->{cfg}->{$key} = $val; + } +} + +sub set($@) { + my $self = shift; + while (my $key = shift @_) { + my $val = shift @_; + exists $self->{cfg}->{$key} or warn "Gallery: Config item $key does not exist\n"; + $self->{cfg}->{$key} = $val; } } -sub RequireThumbnails($$) { - my ($w, $h) = @_; +sub get_config_keys($) { + my ($self) = @_; + return keys %{$self->{cfg}}; +} + +sub require_thumbnails($$$) { + my ($self, $w, $h) = @_; my $fmt = "${w}x${h}"; - $CF{'ThumbFormats'}->{$fmt} = 1; + $self->{cfg}->{ThumbFormats}->{$fmt} = 1; + return $fmt; } +### Subroutines (to be converted to methods later) ### + sub WriteList($$) { my ($file, $images) = @_; open LIST, '>', "$file.new" or die "Cannot create $file.new: $!\n"; @@ -123,6 +144,7 @@ sub WriteMeta($$) { sub ReadMeta($) { my ($file) = @_; + # FIXME: open my META open META, '<', $file or die "Cannot read $file: $!\n"; my $meta = Storable::fd_retrieve(\*META) or die "Cannot parse $file\n"; close META; diff --git a/gal2/UCW/Gallery/Web.pm b/gal2/UCW/Gallery/Web.pm index d2a718a..95532d8 100644 --- a/gal2/UCW/Gallery/Web.pm +++ b/gal2/UCW/Gallery/Web.pm @@ -3,7 +3,10 @@ package UCW::Gallery::Web; -use UCW::Gallery qw(%CF); +use strict; +use warnings; + +use UCW::Gallery; use UCW::CGI; use File::Spec; @@ -13,124 +16,121 @@ my %args = ( 'i' => { 'var' => \$show_img, 'check' => '\d+' }, ); -our $meta; -our $num_photos; - sub error($) { print "

Bad luck, the script is broken. Sorry.\n

$_[0]\n"; print "\n"; } -sub show_links($$$) { - my ($prev, $up, $next) = @_; - print "

"; - print ""; - print "Back" if $prev ne ""; - print "\n"; - print ""; - print "Forward" if $next ne ""; - print "\n"; - print "Up\n" if $up ne ""; +sub get($$) { + my ($self, $key) = @_; + return $self->{gal}->get($key); } -sub html_top() { - my $title = UCW::CGI::html_escape($CF{"Title"}); +sub html_top($) { + my ($self) = @_; + my $title = UCW::CGI::html_escape($self->get('Title')); + my $hextras = $self->get('WebHeadExtras'); + my $textras = $self->get('WebTopExtras'); + my $theme_hextras = $self->theme_head_extras; print < -$CF{"HeadExtras"} - +$hextras +$theme_hextras $title -$CF{"TopExtras"} +$textras EOF $UCW::CGI::error_hook = \&error; } -sub html_bot() { - print "$CF{'BotExtras'}\n\n"; +sub html_bot($) { + my ($self) = @_; + print $self->get('WebBotExtras'), "\n\n"; } -sub show_img() { - if ($show_img < 1 || $show_img > $num_photos) { +sub show_img($) { + my ($self) = @_; + + if ($show_img < 1 || $show_img > $self->{num_photos}) { print "Status: 404\n"; - html_top(); + $self->html_top; print "

No such photo

\n"; - html_bot(); + $self->html_bot; return; } + my $meta = $self->{meta}; my $id = $meta->{sequence}->[$show_img-1]; my $m = $meta->{photo}->{$id} or die; - html_top(); + $self->html_top; - show_links(($show_img > 1 ? ("?i=".($show_img-1)) : ""), - "?", - ($show_img < $num_photos ? ("?i=".($show_img+1)) : "")); + $self->show_links(($show_img > 1 ? ("?i=".($show_img-1)) : ""), + "?", + ($show_img < $self->{num_photos} ? ("?i=".($show_img+1)) : "")); my $t = UCW::CGI::html_escape($m->{title}); my $w = $m->{w}; my $h = $m->{h}; print "

$t

\n" if $t ne ""; - my $img = $CF{'PhotoUrlPrefix'} . $id . ".jpg"; + my $img = $self->get('PhotoUrlPrefix') . $id . '.jpg'; print "

$t\n"; - html_bot(); + $self->html_bot; } -sub show_list() { - html_top(); +sub show_list($) { + my ($self) = @_; + $self->html_top; - show_links($CF{'BackURL'}, $CF{'ParentURL'}, $CF{'FwdURL'}); - print "

$CF{'Title'}

\n"; - print "

$CF{'SubTitle'}

\n" if defined $CF{'SubTitle'}; + $self->show_links($self->get('BackURL'), $self->get('ParentURL'), $self->get('FwdURL')); + print "

", $self->get('Title'), "

\n"; + my $subtitle = $self->get('SubTitle'); + print "

$subtitle

\n" if $subtitle ne ""; - my $thumb_fmt = $CF{'ThumbW'} . "x" . $CF{'ThumbH'}; - my $thumb_meta = $meta->{thumb}->{$thumb_fmt} or die "No thumbnails for format $thumb_fmt found!\n"; - - for my $idx (1..$num_photos) { + my $meta = $self->{meta}; + for my $idx (1..$self->{num_photos}) { my $id = $meta->{sequence}->[$idx-1]; - my $m = $meta->{photo}->{$id} or die; - my $tm = $thumb_meta->{$id} or die; - - my $annot = UCW::CGI::html_escape($m->{title}); - my $tw = $tm->{w}; - my $th = $tm->{h}; - my $thumb = $CF{'ThumbUrlPrefix'} . "$thumb_fmt/$id.jpg"; - my $side_w = $CF{"ThumbW"} + 2*$CF{"InteriorMargin"}; - my $side_h = $CF{"ThumbH"} + 2*$CF{"InteriorMargin"}; - my $box_w = $CF{"LeftW"} + $side_w + $CF{"RightW"}; - my $box_h = $CF{"TopH"} + $side_h + $CF{"BotH"}; - print "
\n"; - print "\n"; - print "\n"; - my $ol = $CF{'LeftW'} + $CF{'InteriorMargin'} + int(($CF{'ThumbW'} - $tw)/2); - my $ot = $CF{'TopH'} + $CF{'InteriorMargin'} + int(($CF{'ThumbH'} - $th)/2); - my $tit = ($annot ne "") ? " title=\"$annot\"" : ""; - my $url = ($CF{"ImageSubpages"} ? "?i=$idx" : $orig); - print "$orig\n"; - print "\n"; - print "\n"; - print "
\n"; - print "
\n\n"; + my $click_url; + if ($self->get('WebImageSubpages')) { + $click_url = "?i=$idx"; + } else { + $click_url = $self->get('PhotoUrlPrefix') . "$id.jpg"; + } + $self->show_thumb($meta, $id, $click_url); } - html_bot(); + $self->html_bot(); } -sub Dispatch() { +sub dispatch($) { + my ($self) = @_; UCW::CGI::parse_args(\%args); - UCW::Gallery::LoadConfig(); - $meta = UCW::Gallery::ReadMeta(File::Spec->catfile($CF{'CacheDir'}, 'cache.meta')); - $num_photos = scalar @{$meta->{sequence}}; + $self->{meta} = UCW::Gallery::ReadMeta(File::Spec->catfile($self->get('CacheDir'), 'cache.meta')); + $self->{num_photos} = scalar @{$self->{meta}->{sequence}}; if ($show_img ne "") { - show_img(); + $self->show_img; } else { - show_list(); + $self->show_list; } } +sub attach($$) { + my ($class, $gal) = @_; + my $self = { gal => $gal }; + $gal->def( + WebFE => $self, + WebHeadExtras => "", + WebTopExtras => "", + WebBotExtras => "", + WebThemeCSS => undef, + WebImageSubpages => 1, + ); + bless $self, $class; + return $self; +} + 42; diff --git a/gal2/UCW/Gallery/Web/NrtBlue.pm b/gal2/UCW/Gallery/Web/NrtBlue.pm new file mode 100644 index 0000000..de05937 --- /dev/null +++ b/gal2/UCW/Gallery/Web/NrtBlue.pm @@ -0,0 +1,84 @@ +# NRT Theme for MJ's Photo Gallery +# (c) 2003--2004 Martin Mares ; GPL'ed +# Theme images taken from the cthumb package (c) Carlos Puchol + +package UCW::Gallery::Web::NrtBlue; + +use strict; +use warnings; + +use UCW::Gallery; +use UCW::Gallery::Web; + +our @ISA = qw(UCW::Gallery::Web); + +my $theme_name = "nrt-blue"; +my $navw = 48; +my $navh = 48; +my $thumb_w = 114; +my $thumb_h = 94; +my $interior_margin = 4; +my $left_w = 14; +my $right_w = 18; +my $top_h = 14; +my $bot_h = 18; + +sub theme_dir($) { + my ($self) = @_; + return $self->get('ThemeUrlPrefix') . "/" . $theme_name; +} + +sub theme_head_extras($) { + my ($self) = @_; + my $stylesheet = $self->theme_dir . "/style.css"; + return ""; +} + +sub show_links($$$$) { + my ($self, $prev, $up, $next) = @_; + my $theme = $self->theme_dir; + print "

"; + print ""; + print "Back" if $prev ne ""; + print "\n"; + printf ""; + printf "Forward" if $next ne ""; + print "\n"; + printf "Up" if $up ne ""; +} + +sub show_thumb($) { + my ($self, $meta, $photo_id, $click_url) = @_; + my $theme = $self->theme_dir; + my $m = $meta->{photo}->{$photo_id}; + my $annot = UCW::CGI::html_escape($m->{title}); + my $tf = $self->{thumb_fmt}; + my $tm = $meta->{thumb}->{$tf}->{$photo_id} or die "No thumbnails for format $tf found!\n"; + my $tw = $tm->{w}; + my $th = $tm->{h}; + my $thumb = $self->get('ThumbUrlPrefix') . "$tf/$photo_id.jpg"; + my $side_w = $thumb_w + 2*$interior_margin; + my $side_h = $thumb_h + 2*$interior_margin; + my $box_w = $left_w + $side_w + $right_w; + my $box_h = $top_h + $side_h + $bot_h; + print "

\n"; + print "\n"; + print "\n"; + my $ol = $left_w + $interior_margin + int(($thumb_w - $tw)/2); + my $ot = $top_h + $interior_margin + int(($thumb_h - $th)/2); + my $tit = ($annot ne "") ? " title=\"$annot\"" : ""; + print "Photo\n"; + print "\n"; + print "\n"; + print "
\n"; + print "
\n\n"; +} + +sub attach($$) { + my ($class, $gal) = @_; + my $self = $class->SUPER::attach($gal); + $self->{thumb_fmt} = $gal->require_thumbnails($thumb_w, $thumb_h); + return $self; +} + +1; diff --git a/gal2/gal-show-cf b/gal2/gal-show-cf index c39e98c..1432c2e 100755 --- a/gal2/gal-show-cf +++ b/gal2/gal-show-cf @@ -7,13 +7,13 @@ use warnings; use FindBin; use lib $FindBin::Bin; -use UCW::Gallery qw(%CF); +use UCW::Gallery; use Data::Dumper; -UCW::Gallery::LoadConfig; +my $gal = UCW::Gallery->load_config(); -for my $k (sort keys %CF) { - my $d = Data::Dumper->new([ $CF{$k} ]); +for my $k (sort $gal->get_config_keys) { + my $d = Data::Dumper->new([ $gal->get($k) ]); $d->Terse(1); print "$k=", $d->Dump; } diff --git a/gal2/nrt-blue/theme.conf b/gal2/nrt-blue/theme.conf deleted file mode 100644 index 4242eba..0000000 --- a/gal2/nrt-blue/theme.conf +++ /dev/null @@ -1,40 +0,0 @@ - -PicturesPerRow: 5 -BGColor: #ffffff -TextColor: #000000 -CommentColor: #000000 -FilmBGColor: #322207 -LinkColor: #000000 -ALinkColor: gold -VLinkColor: #000000 -LinkSize: 1 - -# linksize | top -# | v -# ------------------|---------------- --- -# | | | | -# ------------------v---------------- --- -# | | ______________________ | | ^ -# | | | ^ | | | | -# | | | | height | | | -# | | | | | | | -# l | | | | | | | right -# --->|---| |<--|----width------->| |---|<----- -# | | | | | | | -# | | | | | | | -# | | | | | | | -# | | | v | | | -# | | |---------------------| | | | -# | | | | v -# ----------------------------------- --- -# | | | -# ----------------------------------- --- -# ^ -# | bottom -# -# total = left + linksize + width + linksize + right - -# Decorations: top bottom left right -Decorations: 14 18 14 18 -ThumbWidth: 120 -ThumbHeight: 100 diff --git a/gal2/nrt-blue/theme.pm b/gal2/nrt-blue/theme.pm deleted file mode 100644 index f9674ad..0000000 --- a/gal2/nrt-blue/theme.pm +++ /dev/null @@ -1,40 +0,0 @@ -# NRT Theme for MJ's Photo Gallery -# (c) 2003--2004 Martin Mares ; GPL'ed -# Theme images taken from the cthumb package (c) Carlos Puchol - -package UCW::Gallery::Theme; - -use strict; -use warnings; - -use UCW::Gallery; - -sub Init($) { - my ($u) = @_; - UCW::Gallery::SetOptions( - "StyleSheet" => "$u/style.css", - "ThumbW" => 114, - "ThumbH" => 94, - "TopImg" => "$u/top.png", - "TopH" => 14, - "BotImg" => "$u/bot.png", - "BotH" => 18, - "LeftImg" => "$u/left.png", - "LeftW" => 14, - "RightImg" => "$u/right.png", - "RightW" => 18, - "InteriorMargin" => 4, - "ParentImg" => "$u/back.png", - "ParentH" => 48, - "ParentW" => 48, - "BackImg" => "$u/prev.png", - "BackH" => 48, - "BackW" => 48, - "FwdImg" => "$u/next.png", - "FwdH" => 48, - "FwdW" => 48 - ); - UCW::Gallery::RequireThumbnails(114, 94); -} - -1; -- 2.39.2