source: extensions/pLoader/trunk/src/Uploader/GUI/wxApp.pm @ 6088

Last change on this file since 6088 was 6088, checked in by ronosman, 14 years ago

Feature 1609 added : Verify upload access while connecting and before upload.

  • Property svn:eol-style set to LF
File size: 26.4 KB
Line 
1# +-----------------------------------------------------------------------+
2# | pLoader - a Perl photo uploader for Piwigo                            |
3# +-----------------------------------------------------------------------+
4# | Copyright(C) 2008-2010 Piwigo Team                  http://piwigo.org |
5# +-----------------------------------------------------------------------+
6# | This program is free software; you can redistribute it and/or modify  |
7# | it under the terms of the GNU General Public License as published by  |
8# | the Free Software Foundation                                          |
9# |                                                                       |
10# | This program is distributed in the hope that it will be useful, but   |
11# | WITHOUT ANY WARRANTY; without even the implied warranty of            |
12# | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU      |
13# | General Public License for more details.                              |
14# |                                                                       |
15# | You should have received a copy of the GNU General Public License     |
16# | along with this program; if not, write to the Free Software           |
17# | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, |
18# | USA.                                                                  |
19# +-----------------------------------------------------------------------+
20package Uploader::GUI::wxApp;
21use strict;
22use base qw/Wx::App Class::Accessor::Fast/;   
23use Wx qw/
24             wxBITMAP_TYPE_GIF
25             wxBITMAP_TYPE_ICO
26             wxBITMAP_TYPE_BMP
27             wxBITMAP_TYPE_PNG
28             wxBITMAP_TYPE_JPEG
29             wxIMAGE_QUALITY_NORMAL
30             wxIMAGE_QUALITY_HIGH
31             wxSPLASH_CENTRE_ON_SCREEN
32             wxSPLASH_TIMEOUT
33             wxDefaultPosition
34             wxDefaultSize
35             wxSIMPLE_BORDER
36             wxFRAME_TOOL_WINDOW
37             wxFRAME_NO_TASKBAR
38             wxWHITE
39             wxICON_EXCLAMATION
40             wxOK
41             wxLANGUAGE_CHINESE_SIMPLIFIED   
42             wxLANGUAGE_CZECH   
43             wxLANGUAGE_DANISH   
44             wxLANGUAGE_DUTCH   
45             wxLANGUAGE_ENGLISH   
46             wxLANGUAGE_FRENCH   
47             wxLANGUAGE_GERMAN   
48             wxLANGUAGE_HUNGARIAN
49             wxLANGUAGE_ITALIAN   
50             wxLANGUAGE_JAPANESE   
51             wxLANGUAGE_POLISH   
52             wxLANGUAGE_PORTUGUESE   
53             wxLANGUAGE_PORTUGUESE_BRAZILIAN   
54             wxLANGUAGE_RUSSIAN   
55             wxLANGUAGE_SLOVAK   
56             wxLANGUAGE_SPANISH   
57         /;
58use File::Slurp ;
59use Data::Dumper;
60use Storable;
61use File::HomeDir;
62use File::Spec;
63use Uploader::PWG;
64use Uploader::ImageList;
65use Uploader::GUI::wxLoginDlg;
66use Wx::Locale qw/:default/;
67use utf8;
68
69$|=1;
70
71__PACKAGE__->mk_accessors( 
72    qw/
73          pwg
74          site_url
75          site_username
76          site_password
77          http_username
78          http_password
79          rejects
80          imagelist
81          storable_file
82          wx_thumb_dir
83          resized_dir
84          userdata_dir
85          resized_dir
86          thumb_dir
87          conf_file
88          layout_file
89          locale
90          current_language
91          languages
92          available_languages
93          version
94          imagelist_version
95          use_offline
96          login_dlg
97          branding
98          frame
99          imageviewerIndex
100          frameLayout
101          chunk_size
102          layout_clean
103          colors
104          eng_colors
105          positions
106          eng_positions
107          caption_patterns
108          eng_caption_patterns
109          upload_hd
110          eng_upload_hd
111          privacy_level
112          user_defined_properties
113          root_dir
114          bin_dir
115          resource_dir
116          locale_dir
117          argv
118          default_openfile_dir
119      / 
120);
121
122sub FilterEvent {
123    my( $self, $event ) = @_;
124   
125    Wx::LogMessage "EventType %s", $event->GetEventType();
126   
127    return -1;
128}
129
130# not very clean but :
131# at that moment, object is not yet instanciated
132# only way : class variable
133# there is only one instance of wxApp
134my $root_dir;
135my $bin_dir;
136my $resource_dir;
137my $locale_dir;
138my $argv;
139sub new {
140    my ( $self, $params ) = @_;
141
142    $root_dir      = $params->{root_dir}||'.';
143    $bin_dir      = $params->{bin_dir}||'bin';
144    $locale_dir   = $params->{locale_dir}||'locale';
145    $resource_dir = $params->{resource_dir}||'res';
146    $argv         = $params->{argv};
147
148    $self->SUPER::new();
149}
150
151sub OnInit {
152    my( $self ) = @_;
153
154    $self->root_dir(
155        $root_dir
156    );
157
158    $self->bin_dir(
159        $bin_dir
160    );
161
162    $self->locale_dir(
163        $locale_dir
164    );
165
166    $self->resource_dir(
167        $resource_dir
168    );
169
170    $self->version(
171        '1.5'
172    );
173   
174    # to check if we can use stored cache
175    $self->imagelist_version(
176        '16'
177    );
178    $self->argv(
179        $argv
180    );
181
182    $self->languages(
183      [
184             ["中文 (%s)", wxLANGUAGE_CHINESE_SIMPLIFIED, 'Chinese simplified'],   
185             ["Česky (%s)", wxLANGUAGE_CZECH, 'Czech'],   
186             ["Dansk (%s)", wxLANGUAGE_DANISH, 'Danish'],   
187             ["Deutsch (%s)", wxLANGUAGE_GERMAN, 'German'],   
188             ["English (%s)", wxLANGUAGE_ENGLISH, 'English'],   
189             ["Español (%s)", wxLANGUAGE_SPANISH, 'Spanish'],   
190             ["Français (%s)", wxLANGUAGE_FRENCH, 'French'],   
191             ["Italiano (%s)", wxLANGUAGE_ITALIAN, 'Italian'],   
192             ["日本語 (にほんご) (%s)", wxLANGUAGE_JAPANESE, 'Japanese'],   
193             ["Magyar (%s)", wxLANGUAGE_HUNGARIAN, 'Hungarian'],
194             ["Nederlands (%s)", wxLANGUAGE_DUTCH, 'Dutch'],   
195             ["Polski (%s)", wxLANGUAGE_POLISH, 'Polish'],   
196             ["Português Brasileiro (%s)", wxLANGUAGE_PORTUGUESE_BRAZILIAN, 'Portuguese Brazil'],   
197             ["Português Portugal (%s)", wxLANGUAGE_PORTUGUESE, 'Portuguese Portugal'],   
198             ["Русский (%s)", wxLANGUAGE_RUSSIAN, 'Russian'],
199             ["Slovenčina (%s)", wxLANGUAGE_SLOVAK, 'Slovak'],
200      ]
201    );
202    # some languages may be unavailable due to system configuration.
203    $self->_filter_available_languages;
204    Wx::InitAllImageHandlers();
205    my $applicationName = "pLoader" ;
206    $self->SetAppName( $applicationName );
207    $self->SetVendorName( "Piwigo Team" );
208
209    $self->{IMGTYPE} = {
210        'jpg' => wxBITMAP_TYPE_JPEG,
211        'gif' => wxBITMAP_TYPE_GIF,
212        'png' => wxBITMAP_TYPE_PNG,
213    };
214
215    $self->_init_userdir;
216
217    my $conf = retrieve $self->conf_file if -e $self->conf_file;
218
219    if(defined $conf ){
220        $self->SetKeyValues($conf);
221    }
222    else {
223        $self->_readParams( $self->bin_path('pLoader.ini') ) unless defined $conf ;
224    }
225
226    $self->_read_default_user_properties( $self->bin_path('properties.ini') ) ;
227
228    $self->site_url(
229        $self->{site_url}
230    );
231   
232
233    $self->site_username(
234        $self->{site_username}
235    );
236    $self->site_password(
237        $self->{site_password}
238    );
239
240    $self->http_username(
241        $self->{http_username}
242    );
243    $self->http_password(
244        $self->{http_password}
245    );
246
247
248    $self->current_language(
249        $self->{current_language}||Wx::Locale::GetSystemLanguage()
250    );
251
252    $self->chunk_size(
253        $self->{chunk_size}||500_000
254    );
255   
256    $self->init_locale;
257    $self->_init_localized_properties;
258
259    my $not_exit = $self->Login();
260    # user pressed OK
261    if($not_exit){
262        $self->StoreConnectionProperties;
263        if( !$self->use_offline ){
264            while( $not_exit and !$self->_is_connected ){
265                $not_exit = $self->Login();
266                last if $self->use_offline;
267            }
268            $self->CheckUpload;
269        }
270        $self->_init_imagelist;
271        $self->_init_thumbimagelist;
272        $self->_set_stored_layout;
273    }
274
275    $not_exit;
276}
277
278
279# helper method to get the full path for a resource
280sub resource_path{
281    my ( $self, $file ) = @_;
282
283    File::Spec->catfile($self->root_dir, $self->resource_dir, $file);
284}
285
286sub bin_path{
287    my ( $self, $file ) = @_;
288
289    File::Spec->catfile($self->root_dir, $self->bin_dir, $file);
290}
291
292sub locale_path{
293    my ( $self, $file ) = @_;
294
295    File::Spec->catfile($self->root_dir, $self->locale_dir, $file);
296}
297
298sub locale_catalog_path{
299    my ( $self, $file ) = @_;
300
301    File::Spec->catfile($self->root_dir, $self->locale_dir, $self->locale->GetCanonicalName, $file);
302}
303
304sub _set_stored_layout {
305    my ( $self ) = @_;
306
307
308    # when language is switched, need a new layout
309    unlink $self->layout_file if $self->layout_clean;
310    $self->layout_clean(0);
311
312    my $layout = retrieve $self->layout_file if -e $self->layout_file; 
313    if(defined $layout ){
314        $self->SetKeyValues($layout);
315    }
316
317}
318
319# some properties are displayed in choice list, with translated strings
320# the translation has to be reverted to store a language independant value
321sub _init_localized_properties {
322    my ( $self ) = @_;
323
324    $self->colors(
325        ['Black', 'White']
326    );
327    # We need to translate back to english when we store properties
328    $self->eng_colors(
329        {
330            map { gettext($_) => $_ } @{$self->colors} 
331        }
332    );
333    $self->positions(
334        [
335            'Top',
336            'Left',
337            'Right',
338            'Bottom',
339            'Top left',
340            'Top right',
341            'Bottom left',
342            'Bottom right',
343            'Center',
344        ]
345    );
346
347    $self->eng_positions(
348        { 
349             map { gettext($_) => $_ } @{$self->positions} 
350        }
351    );
352       
353        $self->upload_hd(
354            [
355                    'No',
356                        'Yes, use HD resized of the original photo',
357                        'Yes, use a copy of the original photo',
358                ]
359        );
360
361    $self->eng_upload_hd(
362        {
363             map { gettext($_) => $_ } @{$self->upload_hd} 
364        }
365    );
366       
367    $self->caption_patterns(
368        [
369             'None',
370             'File name',
371             'File path and name',
372             'Caption',
373             'Caption + rank number',
374             'Rank number + caption',
375             'Caption + create date chrono',
376             'Create date chrono + caption',
377             'Create date chrono + rank',
378             'Rank + create date chrono',
379        ]
380    );
381    $self->eng_caption_patterns(
382        {
383             map { gettext($_) => $_ } @{$self->caption_patterns} 
384        }
385    );
386       
387        # hard coded because the piwigo api to read custom privacy level is not yet available
388        $self->privacy_level(
389            [
390                    'everybody',
391                        'contacts',
392                        'friends',
393                        'family',
394                        'admins'
395                ]
396        );
397
398       
399}
400
401# display privacy level list in a pyramid way :
402# ['everybody', 'contacts', friends, family, admins] -> [everybody, 'contacts, friends, family, admins', 'friends, family, admins', 'family, admins', 'admins only' ]
403sub privacy_level_choices{
404    my ( $self ) = @_;
405
406    my $pl = $self->privacy_level;     
407    my $n = scalar @$pl - 1;
408    my $list = [ gettext($pl->[0]) ];
409    my $i=0;
410    while(++$i<$n){   
411        push @$list, join(
412            ', ', 
413            map{ gettext($_) }
414            @$pl[$i..$n] 
415        );
416    }
417    push @$list, gettext($pl->[$n]);
418   
419    $list;
420}
421
422sub _filter_available_languages {
423    my ( $self ) = @_;
424
425    # check if the locale can be set and the translation catalog available
426    $self->available_languages(
427        [
428            grep {$_} 
429            map{
430                #  a locale may be unavailable due to system limitations ( ex: chinese, japanese when language pack are not installed )
431                if(Wx::Locale::IsAvailable($_->[1])){
432                    my $locale = Wx::Locale->new($_->[1]);
433                    $locale->AddCatalogLookupPathPrefix(
434                        File::Spec->catfile($self->root_dir, $self->locale_dir)
435                    );
436                    $_ if $locale->AddCatalog('pLoader');
437                }
438            }
439            @{$self->languages}
440        ]
441    );
442}
443
444sub _is_connected {
445    my ( $self ) = @_;
446
447    my $is_connected;
448
449    if($self->pwg->login_result->{stat} eq 'ok'){
450        $is_connected = 1;
451    }
452    else{
453        Wx::MessageBox( 
454            sprintf(
455                "%s\n\n%s %s %s",
456                $self->pwg->login_result->{message},
457                gettext("Connection to"),
458                $self->site_url,
459                gettext("failed"),
460            ),
461            gettext("Piwigo login error"),
462            wxOK | wxICON_EXCLAMATION, 
463        );
464    }
465   
466    $is_connected;
467}
468
469
470my $locale;
471sub init_locale {
472    my ( $self, $language ) = @_;
473   
474    $self->current_language(
475        $language
476    ) if defined $language;
477
478    undef $locale;
479    $locale = Wx::Locale->new(
480        $self->current_language
481    );
482    $locale->AddCatalogLookupPathPrefix(
483        File::Spec->catfile($self->root_dir, $self->locale_dir)
484    );
485    if(!$locale->AddCatalog( 'pLoader.mo' )){
486        Wx::LogMessage gettext("Cannot find translation catalog files for %s. Use default language"), $locale->GetCanonicalName();
487    }
488    $self->locale($locale);   
489}
490
491sub StoreConnectionProperties {
492    my ( $self ) = @_;
493
494    eval {   
495        store( 
496            {
497                map{
498                   $_ => $self->{$_},
499                }
500                qw/
501                    site_url
502                    site_username
503                    site_password
504                    http_username
505                    http_password
506                    current_language
507                    chunk_size
508                    layout_clean
509                    default_openfile_dir
510                /
511            },
512            $self->conf_file
513        );
514    };
515}
516
517sub StoreLayoutProperties {
518    my ( $self ) = @_;
519
520    eval {   
521        store( 
522            {
523                map{
524                   $_ => $self->{$_},
525                }
526                qw/
527                      imageviewerIndex
528                      frameLayout
529                /
530            },
531            $self->layout_file
532        );
533    };
534}
535
536sub _init_imagelist {
537    my ( $self ) = @_;   
538
539    my $stored_imagelist;
540   
541    my $use_new_imagelist;
542   
543    if( -e $self->storable_file ){
544        eval {
545            $stored_imagelist = retrieve $self->storable_file;
546        };
547        if($@){
548            Wx::LogMessage(
549                gettext("An error has occured. Can not read %s\n%s"),
550                $self->storable_file, 
551                $@
552            );
553            $use_new_imagelist = 1 ;
554        }
555        # should have a valid imagelist
556        else{
557            $use_new_imagelist = 1 unless $self->imagelist_version eq $stored_imagelist->{imagelist_version};
558        }
559        # make sure we really have
560        $use_new_imagelist = 1 unless defined $stored_imagelist;
561
562        if($use_new_imagelist){
563            Wx::LogMessage(gettext("pLoader has to reset image cache."));
564            # and silently clean layout
565            $self->layout_clean(1);
566        }
567    }
568    else{
569        $use_new_imagelist = 1 ;
570    }
571
572    if($use_new_imagelist){
573        $stored_imagelist = $self->_default_imagelist_params ;
574    }
575
576
577    $self->imagelist(
578        Uploader::ImageList->new(
579            $stored_imagelist
580        )
581    );
582
583    $self->imagelist->SetCurrentImage(-1);
584
585    $self->imagelist->ResizeCallback(
586        sub { $self->ResizeImage(@_) }
587    );
588
589    $self->imagelist->YieldCallback(
590        sub { Wx::Yield }
591    );
592}
593
594
595sub _default_imagelist_params {
596    my ( $self ) = @_ ;
597
598    my $params = {
599        new_files        => [],
600        hd_filter        => $self->user_defined_properties->{hd_filter}||'Lanczos',
601        hd_blur          => $self->user_defined_properties->{hd_blur}||0.9,
602        hd_quality       => $self->user_defined_properties->{hd_quality}||95,
603        hd_w             => $self->user_defined_properties->{hd_w}||1600,
604        hd_h             => $self->user_defined_properties->{hd_h}||1200,
605        hd_interlace     => $self->user_defined_properties->{hd_interlace}||'Line',
606        thumb_size       => $self->user_defined_properties->{thumbnail_size}||120,
607        site_thumb_dir   => $self->thumb_dir,
608        wx_thumb_size    => $self->user_defined_properties->{wx_thumbnail_size}||100,
609        wx_thumb_dir     => $self->wx_thumb_dir,
610        resize_w         => $self->user_defined_properties->{resize_w}||800,
611        resize_h         => $self->user_defined_properties->{resize_h}||600,
612        site_resized_dir => $self->resized_dir,
613        type             => 'jpg',
614        filter           => $self->user_defined_properties->{resize_filter}||'Lanczos',
615        blur             => $self->user_defined_properties->{resize_blur}||0.9,
616        quality          => $self->user_defined_properties->{resize_quality}||95,
617        wx_quality       => $self->user_defined_properties->{wx_thumbnail_quality}||90,
618        th_quality       => $self->user_defined_properties->{thumbnail_quality}||90,
619        auto_rotate      => $self->user_defined_properties->{auto_rotate}||1,
620        upload_hd        => $self->user_defined_properties->{upload_hd}||'No',
621        remove_uploaded_from_selection => $self->user_defined_properties->{remove_uploaded_from_selection}||1,
622        interlace        => $self->user_defined_properties->{resize_interlace}||'Line',
623        create_resized   => $self->user_defined_properties->{create_resized}||1,
624        prefix           => 'TN',
625        count            => 0,
626        storable_file    => $self->storable_file,
627        userdata_dir     => $self->userdata_dir,
628        default_caption_pattern => $self->user_defined_properties->{default_caption_pattern}||'File name',
629        default_caption  => $self->user_defined_properties->{default_caption}||gettext('Photo '),
630        upload_rejects   =>  [],
631        image_sums       => {},
632        sums             => [],
633        version          => $self->version,
634        imagelist_version => $self->imagelist_version,
635        ResizeCallback   => sub { $self->ResizeImage(@_) },
636        watermark_text => $self->user_defined_properties->{watermark_text}||gettext("my watermark"),
637        watermark_text_size => $self->user_defined_properties->{watermark_text_size}||12,
638        watermark_position => $self->user_defined_properties->{watermark_position}||'Center',
639        watermark_y => $self->user_defined_properties->{watermark_y}||10,
640        watermark_x => $self->user_defined_properties->{watermark_x}||10,
641        watermark_color => $self->user_defined_properties->{watermark_color}||'White',
642        reupload_action_files => 1,
643        reupload_action_properties => 2,
644        reupload_action_properties_m => 1,
645        display_mode => $self->user_defined_properties->{display_mode}||'Thumbnail and caption',
646    };
647
648    return $params;
649}
650
651sub Login {
652    my ( $self ) = @_;   
653
654    $self->login_dlg( 
655        Uploader::GUI::wxLoginDlg->new(
656            {
657                title         => gettext("Piwigo login"),
658                site_url      => sub { $self->site_url(@_) },
659                site_username => sub { $self->site_username(@_) },   
660                site_password => sub { $self->site_password(@_) },
661                use_offline   => sub { $self->use_offline(@_) },   
662            }
663        )
664    ) unless $self->login_dlg;
665
666    my $icon = Wx::Icon->new();
667    $icon->LoadFile(
668        $self->resource_path('favicon.ico'), 
669        wxBITMAP_TYPE_ICO
670    );
671
672    $self->login_dlg->SetIcon($icon);
673
674   
675    my $rval = $self->login_dlg->ShowModal();
676    $self->login_dlg->Show(0);
677
678    $self->_init_branding;
679   
680    if ($self->site_url !~ /^http:/){
681        $self->site_url(
682            sprintf(
683                "http://%s",
684                $self->site_url
685            )
686        );   
687    }
688
689    $self->pwg(
690        # get these parameters from dialog or from file
691        Uploader::PWG->new(
692            {
693                site_url       => $self->site_url,
694                site_username  => $self->site_username,
695                site_password  => $self->site_password,
696                http_username  => $self->http_username,
697                http_password  => $self->http_password,
698                branding       => $self->branding,
699                chunk_size     => sub { $self->chunk_size(@_) },
700                use_offline    => $self->use_offline,
701                version        => $self->version,
702            }
703        )
704    );
705
706    $rval;
707}
708
709sub _init_userdir {
710    my ( $self ) = @_;
711   
712    my $applicationName = $self->GetAppName ;
713    my $userdatadir = File::Spec->canonpath(
714        File::Spec->catfile(
715            File::HomeDir->my_data(), 
716            "\.$applicationName"
717        )
718    );
719
720    if(! -d $userdatadir){
721        if(! mkdir $userdatadir){
722            Wx::MessageBox( 
723                sprintf(
724                    "%s directory creation failed",
725                    $userdatadir,
726                ),
727                "pLoader working directory creation error",
728                wxOK | wxICON_EXCLAMATION, 
729            );
730
731            $userdatadir = File::Spec->canonpath(
732                File::Spec->catfile(
733                    File::Spec->tmpdir(), 
734                    "\.$applicationName"
735                )
736            );
737            mkdir $userdatadir;
738        }   
739    }
740
741    $self->userdata_dir($userdatadir);
742   
743    $self->conf_file(
744        File::Spec->catfile(
745            $self->userdata_dir, 
746            ".$applicationName.conf"
747        )
748    );
749
750    $self->layout_file(
751        File::Spec->catfile(
752            $self->userdata_dir, 
753            ".$applicationName.layout"
754        )
755    );
756
757    $self->storable_file(
758        File::Spec->catfile($self->userdata_dir, 'pLoader.dat')
759    );
760   
761    my $thumbdir = File::Spec->catfile($self->userdata_dir, 'thumbnails');
762    mkdir $thumbdir unless -d $thumbdir ;
763    $self->thumb_dir($thumbdir);
764
765    my $wxthumbdir = File::Spec->catfile($self->userdata_dir, 'wxthumbnails');
766    mkdir $wxthumbdir unless -d $wxthumbdir ;
767    $self->wx_thumb_dir($wxthumbdir);
768
769
770    my $resizedir = File::Spec->catfile($self->userdata_dir, 'resize');
771    mkdir $resizedir unless -d $resizedir ;
772    $self->resized_dir($resizedir);
773}
774
775sub _init_thumbimagelist {
776    my ( $self ) = @_;
777
778   
779    $self->imagelist->wx_thumb_imglist(
780        Wx::ImageList->new( 
781            $self->imagelist->wx_thumb_size, 
782            $self->imagelist->wx_thumb_size, 
783            1,
784            0
785        )
786    );
787   
788    # reload images
789    $self->_reload_thumb_images;
790}
791
792
793sub _reload_thumb_images {
794    my ( $self ) = @_;
795   
796    my $wximagelist = $self->imagelist->wx_thumb_imglist;
797    my $sums = $self->imagelist->sums;
798
799    map {
800        my $image = $self->imagelist->image_sums->{$_};
801
802        $wximagelist->Add(
803            Wx::Bitmap->new( 
804                $image->wx_thumb_file, 
805                $self->GetWxBitmapType($self->imagelist->type), 
806            )
807        );
808    }
809    @$sums ;
810
811}
812
813sub GetWxBitmapType {
814    my ( $self, $type ) = @_;
815   
816    $self->{IMGTYPE}->{$type};
817}
818
819
820
821sub ResizeImage {
822    my ( $self, $image_file, $image_file_out, $type, $width, $height, $quality ) = @_;
823   
824
825    my $image = Wx::Image->new(
826            $image_file, 
827            $self->GetWxBitmapType($type),
828            0
829    );
830
831    my $w;
832    my $h;
833
834    my $img_w = $image->GetWidth;
835    my $img_h = $image->GetHeight;
836   
837        # portrait
838        if( $img_w < $img_h ){
839            $w = $height;
840        }
841        else{
842            $w = $width;
843        }
844        # to respect aspect ratio
845        $h = sprintf(
846            "%.0f",
847            ($w*$img_h)/$img_w
848        );
849
850   
851
852    $image->Rescale(
853        $w,
854        $h,
855        wxIMAGE_QUALITY_HIGH
856    );
857
858    #$image->Resize(
859    #    [ $width, $height ], [ 0, 20],
860    #);
861   
862    $quality ||= 90;
863   
864    $image->SetOption( 
865        "quality", 
866        $quality 
867    );
868   
869    if(!$image->SaveFile(
870        $image_file_out,
871        $self->GetWxBitmapType($type),
872    )){
873        Wx::LogMessage(
874            gettext("An error has occured. Can not save file %s"),
875            $image_file_out,
876        )
877    };
878}
879
880# some labels differ with branding ( piwigo.com or piwigo.org )
881sub _init_branding {
882    my ( $self ) =@_;
883   
884    if( $self->site_url =~ /\.piwigo\.com/ ){
885        $self->branding(
886            {
887                category  => gettext("album"),
888                Category  => gettext("Album"),
889                categories => gettext("albums"),
890                Categories => gettext("Albums"),
891                'Add new category' => gettext("Add new album"),
892                'Category name' => gettext("Album name :"),
893                'New category' => gettext("New album"),
894                'What is the destination category?' => gettext("What is the destination album?")
895            }
896        );
897    }
898    else{
899        $self->branding(
900            {
901                category  => gettext("categorie"),     
902                Category  => gettext("Categorie"),     
903                categories => gettext("categories"),   
904                Categories => gettext("Categories"),   
905                'Add new category' => gettext("Add new category"),
906                'Category name' => gettext("Category name :"),
907                'New category' => gettext("New category"),
908                'What is the destination category?' => gettext("What is the destination category?")
909            }
910        );
911    }   
912}
913
914
915sub SetFrame {
916    my ( $self, $frame ) = @_; 
917
918    my $url = $self->site_url;
919   
920    if($self->use_offline){
921        $url = gettext("Work Offline");
922    }
923
924    $self->frame($frame);
925
926    my $icon = Wx::Icon->new();
927    $icon->LoadFile(
928        File::Spec->catfile(
929            $self->root_dir, $self->resource_dir, 'favicon.ico'
930        ), 
931        wxBITMAP_TYPE_ICO
932    );
933    $self->frame->SetIcon($icon);
934}
935
936sub _readParams {
937        my( $self, $file ) = @_ ;
938
939
940    my $expr_params ;
941    eval { $expr_params = read_file( $file ); } ;
942   
943    my $paramValues = [] ;
944    if($expr_params){
945        my $expr = '$paramValues = ' ;
946        $expr .=  "$expr_params ; " ;
947        eval $expr ;
948    }
949   
950    return unless 'ARRAY' eq ref $paramValues ;
951   
952    if(scalar(@$paramValues )){
953        my $params = $paramValues->[0] ;
954        $self->SetKeyValues($params);
955    }
956}
957
958sub _read_default_user_properties {
959    my( $self, $file ) = @_ ;
960
961
962    my $expr_params ;
963    eval { $expr_params = read_file( $file ); } ;
964    my $properties;
965    if($expr_params){
966        my $expr = '$properties = ' ;
967        $expr .=  " { $expr_params }; " ;
968        eval $expr ;
969    }
970    $properties = {} unless 'HASH' eq ref $properties;
971    $self->user_defined_properties($properties);
972
973}
974
975
976
977sub SetKeyValues {
978    my ( $self, $params )= @_;   
979
980    foreach( keys %$params ) {
981        $self->{$_} = $params->{$_} ;
982    }
983}
984
985
986sub CheckUpload {
987    my ( $self ) = @_;
988
989    my $err_msg = $self->pwg->CheckUpload;
990
991    $err_msg = gettext("Your user account is not granted to upload photos") if
992        'Access denied' eq $err_msg;
993
994    Wx::LogMessage("%s", $err_msg) if $err_msg;
995
996    $err_msg;
997}
9981;
Note: See TracBrowser for help on using the repository browser.