summaryrefslogtreecommitdiff
path: root/lib/gcstar/GCExport/GCExportExternal.pm
blob: 22633870dbf7cb61fe05a238ce11b0199e4a42ac (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
package GCExport::GCExportExternal;

###################################################
#
#  Copyright 2005-2010 Christian Jodar
#
#  This file is part of GCstar.
#
#  GCstar is free software; you can redistribute it and/or modify
#  it under the terms of the GNU General Public License as published by
#  the Free Software Foundation; either version 2 of the License, or
#  (at your option) any later version.
#
#  GCstar is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#  GNU General Public License for more details.
#
#  You should have received a copy of the GNU General Public License
#  along with GCstar; if not, write to the Free Software
#  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
#
###################################################

use strict;

use GCExport::GCExportBase;

{
    package GCExport::GCExporterExternal;

    use File::Copy;
    use File::Basename;
    use Cwd;
    use XML::Simple;
    use GCUtils 'glob';
    use GCBackend::GCBackendXmlParser;
    use base qw(GCExport::GCExportBaseClass);

    sub new
    {
        my ($proto, $parent) = @_;
        my $class = ref($proto) || $proto;
        my $self  = $class->SUPER::new($parent);
        bless ($self, $class);

        $self->{useZip} = $self->checkOptionalModule('Archive::Zip');
        $self->{covers} = {};

        return $self;
    }
    
    sub wantsOsSeparator
    {
        return 0;
    }    

    sub transformPicturePath
    {
        my ($self, $path, $file, $item, $field) = @_;
        my $newPath = $self->duplicatePicture($path,
                                       $field,
                                       $self->{imageDir},
                                       $item->{$self->{model}->{commonFields}->{title}});
        $self->{isCover}->{$newPath} = 1
            if $self->{model}->{commonFields}->{cover} eq $field;
        return $newPath;
    }

    sub process
    {
        my ($self, $options) = @_;
        $self->{parsingError} = '';
        $self->{options} = $options;
        $self->{options}->{withPictures} = 1;
        #$self->{fileName} = $options->{file};
        my $ext = ($self->{options}->{zip} ? 'gcz' : 'gcs');
        my $outFile = $options->{file};
        $outFile .= ".$ext" if ($outFile !~ m/\.$ext$/);
        #$self->{fileName} .= '.gcs' if ($self->{fileName} !~ m/\.gcs$/);
        $self->{fileName} = $outFile;
        $self->{fileName} =~ s/z$/s/;
        my $listFile = $self->{fileName};
        my $baseDir = dirname($listFile);
        my $baseName = basename($listFile, '.gcs');
        my $imagesSubDir = $baseName.'_pictures';
        $self->{imageDir} = $baseDir.'/'.$imagesSubDir;
        $self->{original} = $options->{collection};
        #$self->{original} =~ s/\\/\//g if ($^O =~ /win32/i);
        $self->{origDir} = dirname($self->{original});

        eval {
            chdir $baseDir;
            die 'Directory not writable' if !-w '.';
            mkdir $self->{imageDir};

            $self->{currentDir} = getcwd;

            my $backend = new GCBackend::GCBeXmlParser($self);
            $backend->setParameters(file => $listFile,
                                    version => $self->{options}->{parent}->{version},
                                    wantRestore => 1,
                                    standAlone => 1);
    
            my $result = $backend->save($options->{items},
                                        $options->{originalList}->getInformation,
                                        undef);

            if ($result->{error})
            {
                die $result->{error}->[1];
            }
        };
        
        if ($@)
        {
            $self->{parsingError} = GCUtils::formatOpenSaveError(
                $self->{options}->{parent}->{lang},
                $self->{fileName},
                ['SaveError', $@]
            );
        }
        
        if ($self->{options}->{zip})
        {
            chdir $baseDir;
            my $zip = Archive::Zip->new();
            $zip->addFile(basename($self->{fileName}));
            $zip->addDirectory(basename($self->{imageDir}));
            my $miniDir = $self->{imageDir}.'-mini';
            mkdir $miniDir;
            $zip->addDirectory(basename($miniDir));
            my @images = glob $imagesSubDir.'/*';
            foreach my $image(@images)
            {
                $zip->addFile($image);
                my $imgFile = basename($image);
                my $fullFileName = $self->{imageDir}."/$imgFile";
                next if !$self->{isCover}->{$image};
                $self->createMiniature($fullFileName, "$miniDir/$imgFile", 150);
                $zip->addFile(basename($miniDir)."/$imgFile");
            }
            my $result = $zip->writeToFileNamed($outFile);
            if ($result)
            {
                $self->{parsingError} = GCUtils::formatOpenSaveError(
                    $self->{options}->{parent}->{lang},
                    $outFile,
                    ['SaveError', $@]
                );
            }
            else
            {
                # Cleanup to remove everything but the .gcz file
                unlink $self->{fileName};
                unlink foreach (@images);
                rmdir $imagesSubDir;
            }
        }
        chdir;
        return $self->getEndInfo;
    }

    sub getOptions
    {
        my $self = shift;
        my @options;

        if ($self->{useZip})
        {
            push @options, {
                name => 'zip',
                type => 'yesno',
                label => 'ZipAll',
                default => '0'
            };
        }

        return \@options;
    }

#    sub getName
#    {
#        my $self = shift;
#        
#        return "External";
#    }
    
    sub getEndInfo
    {
        my $self = shift;
        return ($self->{parsingError}, 'error')
            if $self->{parsingError};
        
        return '';
    }
}