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

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

Bug 1646 fixed : use previously chosen image directory when opening file dialog.

  • Property svn:eol-style set to LF
File size: 26.2 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        }
269        $self->_init_imagelist;
270        $self->_init_thumbimagelist;
271        $self->_set_stored_layout;
272        #$self->_init_frame;
273    }
274    #Wx::LogMessage("bin dir %s", $bin_dir);
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        $use_new_imagelist = 1 unless defined $stored_imagelist;
560
561                if($use_new_imagelist){
562            Wx::LogMessage(gettext("pLoader has to reset image cache."));
563            # and silently clean layout
564            $self->layout_clean(1);
565        }
566    }
567    else{
568        $use_new_imagelist = 1 ;
569    }
570
571    if($use_new_imagelist){
572        $stored_imagelist = $self->_default_imagelist_params ;
573    }
574
575
576    $self->imagelist(
577        Uploader::ImageList->new(
578            $stored_imagelist
579        )
580    );
581
582    $self->imagelist->SetCurrentImage(-1);
583
584    $self->imagelist->ResizeCallback(
585        sub { $self->ResizeImage(@_) }
586    );
587
588    $self->imagelist->YieldCallback(
589        sub { Wx::Yield }
590    );
591}
592
593
594sub _default_imagelist_params {
595    my ( $self ) = @_ ;
596
597    my $params = {
598        new_files        => [],
599        hd_filter        => $self->user_defined_properties->{hd_filter}||'Lanczos',
600        hd_blur          => $self->user_defined_properties->{hd_blur}||0.9,
601        hd_quality       => $self->user_defined_properties->{hd_quality}||95,
602        hd_w             => $self->user_defined_properties->{hd_w}||1600,
603        hd_h             => $self->user_defined_properties->{hd_h}||1200,
604        hd_interlace     => $self->user_defined_properties->{hd_interlace}||'Line',
605        thumb_size       => $self->user_defined_properties->{thumbnail_size}||120,
606        site_thumb_dir   => $self->thumb_dir,
607        wx_thumb_size    => $self->user_defined_properties->{wx_thumbnail_size}||100,
608        wx_thumb_dir     => $self->wx_thumb_dir,
609        resize_w         => $self->user_defined_properties->{resize_w}||800,
610        resize_h         => $self->user_defined_properties->{resize_h}||600,
611        site_resized_dir => $self->resized_dir,
612        type             => 'jpg',
613        filter           => $self->user_defined_properties->{resize_filter}||'Lanczos',
614        blur             => $self->user_defined_properties->{resize_blur}||0.9,
615        quality          => $self->user_defined_properties->{resize_quality}||95,
616        wx_quality       => $self->user_defined_properties->{wx_thumbnail_quality}||90,
617        th_quality       => $self->user_defined_properties->{thumbnail_quality}||90,
618        auto_rotate      => $self->user_defined_properties->{auto_rotate}||1,
619        upload_hd        => $self->user_defined_properties->{upload_hd}||'No',
620        remove_uploaded_from_selection => $self->user_defined_properties->{remove_uploaded_from_selection}||1,
621        interlace        => $self->user_defined_properties->{resize_interlace}||'Line',
622        create_resized   => $self->user_defined_properties->{create_resized}||1,
623        prefix           => 'TN',
624        count            => 0,
625        storable_file    => $self->storable_file,
626        userdata_dir     => $self->userdata_dir,
627        default_caption_pattern => $self->user_defined_properties->{default_caption_pattern}||'File name',
628        default_caption  => $self->user_defined_properties->{default_caption}||gettext('Photo '),
629        upload_rejects   =>  [],
630        image_sums       => {},
631        sums             => [],
632        version          => $self->version,
633        imagelist_version => $self->imagelist_version,
634        ResizeCallback   => sub { $self->ResizeImage(@_) },
635        watermark_text => $self->user_defined_properties->{watermark_text}||gettext("my watermark"),
636        watermark_text_size => $self->user_defined_properties->{watermark_text_size}||12,
637        watermark_position => $self->user_defined_properties->{watermark_position}||'Center',
638        watermark_y => $self->user_defined_properties->{watermark_y}||10,
639        watermark_x => $self->user_defined_properties->{watermark_x}||10,
640        watermark_color => $self->user_defined_properties->{watermark_color}||'White',
641        reupload_action_files => 1,
642        reupload_action_properties => 2,
643        reupload_action_properties_m => 1,
644        display_mode => $self->user_defined_properties->{display_mode}||'Thumbnail and caption',
645    };
646
647    return $params;
648}
649
650sub Login {
651    my ( $self ) = @_;   
652
653    $self->login_dlg( 
654        Uploader::GUI::wxLoginDlg->new(
655            {
656                title         => gettext("Piwigo login"),
657                site_url      => sub { $self->site_url(@_) },
658                site_username => sub { $self->site_username(@_) },   
659                site_password => sub { $self->site_password(@_) },
660                use_offline   => sub { $self->use_offline(@_) },   
661            }
662        )
663    ) unless $self->login_dlg;
664
665    my $icon = Wx::Icon->new();
666    $icon->LoadFile(
667        $self->resource_path('favicon.ico'), 
668        wxBITMAP_TYPE_ICO
669    );
670
671    $self->login_dlg->SetIcon($icon);
672
673   
674    my $rval = $self->login_dlg->ShowModal();
675    $self->login_dlg->Show(0);
676
677    $self->_init_branding;
678   
679    if ($self->site_url !~ /^http:/){
680        $self->site_url(
681            sprintf(
682                "http://%s",
683                $self->site_url
684            )
685        );   
686    }
687
688    $self->pwg(
689        # get these parameters from dialog or from file
690        Uploader::PWG->new(
691            {
692                site_url       => $self->site_url,
693                site_username  => $self->site_username,
694                site_password  => $self->site_password,
695                http_username  => $self->http_username,
696                http_password  => $self->http_password,
697                branding       => $self->branding,
698                chunk_size     => sub { $self->chunk_size(@_) },
699                use_offline    => $self->use_offline,
700                version        => $self->version,
701            }
702        )
703    );
704
705    $rval;
706}
707
708sub _init_userdir {
709    my ( $self ) = @_;
710   
711    my $applicationName = $self->GetAppName ;
712    my $userdatadir = File::Spec->canonpath(
713        File::Spec->catfile(
714            File::HomeDir->my_data(), 
715            "\.$applicationName"
716        )
717    );
718
719    if(! -d $userdatadir){
720        if(! mkdir $userdatadir){
721            Wx::MessageBox( 
722                sprintf(
723                    "%s directory creation failed",
724                    $userdatadir,
725                ),
726                "pLoader working directory creation error",
727                wxOK | wxICON_EXCLAMATION, 
728            );
729
730            $userdatadir = File::Spec->canonpath(
731                File::Spec->catfile(
732                    File::Spec->tmpdir(), 
733                    "\.$applicationName"
734                )
735            );
736            mkdir $userdatadir;
737        }   
738    }
739
740    $self->userdata_dir($userdatadir);
741   
742    $self->conf_file(
743        File::Spec->catfile(
744            $self->userdata_dir, 
745            ".$applicationName.conf"
746        )
747    );
748
749    $self->layout_file(
750        File::Spec->catfile(
751            $self->userdata_dir, 
752            ".$applicationName.layout"
753        )
754    );
755
756    $self->storable_file(
757        File::Spec->catfile($self->userdata_dir, 'pLoader.dat')
758    );
759   
760    my $thumbdir = File::Spec->catfile($self->userdata_dir, 'thumbnails');
761    mkdir $thumbdir unless -d $thumbdir ;
762    $self->thumb_dir($thumbdir);
763
764    my $wxthumbdir = File::Spec->catfile($self->userdata_dir, 'wxthumbnails');
765    mkdir $wxthumbdir unless -d $wxthumbdir ;
766    $self->wx_thumb_dir($wxthumbdir);
767
768
769    my $resizedir = File::Spec->catfile($self->userdata_dir, 'resize');
770    mkdir $resizedir unless -d $resizedir ;
771    $self->resized_dir($resizedir);
772}
773
774sub _init_thumbimagelist {
775    my ( $self ) = @_;
776
777   
778    $self->imagelist->wx_thumb_imglist(
779        Wx::ImageList->new( 
780            $self->imagelist->wx_thumb_size, 
781            $self->imagelist->wx_thumb_size, 
782            1,
783            0
784        )
785    );
786   
787    # reload images
788    $self->_reload_thumb_images;
789}
790
791
792sub _reload_thumb_images {
793    my ( $self ) = @_;
794   
795    my $wximagelist = $self->imagelist->wx_thumb_imglist;
796    my $sums = $self->imagelist->sums;
797
798    map {
799        my $image = $self->imagelist->image_sums->{$_};
800
801        $wximagelist->Add(
802            Wx::Bitmap->new( 
803                $image->wx_thumb_file, 
804                $self->GetWxBitmapType($self->imagelist->type), 
805            )
806        );
807    }
808    @$sums ;
809
810}
811
812sub GetWxBitmapType {
813    my ( $self, $type ) = @_;
814   
815    $self->{IMGTYPE}->{$type};
816}
817
818
819
820sub ResizeImage {
821    my ( $self, $image_file, $image_file_out, $type, $width, $height, $quality ) = @_;
822   
823
824    my $image = Wx::Image->new(
825            $image_file, 
826            $self->GetWxBitmapType($type),
827            0
828    );
829
830    my $w;
831    my $h;
832
833    my $img_w = $image->GetWidth;
834    my $img_h = $image->GetHeight;
835   
836        # portrait
837        if( $img_w < $img_h ){
838            $w = $height;
839        }
840        else{
841            $w = $width;
842        }
843        # to respect aspect ratio
844        $h = sprintf(
845            "%.0f",
846            ($w*$img_h)/$img_w
847        );
848
849   
850
851    $image->Rescale(
852        $w,
853        $h,
854        wxIMAGE_QUALITY_HIGH
855    );
856
857    #$image->Resize(
858    #    [ $width, $height ], [ 0, 20],
859    #);
860   
861    $quality ||= 90;
862   
863    $image->SetOption( 
864        "quality", 
865        $quality 
866    );
867   
868    if(!$image->SaveFile(
869        $image_file_out,
870        $self->GetWxBitmapType($type),
871    )){
872        Wx::LogMessage(
873            gettext("An error has occured. Can not save file %s"),
874            $image_file_out,
875        )
876    };
877}
878
879# some labels differ with branding ( piwigo.com or piwigo.org )
880sub _init_branding {
881    my ( $self ) =@_;
882   
883    if( $self->site_url =~ /\.piwigo\.com/ ){
884        $self->branding(
885            {
886                category  => gettext("album"),
887                Category  => gettext("Album"),
888                categories => gettext("albums"),
889                Categories => gettext("Albums"),
890                'Add new category' => gettext("Add new album"),
891                'Category name' => gettext("Album name :"),
892                'New category' => gettext("New album"),
893                'What is the destination category?' => gettext("What is the destination album?")
894            }
895        );
896    }
897    else{
898        $self->branding(
899            {
900                category  => gettext("categorie"),     
901                Category  => gettext("Categorie"),     
902                categories => gettext("categories"),   
903                Categories => gettext("Categories"),   
904                'Add new category' => gettext("Add new category"),
905                'Category name' => gettext("Category name :"),
906                'New category' => gettext("New category"),
907                'What is the destination category?' => gettext("What is the destination category?")
908            }
909        );
910    }   
911}
912
913
914sub SetFrame {
915    my ( $self, $frame ) = @_; 
916
917    my $url = $self->site_url;
918   
919    if($self->use_offline){
920        $url = gettext("Work Offline");
921    }
922
923    $self->frame($frame);
924
925    my $icon = Wx::Icon->new();
926    $icon->LoadFile(
927        File::Spec->catfile(
928            $self->root_dir, $self->resource_dir, 'favicon.ico'
929        ), 
930        wxBITMAP_TYPE_ICO
931    );
932    $self->frame->SetIcon($icon);
933}
934
935sub _readParams {
936        my( $self, $file ) = @_ ;
937
938
939    my $expr_params ;
940    eval { $expr_params = read_file( $file ); } ;
941   
942    my $paramValues = [] ;
943    if($expr_params){
944        my $expr = '$paramValues = ' ;
945        $expr .=  "$expr_params ; " ;
946        eval $expr ;
947    }
948   
949    return unless 'ARRAY' eq ref $paramValues ;
950   
951    if(scalar(@$paramValues )){
952        my $params = $paramValues->[0] ;
953        $self->SetKeyValues($params);
954    }
955}
956
957sub _read_default_user_properties {
958    my( $self, $file ) = @_ ;
959
960
961    my $expr_params ;
962    eval { $expr_params = read_file( $file ); } ;
963    my $properties;
964    if($expr_params){
965        my $expr = '$properties = ' ;
966        $expr .=  " { $expr_params }; " ;
967        eval $expr ;
968    }
969    $properties = {} unless 'HASH' eq ref $properties;
970    $self->user_defined_properties($properties);
971
972}
973
974
975
976sub SetKeyValues {
977    my ( $self, $params )= @_;   
978
979    foreach( keys %$params ) {
980        $self->{$_} = $params->{$_} ;
981    }
982}
983
984
9851;
Note: See TracBrowser for help on using the repository browser.