]> mj.ucw.cz Git - gallery.git/commitdiff
Gallery2: Moving to object-oriented interface (not finished yet)
authorMartin Mares <mj@ucw.cz>
Wed, 26 Dec 2012 18:22:42 +0000 (19:22 +0100)
committerMartin Mares <mj@ucw.cz>
Sun, 8 Feb 2015 20:14:09 +0000 (21:14 +0100)
gal2/Makefile
gal2/UCW/Gallery.pm
gal2/UCW/Gallery/Web.pm
gal2/UCW/Gallery/Web/NrtBlue.pm [new file with mode: 0644]
gal2/gal-show-cf
gal2/nrt-blue/theme.conf [deleted file]
gal2/nrt-blue/theme.pm [deleted file]

index 88b61de44c63ac4b48158d8a14c88dde1f277a6d..e19fbcfa6d011d7627559b317044adc0b08be2de 100644 (file)
@@ -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)
index c8bdc68a5fd52f45e46ae682889fc552714084cf..8b4c3fac2c3686222852e1dfb2b9c2f8357944df 100644 (file)
@@ -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;
index d2a718ad372d277a644b8b3cc3fced5fe9aa6ba8..95532d885620138499b87e334c7d71c9aa7ddafb 100644 (file)
@@ -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 "<p style='color:red'>Bad luck, the script is broken. Sorry.\n<p>$_[0]\n";
        print "</body></html>\n";
 }
 
-sub show_links($$$) {
-       my ($prev, $up, $next) = @_;
-       print "<p class=parent>";
-       print "<span class=back style='width: $CF{'BackW'}px; height: $CF{'BackH'}px'>";
-       print "<a href='$prev'><img src='$CF{'BackImg'}' width=$CF{'BackW'} height=$CF{'BackH'} alt='Back'></a>" if $prev ne "";
-       print "</span>\n";
-       print "<span class=fwd style='width: $CF{'FwdW'}px; height: $CF{'FwdH'}px'>";
-       print "<a href='$next'><img src='$CF{'FwdImg'}' width=$CF{'FwdW'} height=$CF{'FwdH'} alt='Forward'></a>" if $next ne "";
-       print "</span>\n";
-       print "<a href='$up'><img src='$CF{'ParentImg'}' width=$CF{'ParentW'} height=$CF{'ParentH'} alt='Up'></a>\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 <<EOF ;
 Content-Type: text/html
 
 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
 <html><head>
-$CF{"HeadExtras"}
-<link rel=stylesheet href="$CF{"StyleSheet"}" type="text/css" media=all>
+$hextras
+$theme_hextras
 <title>$title</title>
 </head><body>
-$CF{"TopExtras"}
+$textras
 EOF
        $UCW::CGI::error_hook = \&error;
 }
 
-sub html_bot() {
-       print "$CF{'BotExtras'}\n</body></html>\n";
+sub html_bot($) {
+       my ($self) = @_;
+       print $self->get('WebBotExtras'), "\n</body></html>\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 "<h1>No such photo</h1>\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 "<h1>$t</h1>\n" if $t ne "";
-       my $img = $CF{'PhotoUrlPrefix'} . $id . ".jpg";
+       my $img = $self->get('PhotoUrlPrefix') . $id . '.jpg';
        print "<p class=large><img src='$img' width=$w height=$h alt='$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 "<h1>$CF{'Title'}</h1>\n";
-       print "<h2>$CF{'SubTitle'}</h2>\n" if defined $CF{'SubTitle'};
+       $self->show_links($self->get('BackURL'), $self->get('ParentURL'), $self->get('FwdURL'));
+       print "<h1>", $self->get('Title'), "</h1>\n";
+       my $subtitle = $self->get('SubTitle');
+       print "<h2>$subtitle</h2>\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 "<div class=thf><div class=thumb>\n";
-               print "<img src='$CF{'TopImg'}' width=$box_w height=$CF{'TopH'} alt='' class=tt>\n";
-               print "<img src='$CF{'LeftImg'}' width=$CF{'LeftW'} height=$side_h alt='' class=tl>\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 "<a href='$url'><img src='$thumb' width=$w height=$h alt='$orig'$tit class=ti style='left: ${ol}px; top: ${ot}px'></a>\n";
-               print "<img src='$CF{'RightImg'}' width=$CF{'RightW'} height=$side_h alt='' class=tr>\n";
-               print "<img src='$CF{'BotImg'}' width=$box_w height=$CF{'BotH'} alt='' class=tb>\n";
-               print "</div>\n";
-               print "</div>\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 (file)
index 0000000..de05937
--- /dev/null
@@ -0,0 +1,84 @@
+# NRT Theme for MJ's Photo Gallery
+# (c) 2003--2004 Martin Mares <mj@ucw.cz>; 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 "<link rel=stylesheet href='$stylesheet' type='text/css' media=all>";
+}
+
+sub show_links($$$$) {
+       my ($self, $prev, $up, $next) = @_;
+       my $theme = $self->theme_dir;
+       print "<p class=parent>";
+       print "<span class=back style='width: ${navw}px; height: ${navh}px'>";
+       print "<a href='$prev'><img src='$theme/prev.png' width=${navw} height=${navh} alt='Back'></a>" if $prev ne "";
+       print "</span>\n";
+       printf "<span class=fwd style='width: ${navw}px; height: ${navh}px'>";
+       printf "<a href='$next'><img src='$theme/next.png' width=${navw} height=${navh} alt='Forward'></a>" if $next ne "";
+       print "</span>\n";
+       printf "<a href='$up'><img src='$theme/back.png' width=${navw} height=${navh} alt='Up'></a>" 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 "<div class=thf><div class=thumb>\n";
+       print "<img src='$theme/top.png' width=$box_w height=$top_h alt='' class=tt>\n";
+       print "<img src='$theme/left.png' width=$left_w height=$side_h alt='' class=tl>\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 "<a href='$click_url'><img src='$thumb' width=$tw height=$th alt='Photo'$tit class=ti style='left: ${ol}px; top: ${ot}px'></a>\n";
+       print "<img src='$theme/right.png' width=$right_w height=$side_h alt='' class=tr>\n";
+       print "<img src='$theme/bot.png' width=$box_w height=$bot_h alt='' class=tb>\n";
+       print "</div>\n";
+       print "</div>\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;
index c39e98c065b02f40ad218ff2c6144c2bfb2f78ad..1432c2e447c77bb5487551b6e919771b09e4bcdb 100755 (executable)
@@ -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 (file)
index 4242eba..0000000
+++ /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 (file)
index f9674ad..0000000
+++ /dev/null
@@ -1,40 +0,0 @@
-# NRT Theme for MJ's Photo Gallery
-# (c) 2003--2004 Martin Mares <mj@ucw.cz>; 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;