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

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

double check retrieved image cache.

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