]> mj.ucw.cz Git - gallery.git/blob - gal2/UCW/Gallery.pm
f13889d2d032f936dde83e04ac89ca51a8a54b1f
[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 BEGIN {
12         # Standard Perl module stuff
13         use Exporter();
14         our ($VERSION, @ISA, @EXPORT, @EXPORT_OK, %EXPORT_TAGS);
15         $VERSION = 1.00;
16         @ISA = qw(Exporter);
17         @EXPORT = qw(&SetOptions);
18         %EXPORT_TAGS = ();
19         @EXPORT_OK = qw(%CF);
20 }
21
22 our %CF;
23 our $th;
24
25 BEGIN {
26         $CF{'Title'} = 'An Unnamed Gallery',
27         $CF{'HeadExtras'} = "",
28         $CF{'TopExtras'} = "",
29         $CF{'BotExtras'} = "",
30         $CF{'ParentURL'} = '../',
31         $CF{'BackURL'} = "",
32         $CF{'FwdURL'} = "",
33         $CF{'ImageSubpages'} = 1,
34         $CF{'AllowArchives'} = 1,
35         $CF{'PhotoUrlPrefix'} = "",
36         $CF{'ThumbUrlPrefix'} = "",
37         $CF{'MetaDataDir'} = '.',
38         $CF{'PhotoDir'} = '.',
39
40         $CF{'ScanDefaultTransform'} = 'n';
41         $CF{'OrigDir'} = '.';
42         $CF{'PhotoDir'} = 'photo';
43         $CF{'CacheDir'} = "cache",
44         $CF{'PhotoMaxWidth'} = 1024,
45         $CF{'PhotoMaxHeight'} = 1024,
46 }
47
48 sub LoadConfig() {
49         my $cfg = "./gallery.cf";
50         unless (defined do $cfg) {
51                 if ($@) {
52                         die "Error parsing $cfg: $@";
53                 } elsif ($!) {
54                         die "Cannot load $cfg: $!\n";
55                 } else {
56                         die "Cannot load $cfg, check that it returns true\n";
57                 }
58         }
59 }
60
61 sub SetOptions(@) {
62         while (my $o = shift @_) {
63                 my $v = shift @_;
64                 $CF{$o} = $v;
65                 if ($o eq "Theme") {
66                         require $CF{"GalDir"} . "/$v/theme.pm";
67                         Gallery::Theme::Init($CF{"GalURL"} . "/$v");
68                 }
69         }
70 }
71
72 sub WriteList($$) {
73         my ($file, $images) = @_;
74         open LIST, '>', "$file.new" or die "Cannot create $file.new: $!\n";
75         print LIST "# Image\tID\tRotate\tXform\tTitle\n";
76         for my $i (@$images) {
77                 print LIST join("\t",
78                         $i->{file},
79                         $i->{id},
80                         $i->{orientation},
81                         $i->{xfrm},
82                         ($i->{title} eq '' ? '-' : $i->{title}),
83                 ), "\n";
84         }
85         close LIST;
86         rename "$file.new", $file or die "Cannot rename $file.new to $file: $!\n";
87 }
88
89 sub ReadList($) {
90         my ($file) = @_;
91         my @images = ();
92         open LIST, '<', $file or return;
93         while (<LIST>) {
94                 chomp;
95                 /^$/ and next;
96                 /^#/ and next;
97                 my $i = {};
98                 ($i->{file}, $i->{id}, $i->{orientation}, $i->{xfrm}, $i->{title}) = split /\t/;
99                 if ($i->{title} eq '-') { $i->{title} = ""; }
100                 push @images, $i;
101         }
102         close LIST;
103         return \@images;
104 }
105
106 sub WriteMeta($$) {
107         my ($file, $meta) = @_;
108         open META, '>', "$file.new" or die "Cannot create $file.new: $!\n";
109         Storable::nstore_fd($meta, \*META);
110         close META;
111         rename "$file.new", $file or die "Cannot rename $file.new to $file: $!\n";
112 }
113
114 sub ReadMeta($) {
115         my ($file) = @_;
116         open META, '<', $file or die "Cannot read $file: $!\n";
117         my $meta = Storable::fd_retrieve(\*META) or die "Cannot parse $file\n";
118         close META;
119         return $meta;
120 }
121
122 1;