]> mj.ucw.cz Git - gallery.git/blobdiff - gal2/UCW/Gallery.pm
Gallery2: More renames
[gallery.git] / gal2 / UCW / Gallery.pm
index b2d41a6ed84851f632e996a1323a43c1ea578f11..8b4c3fac2c3686222852e1dfb2b9c2f8357944df 100644 (file)
@@ -6,44 +6,44 @@ package UCW::Gallery;
 use strict;
 use warnings;
 
 use strict;
 use warnings;
 
-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);
-}
+use Storable;
+
+### 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
 
 
-our %CF;
-our $th;
-
-BEGIN {
-       $CF{'Title'} = 'An Unnamed Gallery',
-       $CF{'HeadExtras'} = "",
-       $CF{'TopExtras'} = "",
-       $CF{'BotExtras'} = "",
-       $CF{'ParentURL'} = '../',
-       $CF{'BackURL'} = "",
-       $CF{'FwdURL'} = "",
-       $CF{'ImageSubpages'} = 1,
-       $CF{'AllowArchives'} = 1,
-       $CF{'PhotoUrlPrefix'} = "",
-       $CF{'ThumbUrlPrefix'} = "",
-       $CF{'MetaDataDir'} = '.',
-       $CF{'PhotoDir'} = '.',
-
-       $CF{'ScanDefaultTransform'} = 'n';
-       $CF{'OrigDir'} = '.';
-       $CF{'PhotoDir'} = 'photo';
-       $CF{'CacheDir'} = "cache",
+               # 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;
 }
 
 }
 
-sub LoadConfig() {
+sub load_config($) {
        my $cfg = "./gallery.cf";
        my $cfg = "./gallery.cf";
-       unless (defined do $cfg) {
+       my $self = do $cfg;
+       unless (defined $self) {
                if ($@) {
                        die "Error parsing $cfg: $@";
                } elsif ($!) {
                if ($@) {
                        die "Error parsing $cfg: $@";
                } elsif ($!) {
@@ -52,23 +52,58 @@ sub LoadConfig() {
                        die "Cannot load $cfg, check that it returns true\n";
                }
        }
                        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{"GalDir"} . "/$v/theme.pm";
-                       Gallery::Theme::Init($CF{"GalURL"} . "/$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 get_config_keys($) {
+       my ($self) = @_;
+       return keys %{$self->{cfg}};
+}
+
+sub require_thumbnails($$$) {
+       my ($self, $w, $h) = @_;
+       my $fmt = "${w}x${h}";
+       $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";
 sub WriteList($$) {
        my ($file, $images) = @_;
        open LIST, '>', "$file.new" or die "Cannot create $file.new: $!\n";
-       print LIST "# Image\tID\tRotate\tXform\tTitle\n";
        for my $i (@$images) {
                print LIST join("\t",
                        $i->{file},
        for my $i (@$images) {
                print LIST join("\t",
                        $i->{file},
@@ -99,4 +134,21 @@ sub ReadList($) {
        return \@images;
 }
 
        return \@images;
 }
 
+sub WriteMeta($$) {
+       my ($file, $meta) = @_;
+       open META, '>', "$file.new" or die "Cannot create $file.new: $!\n";
+       Storable::nstore_fd($meta, \*META);
+       close META;
+       rename "$file.new", $file or die "Cannot rename $file.new to $file: $!\n";
+}
+
+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;
+       return $meta;
+}
+
 1;
 1;