]> mj.ucw.cz Git - gallery.git/blob - gal2/UCW/Gallery.pm
Gallery2: More renames
[gallery.git] / gal2 / UCW / Gallery.pm
1 # Simple Photo Gallery
2 # (c) 2003--2012 Martin Mares <mj@ucw.cz>
3
4 package UCW::Gallery;
5
6 use strict;
7 use warnings;
8
9 use Storable;
10
11 ### Class methods ###
12
13 sub new($) {
14         my ($class) = @_;
15         my $self = { };
16         $self->{cfg} = {
17                 # Directories
18                 OrigDir => '.',                 # Original images
19                 PhotoDir => 'photo',            # Scaled-down photos for web
20                 CacheDir => 'cache',            # Cache with meta-data and thumbnails
21
22                 # URL prefixes
23                 PhotoUrlPrefix => 'photo/',
24                 ThumbUrlPrefix => 'thumb/',
25                 ThemeUrlPrefix => 'gal/',
26
27                 # Processing machinery settings
28                 ScanDefaultTransform => 's',
29                 PhotoMaxWidth => 1024,
30                 PhotoMaxHeight => 1024,
31                 ThumbFormats => {},             # Set up by themes
32
33                 # Titles and navigation
34                 Title => 'An Unnamed Gallery',
35                 SubTitle => "",
36                 ParentURL => '../',
37                 BackURL => "",
38                 FwdURL => "",
39         };
40         return bless $self, $class;
41 }
42
43 sub load_config($) {
44         my $cfg = "./gallery.cf";
45         my $self = do $cfg;
46         unless (defined $self) {
47                 if ($@) {
48                         die "Error parsing $cfg: $@";
49                 } elsif ($!) {
50                         die "Cannot load $cfg: $!\n";
51                 } else {
52                         die "Cannot load $cfg, check that it returns true\n";
53                 }
54         }
55         return $self;
56 }
57
58 ### Object methods ###
59
60 sub get($$) {
61         my ($self, $key) = @_;
62         if (exists $self->{cfg}->{$key}) {
63                 my $val = $self->{cfg}->{$key};
64                 defined $val or warn "Gallery: Config item $key is not set\n";
65                 return $val;
66         } else {
67                 warn "Gallery: Config item $key does not exist\n";
68                 return;
69         }
70 }
71
72 sub def($@) {
73         my $self = shift;
74         while (my $key = shift @_) {
75                 my $val = shift @_;
76                 !exists $self->{cfg}->{$key} or warn "Gallery: Re-definining config item $key\n";
77                 $self->{cfg}->{$key} = $val;
78         }
79 }
80
81 sub set($@) {
82         my $self = shift;
83         while (my $key = shift @_) {
84                 my $val = shift @_;
85                 exists $self->{cfg}->{$key} or warn "Gallery: Config item $key does not exist\n";
86                 $self->{cfg}->{$key} = $val;
87         }
88 }
89
90 sub get_config_keys($) {
91         my ($self) = @_;
92         return keys %{$self->{cfg}};
93 }
94
95 sub require_thumbnails($$$) {
96         my ($self, $w, $h) = @_;
97         my $fmt = "${w}x${h}";
98         $self->{cfg}->{ThumbFormats}->{$fmt} = 1;
99         return $fmt;
100 }
101
102 ### Subroutines (to be converted to methods later) ###
103
104 sub WriteList($$) {
105         my ($file, $images) = @_;
106         open LIST, '>', "$file.new" or die "Cannot create $file.new: $!\n";
107         for my $i (@$images) {
108                 print LIST join("\t",
109                         $i->{file},
110                         $i->{id},
111                         $i->{orientation},
112                         $i->{xfrm},
113                         ($i->{title} eq '' ? '-' : $i->{title}),
114                 ), "\n";
115         }
116         close LIST;
117         rename "$file.new", $file or die "Cannot rename $file.new to $file: $!\n";
118 }
119
120 sub ReadList($) {
121         my ($file) = @_;
122         my @images = ();
123         open LIST, '<', $file or return;
124         while (<LIST>) {
125                 chomp;
126                 /^$/ and next;
127                 /^#/ and next;
128                 my $i = {};
129                 ($i->{file}, $i->{id}, $i->{orientation}, $i->{xfrm}, $i->{title}) = split /\t/;
130                 if ($i->{title} eq '-') { $i->{title} = ""; }
131                 push @images, $i;
132         }
133         close LIST;
134         return \@images;
135 }
136
137 sub WriteMeta($$) {
138         my ($file, $meta) = @_;
139         open META, '>', "$file.new" or die "Cannot create $file.new: $!\n";
140         Storable::nstore_fd($meta, \*META);
141         close META;
142         rename "$file.new", $file or die "Cannot rename $file.new to $file: $!\n";
143 }
144
145 sub ReadMeta($) {
146         my ($file) = @_;
147         # FIXME: open my META
148         open META, '<', $file or die "Cannot read $file: $!\n";
149         my $meta = Storable::fd_retrieve(\*META) or die "Cannot parse $file\n";
150         close META;
151         return $meta;
152 }
153
154 1;